| 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 21 matching lines...) Expand all Loading... |
| 32 #endif // HAVE_CONFIG_H | 32 #endif // HAVE_CONFIG_H |
| 33 | 33 |
| 34 #include "webrtc/base/common.h" | 34 #include "webrtc/base/common.h" |
| 35 #include "webrtc/base/logging.h" | 35 #include "webrtc/base/logging.h" |
| 36 #include "webrtc/base/openssl.h" | 36 #include "webrtc/base/openssl.h" |
| 37 #include "webrtc/base/safe_conversions.h" | 37 #include "webrtc/base/safe_conversions.h" |
| 38 #include "webrtc/base/sslroots.h" | 38 #include "webrtc/base/sslroots.h" |
| 39 #include "webrtc/base/stringutils.h" | 39 #include "webrtc/base/stringutils.h" |
| 40 #include "webrtc/base/thread.h" | 40 #include "webrtc/base/thread.h" |
| 41 | 41 |
| 42 #ifndef OPENSSL_IS_BORINGSSL |
| 43 |
| 42 // TODO: Use a nicer abstraction for mutex. | 44 // TODO: Use a nicer abstraction for mutex. |
| 43 | 45 |
| 44 #if defined(WEBRTC_WIN) | 46 #if defined(WEBRTC_WIN) |
| 45 #define MUTEX_TYPE HANDLE | 47 #define MUTEX_TYPE HANDLE |
| 46 #define MUTEX_SETUP(x) (x) = CreateMutex(NULL, FALSE, NULL) | 48 #define MUTEX_SETUP(x) (x) = CreateMutex(NULL, FALSE, NULL) |
| 47 #define MUTEX_CLEANUP(x) CloseHandle(x) | 49 #define MUTEX_CLEANUP(x) CloseHandle(x) |
| 48 #define MUTEX_LOCK(x) WaitForSingleObject((x), INFINITE) | 50 #define MUTEX_LOCK(x) WaitForSingleObject((x), INFINITE) |
| 49 #define MUTEX_UNLOCK(x) ReleaseMutex(x) | 51 #define MUTEX_UNLOCK(x) ReleaseMutex(x) |
| 50 #define THREAD_ID GetCurrentThreadId() | 52 #define THREAD_ID GetCurrentThreadId() |
| 51 #elif defined(WEBRTC_POSIX) | 53 #elif defined(WEBRTC_POSIX) |
| 52 #define MUTEX_TYPE pthread_mutex_t | 54 #define MUTEX_TYPE pthread_mutex_t |
| 53 #define MUTEX_SETUP(x) pthread_mutex_init(&(x), NULL) | 55 #define MUTEX_SETUP(x) pthread_mutex_init(&(x), NULL) |
| 54 #define MUTEX_CLEANUP(x) pthread_mutex_destroy(&(x)) | 56 #define MUTEX_CLEANUP(x) pthread_mutex_destroy(&(x)) |
| 55 #define MUTEX_LOCK(x) pthread_mutex_lock(&(x)) | 57 #define MUTEX_LOCK(x) pthread_mutex_lock(&(x)) |
| 56 #define MUTEX_UNLOCK(x) pthread_mutex_unlock(&(x)) | 58 #define MUTEX_UNLOCK(x) pthread_mutex_unlock(&(x)) |
| 57 #define THREAD_ID pthread_self() | 59 #define THREAD_ID pthread_self() |
| 58 #else | 60 #else |
| 59 #error You must define mutex operations appropriate for your platform! | 61 #error You must define mutex operations appropriate for your platform! |
| 60 #endif | 62 #endif |
| 61 | 63 |
| 62 struct CRYPTO_dynlock_value { | 64 struct CRYPTO_dynlock_value { |
| 63 MUTEX_TYPE mutex; | 65 MUTEX_TYPE mutex; |
| 64 }; | 66 }; |
| 65 | 67 |
| 68 #endif // #ifndef OPENSSL_IS_BORINGSSL |
| 69 |
| 66 ////////////////////////////////////////////////////////////////////// | 70 ////////////////////////////////////////////////////////////////////// |
| 67 // SocketBIO | 71 // SocketBIO |
| 68 ////////////////////////////////////////////////////////////////////// | 72 ////////////////////////////////////////////////////////////////////// |
| 69 | 73 |
| 70 static int socket_write(BIO* h, const char* buf, int num); | 74 static int socket_write(BIO* h, const char* buf, int num); |
| 71 static int socket_read(BIO* h, char* buf, int size); | 75 static int socket_read(BIO* h, char* buf, int size); |
| 72 static int socket_puts(BIO* h, const char* str); | 76 static int socket_puts(BIO* h, const char* str); |
| 73 static long socket_ctrl(BIO* h, int cmd, long arg1, void* arg2); | 77 static long socket_ctrl(BIO* h, int cmd, long arg1, void* arg2); |
| 74 static int socket_new(BIO* h); | 78 static int socket_new(BIO* h); |
| 75 static int socket_free(BIO* data); | 79 static int socket_free(BIO* data); |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 return 0; | 169 return 0; |
| 166 } | 170 } |
| 167 } | 171 } |
| 168 | 172 |
| 169 ///////////////////////////////////////////////////////////////////////////// | 173 ///////////////////////////////////////////////////////////////////////////// |
| 170 // OpenSSLAdapter | 174 // OpenSSLAdapter |
| 171 ///////////////////////////////////////////////////////////////////////////// | 175 ///////////////////////////////////////////////////////////////////////////// |
| 172 | 176 |
| 173 namespace rtc { | 177 namespace rtc { |
| 174 | 178 |
| 179 #ifndef OPENSSL_IS_BORINGSSL |
| 180 |
| 175 // This array will store all of the mutexes available to OpenSSL. | 181 // This array will store all of the mutexes available to OpenSSL. |
| 176 static MUTEX_TYPE* mutex_buf = NULL; | 182 static MUTEX_TYPE* mutex_buf = NULL; |
| 177 | 183 |
| 178 static void locking_function(int mode, int n, const char * file, int line) { | 184 static void locking_function(int mode, int n, const char * file, int line) { |
| 179 if (mode & CRYPTO_LOCK) { | 185 if (mode & CRYPTO_LOCK) { |
| 180 MUTEX_LOCK(mutex_buf[n]); | 186 MUTEX_LOCK(mutex_buf[n]); |
| 181 } else { | 187 } else { |
| 182 MUTEX_UNLOCK(mutex_buf[n]); | 188 MUTEX_UNLOCK(mutex_buf[n]); |
| 183 } | 189 } |
| 184 } | 190 } |
| (...skipping 21 matching lines...) Expand all Loading... |
| 206 MUTEX_UNLOCK(l->mutex); | 212 MUTEX_UNLOCK(l->mutex); |
| 207 } | 213 } |
| 208 } | 214 } |
| 209 | 215 |
| 210 static void dyn_destroy_function(CRYPTO_dynlock_value* l, | 216 static void dyn_destroy_function(CRYPTO_dynlock_value* l, |
| 211 const char* file, int line) { | 217 const char* file, int line) { |
| 212 MUTEX_CLEANUP(l->mutex); | 218 MUTEX_CLEANUP(l->mutex); |
| 213 delete l; | 219 delete l; |
| 214 } | 220 } |
| 215 | 221 |
| 222 #endif // #ifndef OPENSSL_IS_BORINGSSL |
| 223 |
| 216 VerificationCallback OpenSSLAdapter::custom_verify_callback_ = NULL; | 224 VerificationCallback OpenSSLAdapter::custom_verify_callback_ = NULL; |
| 217 | 225 |
| 218 bool OpenSSLAdapter::InitializeSSL(VerificationCallback callback) { | 226 bool OpenSSLAdapter::InitializeSSL(VerificationCallback callback) { |
| 219 if (!InitializeSSLThread() || !SSL_library_init()) | 227 if (!InitializeSSLThread() || !SSL_library_init()) |
| 220 return false; | 228 return false; |
| 221 #if !defined(ADDRESS_SANITIZER) || !defined(WEBRTC_MAC) || defined(WEBRTC_IOS) | 229 #if !defined(ADDRESS_SANITIZER) || !defined(WEBRTC_MAC) || defined(WEBRTC_IOS) |
| 222 // Loading the error strings crashes mac_asan. Omit this debugging aid there. | 230 // Loading the error strings crashes mac_asan. Omit this debugging aid there. |
| 223 SSL_load_error_strings(); | 231 SSL_load_error_strings(); |
| 224 #endif | 232 #endif |
| 225 ERR_load_BIO_strings(); | 233 ERR_load_BIO_strings(); |
| 226 OpenSSL_add_all_algorithms(); | 234 OpenSSL_add_all_algorithms(); |
| 227 RAND_poll(); | 235 RAND_poll(); |
| 228 custom_verify_callback_ = callback; | 236 custom_verify_callback_ = callback; |
| 229 return true; | 237 return true; |
| 230 } | 238 } |
| 231 | 239 |
| 232 bool OpenSSLAdapter::InitializeSSLThread() { | 240 bool OpenSSLAdapter::InitializeSSLThread() { |
| 241 // BoringSSL is doing the locking internally, so the callbacks are not used |
| 242 // in this case (and are no-ops anyways). |
| 243 #ifndef OPENSSL_IS_BORINGSSL |
| 233 mutex_buf = new MUTEX_TYPE[CRYPTO_num_locks()]; | 244 mutex_buf = new MUTEX_TYPE[CRYPTO_num_locks()]; |
| 234 if (!mutex_buf) | 245 if (!mutex_buf) |
| 235 return false; | 246 return false; |
| 236 for (int i = 0; i < CRYPTO_num_locks(); ++i) | 247 for (int i = 0; i < CRYPTO_num_locks(); ++i) |
| 237 MUTEX_SETUP(mutex_buf[i]); | 248 MUTEX_SETUP(mutex_buf[i]); |
| 238 | 249 |
| 239 // we need to cast our id_function to return an unsigned long -- pthread_t is | 250 // we need to cast our id_function to return an unsigned long -- pthread_t is |
| 240 // a pointer | 251 // a pointer |
| 241 CRYPTO_set_id_callback(id_function); | 252 CRYPTO_set_id_callback(id_function); |
| 242 CRYPTO_set_locking_callback(locking_function); | 253 CRYPTO_set_locking_callback(locking_function); |
| 243 CRYPTO_set_dynlock_create_callback(dyn_create_function); | 254 CRYPTO_set_dynlock_create_callback(dyn_create_function); |
| 244 CRYPTO_set_dynlock_lock_callback(dyn_lock_function); | 255 CRYPTO_set_dynlock_lock_callback(dyn_lock_function); |
| 245 CRYPTO_set_dynlock_destroy_callback(dyn_destroy_function); | 256 CRYPTO_set_dynlock_destroy_callback(dyn_destroy_function); |
| 257 #endif // #ifndef OPENSSL_IS_BORINGSSL |
| 246 return true; | 258 return true; |
| 247 } | 259 } |
| 248 | 260 |
| 249 bool OpenSSLAdapter::CleanupSSL() { | 261 bool OpenSSLAdapter::CleanupSSL() { |
| 262 #ifndef OPENSSL_IS_BORINGSSL |
| 250 if (!mutex_buf) | 263 if (!mutex_buf) |
| 251 return false; | 264 return false; |
| 252 CRYPTO_set_id_callback(NULL); | 265 CRYPTO_set_id_callback(NULL); |
| 253 CRYPTO_set_locking_callback(NULL); | 266 CRYPTO_set_locking_callback(NULL); |
| 254 CRYPTO_set_dynlock_create_callback(NULL); | 267 CRYPTO_set_dynlock_create_callback(NULL); |
| 255 CRYPTO_set_dynlock_lock_callback(NULL); | 268 CRYPTO_set_dynlock_lock_callback(NULL); |
| 256 CRYPTO_set_dynlock_destroy_callback(NULL); | 269 CRYPTO_set_dynlock_destroy_callback(NULL); |
| 257 for (int i = 0; i < CRYPTO_num_locks(); ++i) | 270 for (int i = 0; i < CRYPTO_num_locks(); ++i) |
| 258 MUTEX_CLEANUP(mutex_buf[i]); | 271 MUTEX_CLEANUP(mutex_buf[i]); |
| 259 delete [] mutex_buf; | 272 delete [] mutex_buf; |
| 260 mutex_buf = NULL; | 273 mutex_buf = NULL; |
| 274 #endif // #ifndef OPENSSL_IS_BORINGSSL |
| 261 return true; | 275 return true; |
| 262 } | 276 } |
| 263 | 277 |
| 264 OpenSSLAdapter::OpenSSLAdapter(AsyncSocket* socket) | 278 OpenSSLAdapter::OpenSSLAdapter(AsyncSocket* socket) |
| 265 : SSLAdapter(socket), | 279 : SSLAdapter(socket), |
| 266 state_(SSL_NONE), | 280 state_(SSL_NONE), |
| 267 ssl_read_needs_write_(false), | 281 ssl_read_needs_write_(false), |
| 268 ssl_write_needs_read_(false), | 282 ssl_write_needs_read_(false), |
| 269 restartable_(false), | 283 restartable_(false), |
| 270 ssl_(NULL), ssl_ctx_(NULL), | 284 ssl_(NULL), ssl_ctx_(NULL), |
| (...skipping 675 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 946 if (ssl_mode_ == SSL_MODE_DTLS) { | 960 if (ssl_mode_ == SSL_MODE_DTLS) { |
| 947 SSL_CTX_set_read_ahead(ctx, 1); | 961 SSL_CTX_set_read_ahead(ctx, 1); |
| 948 } | 962 } |
| 949 | 963 |
| 950 return ctx; | 964 return ctx; |
| 951 } | 965 } |
| 952 | 966 |
| 953 } // namespace rtc | 967 } // namespace rtc |
| 954 | 968 |
| 955 #endif // HAVE_OPENSSL_SSL_H | 969 #endif // HAVE_OPENSSL_SSL_H |
| OLD | NEW |