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/SpinLock.h>
28 // ---------------------------------------------------------------------
29 // SSLContext implementation
30 // ---------------------------------------------------------------------
32 struct CRYPTO_dynlock_value {
38 bool SSLContext::initialized_ = false;
42 std::mutex& initMutex() {
47 } // anonymous namespace
49 #ifdef OPENSSL_NPN_NEGOTIATED
50 int SSLContext::sNextProtocolsExDataIndex_ = -1;
53 // SSLContext implementation
54 SSLContext::SSLContext(SSLVersion version) {
56 std::lock_guard<std::mutex> g(initMutex());
57 initializeOpenSSLLocked();
60 ctx_ = SSL_CTX_new(SSLv23_method());
61 if (ctx_ == nullptr) {
62 throw std::runtime_error("SSL_CTX_new: " + getErrors());
68 opt = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3;
71 opt = SSL_OP_NO_SSLv2;
77 int newOpt = SSL_CTX_set_options(ctx_, opt);
78 DCHECK((newOpt & opt) == opt);
80 SSL_CTX_set_mode(ctx_, SSL_MODE_AUTO_RETRY);
82 checkPeerName_ = false;
84 #if OPENSSL_VERSION_NUMBER >= 0x1000105fL && !defined(OPENSSL_NO_TLSEXT)
85 SSL_CTX_set_tlsext_servername_callback(ctx_, baseServerNameOpenSSLCallback);
86 SSL_CTX_set_tlsext_servername_arg(ctx_, this);
89 Random::seed(randomGenerator_);
92 SSLContext::~SSLContext() {
93 if (ctx_ != nullptr) {
98 #ifdef OPENSSL_NPN_NEGOTIATED
99 deleteNextProtocolsStrings();
103 void SSLContext::ciphers(const std::string& ciphers) {
104 providedCiphersString_ = ciphers;
105 setCiphersOrThrow(ciphers);
108 void SSLContext::setCiphersOrThrow(const std::string& ciphers) {
109 int rc = SSL_CTX_set_cipher_list(ctx_, ciphers.c_str());
110 if (ERR_peek_error() != 0) {
111 throw std::runtime_error("SSL_CTX_set_cipher_list: " + getErrors());
114 throw std::runtime_error("None of specified ciphers are supported");
118 void SSLContext::setVerificationOption(const SSLContext::SSLVerifyPeerEnum&
120 CHECK(verifyPeer != SSLVerifyPeerEnum::USE_CTX); // dont recurse
121 verifyPeer_ = verifyPeer;
124 int SSLContext::getVerificationMode(const SSLContext::SSLVerifyPeerEnum&
126 CHECK(verifyPeer != SSLVerifyPeerEnum::USE_CTX);
127 int mode = SSL_VERIFY_NONE;
129 // case SSLVerifyPeerEnum::USE_CTX: // can't happen
132 case SSLVerifyPeerEnum::VERIFY:
133 mode = SSL_VERIFY_PEER;
136 case SSLVerifyPeerEnum::VERIFY_REQ_CLIENT_CERT:
137 mode = SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
140 case SSLVerifyPeerEnum::NO_VERIFY:
141 mode = SSL_VERIFY_NONE;
150 int SSLContext::getVerificationMode() {
151 return getVerificationMode(verifyPeer_);
154 void SSLContext::authenticate(bool checkPeerCert, bool checkPeerName,
155 const std::string& peerName) {
158 mode = SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT | SSL_VERIFY_CLIENT_ONCE;
159 checkPeerName_ = checkPeerName;
160 peerFixedName_ = peerName;
162 mode = SSL_VERIFY_NONE;
163 checkPeerName_ = false; // can't check name without cert!
164 peerFixedName_.clear();
166 SSL_CTX_set_verify(ctx_, mode, nullptr);
169 void SSLContext::loadCertificate(const char* path, const char* format) {
170 if (path == nullptr || format == nullptr) {
171 throw std::invalid_argument(
172 "loadCertificateChain: either <path> or <format> is nullptr");
174 if (strcmp(format, "PEM") == 0) {
175 if (SSL_CTX_use_certificate_chain_file(ctx_, path) == 0) {
176 int errnoCopy = errno;
177 std::string reason("SSL_CTX_use_certificate_chain_file: ");
180 reason.append(getErrors(errnoCopy));
181 throw std::runtime_error(reason);
184 throw std::runtime_error("Unsupported certificate format: " + std::string(format));
188 void SSLContext::loadCertificateFromBufferPEM(folly::StringPiece cert) {
189 if (cert.data() == nullptr) {
190 throw std::invalid_argument("loadCertificate: <cert> is nullptr");
193 ssl::BioUniquePtr bio(BIO_new(BIO_s_mem()));
194 if (bio == nullptr) {
195 throw std::runtime_error("BIO_new: " + getErrors());
198 int written = BIO_write(bio.get(), cert.data(), cert.size());
199 if (written <= 0 || static_cast<unsigned>(written) != cert.size()) {
200 throw std::runtime_error("BIO_write: " + getErrors());
203 ssl::X509UniquePtr x509(
204 PEM_read_bio_X509(bio.get(), nullptr, nullptr, nullptr));
205 if (x509 == nullptr) {
206 throw std::runtime_error("PEM_read_bio_X509: " + getErrors());
209 if (SSL_CTX_use_certificate(ctx_, x509.get()) == 0) {
210 throw std::runtime_error("SSL_CTX_use_certificate: " + getErrors());
214 void SSLContext::loadPrivateKey(const char* path, const char* format) {
215 if (path == nullptr || format == nullptr) {
216 throw std::invalid_argument(
217 "loadPrivateKey: either <path> or <format> is nullptr");
219 if (strcmp(format, "PEM") == 0) {
220 if (SSL_CTX_use_PrivateKey_file(ctx_, path, SSL_FILETYPE_PEM) == 0) {
221 throw std::runtime_error("SSL_CTX_use_PrivateKey_file: " + getErrors());
224 throw std::runtime_error("Unsupported private key format: " + std::string(format));
228 void SSLContext::loadPrivateKeyFromBufferPEM(folly::StringPiece pkey) {
229 if (pkey.data() == nullptr) {
230 throw std::invalid_argument("loadPrivateKey: <pkey> is nullptr");
233 ssl::BioUniquePtr bio(BIO_new(BIO_s_mem()));
234 if (bio == nullptr) {
235 throw std::runtime_error("BIO_new: " + getErrors());
238 int written = BIO_write(bio.get(), pkey.data(), pkey.size());
239 if (written <= 0 || static_cast<unsigned>(written) != pkey.size()) {
240 throw std::runtime_error("BIO_write: " + getErrors());
243 ssl::EvpPkeyUniquePtr key(
244 PEM_read_bio_PrivateKey(bio.get(), nullptr, nullptr, nullptr));
245 if (key == nullptr) {
246 throw std::runtime_error("PEM_read_bio_PrivateKey: " + getErrors());
249 if (SSL_CTX_use_PrivateKey(ctx_, key.get()) == 0) {
250 throw std::runtime_error("SSL_CTX_use_PrivateKey: " + getErrors());
254 void SSLContext::loadTrustedCertificates(const char* path) {
255 if (path == nullptr) {
256 throw std::invalid_argument("loadTrustedCertificates: <path> is nullptr");
258 if (SSL_CTX_load_verify_locations(ctx_, path, nullptr) == 0) {
259 throw std::runtime_error("SSL_CTX_load_verify_locations: " + getErrors());
263 void SSLContext::loadTrustedCertificates(X509_STORE* store) {
264 SSL_CTX_set_cert_store(ctx_, store);
267 void SSLContext::loadClientCAList(const char* path) {
268 auto clientCAs = SSL_load_client_CA_file(path);
269 if (clientCAs == nullptr) {
270 LOG(ERROR) << "Unable to load ca file: " << path;
273 SSL_CTX_set_client_CA_list(ctx_, clientCAs);
276 void SSLContext::randomize() {
280 void SSLContext::passwordCollector(std::shared_ptr<PasswordCollector> collector) {
281 if (collector == nullptr) {
282 LOG(ERROR) << "passwordCollector: ignore invalid password collector";
285 collector_ = collector;
286 SSL_CTX_set_default_passwd_cb(ctx_, passwordCallback);
287 SSL_CTX_set_default_passwd_cb_userdata(ctx_, this);
290 #if OPENSSL_VERSION_NUMBER >= 0x1000105fL && !defined(OPENSSL_NO_TLSEXT)
292 void SSLContext::setServerNameCallback(const ServerNameCallback& cb) {
296 void SSLContext::addClientHelloCallback(const ClientHelloCallback& cb) {
297 clientHelloCbs_.push_back(cb);
300 int SSLContext::baseServerNameOpenSSLCallback(SSL* ssl, int* al, void* data) {
301 SSLContext* context = (SSLContext*)data;
303 if (context == nullptr) {
304 return SSL_TLSEXT_ERR_NOACK;
307 for (auto& cb : context->clientHelloCbs_) {
308 // Generic callbacks to happen after we receive the Client Hello.
309 // For example, we use one to switch which cipher we use depending
310 // on the user's TLS version. Because the primary purpose of
311 // baseServerNameOpenSSLCallback is for SNI support, and these callbacks
312 // are side-uses, we ignore any possible failures other than just logging
317 if (!context->serverNameCb_) {
318 return SSL_TLSEXT_ERR_NOACK;
321 ServerNameCallbackResult ret = context->serverNameCb_(ssl);
323 case SERVER_NAME_FOUND:
324 return SSL_TLSEXT_ERR_OK;
325 case SERVER_NAME_NOT_FOUND:
326 return SSL_TLSEXT_ERR_NOACK;
327 case SERVER_NAME_NOT_FOUND_ALERT_FATAL:
328 *al = TLS1_AD_UNRECOGNIZED_NAME;
329 return SSL_TLSEXT_ERR_ALERT_FATAL;
334 return SSL_TLSEXT_ERR_NOACK;
337 void SSLContext::switchCiphersIfTLS11(
339 const std::string& tls11CipherString,
340 const std::vector<std::pair<std::string, int>>& tls11AltCipherlist) {
341 CHECK(!(tls11CipherString.empty() && tls11AltCipherlist.empty()))
342 << "Shouldn't call if empty ciphers / alt ciphers";
344 if (TLS1_get_client_version(ssl) <= TLS1_VERSION) {
345 // We only do this for TLS v 1.1 and later
349 const std::string* ciphers = &tls11CipherString;
350 if (!tls11AltCipherlist.empty()) {
351 if (!cipherListPicker_) {
352 std::vector<int> weights;
354 tls11AltCipherlist.begin(),
355 tls11AltCipherlist.end(),
356 [&](const std::pair<std::string, int>& e) {
357 weights.push_back(e.second);
359 cipherListPicker_.reset(
360 new std::discrete_distribution<int>(weights.begin(), weights.end()));
362 auto index = (*cipherListPicker_)(randomGenerator_);
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 return nextProtocolDistribution_(randomGenerator_);
505 int SSLContext::advertisedNextProtocolCallback(SSL* ssl,
506 const unsigned char** out, unsigned int* outlen, void* data) {
507 SSLContext* context = (SSLContext*)data;
508 if (context == nullptr || context->advertisedNextProtocols_.empty()) {
511 } else if (context->advertisedNextProtocols_.size() == 1) {
512 *out = context->advertisedNextProtocols_[0].protocols;
513 *outlen = context->advertisedNextProtocols_[0].length;
515 uintptr_t selected_index = reinterpret_cast<uintptr_t>(SSL_get_ex_data(ssl,
516 sNextProtocolsExDataIndex_));
517 if (selected_index) {
519 *out = context->advertisedNextProtocols_[selected_index].protocols;
520 *outlen = context->advertisedNextProtocols_[selected_index].length;
522 auto i = context->pickNextProtocols();
523 uintptr_t selected = i + 1;
524 SSL_set_ex_data(ssl, sNextProtocolsExDataIndex_, (void*)selected);
525 *out = context->advertisedNextProtocols_[i].protocols;
526 *outlen = context->advertisedNextProtocols_[i].length;
529 return SSL_TLSEXT_ERR_OK;
532 int SSLContext::selectNextProtocolCallback(SSL* ssl,
534 unsigned char* outlen,
535 const unsigned char* server,
536 unsigned int server_len,
538 (void)ssl; // Make -Wunused-parameters happy
539 SSLContext* ctx = (SSLContext*)data;
540 if (ctx->advertisedNextProtocols_.size() > 1) {
541 VLOG(3) << "SSLContext::selectNextProcolCallback() "
542 << "client should be deterministic in selecting protocols.";
545 unsigned char *client;
546 unsigned int client_len;
547 bool filtered = false;
548 auto cpf = ctx->getClientProtocolFilterCallback();
550 filtered = (*cpf)(&client, &client_len, server, server_len);
554 if (ctx->advertisedNextProtocols_.empty()) {
555 client = (unsigned char *) "";
558 client = ctx->advertisedNextProtocols_[0].protocols;
559 client_len = ctx->advertisedNextProtocols_[0].length;
563 int retval = SSL_select_next_proto(out, outlen, server, server_len,
565 if (retval != OPENSSL_NPN_NEGOTIATED) {
566 VLOG(3) << "SSLContext::selectNextProcolCallback() "
567 << "unable to pick a next protocol.";
569 return SSL_TLSEXT_ERR_OK;
571 #endif // OPENSSL_NPN_NEGOTIATED
573 SSL* SSLContext::createSSL() const {
574 SSL* ssl = SSL_new(ctx_);
575 if (ssl == nullptr) {
576 throw std::runtime_error("SSL_new: " + getErrors());
581 void SSLContext::setSessionCacheContext(const std::string& context) {
582 SSL_CTX_set_session_id_context(
584 reinterpret_cast<const unsigned char*>(context.data()),
586 static_cast<int>(context.length()), SSL_MAX_SSL_SESSION_ID_LENGTH));
590 * Match a name with a pattern. The pattern may include wildcard. A single
591 * wildcard "*" can match up to one component in the domain name.
593 * @param host Host name, typically the name of the remote host
594 * @param pattern Name retrieved from certificate
595 * @param size Size of "pattern"
596 * @return True, if "host" matches "pattern". False otherwise.
598 bool SSLContext::matchName(const char* host, const char* pattern, int size) {
601 while (i < size && host[j] != '\0') {
602 if (toupper(pattern[i]) == toupper(host[j])) {
607 if (pattern[i] == '*') {
608 while (host[j] != '.' && host[j] != '\0') {
616 if (i == size && host[j] == '\0') {
622 int SSLContext::passwordCallback(char* password,
626 SSLContext* context = (SSLContext*)data;
627 if (context == nullptr || context->passwordCollector() == nullptr) {
630 std::string userPassword;
631 // call user defined password collector to get password
632 context->passwordCollector()->getPassword(userPassword, size);
633 int length = userPassword.size();
637 strncpy(password, userPassword.c_str(), length);
643 SSLContext::SSLLockType inLockType = SSLContext::LOCK_MUTEX) :
644 lockType(inLockType) {
648 if (lockType == SSLContext::LOCK_MUTEX) {
650 } else if (lockType == SSLContext::LOCK_SPINLOCK) {
653 // lockType == LOCK_NONE, no-op
657 if (lockType == SSLContext::LOCK_MUTEX) {
659 } else if (lockType == SSLContext::LOCK_SPINLOCK) {
662 // lockType == LOCK_NONE, no-op
665 SSLContext::SSLLockType lockType;
666 folly::SpinLock spinLock{};
670 // Statics are unsafe in environments that call exit().
671 // If one thread calls exit() while another thread is
672 // references a member of SSLContext, bad things can happen.
673 // SSLContext runs in such environments.
674 // Instead of declaring a static member we "new" the static
675 // member so that it won't be destructed on exit().
676 static std::unique_ptr<SSLLock[]>& locks() {
677 static auto locksInst = new std::unique_ptr<SSLLock[]>();
681 static std::map<int, SSLContext::SSLLockType>& lockTypes() {
682 static auto lockTypesInst = new std::map<int, SSLContext::SSLLockType>();
683 return *lockTypesInst;
686 static void callbackLocking(int mode, int n, const char*, int) {
687 if (mode & CRYPTO_LOCK) {
694 static unsigned long callbackThreadID() {
695 return static_cast<unsigned long>(
697 pthread_mach_thread_np(pthread_self())
704 static CRYPTO_dynlock_value* dyn_create(const char*, int) {
705 return new CRYPTO_dynlock_value;
708 static void dyn_lock(int mode,
709 struct CRYPTO_dynlock_value* lock,
711 if (lock != nullptr) {
712 if (mode & CRYPTO_LOCK) {
715 lock->mutex.unlock();
720 static void dyn_destroy(struct CRYPTO_dynlock_value* lock, const char*, int) {
724 void SSLContext::setSSLLockTypes(std::map<int, SSLLockType> inLockTypes) {
725 lockTypes() = inLockTypes;
728 #if defined(SSL_MODE_HANDSHAKE_CUTTHROUGH)
729 void SSLContext::enableFalseStart() {
730 SSL_CTX_set_mode(ctx_, SSL_MODE_HANDSHAKE_CUTTHROUGH);
734 void SSLContext::markInitialized() {
735 std::lock_guard<std::mutex> g(initMutex());
739 void SSLContext::initializeOpenSSL() {
740 std::lock_guard<std::mutex> g(initMutex());
741 initializeOpenSSLLocked();
744 void SSLContext::initializeOpenSSLLocked() {
749 SSL_load_error_strings();
750 ERR_load_crypto_strings();
752 locks().reset(new SSLLock[::CRYPTO_num_locks()]);
753 for (auto it: lockTypes()) {
754 locks()[it.first].lockType = it.second;
756 CRYPTO_set_id_callback(callbackThreadID);
757 CRYPTO_set_locking_callback(callbackLocking);
759 CRYPTO_set_dynlock_create_callback(dyn_create);
760 CRYPTO_set_dynlock_lock_callback(dyn_lock);
761 CRYPTO_set_dynlock_destroy_callback(dyn_destroy);
763 #ifdef OPENSSL_NPN_NEGOTIATED
764 sNextProtocolsExDataIndex_ = SSL_get_ex_new_index(0,
765 (void*)"Advertised next protocol index", nullptr, nullptr, nullptr);
770 void SSLContext::cleanupOpenSSL() {
771 std::lock_guard<std::mutex> g(initMutex());
772 cleanupOpenSSLLocked();
775 void SSLContext::cleanupOpenSSLLocked() {
780 CRYPTO_set_id_callback(nullptr);
781 CRYPTO_set_locking_callback(nullptr);
782 CRYPTO_set_dynlock_create_callback(nullptr);
783 CRYPTO_set_dynlock_lock_callback(nullptr);
784 CRYPTO_set_dynlock_destroy_callback(nullptr);
785 CRYPTO_cleanup_all_ex_data();
790 initialized_ = false;
793 void SSLContext::setOptions(long options) {
794 long newOpt = SSL_CTX_set_options(ctx_, options);
795 if ((newOpt & options) != options) {
796 throw std::runtime_error("SSL_CTX_set_options failed");
800 std::string SSLContext::getErrors(int errnoCopy) {
802 unsigned long errorCode;
806 while ((errorCode = ERR_get_error()) != 0) {
807 if (!errors.empty()) {
810 const char* reason = ERR_reason_error_string(errorCode);
811 if (reason == nullptr) {
812 snprintf(message, sizeof(message) - 1, "SSL error # %lu", errorCode);
817 if (errors.empty()) {
818 errors = "error code: " + folly::to<std::string>(errnoCopy);
824 operator<<(std::ostream& os, const PasswordCollector& collector) {
825 os << collector.describe();