SSLContextTest.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright 2017-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <folly/io/async/SSLContext.h>
  17. #include <folly/FileUtil.h>
  18. #include <folly/portability/GTest.h>
  19. #include <folly/ssl/OpenSSLPtrTypes.h>
  20. using namespace std;
  21. using namespace testing;
  22. namespace folly {
  23. class SSLContextTest : public testing::Test {
  24. public:
  25. SSLContext ctx;
  26. void verifySSLCipherList(const vector<string>& ciphers);
  27. };
  28. void SSLContextTest::verifySSLCipherList(const vector<string>& ciphers) {
  29. int i = 0;
  30. ssl::SSLUniquePtr ssl(ctx.createSSL());
  31. for (auto& cipher : ciphers) {
  32. ASSERT_STREQ(cipher.c_str(), SSL_get_cipher_list(ssl.get(), i++));
  33. }
  34. ASSERT_EQ(nullptr, SSL_get_cipher_list(ssl.get(), i));
  35. }
  36. TEST_F(SSLContextTest, TestSetCipherString) {
  37. ctx.ciphers("AES128-SHA:ECDHE-RSA-AES256-SHA384");
  38. verifySSLCipherList({"AES128-SHA", "ECDHE-RSA-AES256-SHA384"});
  39. }
  40. TEST_F(SSLContextTest, TestSetCipherList) {
  41. const vector<string> ciphers = {"ECDHE-RSA-AES128-SHA", "AES256-SHA"};
  42. ctx.setCipherList(ciphers);
  43. verifySSLCipherList(ciphers);
  44. }
  45. TEST_F(SSLContextTest, TestLoadCertKey) {
  46. std::string certData, keyData, anotherKeyData;
  47. const char* certPath = "folly/io/async/test/certs/tests-cert.pem";
  48. const char* keyPath = "folly/io/async/test/certs/tests-key.pem";
  49. const char* anotherKeyPath = "folly/io/async/test/certs/client_key.pem";
  50. folly::readFile(certPath, certData);
  51. folly::readFile(keyPath, keyData);
  52. folly::readFile(anotherKeyPath, anotherKeyData);
  53. {
  54. SCOPED_TRACE("Valid cert/key pair from buffer");
  55. SSLContext tmpCtx;
  56. tmpCtx.loadCertificateFromBufferPEM(certData);
  57. tmpCtx.loadPrivateKeyFromBufferPEM(keyData);
  58. EXPECT_TRUE(tmpCtx.isCertKeyPairValid());
  59. }
  60. {
  61. SCOPED_TRACE("Valid cert/key pair from files");
  62. SSLContext tmpCtx;
  63. tmpCtx.loadCertificate(certPath);
  64. tmpCtx.loadPrivateKey(keyPath);
  65. EXPECT_TRUE(tmpCtx.isCertKeyPairValid());
  66. }
  67. {
  68. SCOPED_TRACE("Invalid cert/key pair from file. Load cert first");
  69. SSLContext tmpCtx;
  70. tmpCtx.loadCertificate(certPath);
  71. EXPECT_THROW(tmpCtx.loadPrivateKey(anotherKeyPath), std::runtime_error);
  72. }
  73. {
  74. SCOPED_TRACE("Invalid cert/key pair from file. Load key first");
  75. SSLContext tmpCtx;
  76. tmpCtx.loadPrivateKey(anotherKeyPath);
  77. tmpCtx.loadCertificate(certPath);
  78. EXPECT_FALSE(tmpCtx.isCertKeyPairValid());
  79. }
  80. {
  81. SCOPED_TRACE("Invalid key/cert pair from buf. Load cert first");
  82. SSLContext tmpCtx;
  83. tmpCtx.loadCertificateFromBufferPEM(certData);
  84. EXPECT_THROW(
  85. tmpCtx.loadPrivateKeyFromBufferPEM(anotherKeyData), std::runtime_error);
  86. }
  87. {
  88. SCOPED_TRACE("Invalid key/cert pair from buf. Load key first");
  89. SSLContext tmpCtx;
  90. tmpCtx.loadPrivateKeyFromBufferPEM(anotherKeyData);
  91. tmpCtx.loadCertificateFromBufferPEM(certData);
  92. EXPECT_FALSE(tmpCtx.isCertKeyPairValid());
  93. }
  94. {
  95. SCOPED_TRACE(
  96. "loadCertKeyPairFromBufferPEM() must throw when cert/key mismatch");
  97. SSLContext tmpCtx;
  98. EXPECT_THROW(
  99. tmpCtx.loadCertKeyPairFromBufferPEM(certData, anotherKeyData),
  100. std::runtime_error);
  101. }
  102. {
  103. SCOPED_TRACE(
  104. "loadCertKeyPairFromBufferPEM() must succeed when cert/key match");
  105. SSLContext tmpCtx;
  106. tmpCtx.loadCertKeyPairFromBufferPEM(certData, keyData);
  107. }
  108. {
  109. SCOPED_TRACE(
  110. "loadCertKeyPairFromFiles() must throw when cert/key mismatch");
  111. SSLContext tmpCtx;
  112. EXPECT_THROW(
  113. tmpCtx.loadCertKeyPairFromFiles(certPath, anotherKeyPath),
  114. std::runtime_error);
  115. }
  116. {
  117. SCOPED_TRACE("loadCertKeyPairFromFiles() must succeed when cert/key match");
  118. SSLContext tmpCtx;
  119. tmpCtx.loadCertKeyPairFromFiles(certPath, keyPath);
  120. }
  121. }
  122. } // namespace folly