2 * Copyright 2004-present 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 <folly/ssl/detail/OpenSSLThreading.h>
22 #include <folly/Portability.h>
23 #include <folly/SharedMutex.h>
24 #include <folly/SpinLock.h>
26 #include <glog/logging.h>
28 // We cannot directly use portability/openssl because it also depends on us.
29 // Therefore we directly use openssl includes. Order of includes is important
30 // here. See portability/openssl.h.
31 #include <folly/portability/Windows.h>
32 #include <openssl/crypto.h>
34 #if !defined(OPENSSL_IS_BORINGSSL)
35 #define FOLLY_SSL_DETAIL_OPENSSL_IS_110 (OPENSSL_VERSION_NUMBER >= 0x10100000L)
37 #define FOLLY_SSL_DETAIL_OPENSSL_IS_110 (false)
40 // OpenSSL requires us to provide the implementation of CRYPTO_dynlock_value
41 // so it must be done in the global namespace.
42 struct CRYPTO_dynlock_value {
50 static std::map<int, LockType>& lockTypes() {
51 static auto lockTypesInst = new std::map<int, LockType>();
52 return *lockTypesInst;
55 void setLockTypes(std::map<int, LockType> inLockTypes) {
56 #if FOLLY_SSL_DETAIL_OPENSSL_IS_110
57 LOG(INFO) << "setLockTypes() is unsupported on OpenSSL >= 1.1.0. "
58 << "OpenSSL now uses platform native mutexes";
61 lockTypes() = inLockTypes;
64 bool isSSLLockDisabled(int lockId) {
65 const auto& sslLocks = lockTypes();
66 const auto it = sslLocks.find(lockId);
67 return it != sslLocks.end() && it->second == LockType::NONE;
72 explicit SSLLock(LockType inLockType = LockType::MUTEX)
73 : lockType(inLockType) {}
75 void lock(bool read) {
76 if (lockType == LockType::MUTEX) {
78 } else if (lockType == LockType::SPINLOCK) {
80 } else if (lockType == LockType::SHAREDMUTEX) {
82 sharedMutex.lock_shared();
87 // lockType == LOCK_NONE, no-op
90 void unlock(bool read) {
91 if (lockType == LockType::MUTEX) {
93 } else if (lockType == LockType::SPINLOCK) {
95 } else if (lockType == LockType::SHAREDMUTEX) {
97 sharedMutex.unlock_shared();
102 // lockType == LOCK_NONE, no-op
106 folly::SpinLock spinLock{};
108 SharedMutex sharedMutex;
112 // Statics are unsafe in environments that call exit().
113 // If one thread calls exit() while another thread is
114 // references a member of SSLContext, bad things can happen.
115 // SSLContext runs in such environments.
116 // Instead of declaring a static member we "new" the static
117 // member so that it won't be destructed on exit().
118 static std::unique_ptr<SSLLock[]>& locks() {
119 static auto locksInst = new std::unique_ptr<SSLLock[]>();
123 static void callbackLocking(int mode, int n, const char*, int) {
124 if (mode & CRYPTO_LOCK) {
125 locks()[size_t(n)].lock(mode & CRYPTO_READ);
127 locks()[size_t(n)].unlock(mode & CRYPTO_READ);
131 static unsigned long callbackThreadID() {
132 return static_cast<unsigned long>(folly::getCurrentThreadID());
135 static CRYPTO_dynlock_value* dyn_create(const char*, int) {
136 return new CRYPTO_dynlock_value;
140 dyn_lock(int mode, struct CRYPTO_dynlock_value* lock, const char*, int) {
141 if (lock != nullptr) {
142 if (mode & CRYPTO_LOCK) {
145 lock->mutex.unlock();
150 static void dyn_destroy(struct CRYPTO_dynlock_value* lock, const char*, int) {
154 void installThreadingLocks() {
156 locks() = std::make_unique<SSLLock[]>(size_t(CRYPTO_num_locks()));
157 for (auto it : lockTypes()) {
158 locks()[size_t(it.first)].lockType = it.second;
160 CRYPTO_set_id_callback(callbackThreadID);
161 CRYPTO_set_locking_callback(callbackLocking);
163 CRYPTO_set_dynlock_create_callback(dyn_create);
164 CRYPTO_set_dynlock_lock_callback(dyn_lock);
165 CRYPTO_set_dynlock_destroy_callback(dyn_destroy);
168 void cleanupThreadingLocks() {
169 CRYPTO_set_id_callback(nullptr);
170 CRYPTO_set_locking_callback(nullptr);
171 CRYPTO_set_dynlock_create_callback(nullptr);
172 CRYPTO_set_dynlock_lock_callback(nullptr);
173 CRYPTO_set_dynlock_destroy_callback(nullptr);
177 } // namespace detail