2 * Copyright 2016 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "SSLContext.h"
19 #include <openssl/err.h>
20 #include <openssl/rand.h>
21 #include <openssl/ssl.h>
22 #include <openssl/x509v3.h>
24 #include <folly/Format.h>
25 #include <folly/Memory.h>
26 #include <folly/Random.h>
27 #include <folly/SpinLock.h>
29 // ---------------------------------------------------------------------
30 // SSLContext implementation
31 // ---------------------------------------------------------------------
33 struct CRYPTO_dynlock_value {
39 bool SSLContext::initialized_ = false;
43 std::mutex& initMutex() {
48 } // anonymous namespace
50 #ifdef OPENSSL_NPN_NEGOTIATED
51 int SSLContext::sNextProtocolsExDataIndex_ = -1;
54 // SSLContext implementation
55 SSLContext::SSLContext(SSLVersion version) {
57 std::lock_guard<std::mutex> g(initMutex());
58 initializeOpenSSLLocked();
61 ctx_ = SSL_CTX_new(SSLv23_method());
62 if (ctx_ == nullptr) {
63 throw std::runtime_error("SSL_CTX_new: " + getErrors());
69 opt = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3;
72 opt = SSL_OP_NO_SSLv2;
78 int newOpt = SSL_CTX_set_options(ctx_, opt);
79 DCHECK((newOpt & opt) == opt);
81 SSL_CTX_set_mode(ctx_, SSL_MODE_AUTO_RETRY);
83 checkPeerName_ = false;
85 #if OPENSSL_VERSION_NUMBER >= 0x1000105fL && !defined(OPENSSL_NO_TLSEXT)
86 SSL_CTX_set_tlsext_servername_callback(ctx_, baseServerNameOpenSSLCallback);
87 SSL_CTX_set_tlsext_servername_arg(ctx_, this);
91 SSLContext::~SSLContext() {
92 if (ctx_ != nullptr) {
97 #ifdef OPENSSL_NPN_NEGOTIATED
98 deleteNextProtocolsStrings();
102 void SSLContext::ciphers(const std::string& ciphers) {
103 providedCiphersString_ = ciphers;
104 setCiphersOrThrow(ciphers);
107 void SSLContext::setCiphersOrThrow(const std::string& ciphers) {
108 int rc = SSL_CTX_set_cipher_list(ctx_, ciphers.c_str());
109 if (ERR_peek_error() != 0) {
110 throw std::runtime_error("SSL_CTX_set_cipher_list: " + getErrors());
113 throw std::runtime_error("None of specified ciphers are supported");
117 void SSLContext::setVerificationOption(const SSLContext::SSLVerifyPeerEnum&
119 CHECK(verifyPeer != SSLVerifyPeerEnum::USE_CTX); // dont recurse
120 verifyPeer_ = verifyPeer;
123 int SSLContext::getVerificationMode(const SSLContext::SSLVerifyPeerEnum&
125 CHECK(verifyPeer != SSLVerifyPeerEnum::USE_CTX);
126 int mode = SSL_VERIFY_NONE;
128 // case SSLVerifyPeerEnum::USE_CTX: // can't happen
131 case SSLVerifyPeerEnum::VERIFY:
132 mode = SSL_VERIFY_PEER;
135 case SSLVerifyPeerEnum::VERIFY_REQ_CLIENT_CERT:
136 mode = SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
139 case SSLVerifyPeerEnum::NO_VERIFY:
140 mode = SSL_VERIFY_NONE;
149 int SSLContext::getVerificationMode() {
150 return getVerificationMode(verifyPeer_);
153 void SSLContext::authenticate(bool checkPeerCert, bool checkPeerName,
154 const std::string& peerName) {
157 mode = SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT | SSL_VERIFY_CLIENT_ONCE;
158 checkPeerName_ = checkPeerName;
159 peerFixedName_ = peerName;
161 mode = SSL_VERIFY_NONE;
162 checkPeerName_ = false; // can't check name without cert!
163 peerFixedName_.clear();
165 SSL_CTX_set_verify(ctx_, mode, nullptr);
168 void SSLContext::loadCertificate(const char* path, const char* format) {
169 if (path == nullptr || format == nullptr) {
170 throw std::invalid_argument(
171 "loadCertificateChain: either <path> or <format> is nullptr");
173 if (strcmp(format, "PEM") == 0) {
174 if (SSL_CTX_use_certificate_chain_file(ctx_, path) == 0) {
175 int errnoCopy = errno;
176 std::string reason("SSL_CTX_use_certificate_chain_file: ");
179 reason.append(getErrors(errnoCopy));
180 throw std::runtime_error(reason);
183 throw std::runtime_error("Unsupported certificate format: " + std::string(format));
187 void SSLContext::loadCertificateFromBufferPEM(folly::StringPiece cert) {
188 if (cert.data() == nullptr) {
189 throw std::invalid_argument("loadCertificate: <cert> is nullptr");
192 ssl::BioUniquePtr bio(BIO_new(BIO_s_mem()));
193 if (bio == nullptr) {
194 throw std::runtime_error("BIO_new: " + getErrors());
197 int written = BIO_write(bio.get(), cert.data(), cert.size());
198 if (written <= 0 || static_cast<unsigned>(written) != cert.size()) {
199 throw std::runtime_error("BIO_write: " + getErrors());
202 ssl::X509UniquePtr x509(
203 PEM_read_bio_X509(bio.get(), nullptr, nullptr, nullptr));
204 if (x509 == nullptr) {
205 throw std::runtime_error("PEM_read_bio_X509: " + getErrors());
208 if (SSL_CTX_use_certificate(ctx_, x509.get()) == 0) {
209 throw std::runtime_error("SSL_CTX_use_certificate: " + getErrors());
213 void SSLContext::loadPrivateKey(const char* path, const char* format) {
214 if (path == nullptr || format == nullptr) {
215 throw std::invalid_argument(
216 "loadPrivateKey: either <path> or <format> is nullptr");
218 if (strcmp(format, "PEM") == 0) {
219 if (SSL_CTX_use_PrivateKey_file(ctx_, path, SSL_FILETYPE_PEM) == 0) {
220 throw std::runtime_error("SSL_CTX_use_PrivateKey_file: " + getErrors());
223 throw std::runtime_error("Unsupported private key format: " + std::string(format));
227 void SSLContext::loadPrivateKeyFromBufferPEM(folly::StringPiece pkey) {
228 if (pkey.data() == nullptr) {
229 throw std::invalid_argument("loadPrivateKey: <pkey> is nullptr");
232 ssl::BioUniquePtr bio(BIO_new(BIO_s_mem()));
233 if (bio == nullptr) {
234 throw std::runtime_error("BIO_new: " + getErrors());
237 int written = BIO_write(bio.get(), pkey.data(), pkey.size());
238 if (written <= 0 || static_cast<unsigned>(written) != pkey.size()) {
239 throw std::runtime_error("BIO_write: " + getErrors());
242 ssl::EvpPkeyUniquePtr key(
243 PEM_read_bio_PrivateKey(bio.get(), nullptr, nullptr, nullptr));
244 if (key == nullptr) {
245 throw std::runtime_error("PEM_read_bio_PrivateKey: " + getErrors());
248 if (SSL_CTX_use_PrivateKey(ctx_, key.get()) == 0) {
249 throw std::runtime_error("SSL_CTX_use_PrivateKey: " + getErrors());
253 void SSLContext::loadTrustedCertificates(const char* path) {
254 if (path == nullptr) {
255 throw std::invalid_argument("loadTrustedCertificates: <path> is nullptr");
257 if (SSL_CTX_load_verify_locations(ctx_, path, nullptr) == 0) {
258 throw std::runtime_error("SSL_CTX_load_verify_locations: " + getErrors());
262 void SSLContext::loadTrustedCertificates(X509_STORE* store) {
263 SSL_CTX_set_cert_store(ctx_, store);
266 void SSLContext::loadClientCAList(const char* path) {
267 auto clientCAs = SSL_load_client_CA_file(path);
268 if (clientCAs == nullptr) {
269 LOG(ERROR) << "Unable to load ca file: " << path;
272 SSL_CTX_set_client_CA_list(ctx_, clientCAs);
275 void SSLContext::randomize() {
279 void SSLContext::passwordCollector(std::shared_ptr<PasswordCollector> collector) {
280 if (collector == nullptr) {
281 LOG(ERROR) << "passwordCollector: ignore invalid password collector";
284 collector_ = collector;
285 SSL_CTX_set_default_passwd_cb(ctx_, passwordCallback);
286 SSL_CTX_set_default_passwd_cb_userdata(ctx_, this);
289 #if OPENSSL_VERSION_NUMBER >= 0x1000105fL && !defined(OPENSSL_NO_TLSEXT)
291 void SSLContext::setServerNameCallback(const ServerNameCallback& cb) {
295 void SSLContext::addClientHelloCallback(const ClientHelloCallback& cb) {
296 clientHelloCbs_.push_back(cb);
299 int SSLContext::baseServerNameOpenSSLCallback(SSL* ssl, int* al, void* data) {
300 SSLContext* context = (SSLContext*)data;
302 if (context == nullptr) {
303 return SSL_TLSEXT_ERR_NOACK;
306 for (auto& cb : context->clientHelloCbs_) {
307 // Generic callbacks to happen after we receive the Client Hello.
308 // For example, we use one to switch which cipher we use depending
309 // on the user's TLS version. Because the primary purpose of
310 // baseServerNameOpenSSLCallback is for SNI support, and these callbacks
311 // are side-uses, we ignore any possible failures other than just logging
316 if (!context->serverNameCb_) {
317 return SSL_TLSEXT_ERR_NOACK;
320 ServerNameCallbackResult ret = context->serverNameCb_(ssl);
322 case SERVER_NAME_FOUND:
323 return SSL_TLSEXT_ERR_OK;
324 case SERVER_NAME_NOT_FOUND:
325 return SSL_TLSEXT_ERR_NOACK;
326 case SERVER_NAME_NOT_FOUND_ALERT_FATAL:
327 *al = TLS1_AD_UNRECOGNIZED_NAME;
328 return SSL_TLSEXT_ERR_ALERT_FATAL;
333 return SSL_TLSEXT_ERR_NOACK;
336 void SSLContext::switchCiphersIfTLS11(
338 const std::string& tls11CipherString,
339 const std::vector<std::pair<std::string, int>>& tls11AltCipherlist) {
340 CHECK(!(tls11CipherString.empty() && tls11AltCipherlist.empty()))
341 << "Shouldn't call if empty ciphers / alt ciphers";
343 if (TLS1_get_client_version(ssl) <= TLS1_VERSION) {
344 // We only do this for TLS v 1.1 and later
348 const std::string* ciphers = &tls11CipherString;
349 if (!tls11AltCipherlist.empty()) {
350 if (!cipherListPicker_) {
351 std::vector<int> weights;
353 tls11AltCipherlist.begin(),
354 tls11AltCipherlist.end(),
355 [&](const std::pair<std::string, int>& e) {
356 weights.push_back(e.second);
358 cipherListPicker_.reset(
359 new std::discrete_distribution<int>(weights.begin(), weights.end()));
361 auto rng = ThreadLocalPRNG();
362 auto index = (*cipherListPicker_)(rng);
363 if ((size_t)index >= tls11AltCipherlist.size()) {
364 LOG(ERROR) << "Trying to pick alt TLS11 cipher index " << index
365 << ", but tls11AltCipherlist is of length "
366 << tls11AltCipherlist.size();
368 ciphers = &tls11AltCipherlist[index].first;
372 // Prefer AES for TLS versions 1.1 and later since these are not
373 // vulnerable to BEAST attacks on AES. Note that we're setting the
374 // cipher list on the SSL object, not the SSL_CTX object, so it will
375 // only last for this request.
376 int rc = SSL_set_cipher_list(ssl, ciphers->c_str());
377 if ((rc == 0) || ERR_peek_error() != 0) {
378 // This shouldn't happen since we checked for this when proxygen
380 LOG(WARNING) << "ssl_cipher: No specified ciphers supported for switch";
381 SSL_set_cipher_list(ssl, providedCiphersString_.c_str());
386 #if OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined(OPENSSL_NO_TLSEXT)
387 int SSLContext::alpnSelectCallback(SSL* /* ssl */,
388 const unsigned char** out,
389 unsigned char* outlen,
390 const unsigned char* in,
393 SSLContext* context = (SSLContext*)data;
395 if (context->advertisedNextProtocols_.empty()) {
399 auto i = context->pickNextProtocols();
400 const auto& item = context->advertisedNextProtocols_[i];
401 if (SSL_select_next_proto((unsigned char**)out,
406 inlen) != OPENSSL_NPN_NEGOTIATED) {
407 return SSL_TLSEXT_ERR_NOACK;
410 return SSL_TLSEXT_ERR_OK;
414 #ifdef OPENSSL_NPN_NEGOTIATED
416 bool SSLContext::setAdvertisedNextProtocols(
417 const std::list<std::string>& protocols, NextProtocolType protocolType) {
418 return setRandomizedAdvertisedNextProtocols({{1, protocols}}, protocolType);
421 bool SSLContext::setRandomizedAdvertisedNextProtocols(
422 const std::list<NextProtocolsItem>& items, NextProtocolType protocolType) {
423 unsetNextProtocols();
424 if (items.size() == 0) {
427 int total_weight = 0;
428 for (const auto &item : items) {
429 if (item.protocols.size() == 0) {
432 AdvertisedNextProtocolsItem advertised_item;
433 advertised_item.length = 0;
434 for (const auto& proto : item.protocols) {
435 ++advertised_item.length;
436 unsigned protoLength = proto.length();
437 if (protoLength >= 256) {
438 deleteNextProtocolsStrings();
441 advertised_item.length += protoLength;
443 advertised_item.protocols = new unsigned char[advertised_item.length];
444 if (!advertised_item.protocols) {
445 throw std::runtime_error("alloc failure");
447 unsigned char* dst = advertised_item.protocols;
448 for (auto& proto : item.protocols) {
449 unsigned protoLength = proto.length();
450 *dst++ = (unsigned char)protoLength;
451 memcpy(dst, proto.data(), protoLength);
454 total_weight += item.weight;
455 advertisedNextProtocols_.push_back(advertised_item);
456 advertisedNextProtocolWeights_.push_back(item.weight);
458 if (total_weight == 0) {
459 deleteNextProtocolsStrings();
462 nextProtocolDistribution_ =
463 std::discrete_distribution<>(advertisedNextProtocolWeights_.begin(),
464 advertisedNextProtocolWeights_.end());
465 if ((uint8_t)protocolType & (uint8_t)NextProtocolType::NPN) {
466 SSL_CTX_set_next_protos_advertised_cb(
467 ctx_, advertisedNextProtocolCallback, this);
468 SSL_CTX_set_next_proto_select_cb(ctx_, selectNextProtocolCallback, this);
470 #if OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined(OPENSSL_NO_TLSEXT)
471 if ((uint8_t)protocolType & (uint8_t)NextProtocolType::ALPN) {
472 SSL_CTX_set_alpn_select_cb(ctx_, alpnSelectCallback, this);
473 // Client cannot really use randomized alpn
474 SSL_CTX_set_alpn_protos(ctx_,
475 advertisedNextProtocols_[0].protocols,
476 advertisedNextProtocols_[0].length);
482 void SSLContext::deleteNextProtocolsStrings() {
483 for (auto protocols : advertisedNextProtocols_) {
484 delete[] protocols.protocols;
486 advertisedNextProtocols_.clear();
487 advertisedNextProtocolWeights_.clear();
490 void SSLContext::unsetNextProtocols() {
491 deleteNextProtocolsStrings();
492 SSL_CTX_set_next_protos_advertised_cb(ctx_, nullptr, nullptr);
493 SSL_CTX_set_next_proto_select_cb(ctx_, nullptr, nullptr);
494 #if OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined(OPENSSL_NO_TLSEXT)
495 SSL_CTX_set_alpn_select_cb(ctx_, nullptr, nullptr);
496 SSL_CTX_set_alpn_protos(ctx_, nullptr, 0);
500 size_t SSLContext::pickNextProtocols() {
501 CHECK(!advertisedNextProtocols_.empty()) << "Failed to pickNextProtocols";
502 auto rng = ThreadLocalPRNG();
503 return nextProtocolDistribution_(rng);
506 int SSLContext::advertisedNextProtocolCallback(SSL* ssl,
507 const unsigned char** out, unsigned int* outlen, void* data) {
508 SSLContext* context = (SSLContext*)data;
509 if (context == nullptr || context->advertisedNextProtocols_.empty()) {
512 } else if (context->advertisedNextProtocols_.size() == 1) {
513 *out = context->advertisedNextProtocols_[0].protocols;
514 *outlen = context->advertisedNextProtocols_[0].length;
516 uintptr_t selected_index = reinterpret_cast<uintptr_t>(SSL_get_ex_data(ssl,
517 sNextProtocolsExDataIndex_));
518 if (selected_index) {
520 *out = context->advertisedNextProtocols_[selected_index].protocols;
521 *outlen = context->advertisedNextProtocols_[selected_index].length;
523 auto i = context->pickNextProtocols();
524 uintptr_t selected = i + 1;
525 SSL_set_ex_data(ssl, sNextProtocolsExDataIndex_, (void*)selected);
526 *out = context->advertisedNextProtocols_[i].protocols;
527 *outlen = context->advertisedNextProtocols_[i].length;
530 return SSL_TLSEXT_ERR_OK;
533 int SSLContext::selectNextProtocolCallback(SSL* ssl,
535 unsigned char* outlen,
536 const unsigned char* server,
537 unsigned int server_len,
539 (void)ssl; // Make -Wunused-parameters happy
540 SSLContext* ctx = (SSLContext*)data;
541 if (ctx->advertisedNextProtocols_.size() > 1) {
542 VLOG(3) << "SSLContext::selectNextProcolCallback() "
543 << "client should be deterministic in selecting protocols.";
546 unsigned char *client;
547 unsigned int client_len;
548 bool filtered = false;
549 auto cpf = ctx->getClientProtocolFilterCallback();
551 filtered = (*cpf)(&client, &client_len, server, server_len);
555 if (ctx->advertisedNextProtocols_.empty()) {
556 client = (unsigned char *) "";
559 client = ctx->advertisedNextProtocols_[0].protocols;
560 client_len = ctx->advertisedNextProtocols_[0].length;
564 int retval = SSL_select_next_proto(out, outlen, server, server_len,
566 if (retval != OPENSSL_NPN_NEGOTIATED) {
567 VLOG(3) << "SSLContext::selectNextProcolCallback() "
568 << "unable to pick a next protocol.";
570 return SSL_TLSEXT_ERR_OK;
572 #endif // OPENSSL_NPN_NEGOTIATED
574 SSL* SSLContext::createSSL() const {
575 SSL* ssl = SSL_new(ctx_);
576 if (ssl == nullptr) {
577 throw std::runtime_error("SSL_new: " + getErrors());
582 void SSLContext::setSessionCacheContext(const std::string& context) {
583 SSL_CTX_set_session_id_context(
585 reinterpret_cast<const unsigned char*>(context.data()),
587 static_cast<int>(context.length()), SSL_MAX_SSL_SESSION_ID_LENGTH));
591 * Match a name with a pattern. The pattern may include wildcard. A single
592 * wildcard "*" can match up to one component in the domain name.
594 * @param host Host name, typically the name of the remote host
595 * @param pattern Name retrieved from certificate
596 * @param size Size of "pattern"
597 * @return True, if "host" matches "pattern". False otherwise.
599 bool SSLContext::matchName(const char* host, const char* pattern, int size) {
602 while (i < size && host[j] != '\0') {
603 if (toupper(pattern[i]) == toupper(host[j])) {
608 if (pattern[i] == '*') {
609 while (host[j] != '.' && host[j] != '\0') {
617 if (i == size && host[j] == '\0') {
623 int SSLContext::passwordCallback(char* password,
627 SSLContext* context = (SSLContext*)data;
628 if (context == nullptr || context->passwordCollector() == nullptr) {
631 std::string userPassword;
632 // call user defined password collector to get password
633 context->passwordCollector()->getPassword(userPassword, size);
634 int length = userPassword.size();
638 strncpy(password, userPassword.c_str(), length);
644 SSLContext::SSLLockType inLockType = SSLContext::LOCK_MUTEX) :
645 lockType(inLockType) {
649 if (lockType == SSLContext::LOCK_MUTEX) {
651 } else if (lockType == SSLContext::LOCK_SPINLOCK) {
654 // lockType == LOCK_NONE, no-op
658 if (lockType == SSLContext::LOCK_MUTEX) {
660 } else if (lockType == SSLContext::LOCK_SPINLOCK) {
663 // lockType == LOCK_NONE, no-op
666 SSLContext::SSLLockType lockType;
667 folly::SpinLock spinLock{};
671 // Statics are unsafe in environments that call exit().
672 // If one thread calls exit() while another thread is
673 // references a member of SSLContext, bad things can happen.
674 // SSLContext runs in such environments.
675 // Instead of declaring a static member we "new" the static
676 // member so that it won't be destructed on exit().
677 static std::unique_ptr<SSLLock[]>& locks() {
678 static auto locksInst = new std::unique_ptr<SSLLock[]>();
682 static std::map<int, SSLContext::SSLLockType>& lockTypes() {
683 static auto lockTypesInst = new std::map<int, SSLContext::SSLLockType>();
684 return *lockTypesInst;
687 static void callbackLocking(int mode, int n, const char*, int) {
688 if (mode & CRYPTO_LOCK) {
695 static unsigned long callbackThreadID() {
696 return static_cast<unsigned long>(
698 pthread_mach_thread_np(pthread_self())
700 pthread_getw32threadid_np(pthread_self())
707 static CRYPTO_dynlock_value* dyn_create(const char*, int) {
708 return new CRYPTO_dynlock_value;
711 static void dyn_lock(int mode,
712 struct CRYPTO_dynlock_value* lock,
714 if (lock != nullptr) {
715 if (mode & CRYPTO_LOCK) {
718 lock->mutex.unlock();
723 static void dyn_destroy(struct CRYPTO_dynlock_value* lock, const char*, int) {
727 void SSLContext::setSSLLockTypes(std::map<int, SSLLockType> inLockTypes) {
728 lockTypes() = inLockTypes;
731 #if defined(SSL_MODE_HANDSHAKE_CUTTHROUGH)
732 void SSLContext::enableFalseStart() {
733 SSL_CTX_set_mode(ctx_, SSL_MODE_HANDSHAKE_CUTTHROUGH);
737 void SSLContext::markInitialized() {
738 std::lock_guard<std::mutex> g(initMutex());
742 void SSLContext::initializeOpenSSL() {
743 std::lock_guard<std::mutex> g(initMutex());
744 initializeOpenSSLLocked();
747 void SSLContext::initializeOpenSSLLocked() {
752 SSL_load_error_strings();
753 ERR_load_crypto_strings();
755 locks().reset(new SSLLock[::CRYPTO_num_locks()]);
756 for (auto it: lockTypes()) {
757 locks()[it.first].lockType = it.second;
759 CRYPTO_set_id_callback(callbackThreadID);
760 CRYPTO_set_locking_callback(callbackLocking);
762 CRYPTO_set_dynlock_create_callback(dyn_create);
763 CRYPTO_set_dynlock_lock_callback(dyn_lock);
764 CRYPTO_set_dynlock_destroy_callback(dyn_destroy);
766 #ifdef OPENSSL_NPN_NEGOTIATED
767 sNextProtocolsExDataIndex_ = SSL_get_ex_new_index(0,
768 (void*)"Advertised next protocol index", nullptr, nullptr, nullptr);
773 void SSLContext::cleanupOpenSSL() {
774 std::lock_guard<std::mutex> g(initMutex());
775 cleanupOpenSSLLocked();
778 void SSLContext::cleanupOpenSSLLocked() {
783 CRYPTO_set_id_callback(nullptr);
784 CRYPTO_set_locking_callback(nullptr);
785 CRYPTO_set_dynlock_create_callback(nullptr);
786 CRYPTO_set_dynlock_lock_callback(nullptr);
787 CRYPTO_set_dynlock_destroy_callback(nullptr);
788 CRYPTO_cleanup_all_ex_data();
793 initialized_ = false;
796 void SSLContext::setOptions(long options) {
797 long newOpt = SSL_CTX_set_options(ctx_, options);
798 if ((newOpt & options) != options) {
799 throw std::runtime_error("SSL_CTX_set_options failed");
803 std::string SSLContext::getErrors(int errnoCopy) {
805 unsigned long errorCode;
809 while ((errorCode = ERR_get_error()) != 0) {
810 if (!errors.empty()) {
813 const char* reason = ERR_reason_error_string(errorCode);
814 if (reason == nullptr) {
815 snprintf(message, sizeof(message) - 1, "SSL error # %lu", errorCode);
820 if (errors.empty()) {
821 errors = "error code: " + folly::to<std::string>(errnoCopy);
827 operator<<(std::ostream& os, const PasswordCollector& collector) {
828 os << collector.describe();