OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2008 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2008 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 22 matching lines...) Expand all Loading... |
33 | 33 |
34 #include "webrtc/base/arraysize.h" | 34 #include "webrtc/base/arraysize.h" |
35 #include "webrtc/base/common.h" | 35 #include "webrtc/base/common.h" |
36 #include "webrtc/base/logging.h" | 36 #include "webrtc/base/logging.h" |
37 #include "webrtc/base/openssl.h" | 37 #include "webrtc/base/openssl.h" |
38 #include "webrtc/base/safe_conversions.h" | 38 #include "webrtc/base/safe_conversions.h" |
39 #include "webrtc/base/sslroots.h" | 39 #include "webrtc/base/sslroots.h" |
40 #include "webrtc/base/stringutils.h" | 40 #include "webrtc/base/stringutils.h" |
41 #include "webrtc/base/thread.h" | 41 #include "webrtc/base/thread.h" |
42 | 42 |
| 43 #ifndef OPENSSL_IS_BORINGSSL |
| 44 |
| 45 // TODO: Use a nicer abstraction for mutex. |
| 46 |
| 47 #if defined(WEBRTC_WIN) |
| 48 #define MUTEX_TYPE HANDLE |
| 49 #define MUTEX_SETUP(x) (x) = CreateMutex(NULL, FALSE, NULL) |
| 50 #define MUTEX_CLEANUP(x) CloseHandle(x) |
| 51 #define MUTEX_LOCK(x) WaitForSingleObject((x), INFINITE) |
| 52 #define MUTEX_UNLOCK(x) ReleaseMutex(x) |
| 53 #define THREAD_ID GetCurrentThreadId() |
| 54 #elif defined(WEBRTC_POSIX) |
| 55 #define MUTEX_TYPE pthread_mutex_t |
| 56 #define MUTEX_SETUP(x) pthread_mutex_init(&(x), NULL) |
| 57 #define MUTEX_CLEANUP(x) pthread_mutex_destroy(&(x)) |
| 58 #define MUTEX_LOCK(x) pthread_mutex_lock(&(x)) |
| 59 #define MUTEX_UNLOCK(x) pthread_mutex_unlock(&(x)) |
| 60 #define THREAD_ID pthread_self() |
| 61 #else |
| 62 #error You must define mutex operations appropriate for your platform! |
| 63 #endif |
| 64 |
| 65 struct CRYPTO_dynlock_value { |
| 66 MUTEX_TYPE mutex; |
| 67 }; |
| 68 |
| 69 #endif // #ifndef OPENSSL_IS_BORINGSSL |
| 70 |
43 ////////////////////////////////////////////////////////////////////// | 71 ////////////////////////////////////////////////////////////////////// |
44 // SocketBIO | 72 // SocketBIO |
45 ////////////////////////////////////////////////////////////////////// | 73 ////////////////////////////////////////////////////////////////////// |
46 | 74 |
47 static int socket_write(BIO* h, const char* buf, int num); | 75 static int socket_write(BIO* h, const char* buf, int num); |
48 static int socket_read(BIO* h, char* buf, int size); | 76 static int socket_read(BIO* h, char* buf, int size); |
49 static int socket_puts(BIO* h, const char* str); | 77 static int socket_puts(BIO* h, const char* str); |
50 static long socket_ctrl(BIO* h, int cmd, long arg1, void* arg2); | 78 static long socket_ctrl(BIO* h, int cmd, long arg1, void* arg2); |
51 static int socket_new(BIO* h); | 79 static int socket_new(BIO* h); |
52 static int socket_free(BIO* data); | 80 static int socket_free(BIO* data); |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 return 0; | 170 return 0; |
143 } | 171 } |
144 } | 172 } |
145 | 173 |
146 ///////////////////////////////////////////////////////////////////////////// | 174 ///////////////////////////////////////////////////////////////////////////// |
147 // OpenSSLAdapter | 175 // OpenSSLAdapter |
148 ///////////////////////////////////////////////////////////////////////////// | 176 ///////////////////////////////////////////////////////////////////////////// |
149 | 177 |
150 namespace rtc { | 178 namespace rtc { |
151 | 179 |
| 180 #ifndef OPENSSL_IS_BORINGSSL |
| 181 |
| 182 // This array will store all of the mutexes available to OpenSSL. |
| 183 static MUTEX_TYPE* mutex_buf = NULL; |
| 184 |
| 185 static void locking_function(int mode, int n, const char * file, int line) { |
| 186 if (mode & CRYPTO_LOCK) { |
| 187 MUTEX_LOCK(mutex_buf[n]); |
| 188 } else { |
| 189 MUTEX_UNLOCK(mutex_buf[n]); |
| 190 } |
| 191 } |
| 192 |
| 193 static unsigned long id_function() { // NOLINT |
| 194 // Use old-style C cast because THREAD_ID's type varies with the platform, |
| 195 // in some cases requiring static_cast, and in others requiring |
| 196 // reinterpret_cast. |
| 197 return (unsigned long)THREAD_ID; // NOLINT |
| 198 } |
| 199 |
| 200 static CRYPTO_dynlock_value* dyn_create_function(const char* file, int line) { |
| 201 CRYPTO_dynlock_value* value = new CRYPTO_dynlock_value; |
| 202 if (!value) |
| 203 return NULL; |
| 204 MUTEX_SETUP(value->mutex); |
| 205 return value; |
| 206 } |
| 207 |
| 208 static void dyn_lock_function(int mode, CRYPTO_dynlock_value* l, |
| 209 const char* file, int line) { |
| 210 if (mode & CRYPTO_LOCK) { |
| 211 MUTEX_LOCK(l->mutex); |
| 212 } else { |
| 213 MUTEX_UNLOCK(l->mutex); |
| 214 } |
| 215 } |
| 216 |
| 217 static void dyn_destroy_function(CRYPTO_dynlock_value* l, |
| 218 const char* file, int line) { |
| 219 MUTEX_CLEANUP(l->mutex); |
| 220 delete l; |
| 221 } |
| 222 |
| 223 #endif // #ifndef OPENSSL_IS_BORINGSSL |
| 224 |
152 VerificationCallback OpenSSLAdapter::custom_verify_callback_ = NULL; | 225 VerificationCallback OpenSSLAdapter::custom_verify_callback_ = NULL; |
153 | 226 |
154 bool OpenSSLAdapter::InitializeSSL(VerificationCallback callback) { | 227 bool OpenSSLAdapter::InitializeSSL(VerificationCallback callback) { |
155 CRYPTO_library_init(); | 228 if (!InitializeSSLThread() || !SSL_library_init()) |
| 229 return false; |
| 230 #if !defined(ADDRESS_SANITIZER) || !defined(WEBRTC_MAC) || defined(WEBRTC_IOS) |
| 231 // Loading the error strings crashes mac_asan. Omit this debugging aid there. |
| 232 SSL_load_error_strings(); |
| 233 #endif |
| 234 ERR_load_BIO_strings(); |
| 235 OpenSSL_add_all_algorithms(); |
| 236 RAND_poll(); |
156 custom_verify_callback_ = callback; | 237 custom_verify_callback_ = callback; |
157 return true; | 238 return true; |
158 } | 239 } |
159 | 240 |
| 241 bool OpenSSLAdapter::InitializeSSLThread() { |
| 242 // BoringSSL is doing the locking internally, so the callbacks are not used |
| 243 // in this case (and are no-ops anyways). |
| 244 #ifndef OPENSSL_IS_BORINGSSL |
| 245 mutex_buf = new MUTEX_TYPE[CRYPTO_num_locks()]; |
| 246 if (!mutex_buf) |
| 247 return false; |
| 248 for (int i = 0; i < CRYPTO_num_locks(); ++i) |
| 249 MUTEX_SETUP(mutex_buf[i]); |
| 250 |
| 251 // we need to cast our id_function to return an unsigned long -- pthread_t is |
| 252 // a pointer |
| 253 CRYPTO_set_id_callback(id_function); |
| 254 CRYPTO_set_locking_callback(locking_function); |
| 255 CRYPTO_set_dynlock_create_callback(dyn_create_function); |
| 256 CRYPTO_set_dynlock_lock_callback(dyn_lock_function); |
| 257 CRYPTO_set_dynlock_destroy_callback(dyn_destroy_function); |
| 258 #endif // #ifndef OPENSSL_IS_BORINGSSL |
| 259 return true; |
| 260 } |
| 261 |
| 262 bool OpenSSLAdapter::CleanupSSL() { |
| 263 #ifndef OPENSSL_IS_BORINGSSL |
| 264 if (!mutex_buf) |
| 265 return false; |
| 266 CRYPTO_set_id_callback(NULL); |
| 267 CRYPTO_set_locking_callback(NULL); |
| 268 CRYPTO_set_dynlock_create_callback(NULL); |
| 269 CRYPTO_set_dynlock_lock_callback(NULL); |
| 270 CRYPTO_set_dynlock_destroy_callback(NULL); |
| 271 for (int i = 0; i < CRYPTO_num_locks(); ++i) |
| 272 MUTEX_CLEANUP(mutex_buf[i]); |
| 273 delete [] mutex_buf; |
| 274 mutex_buf = NULL; |
| 275 #endif // #ifndef OPENSSL_IS_BORINGSSL |
| 276 return true; |
| 277 } |
| 278 |
160 OpenSSLAdapter::OpenSSLAdapter(AsyncSocket* socket) | 279 OpenSSLAdapter::OpenSSLAdapter(AsyncSocket* socket) |
161 : SSLAdapter(socket), | 280 : SSLAdapter(socket), |
162 state_(SSL_NONE), | 281 state_(SSL_NONE), |
163 ssl_read_needs_write_(false), | 282 ssl_read_needs_write_(false), |
164 ssl_write_needs_read_(false), | 283 ssl_write_needs_read_(false), |
165 restartable_(false), | 284 restartable_(false), |
166 ssl_(NULL), ssl_ctx_(NULL), | 285 ssl_(NULL), ssl_ctx_(NULL), |
167 ssl_mode_(SSL_MODE_TLS), | 286 ssl_mode_(SSL_MODE_TLS), |
168 custom_verification_succeeded_(false) { | 287 custom_verification_succeeded_(false) { |
169 } | 288 } |
(...skipping 672 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
842 if (ssl_mode_ == SSL_MODE_DTLS) { | 961 if (ssl_mode_ == SSL_MODE_DTLS) { |
843 SSL_CTX_set_read_ahead(ctx, 1); | 962 SSL_CTX_set_read_ahead(ctx, 1); |
844 } | 963 } |
845 | 964 |
846 return ctx; | 965 return ctx; |
847 } | 966 } |
848 | 967 |
849 } // namespace rtc | 968 } // namespace rtc |
850 | 969 |
851 #endif // HAVE_OPENSSL_SSL_H | 970 #endif // HAVE_OPENSSL_SSL_H |
OLD | NEW |