2 * Copyright 2017 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 <folly/Format.h>
20 #include <folly/Memory.h>
21 #include <folly/Random.h>
22 #include <folly/SharedMutex.h>
23 #include <folly/SpinLock.h>
24 #include <folly/ThreadId.h>
26 // ---------------------------------------------------------------------
27 // SSLContext implementation
28 // ---------------------------------------------------------------------
30 struct CRYPTO_dynlock_value {
36 // For OpenSSL portability API
37 using namespace folly::ssl;
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 SSL_CTX_set_options(ctx_, SSL_OP_NO_COMPRESSION);
87 #if FOLLY_OPENSSL_HAS_SNI
88 SSL_CTX_set_tlsext_servername_callback(ctx_, baseServerNameOpenSSLCallback);
89 SSL_CTX_set_tlsext_servername_arg(ctx_, this);
93 SSLContext::~SSLContext() {
94 if (ctx_ != nullptr) {
99 #ifdef OPENSSL_NPN_NEGOTIATED
100 deleteNextProtocolsStrings();
104 void SSLContext::ciphers(const std::string& ciphers) {
105 setCiphersOrThrow(ciphers);
108 void SSLContext::setCipherList(const std::vector<std::string>& ciphers) {
109 if (ciphers.size() == 0) {
112 std::string opensslCipherList;
113 join(":", ciphers, opensslCipherList);
114 setCiphersOrThrow(opensslCipherList);
117 void SSLContext::setSignatureAlgorithms(
118 const std::vector<std::string>& sigalgs) {
119 if (sigalgs.size() == 0) {
122 #if OPENSSL_VERSION_NUMBER >= 0x1000200fL
123 std::string opensslSigAlgsList;
124 join(":", sigalgs, opensslSigAlgsList);
125 int rc = SSL_CTX_set1_sigalgs_list(ctx_, opensslSigAlgsList.c_str());
127 throw std::runtime_error("SSL_CTX_set1_sigalgs_list " + getErrors());
132 void SSLContext::setClientECCurvesList(
133 const std::vector<std::string>& ecCurves) {
134 if (ecCurves.size() == 0) {
137 #if OPENSSL_VERSION_NUMBER >= 0x1000200fL
138 std::string ecCurvesList;
139 join(":", ecCurves, ecCurvesList);
140 int rc = SSL_CTX_set1_curves_list(ctx_, ecCurvesList.c_str());
142 throw std::runtime_error("SSL_CTX_set1_curves_list " + getErrors());
147 void SSLContext::setServerECCurve(const std::string& curveName) {
148 #if OPENSSL_VERSION_NUMBER >= 0x0090800fL && !defined(OPENSSL_NO_ECDH)
149 EC_KEY* ecdh = nullptr;
153 * Elliptic-Curve Diffie-Hellman parameters are either "named curves"
154 * from RFC 4492 section 5.1.1, or explicitly described curves over
155 * binary fields. OpenSSL only supports the "named curves", which provide
156 * maximum interoperability.
159 nid = OBJ_sn2nid(curveName.c_str());
161 LOG(FATAL) << "Unknown curve name:" << curveName.c_str();
163 ecdh = EC_KEY_new_by_curve_name(nid);
164 if (ecdh == nullptr) {
165 LOG(FATAL) << "Unable to create curve:" << curveName.c_str();
168 SSL_CTX_set_tmp_ecdh(ctx_, ecdh);
171 throw std::runtime_error("Elliptic curve encryption not allowed");
175 void SSLContext::setX509VerifyParam(
176 const ssl::X509VerifyParam& x509VerifyParam) {
177 if (!x509VerifyParam) {
180 if (SSL_CTX_set1_param(ctx_, x509VerifyParam.get()) != 1) {
181 throw std::runtime_error("SSL_CTX_set1_param " + getErrors());
185 void SSLContext::setCiphersOrThrow(const std::string& ciphers) {
186 int rc = SSL_CTX_set_cipher_list(ctx_, ciphers.c_str());
188 throw std::runtime_error("SSL_CTX_set_cipher_list: " + getErrors());
190 providedCiphersString_ = ciphers;
193 void SSLContext::setVerificationOption(const SSLContext::SSLVerifyPeerEnum&
195 CHECK(verifyPeer != SSLVerifyPeerEnum::USE_CTX); // dont recurse
196 verifyPeer_ = verifyPeer;
199 int SSLContext::getVerificationMode(const SSLContext::SSLVerifyPeerEnum&
201 CHECK(verifyPeer != SSLVerifyPeerEnum::USE_CTX);
202 int mode = SSL_VERIFY_NONE;
204 // case SSLVerifyPeerEnum::USE_CTX: // can't happen
207 case SSLVerifyPeerEnum::VERIFY:
208 mode = SSL_VERIFY_PEER;
211 case SSLVerifyPeerEnum::VERIFY_REQ_CLIENT_CERT:
212 mode = SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
215 case SSLVerifyPeerEnum::NO_VERIFY:
216 mode = SSL_VERIFY_NONE;
225 int SSLContext::getVerificationMode() {
226 return getVerificationMode(verifyPeer_);
229 void SSLContext::authenticate(bool checkPeerCert, bool checkPeerName,
230 const std::string& peerName) {
233 mode = SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT |
234 SSL_VERIFY_CLIENT_ONCE;
235 checkPeerName_ = checkPeerName;
236 peerFixedName_ = peerName;
238 mode = SSL_VERIFY_NONE;
239 checkPeerName_ = false; // can't check name without cert!
240 peerFixedName_.clear();
242 SSL_CTX_set_verify(ctx_, mode, nullptr);
245 void SSLContext::loadCertificate(const char* path, const char* format) {
246 if (path == nullptr || format == nullptr) {
247 throw std::invalid_argument(
248 "loadCertificateChain: either <path> or <format> is nullptr");
250 if (strcmp(format, "PEM") == 0) {
251 if (SSL_CTX_use_certificate_chain_file(ctx_, path) == 0) {
252 int errnoCopy = errno;
253 std::string reason("SSL_CTX_use_certificate_chain_file: ");
256 reason.append(getErrors(errnoCopy));
257 throw std::runtime_error(reason);
260 throw std::runtime_error(
261 "Unsupported certificate format: " + std::string(format));
265 void SSLContext::loadCertificateFromBufferPEM(folly::StringPiece cert) {
266 if (cert.data() == nullptr) {
267 throw std::invalid_argument("loadCertificate: <cert> is nullptr");
270 ssl::BioUniquePtr bio(BIO_new(BIO_s_mem()));
271 if (bio == nullptr) {
272 throw std::runtime_error("BIO_new: " + getErrors());
275 int written = BIO_write(bio.get(), cert.data(), int(cert.size()));
276 if (written <= 0 || static_cast<unsigned>(written) != cert.size()) {
277 throw std::runtime_error("BIO_write: " + getErrors());
280 ssl::X509UniquePtr x509(
281 PEM_read_bio_X509(bio.get(), nullptr, nullptr, nullptr));
282 if (x509 == nullptr) {
283 throw std::runtime_error("PEM_read_bio_X509: " + getErrors());
286 if (SSL_CTX_use_certificate(ctx_, x509.get()) == 0) {
287 throw std::runtime_error("SSL_CTX_use_certificate: " + getErrors());
291 void SSLContext::loadPrivateKey(const char* path, const char* format) {
292 if (path == nullptr || format == nullptr) {
293 throw std::invalid_argument(
294 "loadPrivateKey: either <path> or <format> is nullptr");
296 if (strcmp(format, "PEM") == 0) {
297 if (SSL_CTX_use_PrivateKey_file(ctx_, path, SSL_FILETYPE_PEM) == 0) {
298 throw std::runtime_error("SSL_CTX_use_PrivateKey_file: " + getErrors());
301 throw std::runtime_error(
302 "Unsupported private key format: " + std::string(format));
306 void SSLContext::loadPrivateKeyFromBufferPEM(folly::StringPiece pkey) {
307 if (pkey.data() == nullptr) {
308 throw std::invalid_argument("loadPrivateKey: <pkey> is nullptr");
311 ssl::BioUniquePtr bio(BIO_new(BIO_s_mem()));
312 if (bio == nullptr) {
313 throw std::runtime_error("BIO_new: " + getErrors());
316 int written = BIO_write(bio.get(), pkey.data(), int(pkey.size()));
317 if (written <= 0 || static_cast<unsigned>(written) != pkey.size()) {
318 throw std::runtime_error("BIO_write: " + getErrors());
321 ssl::EvpPkeyUniquePtr key(
322 PEM_read_bio_PrivateKey(bio.get(), nullptr, nullptr, nullptr));
323 if (key == nullptr) {
324 throw std::runtime_error("PEM_read_bio_PrivateKey: " + getErrors());
327 if (SSL_CTX_use_PrivateKey(ctx_, key.get()) == 0) {
328 throw std::runtime_error("SSL_CTX_use_PrivateKey: " + getErrors());
332 void SSLContext::loadTrustedCertificates(const char* path) {
333 if (path == nullptr) {
334 throw std::invalid_argument("loadTrustedCertificates: <path> is nullptr");
336 if (SSL_CTX_load_verify_locations(ctx_, path, nullptr) == 0) {
337 throw std::runtime_error("SSL_CTX_load_verify_locations: " + getErrors());
342 void SSLContext::loadTrustedCertificates(X509_STORE* store) {
343 SSL_CTX_set_cert_store(ctx_, store);
346 void SSLContext::loadClientCAList(const char* path) {
347 auto clientCAs = SSL_load_client_CA_file(path);
348 if (clientCAs == nullptr) {
349 LOG(ERROR) << "Unable to load ca file: " << path;
352 SSL_CTX_set_client_CA_list(ctx_, clientCAs);
355 void SSLContext::randomize() {
359 void SSLContext::passwordCollector(
360 std::shared_ptr<PasswordCollector> collector) {
361 if (collector == nullptr) {
362 LOG(ERROR) << "passwordCollector: ignore invalid password collector";
365 collector_ = collector;
366 SSL_CTX_set_default_passwd_cb(ctx_, passwordCallback);
367 SSL_CTX_set_default_passwd_cb_userdata(ctx_, this);
370 #if FOLLY_OPENSSL_HAS_SNI
372 void SSLContext::setServerNameCallback(const ServerNameCallback& cb) {
376 void SSLContext::addClientHelloCallback(const ClientHelloCallback& cb) {
377 clientHelloCbs_.push_back(cb);
380 int SSLContext::baseServerNameOpenSSLCallback(SSL* ssl, int* al, void* data) {
381 SSLContext* context = (SSLContext*)data;
383 if (context == nullptr) {
384 return SSL_TLSEXT_ERR_NOACK;
387 for (auto& cb : context->clientHelloCbs_) {
388 // Generic callbacks to happen after we receive the Client Hello.
389 // For example, we use one to switch which cipher we use depending
390 // on the user's TLS version. Because the primary purpose of
391 // baseServerNameOpenSSLCallback is for SNI support, and these callbacks
392 // are side-uses, we ignore any possible failures other than just logging
397 if (!context->serverNameCb_) {
398 return SSL_TLSEXT_ERR_NOACK;
401 ServerNameCallbackResult ret = context->serverNameCb_(ssl);
403 case SERVER_NAME_FOUND:
404 return SSL_TLSEXT_ERR_OK;
405 case SERVER_NAME_NOT_FOUND:
406 return SSL_TLSEXT_ERR_NOACK;
407 case SERVER_NAME_NOT_FOUND_ALERT_FATAL:
408 *al = TLS1_AD_UNRECOGNIZED_NAME;
409 return SSL_TLSEXT_ERR_ALERT_FATAL;
414 return SSL_TLSEXT_ERR_NOACK;
417 void SSLContext::switchCiphersIfTLS11(
419 const std::string& tls11CipherString,
420 const std::vector<std::pair<std::string, int>>& tls11AltCipherlist) {
421 CHECK(!(tls11CipherString.empty() && tls11AltCipherlist.empty()))
422 << "Shouldn't call if empty ciphers / alt ciphers";
424 if (TLS1_get_client_version(ssl) <= TLS1_VERSION) {
425 // We only do this for TLS v 1.1 and later
429 const std::string* ciphers = &tls11CipherString;
430 if (!tls11AltCipherlist.empty()) {
431 if (!cipherListPicker_) {
432 std::vector<int> weights;
434 tls11AltCipherlist.begin(),
435 tls11AltCipherlist.end(),
436 [&](const std::pair<std::string, int>& e) {
437 weights.push_back(e.second);
439 cipherListPicker_.reset(
440 new std::discrete_distribution<int>(weights.begin(), weights.end()));
442 auto rng = ThreadLocalPRNG();
443 auto index = (*cipherListPicker_)(rng);
444 if ((size_t)index >= tls11AltCipherlist.size()) {
445 LOG(ERROR) << "Trying to pick alt TLS11 cipher index " << index
446 << ", but tls11AltCipherlist is of length "
447 << tls11AltCipherlist.size();
449 ciphers = &tls11AltCipherlist[size_t(index)].first;
453 // Prefer AES for TLS versions 1.1 and later since these are not
454 // vulnerable to BEAST attacks on AES. Note that we're setting the
455 // cipher list on the SSL object, not the SSL_CTX object, so it will
456 // only last for this request.
457 int rc = SSL_set_cipher_list(ssl, ciphers->c_str());
458 if ((rc == 0) || ERR_peek_error() != 0) {
459 // This shouldn't happen since we checked for this when proxygen
461 LOG(WARNING) << "ssl_cipher: No specified ciphers supported for switch";
462 SSL_set_cipher_list(ssl, providedCiphersString_.c_str());
465 #endif // FOLLY_OPENSSL_HAS_SNI
467 #if FOLLY_OPENSSL_HAS_ALPN
468 int SSLContext::alpnSelectCallback(SSL* /* ssl */,
469 const unsigned char** out,
470 unsigned char* outlen,
471 const unsigned char* in,
474 SSLContext* context = (SSLContext*)data;
476 if (context->advertisedNextProtocols_.empty()) {
480 auto i = context->pickNextProtocols();
481 const auto& item = context->advertisedNextProtocols_[i];
482 if (SSL_select_next_proto((unsigned char**)out,
487 inlen) != OPENSSL_NPN_NEGOTIATED) {
488 return SSL_TLSEXT_ERR_NOACK;
491 return SSL_TLSEXT_ERR_OK;
493 #endif // FOLLY_OPENSSL_HAS_ALPN
495 #ifdef OPENSSL_NPN_NEGOTIATED
497 bool SSLContext::setAdvertisedNextProtocols(
498 const std::list<std::string>& protocols, NextProtocolType protocolType) {
499 return setRandomizedAdvertisedNextProtocols({{1, protocols}}, protocolType);
502 bool SSLContext::setRandomizedAdvertisedNextProtocols(
503 const std::list<NextProtocolsItem>& items, NextProtocolType protocolType) {
504 unsetNextProtocols();
505 if (items.size() == 0) {
508 int total_weight = 0;
509 for (const auto &item : items) {
510 if (item.protocols.size() == 0) {
513 AdvertisedNextProtocolsItem advertised_item;
514 advertised_item.length = 0;
515 for (const auto& proto : item.protocols) {
516 ++advertised_item.length;
517 auto protoLength = proto.length();
518 if (protoLength >= 256) {
519 deleteNextProtocolsStrings();
522 advertised_item.length += unsigned(protoLength);
524 advertised_item.protocols = new unsigned char[advertised_item.length];
525 if (!advertised_item.protocols) {
526 throw std::runtime_error("alloc failure");
528 unsigned char* dst = advertised_item.protocols;
529 for (auto& proto : item.protocols) {
530 uint8_t protoLength = uint8_t(proto.length());
531 *dst++ = (unsigned char)protoLength;
532 memcpy(dst, proto.data(), protoLength);
535 total_weight += item.weight;
536 advertisedNextProtocols_.push_back(advertised_item);
537 advertisedNextProtocolWeights_.push_back(item.weight);
539 if (total_weight == 0) {
540 deleteNextProtocolsStrings();
543 nextProtocolDistribution_ =
544 std::discrete_distribution<>(advertisedNextProtocolWeights_.begin(),
545 advertisedNextProtocolWeights_.end());
546 if ((uint8_t)protocolType & (uint8_t)NextProtocolType::NPN) {
547 SSL_CTX_set_next_protos_advertised_cb(
548 ctx_, advertisedNextProtocolCallback, this);
549 SSL_CTX_set_next_proto_select_cb(ctx_, selectNextProtocolCallback, this);
551 #if FOLLY_OPENSSL_HAS_ALPN
552 if ((uint8_t)protocolType & (uint8_t)NextProtocolType::ALPN) {
553 SSL_CTX_set_alpn_select_cb(ctx_, alpnSelectCallback, this);
554 // Client cannot really use randomized alpn
555 SSL_CTX_set_alpn_protos(ctx_,
556 advertisedNextProtocols_[0].protocols,
557 advertisedNextProtocols_[0].length);
563 void SSLContext::deleteNextProtocolsStrings() {
564 for (auto protocols : advertisedNextProtocols_) {
565 delete[] protocols.protocols;
567 advertisedNextProtocols_.clear();
568 advertisedNextProtocolWeights_.clear();
571 void SSLContext::unsetNextProtocols() {
572 deleteNextProtocolsStrings();
573 SSL_CTX_set_next_protos_advertised_cb(ctx_, nullptr, nullptr);
574 SSL_CTX_set_next_proto_select_cb(ctx_, nullptr, nullptr);
575 #if FOLLY_OPENSSL_HAS_ALPN
576 SSL_CTX_set_alpn_select_cb(ctx_, nullptr, nullptr);
577 SSL_CTX_set_alpn_protos(ctx_, nullptr, 0);
581 size_t SSLContext::pickNextProtocols() {
582 CHECK(!advertisedNextProtocols_.empty()) << "Failed to pickNextProtocols";
583 auto rng = ThreadLocalPRNG();
584 return size_t(nextProtocolDistribution_(rng));
587 int SSLContext::advertisedNextProtocolCallback(SSL* ssl,
588 const unsigned char** out, unsigned int* outlen, void* data) {
589 SSLContext* context = (SSLContext*)data;
590 if (context == nullptr || context->advertisedNextProtocols_.empty()) {
593 } else if (context->advertisedNextProtocols_.size() == 1) {
594 *out = context->advertisedNextProtocols_[0].protocols;
595 *outlen = context->advertisedNextProtocols_[0].length;
597 uintptr_t selected_index = reinterpret_cast<uintptr_t>(SSL_get_ex_data(ssl,
598 sNextProtocolsExDataIndex_));
599 if (selected_index) {
601 *out = context->advertisedNextProtocols_[selected_index].protocols;
602 *outlen = context->advertisedNextProtocols_[selected_index].length;
604 auto i = context->pickNextProtocols();
605 uintptr_t selected = i + 1;
606 SSL_set_ex_data(ssl, sNextProtocolsExDataIndex_, (void*)selected);
607 *out = context->advertisedNextProtocols_[i].protocols;
608 *outlen = context->advertisedNextProtocols_[i].length;
611 return SSL_TLSEXT_ERR_OK;
614 int SSLContext::selectNextProtocolCallback(SSL* ssl,
616 unsigned char* outlen,
617 const unsigned char* server,
618 unsigned int server_len,
620 (void)ssl; // Make -Wunused-parameters happy
621 SSLContext* ctx = (SSLContext*)data;
622 if (ctx->advertisedNextProtocols_.size() > 1) {
623 VLOG(3) << "SSLContext::selectNextProcolCallback() "
624 << "client should be deterministic in selecting protocols.";
627 unsigned char* client = nullptr;
628 unsigned int client_len = 0;
629 bool filtered = false;
630 auto cpf = ctx->getClientProtocolFilterCallback();
632 filtered = (*cpf)(&client, &client_len, server, server_len);
636 if (ctx->advertisedNextProtocols_.empty()) {
637 client = (unsigned char *) "";
640 client = ctx->advertisedNextProtocols_[0].protocols;
641 client_len = ctx->advertisedNextProtocols_[0].length;
645 int retval = SSL_select_next_proto(out, outlen, server, server_len,
647 if (retval != OPENSSL_NPN_NEGOTIATED) {
648 VLOG(3) << "SSLContext::selectNextProcolCallback() "
649 << "unable to pick a next protocol.";
651 return SSL_TLSEXT_ERR_OK;
653 #endif // OPENSSL_NPN_NEGOTIATED
655 SSL* SSLContext::createSSL() const {
656 SSL* ssl = SSL_new(ctx_);
657 if (ssl == nullptr) {
658 throw std::runtime_error("SSL_new: " + getErrors());
663 void SSLContext::setSessionCacheContext(const std::string& context) {
664 SSL_CTX_set_session_id_context(
666 reinterpret_cast<const unsigned char*>(context.data()),
667 std::min<unsigned int>(
668 static_cast<unsigned int>(context.length()),
669 SSL_MAX_SSL_SESSION_ID_LENGTH));
673 * Match a name with a pattern. The pattern may include wildcard. A single
674 * wildcard "*" can match up to one component in the domain name.
676 * @param host Host name, typically the name of the remote host
677 * @param pattern Name retrieved from certificate
678 * @param size Size of "pattern"
679 * @return True, if "host" matches "pattern". False otherwise.
681 bool SSLContext::matchName(const char* host, const char* pattern, int size) {
684 while (i < size && host[j] != '\0') {
685 if (toupper(pattern[i]) == toupper(host[j])) {
690 if (pattern[i] == '*') {
691 while (host[j] != '.' && host[j] != '\0') {
699 if (i == size && host[j] == '\0') {
705 int SSLContext::passwordCallback(char* password,
709 SSLContext* context = (SSLContext*)data;
710 if (context == nullptr || context->passwordCollector() == nullptr) {
713 std::string userPassword;
714 // call user defined password collector to get password
715 context->passwordCollector()->getPassword(userPassword, size);
716 auto length = int(userPassword.size());
720 strncpy(password, userPassword.c_str(), size_t(length));
726 SSLContext::SSLLockType inLockType = SSLContext::LOCK_MUTEX) :
727 lockType(inLockType) {
730 void lock(bool read) {
731 if (lockType == SSLContext::LOCK_MUTEX) {
733 } else if (lockType == SSLContext::LOCK_SPINLOCK) {
735 } else if (lockType == SSLContext::LOCK_SHAREDMUTEX) {
737 sharedMutex.lock_shared();
742 // lockType == LOCK_NONE, no-op
745 void unlock(bool read) {
746 if (lockType == SSLContext::LOCK_MUTEX) {
748 } else if (lockType == SSLContext::LOCK_SPINLOCK) {
750 } else if (lockType == SSLContext::LOCK_SHAREDMUTEX) {
752 sharedMutex.unlock_shared();
754 sharedMutex.unlock();
757 // lockType == LOCK_NONE, no-op
760 SSLContext::SSLLockType lockType;
761 folly::SpinLock spinLock{};
763 SharedMutex sharedMutex;
766 // Statics are unsafe in environments that call exit().
767 // If one thread calls exit() while another thread is
768 // references a member of SSLContext, bad things can happen.
769 // SSLContext runs in such environments.
770 // Instead of declaring a static member we "new" the static
771 // member so that it won't be destructed on exit().
772 static std::unique_ptr<SSLLock[]>& locks() {
773 static auto locksInst = new std::unique_ptr<SSLLock[]>();
777 static std::map<int, SSLContext::SSLLockType>& lockTypes() {
778 static auto lockTypesInst = new std::map<int, SSLContext::SSLLockType>();
779 return *lockTypesInst;
782 static void callbackLocking(int mode, int n, const char*, int) {
783 if (mode & CRYPTO_LOCK) {
784 locks()[size_t(n)].lock(mode & CRYPTO_READ);
786 locks()[size_t(n)].unlock(mode & CRYPTO_READ);
790 static unsigned long callbackThreadID() {
791 return static_cast<unsigned long>(folly::getCurrentThreadID());
794 static CRYPTO_dynlock_value* dyn_create(const char*, int) {
795 return new CRYPTO_dynlock_value;
798 static void dyn_lock(int mode,
799 struct CRYPTO_dynlock_value* lock,
801 if (lock != nullptr) {
802 if (mode & CRYPTO_LOCK) {
805 lock->mutex.unlock();
810 static void dyn_destroy(struct CRYPTO_dynlock_value* lock, const char*, int) {
814 void SSLContext::setSSLLockTypesLocked(std::map<int, SSLLockType> inLockTypes) {
815 lockTypes() = inLockTypes;
818 void SSLContext::setSSLLockTypes(std::map<int, SSLLockType> inLockTypes) {
819 std::lock_guard<std::mutex> g(initMutex());
821 // We set the locks on initialization, so if we are already initialized
822 // this would have no affect.
823 LOG(INFO) << "Ignoring setSSLLockTypes after initialization";
826 setSSLLockTypesLocked(std::move(inLockTypes));
829 void SSLContext::setSSLLockTypesAndInitOpenSSL(
830 std::map<int, SSLLockType> inLockTypes) {
831 std::lock_guard<std::mutex> g(initMutex());
832 CHECK(!initialized_) << "OpenSSL is already initialized";
833 setSSLLockTypesLocked(std::move(inLockTypes));
834 initializeOpenSSLLocked();
837 bool SSLContext::isSSLLockDisabled(int lockId) {
838 std::lock_guard<std::mutex> g(initMutex());
839 CHECK(initialized_) << "OpenSSL is not initialized yet";
840 const auto& sslLocks = lockTypes();
841 const auto it = sslLocks.find(lockId);
842 return it != sslLocks.end() &&
843 it->second == SSLContext::SSLLockType::LOCK_NONE;
846 #if defined(SSL_MODE_HANDSHAKE_CUTTHROUGH)
847 void SSLContext::enableFalseStart() {
848 SSL_CTX_set_mode(ctx_, SSL_MODE_HANDSHAKE_CUTTHROUGH);
852 void SSLContext::markInitialized() {
853 std::lock_guard<std::mutex> g(initMutex());
857 void SSLContext::initializeOpenSSL() {
858 std::lock_guard<std::mutex> g(initMutex());
859 initializeOpenSSLLocked();
862 void SSLContext::initializeOpenSSLLocked() {
867 SSL_load_error_strings();
868 ERR_load_crypto_strings();
870 locks().reset(new SSLLock[size_t(CRYPTO_num_locks())]);
871 for (auto it: lockTypes()) {
872 locks()[size_t(it.first)].lockType = it.second;
874 CRYPTO_set_id_callback(callbackThreadID);
875 CRYPTO_set_locking_callback(callbackLocking);
877 CRYPTO_set_dynlock_create_callback(dyn_create);
878 CRYPTO_set_dynlock_lock_callback(dyn_lock);
879 CRYPTO_set_dynlock_destroy_callback(dyn_destroy);
881 #ifdef OPENSSL_NPN_NEGOTIATED
882 sNextProtocolsExDataIndex_ = SSL_get_ex_new_index(0,
883 (void*)"Advertised next protocol index", nullptr, nullptr, nullptr);
888 void SSLContext::cleanupOpenSSL() {
889 std::lock_guard<std::mutex> g(initMutex());
890 cleanupOpenSSLLocked();
893 void SSLContext::cleanupOpenSSLLocked() {
898 CRYPTO_set_id_callback(nullptr);
899 CRYPTO_set_locking_callback(nullptr);
900 CRYPTO_set_dynlock_create_callback(nullptr);
901 CRYPTO_set_dynlock_lock_callback(nullptr);
902 CRYPTO_set_dynlock_destroy_callback(nullptr);
903 CRYPTO_cleanup_all_ex_data();
908 initialized_ = false;
911 void SSLContext::setOptions(long options) {
912 long newOpt = SSL_CTX_set_options(ctx_, options);
913 if ((newOpt & options) != options) {
914 throw std::runtime_error("SSL_CTX_set_options failed");
918 std::string SSLContext::getErrors(int errnoCopy) {
920 unsigned long errorCode;
924 while ((errorCode = ERR_get_error()) != 0) {
925 if (!errors.empty()) {
928 const char* reason = ERR_reason_error_string(errorCode);
929 if (reason == nullptr) {
930 snprintf(message, sizeof(message) - 1, "SSL error # %lu", errorCode);
935 if (errors.empty()) {
936 errors = "error code: " + folly::to<std::string>(errnoCopy);
942 operator<<(std::ostream& os, const PasswordCollector& collector) {
943 os << collector.describe();