henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2008 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
kjellander | e96c45b | 2017-06-30 10:45:21 -0700 | [diff] [blame] | 11 | #include "webrtc/rtc_base/openssladapter.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 12 | |
| 13 | #if defined(WEBRTC_POSIX) |
| 14 | #include <unistd.h> |
| 15 | #endif |
| 16 | |
| 17 | // Must be included first before openssl headers. |
kjellander | e96c45b | 2017-06-30 10:45:21 -0700 | [diff] [blame] | 18 | #include "webrtc/rtc_base/win32.h" // NOLINT |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 19 | |
| 20 | #include <openssl/bio.h> |
| 21 | #include <openssl/crypto.h> |
| 22 | #include <openssl/err.h> |
| 23 | #include <openssl/opensslv.h> |
| 24 | #include <openssl/rand.h> |
henrike@webrtc.org | d5a0506 | 2014-06-30 20:38:56 +0000 | [diff] [blame] | 25 | #include <openssl/x509.h> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 26 | #include <openssl/x509v3.h> |
| 27 | |
kjellander | e96c45b | 2017-06-30 10:45:21 -0700 | [diff] [blame] | 28 | #include "webrtc/rtc_base/arraysize.h" |
| 29 | #include "webrtc/rtc_base/checks.h" |
| 30 | #include "webrtc/rtc_base/logging.h" |
| 31 | #include "webrtc/rtc_base/openssl.h" |
| 32 | #include "webrtc/rtc_base/safe_conversions.h" |
| 33 | #include "webrtc/rtc_base/sslroots.h" |
| 34 | #include "webrtc/rtc_base/stringutils.h" |
| 35 | #include "webrtc/rtc_base/thread.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 36 | |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 37 | #ifndef OPENSSL_IS_BORINGSSL |
| 38 | |
| 39 | // TODO: Use a nicer abstraction for mutex. |
| 40 | |
| 41 | #if defined(WEBRTC_WIN) |
| 42 | #define MUTEX_TYPE HANDLE |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 43 | #define MUTEX_SETUP(x) (x) = CreateMutex(nullptr, FALSE, nullptr) |
| 44 | #define MUTEX_CLEANUP(x) CloseHandle(x) |
| 45 | #define MUTEX_LOCK(x) WaitForSingleObject((x), INFINITE) |
| 46 | #define MUTEX_UNLOCK(x) ReleaseMutex(x) |
| 47 | #define THREAD_ID GetCurrentThreadId() |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 48 | #elif defined(WEBRTC_POSIX) |
| 49 | #define MUTEX_TYPE pthread_mutex_t |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 50 | #define MUTEX_SETUP(x) pthread_mutex_init(&(x), nullptr) |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 51 | #define MUTEX_CLEANUP(x) pthread_mutex_destroy(&(x)) |
| 52 | #define MUTEX_LOCK(x) pthread_mutex_lock(&(x)) |
| 53 | #define MUTEX_UNLOCK(x) pthread_mutex_unlock(&(x)) |
| 54 | #define THREAD_ID pthread_self() |
| 55 | #else |
| 56 | #error You must define mutex operations appropriate for your platform! |
| 57 | #endif |
| 58 | |
| 59 | struct CRYPTO_dynlock_value { |
| 60 | MUTEX_TYPE mutex; |
| 61 | }; |
| 62 | |
| 63 | #endif // #ifndef OPENSSL_IS_BORINGSSL |
| 64 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 65 | ////////////////////////////////////////////////////////////////////// |
| 66 | // SocketBIO |
| 67 | ////////////////////////////////////////////////////////////////////// |
| 68 | |
| 69 | static int socket_write(BIO* h, const char* buf, int num); |
| 70 | static int socket_read(BIO* h, char* buf, int size); |
| 71 | static int socket_puts(BIO* h, const char* str); |
| 72 | static long socket_ctrl(BIO* h, int cmd, long arg1, void* arg2); |
| 73 | static int socket_new(BIO* h); |
| 74 | static int socket_free(BIO* data); |
| 75 | |
davidben@webrtc.org | 36d5c3c | 2015-01-22 23:06:17 +0000 | [diff] [blame] | 76 | // TODO(davidben): This should be const once BoringSSL is assumed. |
| 77 | static BIO_METHOD methods_socket = { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 78 | BIO_TYPE_BIO, "socket", socket_write, socket_read, socket_puts, 0, |
| 79 | socket_ctrl, socket_new, socket_free, nullptr, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 80 | }; |
| 81 | |
davidben@webrtc.org | 36d5c3c | 2015-01-22 23:06:17 +0000 | [diff] [blame] | 82 | static BIO_METHOD* BIO_s_socket2() { return(&methods_socket); } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 83 | |
henrike@webrtc.org | c50bf7c | 2014-05-14 18:24:13 +0000 | [diff] [blame] | 84 | static BIO* BIO_new_socket(rtc::AsyncSocket* socket) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 85 | BIO* ret = BIO_new(BIO_s_socket2()); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 86 | if (ret == nullptr) { |
| 87 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 88 | } |
| 89 | ret->ptr = socket; |
| 90 | return ret; |
| 91 | } |
| 92 | |
| 93 | static int socket_new(BIO* b) { |
| 94 | b->shutdown = 0; |
| 95 | b->init = 1; |
| 96 | b->num = 0; // 1 means socket closed |
| 97 | b->ptr = 0; |
| 98 | return 1; |
| 99 | } |
| 100 | |
| 101 | static int socket_free(BIO* b) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 102 | if (b == nullptr) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 103 | return 0; |
| 104 | return 1; |
| 105 | } |
| 106 | |
| 107 | static int socket_read(BIO* b, char* out, int outl) { |
| 108 | if (!out) |
| 109 | return -1; |
| 110 | rtc::AsyncSocket* socket = static_cast<rtc::AsyncSocket*>(b->ptr); |
| 111 | BIO_clear_retry_flags(b); |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 112 | int result = socket->Recv(out, outl, nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 113 | if (result > 0) { |
| 114 | return result; |
| 115 | } else if (result == 0) { |
| 116 | b->num = 1; |
| 117 | } else if (socket->IsBlocking()) { |
| 118 | BIO_set_retry_read(b); |
| 119 | } |
| 120 | return -1; |
| 121 | } |
| 122 | |
| 123 | static int socket_write(BIO* b, const char* in, int inl) { |
| 124 | if (!in) |
| 125 | return -1; |
| 126 | rtc::AsyncSocket* socket = static_cast<rtc::AsyncSocket*>(b->ptr); |
| 127 | BIO_clear_retry_flags(b); |
| 128 | int result = socket->Send(in, inl); |
| 129 | if (result > 0) { |
| 130 | return result; |
| 131 | } else if (socket->IsBlocking()) { |
| 132 | BIO_set_retry_write(b); |
| 133 | } |
| 134 | return -1; |
| 135 | } |
| 136 | |
| 137 | static int socket_puts(BIO* b, const char* str) { |
henrike@webrtc.org | d89b69a | 2014-11-06 17:23:09 +0000 | [diff] [blame] | 138 | return socket_write(b, str, rtc::checked_cast<int>(strlen(str))); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | static long socket_ctrl(BIO* b, int cmd, long num, void* ptr) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 142 | switch (cmd) { |
| 143 | case BIO_CTRL_RESET: |
| 144 | return 0; |
| 145 | case BIO_CTRL_EOF: |
| 146 | return b->num; |
| 147 | case BIO_CTRL_WPENDING: |
| 148 | case BIO_CTRL_PENDING: |
| 149 | return 0; |
| 150 | case BIO_CTRL_FLUSH: |
| 151 | return 1; |
| 152 | default: |
| 153 | return 0; |
| 154 | } |
| 155 | } |
| 156 | |
deadbeef | ed3b986 | 2017-06-02 10:33:16 -0700 | [diff] [blame] | 157 | static void LogSslError() { |
| 158 | // Walk down the error stack to find the SSL error. |
| 159 | uint32_t error_code; |
| 160 | const char* file; |
| 161 | int line; |
| 162 | do { |
| 163 | error_code = ERR_get_error_line(&file, &line); |
| 164 | if (ERR_GET_LIB(error_code) == ERR_LIB_SSL) { |
| 165 | LOG(LS_ERROR) << "ERR_LIB_SSL: " << error_code << ", " << file << ":" |
| 166 | << line; |
| 167 | break; |
| 168 | } |
| 169 | } while (error_code != 0); |
| 170 | } |
| 171 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 172 | ///////////////////////////////////////////////////////////////////////////// |
| 173 | // OpenSSLAdapter |
| 174 | ///////////////////////////////////////////////////////////////////////////// |
| 175 | |
| 176 | namespace rtc { |
| 177 | |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 178 | #ifndef OPENSSL_IS_BORINGSSL |
| 179 | |
| 180 | // This array will store all of the mutexes available to OpenSSL. |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 181 | static MUTEX_TYPE* mutex_buf = nullptr; |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 182 | |
| 183 | static void locking_function(int mode, int n, const char * file, int line) { |
| 184 | if (mode & CRYPTO_LOCK) { |
| 185 | MUTEX_LOCK(mutex_buf[n]); |
| 186 | } else { |
| 187 | MUTEX_UNLOCK(mutex_buf[n]); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | static unsigned long id_function() { // NOLINT |
| 192 | // Use old-style C cast because THREAD_ID's type varies with the platform, |
| 193 | // in some cases requiring static_cast, and in others requiring |
| 194 | // reinterpret_cast. |
| 195 | return (unsigned long)THREAD_ID; // NOLINT |
| 196 | } |
| 197 | |
| 198 | static CRYPTO_dynlock_value* dyn_create_function(const char* file, int line) { |
| 199 | CRYPTO_dynlock_value* value = new CRYPTO_dynlock_value; |
| 200 | if (!value) |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 201 | return nullptr; |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 202 | MUTEX_SETUP(value->mutex); |
| 203 | return value; |
| 204 | } |
| 205 | |
| 206 | static void dyn_lock_function(int mode, CRYPTO_dynlock_value* l, |
| 207 | const char* file, int line) { |
| 208 | if (mode & CRYPTO_LOCK) { |
| 209 | MUTEX_LOCK(l->mutex); |
| 210 | } else { |
| 211 | MUTEX_UNLOCK(l->mutex); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | static void dyn_destroy_function(CRYPTO_dynlock_value* l, |
| 216 | const char* file, int line) { |
| 217 | MUTEX_CLEANUP(l->mutex); |
| 218 | delete l; |
| 219 | } |
| 220 | |
| 221 | #endif // #ifndef OPENSSL_IS_BORINGSSL |
| 222 | |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 223 | VerificationCallback OpenSSLAdapter::custom_verify_callback_ = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 224 | |
| 225 | bool OpenSSLAdapter::InitializeSSL(VerificationCallback callback) { |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 226 | if (!InitializeSSLThread() || !SSL_library_init()) |
| 227 | return false; |
| 228 | #if !defined(ADDRESS_SANITIZER) || !defined(WEBRTC_MAC) || defined(WEBRTC_IOS) |
| 229 | // Loading the error strings crashes mac_asan. Omit this debugging aid there. |
| 230 | SSL_load_error_strings(); |
| 231 | #endif |
| 232 | ERR_load_BIO_strings(); |
| 233 | OpenSSL_add_all_algorithms(); |
| 234 | RAND_poll(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 235 | custom_verify_callback_ = callback; |
| 236 | return true; |
| 237 | } |
| 238 | |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 239 | bool OpenSSLAdapter::InitializeSSLThread() { |
| 240 | // BoringSSL is doing the locking internally, so the callbacks are not used |
| 241 | // in this case (and are no-ops anyways). |
| 242 | #ifndef OPENSSL_IS_BORINGSSL |
| 243 | mutex_buf = new MUTEX_TYPE[CRYPTO_num_locks()]; |
| 244 | if (!mutex_buf) |
| 245 | return false; |
| 246 | for (int i = 0; i < CRYPTO_num_locks(); ++i) |
| 247 | MUTEX_SETUP(mutex_buf[i]); |
| 248 | |
| 249 | // we need to cast our id_function to return an unsigned long -- pthread_t is |
| 250 | // a pointer |
| 251 | CRYPTO_set_id_callback(id_function); |
| 252 | CRYPTO_set_locking_callback(locking_function); |
| 253 | CRYPTO_set_dynlock_create_callback(dyn_create_function); |
| 254 | CRYPTO_set_dynlock_lock_callback(dyn_lock_function); |
| 255 | CRYPTO_set_dynlock_destroy_callback(dyn_destroy_function); |
| 256 | #endif // #ifndef OPENSSL_IS_BORINGSSL |
| 257 | return true; |
| 258 | } |
| 259 | |
| 260 | bool OpenSSLAdapter::CleanupSSL() { |
| 261 | #ifndef OPENSSL_IS_BORINGSSL |
| 262 | if (!mutex_buf) |
| 263 | return false; |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 264 | CRYPTO_set_id_callback(nullptr); |
| 265 | CRYPTO_set_locking_callback(nullptr); |
| 266 | CRYPTO_set_dynlock_create_callback(nullptr); |
| 267 | CRYPTO_set_dynlock_lock_callback(nullptr); |
| 268 | CRYPTO_set_dynlock_destroy_callback(nullptr); |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 269 | for (int i = 0; i < CRYPTO_num_locks(); ++i) |
| 270 | MUTEX_CLEANUP(mutex_buf[i]); |
| 271 | delete [] mutex_buf; |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 272 | mutex_buf = nullptr; |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 273 | #endif // #ifndef OPENSSL_IS_BORINGSSL |
| 274 | return true; |
| 275 | } |
| 276 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 277 | OpenSSLAdapter::OpenSSLAdapter(AsyncSocket* socket, |
| 278 | OpenSSLAdapterFactory* factory) |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 279 | : SSLAdapter(socket), |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 280 | factory_(factory), |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 281 | state_(SSL_NONE), |
Steve Anton | 786de70 | 2017-08-17 15:15:46 -0700 | [diff] [blame] | 282 | role_(SSL_CLIENT), |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 283 | ssl_read_needs_write_(false), |
| 284 | ssl_write_needs_read_(false), |
| 285 | restartable_(false), |
| 286 | ssl_(nullptr), |
| 287 | ssl_ctx_(nullptr), |
| 288 | ssl_mode_(SSL_MODE_TLS), |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 289 | custom_verification_succeeded_(false) { |
| 290 | // If a factory is used, take a reference on the factory's SSL_CTX. |
| 291 | // Otherwise, we'll create our own later. |
| 292 | // Either way, we'll release our reference via SSL_CTX_free() in Cleanup(). |
| 293 | if (factory_) { |
| 294 | ssl_ctx_ = factory_->ssl_ctx(); |
| 295 | RTC_DCHECK(ssl_ctx_); |
| 296 | // Note: if using OpenSSL, requires version 1.1.0 or later. |
| 297 | SSL_CTX_up_ref(ssl_ctx_); |
| 298 | } |
| 299 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 300 | |
| 301 | OpenSSLAdapter::~OpenSSLAdapter() { |
| 302 | Cleanup(); |
| 303 | } |
| 304 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 305 | void OpenSSLAdapter::SetMode(SSLMode mode) { |
| 306 | RTC_DCHECK(!ssl_ctx_); |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 307 | RTC_DCHECK(state_ == SSL_NONE); |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 308 | ssl_mode_ = mode; |
| 309 | } |
| 310 | |
Steve Anton | 786de70 | 2017-08-17 15:15:46 -0700 | [diff] [blame] | 311 | void OpenSSLAdapter::SetIdentity(SSLIdentity* identity) { |
| 312 | RTC_DCHECK(!identity_); |
| 313 | identity_.reset(static_cast<OpenSSLIdentity*>(identity)); |
| 314 | } |
| 315 | |
| 316 | void OpenSSLAdapter::SetRole(SSLRole role) { |
| 317 | role_ = role; |
| 318 | } |
| 319 | |
| 320 | AsyncSocket* OpenSSLAdapter::Accept(SocketAddress* paddr) { |
| 321 | RTC_DCHECK(role_ == SSL_SERVER); |
| 322 | AsyncSocket* socket = SSLAdapter::Accept(paddr); |
| 323 | if (!socket) { |
| 324 | return nullptr; |
| 325 | } |
| 326 | |
| 327 | SSLAdapter* adapter = SSLAdapter::Create(socket); |
| 328 | adapter->SetIdentity(identity_->GetReference()); |
| 329 | adapter->SetRole(rtc::SSL_SERVER); |
| 330 | adapter->set_ignore_bad_cert(ignore_bad_cert()); |
| 331 | adapter->StartSSL("", false); |
| 332 | return adapter; |
| 333 | } |
| 334 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 335 | int OpenSSLAdapter::StartSSL(const char* hostname, bool restartable) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 336 | if (state_ != SSL_NONE) |
| 337 | return -1; |
| 338 | |
| 339 | ssl_host_name_ = hostname; |
| 340 | restartable_ = restartable; |
| 341 | |
| 342 | if (socket_->GetState() != Socket::CS_CONNECTED) { |
| 343 | state_ = SSL_WAIT; |
| 344 | return 0; |
| 345 | } |
| 346 | |
| 347 | state_ = SSL_CONNECTING; |
| 348 | if (int err = BeginSSL()) { |
| 349 | Error("BeginSSL", err, false); |
| 350 | return err; |
| 351 | } |
| 352 | |
| 353 | return 0; |
| 354 | } |
| 355 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 356 | int OpenSSLAdapter::BeginSSL() { |
| 357 | LOG(LS_INFO) << "OpenSSLAdapter::BeginSSL: " << ssl_host_name_; |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 358 | RTC_DCHECK(state_ == SSL_CONNECTING); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 359 | |
| 360 | int err = 0; |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 361 | BIO* bio = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 362 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 363 | // First set up the context. We should either have a factory, with its own |
| 364 | // pre-existing context, or be running standalone, in which case we will |
| 365 | // need to create one, and specify |false| to disable session caching. |
| 366 | if (!factory_) { |
| 367 | RTC_DCHECK(!ssl_ctx_); |
| 368 | ssl_ctx_ = CreateContext(ssl_mode_, false); |
| 369 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 370 | if (!ssl_ctx_) { |
| 371 | err = -1; |
| 372 | goto ssl_error; |
| 373 | } |
| 374 | |
Steve Anton | 786de70 | 2017-08-17 15:15:46 -0700 | [diff] [blame] | 375 | if (identity_ && !identity_->ConfigureIdentity(ssl_ctx_)) { |
| 376 | SSL_CTX_free(ssl_ctx_); |
| 377 | err = -1; |
| 378 | goto ssl_error; |
| 379 | } |
| 380 | |
Peter Boström | 0b518bf | 2016-01-27 12:35:40 +0100 | [diff] [blame] | 381 | bio = BIO_new_socket(socket_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 382 | if (!bio) { |
| 383 | err = -1; |
| 384 | goto ssl_error; |
| 385 | } |
| 386 | |
| 387 | ssl_ = SSL_new(ssl_ctx_); |
| 388 | if (!ssl_) { |
| 389 | err = -1; |
| 390 | goto ssl_error; |
| 391 | } |
| 392 | |
| 393 | SSL_set_app_data(ssl_, this); |
| 394 | |
deadbeef | ed3b986 | 2017-06-02 10:33:16 -0700 | [diff] [blame] | 395 | // SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER allows different buffers to be passed |
| 396 | // into SSL_write when a record could only be partially transmitted (and thus |
| 397 | // requires another call to SSL_write to finish transmission). This allows us |
| 398 | // to copy the data into our own buffer when this occurs, since the original |
| 399 | // buffer can't safely be accessed after control exits Send. |
| 400 | // TODO(deadbeef): Do we want SSL_MODE_ENABLE_PARTIAL_WRITE? It doesn't |
| 401 | // appear Send handles partial writes properly, though maybe we never notice |
| 402 | // since we never send more than 16KB at once.. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 403 | SSL_set_mode(ssl_, SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 404 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER); |
| 405 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 406 | // Enable SNI, if a hostname is supplied. |
Emad Omara | dab1d2d | 2017-06-16 15:43:11 -0700 | [diff] [blame] | 407 | if (!ssl_host_name_.empty()) { |
| 408 | SSL_set_tlsext_host_name(ssl_, ssl_host_name_.c_str()); |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 409 | |
| 410 | // Enable session caching, if configured and a hostname is supplied. |
| 411 | if (factory_) { |
| 412 | SSL_SESSION* cached = factory_->LookupSession(ssl_host_name_); |
| 413 | if (cached) { |
| 414 | if (SSL_set_session(ssl_, cached) == 0) { |
| 415 | LOG(LS_WARNING) << "Failed to apply SSL session from cache"; |
| 416 | err = -1; |
| 417 | goto ssl_error; |
| 418 | } |
| 419 | |
| 420 | LOG(LS_INFO) << "Attempting to resume SSL session to " |
| 421 | << ssl_host_name_; |
| 422 | } |
| 423 | } |
Emad Omara | dab1d2d | 2017-06-16 15:43:11 -0700 | [diff] [blame] | 424 | } |
| 425 | |
Emad Omara | cb79d23 | 2017-07-20 16:34:34 -0700 | [diff] [blame] | 426 | // Set a couple common TLS extensions; even though we don't use them yet. |
| 427 | // TODO(emadomara) Add ALPN extension. |
| 428 | SSL_enable_ocsp_stapling(ssl_); |
| 429 | SSL_enable_signed_cert_timestamps(ssl_); |
| 430 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 431 | // Now that the initial config is done, transfer ownership of |bio| to the |
| 432 | // SSL object. If ContinueSSL() fails, the bio will be freed in Cleanup(). |
| 433 | SSL_set_bio(ssl_, bio, bio); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 434 | bio = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 435 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 436 | // Do the connect. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 437 | err = ContinueSSL(); |
| 438 | if (err != 0) |
| 439 | goto ssl_error; |
| 440 | |
| 441 | return err; |
| 442 | |
| 443 | ssl_error: |
| 444 | Cleanup(); |
| 445 | if (bio) |
| 446 | BIO_free(bio); |
| 447 | |
| 448 | return err; |
| 449 | } |
| 450 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 451 | int OpenSSLAdapter::ContinueSSL() { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 452 | RTC_DCHECK(state_ == SSL_CONNECTING); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 453 | |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 454 | // Clear the DTLS timer |
| 455 | Thread::Current()->Clear(this, MSG_TIMEOUT); |
| 456 | |
Steve Anton | 786de70 | 2017-08-17 15:15:46 -0700 | [diff] [blame] | 457 | int code = (role_ == SSL_CLIENT) ? SSL_connect(ssl_) : SSL_accept(ssl_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 458 | switch (SSL_get_error(ssl_, code)) { |
| 459 | case SSL_ERROR_NONE: |
| 460 | if (!SSLPostConnectionCheck(ssl_, ssl_host_name_.c_str())) { |
| 461 | LOG(LS_ERROR) << "TLS post connection check failed"; |
| 462 | // make sure we close the socket |
| 463 | Cleanup(); |
| 464 | // The connect failed so return -1 to shut down the socket |
| 465 | return -1; |
| 466 | } |
| 467 | |
| 468 | state_ = SSL_CONNECTED; |
| 469 | AsyncSocketAdapter::OnConnectEvent(this); |
| 470 | #if 0 // TODO: worry about this |
| 471 | // Don't let ourselves go away during the callbacks |
| 472 | PRefPtr<OpenSSLAdapter> lock(this); |
| 473 | LOG(LS_INFO) << " -- onStreamReadable"; |
| 474 | AsyncSocketAdapter::OnReadEvent(this); |
| 475 | LOG(LS_INFO) << " -- onStreamWriteable"; |
| 476 | AsyncSocketAdapter::OnWriteEvent(this); |
| 477 | #endif |
| 478 | break; |
| 479 | |
| 480 | case SSL_ERROR_WANT_READ: |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 481 | LOG(LS_VERBOSE) << " -- error want read"; |
| 482 | struct timeval timeout; |
| 483 | if (DTLSv1_get_timeout(ssl_, &timeout)) { |
| 484 | int delay = timeout.tv_sec * 1000 + timeout.tv_usec/1000; |
| 485 | |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 486 | Thread::Current()->PostDelayed(RTC_FROM_HERE, delay, this, MSG_TIMEOUT, |
| 487 | 0); |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 488 | } |
| 489 | break; |
| 490 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 491 | case SSL_ERROR_WANT_WRITE: |
| 492 | break; |
| 493 | |
| 494 | case SSL_ERROR_ZERO_RETURN: |
| 495 | default: |
| 496 | LOG(LS_WARNING) << "ContinueSSL -- error " << code; |
| 497 | return (code != 0) ? code : -1; |
| 498 | } |
| 499 | |
| 500 | return 0; |
| 501 | } |
| 502 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 503 | void OpenSSLAdapter::Error(const char* context, int err, bool signal) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 504 | LOG(LS_WARNING) << "OpenSSLAdapter::Error(" |
| 505 | << context << ", " << err << ")"; |
| 506 | state_ = SSL_ERROR; |
| 507 | SetError(err); |
| 508 | if (signal) |
| 509 | AsyncSocketAdapter::OnCloseEvent(this, err); |
| 510 | } |
| 511 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 512 | void OpenSSLAdapter::Cleanup() { |
| 513 | LOG(LS_INFO) << "OpenSSLAdapter::Cleanup"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 514 | |
| 515 | state_ = SSL_NONE; |
| 516 | ssl_read_needs_write_ = false; |
| 517 | ssl_write_needs_read_ = false; |
| 518 | custom_verification_succeeded_ = false; |
deadbeef | ed3b986 | 2017-06-02 10:33:16 -0700 | [diff] [blame] | 519 | pending_data_.Clear(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 520 | |
| 521 | if (ssl_) { |
| 522 | SSL_free(ssl_); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 523 | ssl_ = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | if (ssl_ctx_) { |
| 527 | SSL_CTX_free(ssl_ctx_); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 528 | ssl_ctx_ = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 529 | } |
Steve Anton | 786de70 | 2017-08-17 15:15:46 -0700 | [diff] [blame] | 530 | identity_.reset(); |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 531 | |
| 532 | // Clear the DTLS timer |
| 533 | Thread::Current()->Clear(this, MSG_TIMEOUT); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 534 | } |
| 535 | |
deadbeef | ed3b986 | 2017-06-02 10:33:16 -0700 | [diff] [blame] | 536 | int OpenSSLAdapter::DoSslWrite(const void* pv, size_t cb, int* error) { |
| 537 | // If we have pending data (that was previously only partially written by |
| 538 | // SSL_write), we shouldn't be attempting to write anything else. |
| 539 | RTC_DCHECK(pending_data_.empty() || pv == pending_data_.data()); |
| 540 | RTC_DCHECK(error != nullptr); |
| 541 | |
| 542 | ssl_write_needs_read_ = false; |
| 543 | int ret = SSL_write(ssl_, pv, checked_cast<int>(cb)); |
| 544 | *error = SSL_get_error(ssl_, ret); |
| 545 | switch (*error) { |
| 546 | case SSL_ERROR_NONE: |
| 547 | // Success! |
| 548 | return ret; |
| 549 | case SSL_ERROR_WANT_READ: |
| 550 | LOG(LS_INFO) << " -- error want read"; |
| 551 | ssl_write_needs_read_ = true; |
| 552 | SetError(EWOULDBLOCK); |
| 553 | break; |
| 554 | case SSL_ERROR_WANT_WRITE: |
| 555 | LOG(LS_INFO) << " -- error want write"; |
| 556 | SetError(EWOULDBLOCK); |
| 557 | break; |
| 558 | case SSL_ERROR_ZERO_RETURN: |
| 559 | // LOG(LS_INFO) << " -- remote side closed"; |
| 560 | SetError(EWOULDBLOCK); |
| 561 | // do we need to signal closure? |
| 562 | break; |
| 563 | case SSL_ERROR_SSL: |
| 564 | LogSslError(); |
| 565 | Error("SSL_write", ret ? ret : -1, false); |
| 566 | break; |
| 567 | default: |
| 568 | LOG(LS_WARNING) << "Unknown error from SSL_write: " << *error; |
| 569 | Error("SSL_write", ret ? ret : -1, false); |
| 570 | break; |
| 571 | } |
| 572 | |
| 573 | return SOCKET_ERROR; |
| 574 | } |
| 575 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 576 | // |
| 577 | // AsyncSocket Implementation |
| 578 | // |
| 579 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 580 | int OpenSSLAdapter::Send(const void* pv, size_t cb) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 581 | //LOG(LS_INFO) << "OpenSSLAdapter::Send(" << cb << ")"; |
| 582 | |
| 583 | switch (state_) { |
| 584 | case SSL_NONE: |
| 585 | return AsyncSocketAdapter::Send(pv, cb); |
| 586 | |
| 587 | case SSL_WAIT: |
| 588 | case SSL_CONNECTING: |
skvlad | c309e0e | 2016-07-28 17:15:20 -0700 | [diff] [blame] | 589 | SetError(ENOTCONN); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 590 | return SOCKET_ERROR; |
| 591 | |
| 592 | case SSL_CONNECTED: |
| 593 | break; |
| 594 | |
| 595 | case SSL_ERROR: |
| 596 | default: |
| 597 | return SOCKET_ERROR; |
| 598 | } |
| 599 | |
deadbeef | ed3b986 | 2017-06-02 10:33:16 -0700 | [diff] [blame] | 600 | int ret; |
| 601 | int error; |
| 602 | |
| 603 | if (!pending_data_.empty()) { |
| 604 | ret = DoSslWrite(pending_data_.data(), pending_data_.size(), &error); |
| 605 | if (ret != static_cast<int>(pending_data_.size())) { |
| 606 | // We couldn't finish sending the pending data, so we definitely can't |
| 607 | // send any more data. Return with an EWOULDBLOCK error. |
| 608 | SetError(EWOULDBLOCK); |
| 609 | return SOCKET_ERROR; |
| 610 | } |
| 611 | // We completed sending the data previously passed into SSL_write! Now |
| 612 | // we're allowed to send more data. |
| 613 | pending_data_.Clear(); |
| 614 | } |
| 615 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 616 | // OpenSSL will return an error if we try to write zero bytes |
| 617 | if (cb == 0) |
| 618 | return 0; |
| 619 | |
deadbeef | ed3b986 | 2017-06-02 10:33:16 -0700 | [diff] [blame] | 620 | ret = DoSslWrite(pv, cb, &error); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 621 | |
deadbeef | ed3b986 | 2017-06-02 10:33:16 -0700 | [diff] [blame] | 622 | // If SSL_write fails with SSL_ERROR_WANT_READ or SSL_ERROR_WANT_WRITE, this |
| 623 | // means the underlying socket is blocked on reading or (more typically) |
| 624 | // writing. When this happens, OpenSSL requires that the next call to |
| 625 | // SSL_write uses the same arguments (though, with |
| 626 | // SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER, the actual buffer pointer may be |
| 627 | // different). |
| 628 | // |
| 629 | // However, after Send exits, we will have lost access to data the user of |
| 630 | // this class is trying to send, and there's no guarantee that the user of |
| 631 | // this class will call Send with the same arguements when it fails. So, we |
| 632 | // buffer the data ourselves. When we know the underlying socket is writable |
| 633 | // again from OnWriteEvent (or if Send is called again before that happens), |
| 634 | // we'll retry sending this buffered data. |
deadbeef | e5dce2b | 2017-06-02 11:52:06 -0700 | [diff] [blame] | 635 | if (error == SSL_ERROR_WANT_READ || error == SSL_ERROR_WANT_WRITE) { |
| 636 | // Shouldn't be able to get to this point if we already have pending data. |
| 637 | RTC_DCHECK(pending_data_.empty()); |
deadbeef | ed3b986 | 2017-06-02 10:33:16 -0700 | [diff] [blame] | 638 | LOG(LS_WARNING) |
| 639 | << "SSL_write couldn't write to the underlying socket; buffering data."; |
| 640 | pending_data_.SetData(static_cast<const uint8_t*>(pv), cb); |
| 641 | // Since we're taking responsibility for sending this data, return its full |
| 642 | // size. The user of this class can consider it sent. |
| 643 | return cb; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 644 | } |
| 645 | |
deadbeef | ed3b986 | 2017-06-02 10:33:16 -0700 | [diff] [blame] | 646 | return ret; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 647 | } |
| 648 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 649 | int OpenSSLAdapter::SendTo(const void* pv, |
| 650 | size_t cb, |
| 651 | const SocketAddress& addr) { |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 652 | if (socket_->GetState() == Socket::CS_CONNECTED && |
| 653 | addr == socket_->GetRemoteAddress()) { |
| 654 | return Send(pv, cb); |
| 655 | } |
| 656 | |
| 657 | SetError(ENOTCONN); |
| 658 | |
| 659 | return SOCKET_ERROR; |
| 660 | } |
| 661 | |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 662 | int OpenSSLAdapter::Recv(void* pv, size_t cb, int64_t* timestamp) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 663 | //LOG(LS_INFO) << "OpenSSLAdapter::Recv(" << cb << ")"; |
| 664 | switch (state_) { |
| 665 | |
| 666 | case SSL_NONE: |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 667 | return AsyncSocketAdapter::Recv(pv, cb, timestamp); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 668 | |
| 669 | case SSL_WAIT: |
| 670 | case SSL_CONNECTING: |
skvlad | c309e0e | 2016-07-28 17:15:20 -0700 | [diff] [blame] | 671 | SetError(ENOTCONN); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 672 | return SOCKET_ERROR; |
| 673 | |
| 674 | case SSL_CONNECTED: |
| 675 | break; |
| 676 | |
| 677 | case SSL_ERROR: |
| 678 | default: |
| 679 | return SOCKET_ERROR; |
| 680 | } |
| 681 | |
| 682 | // Don't trust OpenSSL with zero byte reads |
| 683 | if (cb == 0) |
| 684 | return 0; |
| 685 | |
| 686 | ssl_read_needs_write_ = false; |
| 687 | |
henrike@webrtc.org | d89b69a | 2014-11-06 17:23:09 +0000 | [diff] [blame] | 688 | int code = SSL_read(ssl_, pv, checked_cast<int>(cb)); |
deadbeef | ed3b986 | 2017-06-02 10:33:16 -0700 | [diff] [blame] | 689 | int error = SSL_get_error(ssl_, code); |
| 690 | switch (error) { |
| 691 | case SSL_ERROR_NONE: |
| 692 | // LOG(LS_INFO) << " -- success"; |
| 693 | return code; |
| 694 | case SSL_ERROR_WANT_READ: |
| 695 | // LOG(LS_INFO) << " -- error want read"; |
| 696 | SetError(EWOULDBLOCK); |
| 697 | break; |
| 698 | case SSL_ERROR_WANT_WRITE: |
| 699 | // LOG(LS_INFO) << " -- error want write"; |
| 700 | ssl_read_needs_write_ = true; |
| 701 | SetError(EWOULDBLOCK); |
| 702 | break; |
| 703 | case SSL_ERROR_ZERO_RETURN: |
| 704 | // LOG(LS_INFO) << " -- remote side closed"; |
| 705 | SetError(EWOULDBLOCK); |
| 706 | // do we need to signal closure? |
| 707 | break; |
| 708 | case SSL_ERROR_SSL: |
| 709 | LogSslError(); |
| 710 | Error("SSL_read", (code ? code : -1), false); |
| 711 | break; |
| 712 | default: |
| 713 | LOG(LS_WARNING) << "Unknown error from SSL_read: " << error; |
| 714 | Error("SSL_read", (code ? code : -1), false); |
| 715 | break; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | return SOCKET_ERROR; |
| 719 | } |
| 720 | |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 721 | int OpenSSLAdapter::RecvFrom(void* pv, |
| 722 | size_t cb, |
| 723 | SocketAddress* paddr, |
| 724 | int64_t* timestamp) { |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 725 | if (socket_->GetState() == Socket::CS_CONNECTED) { |
Stefan Holmer | 9131efd | 2016-05-23 18:19:26 +0200 | [diff] [blame] | 726 | int ret = Recv(pv, cb, timestamp); |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 727 | |
| 728 | *paddr = GetRemoteAddress(); |
| 729 | |
| 730 | return ret; |
| 731 | } |
| 732 | |
| 733 | SetError(ENOTCONN); |
| 734 | |
| 735 | return SOCKET_ERROR; |
| 736 | } |
| 737 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 738 | int OpenSSLAdapter::Close() { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 739 | Cleanup(); |
| 740 | state_ = restartable_ ? SSL_WAIT : SSL_NONE; |
| 741 | return AsyncSocketAdapter::Close(); |
| 742 | } |
| 743 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 744 | Socket::ConnState OpenSSLAdapter::GetState() const { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 745 | //if (signal_close_) |
| 746 | // return CS_CONNECTED; |
| 747 | ConnState state = socket_->GetState(); |
| 748 | if ((state == CS_CONNECTED) |
| 749 | && ((state_ == SSL_WAIT) || (state_ == SSL_CONNECTING))) |
| 750 | state = CS_CONNECTING; |
| 751 | return state; |
| 752 | } |
| 753 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 754 | bool OpenSSLAdapter::IsResumedSession() { |
| 755 | return (ssl_ && SSL_session_reused(ssl_) == 1); |
| 756 | } |
| 757 | |
| 758 | void OpenSSLAdapter::OnMessage(Message* msg) { |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 759 | if (MSG_TIMEOUT == msg->message_id) { |
| 760 | LOG(LS_INFO) << "DTLS timeout expired"; |
| 761 | DTLSv1_handle_timeout(ssl_); |
| 762 | ContinueSSL(); |
| 763 | } |
| 764 | } |
| 765 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 766 | void OpenSSLAdapter::OnConnectEvent(AsyncSocket* socket) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 767 | LOG(LS_INFO) << "OpenSSLAdapter::OnConnectEvent"; |
| 768 | if (state_ != SSL_WAIT) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 769 | RTC_DCHECK(state_ == SSL_NONE); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 770 | AsyncSocketAdapter::OnConnectEvent(socket); |
| 771 | return; |
| 772 | } |
| 773 | |
| 774 | state_ = SSL_CONNECTING; |
| 775 | if (int err = BeginSSL()) { |
| 776 | AsyncSocketAdapter::OnCloseEvent(socket, err); |
| 777 | } |
| 778 | } |
| 779 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 780 | void OpenSSLAdapter::OnReadEvent(AsyncSocket* socket) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 781 | //LOG(LS_INFO) << "OpenSSLAdapter::OnReadEvent"; |
| 782 | |
| 783 | if (state_ == SSL_NONE) { |
| 784 | AsyncSocketAdapter::OnReadEvent(socket); |
| 785 | return; |
| 786 | } |
| 787 | |
| 788 | if (state_ == SSL_CONNECTING) { |
| 789 | if (int err = ContinueSSL()) { |
| 790 | Error("ContinueSSL", err); |
| 791 | } |
| 792 | return; |
| 793 | } |
| 794 | |
| 795 | if (state_ != SSL_CONNECTED) |
| 796 | return; |
| 797 | |
| 798 | // Don't let ourselves go away during the callbacks |
| 799 | //PRefPtr<OpenSSLAdapter> lock(this); // TODO: fix this |
| 800 | if (ssl_write_needs_read_) { |
| 801 | //LOG(LS_INFO) << " -- onStreamWriteable"; |
| 802 | AsyncSocketAdapter::OnWriteEvent(socket); |
| 803 | } |
| 804 | |
| 805 | //LOG(LS_INFO) << " -- onStreamReadable"; |
| 806 | AsyncSocketAdapter::OnReadEvent(socket); |
| 807 | } |
| 808 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 809 | void OpenSSLAdapter::OnWriteEvent(AsyncSocket* socket) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 810 | //LOG(LS_INFO) << "OpenSSLAdapter::OnWriteEvent"; |
| 811 | |
| 812 | if (state_ == SSL_NONE) { |
| 813 | AsyncSocketAdapter::OnWriteEvent(socket); |
| 814 | return; |
| 815 | } |
| 816 | |
| 817 | if (state_ == SSL_CONNECTING) { |
| 818 | if (int err = ContinueSSL()) { |
| 819 | Error("ContinueSSL", err); |
| 820 | } |
| 821 | return; |
| 822 | } |
| 823 | |
| 824 | if (state_ != SSL_CONNECTED) |
| 825 | return; |
| 826 | |
| 827 | // Don't let ourselves go away during the callbacks |
| 828 | //PRefPtr<OpenSSLAdapter> lock(this); // TODO: fix this |
| 829 | |
| 830 | if (ssl_read_needs_write_) { |
| 831 | //LOG(LS_INFO) << " -- onStreamReadable"; |
| 832 | AsyncSocketAdapter::OnReadEvent(socket); |
| 833 | } |
| 834 | |
deadbeef | ed3b986 | 2017-06-02 10:33:16 -0700 | [diff] [blame] | 835 | // If a previous SSL_write failed due to the underlying socket being blocked, |
| 836 | // this will attempt finishing the write operation. |
| 837 | if (!pending_data_.empty()) { |
| 838 | int error; |
| 839 | if (DoSslWrite(pending_data_.data(), pending_data_.size(), &error) == |
| 840 | static_cast<int>(pending_data_.size())) { |
| 841 | pending_data_.Clear(); |
| 842 | } |
| 843 | } |
| 844 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 845 | //LOG(LS_INFO) << " -- onStreamWriteable"; |
| 846 | AsyncSocketAdapter::OnWriteEvent(socket); |
| 847 | } |
| 848 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 849 | void OpenSSLAdapter::OnCloseEvent(AsyncSocket* socket, int err) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 850 | LOG(LS_INFO) << "OpenSSLAdapter::OnCloseEvent(" << err << ")"; |
| 851 | AsyncSocketAdapter::OnCloseEvent(socket, err); |
| 852 | } |
| 853 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 854 | bool OpenSSLAdapter::VerifyServerName(SSL* ssl, const char* host, |
| 855 | bool ignore_bad_cert) { |
| 856 | if (!host) |
| 857 | return false; |
| 858 | |
| 859 | // Checking the return from SSL_get_peer_certificate here is not strictly |
| 860 | // necessary. With our setup, it is not possible for it to return |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 861 | // null. However, it is good form to check the return. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 862 | X509* certificate = SSL_get_peer_certificate(ssl); |
| 863 | if (!certificate) |
| 864 | return false; |
| 865 | |
| 866 | // Logging certificates is extremely verbose. So it is disabled by default. |
| 867 | #ifdef LOG_CERTIFICATES |
| 868 | { |
| 869 | LOG(LS_INFO) << "Certificate from server:"; |
| 870 | BIO* mem = BIO_new(BIO_s_mem()); |
| 871 | X509_print_ex(mem, certificate, XN_FLAG_SEP_CPLUS_SPC, X509_FLAG_NO_HEADER); |
| 872 | BIO_write(mem, "\0", 1); |
| 873 | char* buffer; |
| 874 | BIO_get_mem_data(mem, &buffer); |
| 875 | LOG(LS_INFO) << buffer; |
| 876 | BIO_free(mem); |
| 877 | |
| 878 | char* cipher_description = |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 879 | SSL_CIPHER_description(SSL_get_current_cipher(ssl), nullptr, 128); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 880 | LOG(LS_INFO) << "Cipher: " << cipher_description; |
| 881 | OPENSSL_free(cipher_description); |
| 882 | } |
| 883 | #endif |
| 884 | |
| 885 | bool ok = false; |
davidben | 4ef903d | 2017-02-17 13:04:43 -0800 | [diff] [blame] | 886 | GENERAL_NAMES* names = reinterpret_cast<GENERAL_NAMES*>( |
| 887 | X509_get_ext_d2i(certificate, NID_subject_alt_name, nullptr, nullptr)); |
| 888 | if (names) { |
| 889 | for (size_t i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 890 | const GENERAL_NAME* name = sk_GENERAL_NAME_value(names, i); |
| 891 | if (name->type != GEN_DNS) |
| 892 | continue; |
| 893 | std::string value( |
| 894 | reinterpret_cast<const char*>(ASN1_STRING_data(name->d.dNSName)), |
| 895 | ASN1_STRING_length(name->d.dNSName)); |
| 896 | // string_match takes NUL-terminated strings, so check for embedded NULs. |
| 897 | if (value.find('\0') != std::string::npos) |
| 898 | continue; |
| 899 | if (string_match(host, value.c_str())) { |
| 900 | ok = true; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 901 | break; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 902 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 903 | } |
davidben | 4ef903d | 2017-02-17 13:04:43 -0800 | [diff] [blame] | 904 | GENERAL_NAMES_free(names); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | char data[256]; |
henrike@webrtc.org | d5a0506 | 2014-06-30 20:38:56 +0000 | [diff] [blame] | 908 | X509_NAME* subject; |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 909 | if (!ok && ((subject = X509_get_subject_name(certificate)) != nullptr) && |
| 910 | (X509_NAME_get_text_by_NID(subject, NID_commonName, data, sizeof(data)) > |
| 911 | 0)) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 912 | data[sizeof(data)-1] = 0; |
| 913 | if (_stricmp(data, host) == 0) |
| 914 | ok = true; |
| 915 | } |
| 916 | |
| 917 | X509_free(certificate); |
| 918 | |
| 919 | // This should only ever be turned on for debugging and development. |
| 920 | if (!ok && ignore_bad_cert) { |
| 921 | LOG(LS_WARNING) << "TLS certificate check FAILED. " |
| 922 | << "Allowing connection anyway."; |
| 923 | ok = true; |
| 924 | } |
| 925 | |
| 926 | return ok; |
| 927 | } |
| 928 | |
| 929 | bool OpenSSLAdapter::SSLPostConnectionCheck(SSL* ssl, const char* host) { |
| 930 | bool ok = VerifyServerName(ssl, host, ignore_bad_cert()); |
| 931 | |
| 932 | if (ok) { |
| 933 | ok = (SSL_get_verify_result(ssl) == X509_V_OK || |
| 934 | custom_verification_succeeded_); |
| 935 | } |
| 936 | |
| 937 | if (!ok && ignore_bad_cert()) { |
| 938 | LOG(LS_INFO) << "Other TLS post connection checks failed."; |
| 939 | ok = true; |
| 940 | } |
| 941 | |
| 942 | return ok; |
| 943 | } |
| 944 | |
tfarina | a41ab93 | 2015-10-30 16:08:48 -0700 | [diff] [blame] | 945 | #if !defined(NDEBUG) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 946 | |
| 947 | // We only use this for tracing and so it is only needed in debug mode |
| 948 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 949 | void OpenSSLAdapter::SSLInfoCallback(const SSL* s, int where, int ret) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 950 | const char* str = "undefined"; |
| 951 | int w = where & ~SSL_ST_MASK; |
| 952 | if (w & SSL_ST_CONNECT) { |
| 953 | str = "SSL_connect"; |
| 954 | } else if (w & SSL_ST_ACCEPT) { |
| 955 | str = "SSL_accept"; |
| 956 | } |
| 957 | if (where & SSL_CB_LOOP) { |
| 958 | LOG(LS_INFO) << str << ":" << SSL_state_string_long(s); |
| 959 | } else if (where & SSL_CB_ALERT) { |
| 960 | str = (where & SSL_CB_READ) ? "read" : "write"; |
| 961 | LOG(LS_INFO) << "SSL3 alert " << str |
| 962 | << ":" << SSL_alert_type_string_long(ret) |
| 963 | << ":" << SSL_alert_desc_string_long(ret); |
| 964 | } else if (where & SSL_CB_EXIT) { |
| 965 | if (ret == 0) { |
| 966 | LOG(LS_INFO) << str << ":failed in " << SSL_state_string_long(s); |
| 967 | } else if (ret < 0) { |
| 968 | LOG(LS_INFO) << str << ":error in " << SSL_state_string_long(s); |
| 969 | } |
| 970 | } |
| 971 | } |
| 972 | |
tfarina | a41ab93 | 2015-10-30 16:08:48 -0700 | [diff] [blame] | 973 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 974 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 975 | int OpenSSLAdapter::SSLVerifyCallback(int ok, X509_STORE_CTX* store) { |
tfarina | a41ab93 | 2015-10-30 16:08:48 -0700 | [diff] [blame] | 976 | #if !defined(NDEBUG) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 977 | if (!ok) { |
| 978 | char data[256]; |
| 979 | X509* cert = X509_STORE_CTX_get_current_cert(store); |
| 980 | int depth = X509_STORE_CTX_get_error_depth(store); |
| 981 | int err = X509_STORE_CTX_get_error(store); |
| 982 | |
| 983 | LOG(LS_INFO) << "Error with certificate at depth: " << depth; |
| 984 | X509_NAME_oneline(X509_get_issuer_name(cert), data, sizeof(data)); |
| 985 | LOG(LS_INFO) << " issuer = " << data; |
| 986 | X509_NAME_oneline(X509_get_subject_name(cert), data, sizeof(data)); |
| 987 | LOG(LS_INFO) << " subject = " << data; |
| 988 | LOG(LS_INFO) << " err = " << err |
| 989 | << ":" << X509_verify_cert_error_string(err); |
| 990 | } |
| 991 | #endif |
| 992 | |
| 993 | // Get our stream pointer from the store |
| 994 | SSL* ssl = reinterpret_cast<SSL*>( |
| 995 | X509_STORE_CTX_get_ex_data(store, |
| 996 | SSL_get_ex_data_X509_STORE_CTX_idx())); |
| 997 | |
| 998 | OpenSSLAdapter* stream = |
| 999 | reinterpret_cast<OpenSSLAdapter*>(SSL_get_app_data(ssl)); |
| 1000 | |
| 1001 | if (!ok && custom_verify_callback_) { |
| 1002 | void* cert = |
| 1003 | reinterpret_cast<void*>(X509_STORE_CTX_get_current_cert(store)); |
| 1004 | if (custom_verify_callback_(cert)) { |
| 1005 | stream->custom_verification_succeeded_ = true; |
| 1006 | LOG(LS_INFO) << "validated certificate using custom callback"; |
| 1007 | ok = true; |
| 1008 | } |
| 1009 | } |
| 1010 | |
| 1011 | // Should only be used for debugging and development. |
| 1012 | if (!ok && stream->ignore_bad_cert()) { |
| 1013 | LOG(LS_WARNING) << "Ignoring cert error while verifying cert chain"; |
| 1014 | ok = 1; |
| 1015 | } |
| 1016 | |
| 1017 | return ok; |
| 1018 | } |
| 1019 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 1020 | int OpenSSLAdapter::NewSSLSessionCallback(SSL* ssl, SSL_SESSION* session) { |
| 1021 | OpenSSLAdapter* stream = |
| 1022 | reinterpret_cast<OpenSSLAdapter*>(SSL_get_app_data(ssl)); |
| 1023 | RTC_DCHECK(stream->factory_); |
| 1024 | LOG(LS_INFO) << "Caching SSL session for " << stream->ssl_host_name_; |
| 1025 | stream->factory_->AddSession(stream->ssl_host_name_, session); |
| 1026 | return 1; // We've taken ownership of the session; OpenSSL shouldn't free it. |
| 1027 | } |
| 1028 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1029 | bool OpenSSLAdapter::ConfigureTrustedRootCertificates(SSL_CTX* ctx) { |
| 1030 | // Add the root cert that we care about to the SSL context |
| 1031 | int count_of_added_certs = 0; |
tfarina | 5237aaf | 2015-11-10 23:44:30 -0800 | [diff] [blame] | 1032 | for (size_t i = 0; i < arraysize(kSSLCertCertificateList); i++) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1033 | const unsigned char* cert_buffer = kSSLCertCertificateList[i]; |
| 1034 | size_t cert_buffer_len = kSSLCertCertificateSizeList[i]; |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 1035 | X509* cert = |
| 1036 | d2i_X509(nullptr, &cert_buffer, checked_cast<long>(cert_buffer_len)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1037 | if (cert) { |
| 1038 | int return_value = X509_STORE_add_cert(SSL_CTX_get_cert_store(ctx), cert); |
| 1039 | if (return_value == 0) { |
| 1040 | LOG(LS_WARNING) << "Unable to add certificate."; |
| 1041 | } else { |
| 1042 | count_of_added_certs++; |
| 1043 | } |
| 1044 | X509_free(cert); |
| 1045 | } |
| 1046 | } |
| 1047 | return count_of_added_certs > 0; |
| 1048 | } |
| 1049 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 1050 | SSL_CTX* OpenSSLAdapter::CreateContext(SSLMode mode, bool enable_cache) { |
Emad Omara | c6de0c9 | 2017-06-21 16:40:56 -0700 | [diff] [blame] | 1051 | // Use (D)TLS 1.2. |
| 1052 | // Note: BoringSSL supports a range of versions by setting max/min version |
| 1053 | // (Default V1.0 to V1.2). However (D)TLSv1_2_client_method functions used |
| 1054 | // below in OpenSSL only support V1.2. |
| 1055 | SSL_CTX* ctx = nullptr; |
| 1056 | #ifdef OPENSSL_IS_BORINGSSL |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 1057 | ctx = SSL_CTX_new(mode == SSL_MODE_DTLS ? DTLS_method() : TLS_method()); |
Emad Omara | c6de0c9 | 2017-06-21 16:40:56 -0700 | [diff] [blame] | 1058 | #else |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 1059 | ctx = SSL_CTX_new(mode == SSL_MODE_DTLS ? DTLSv1_2_client_method() |
| 1060 | : TLSv1_2_client_method()); |
Emad Omara | c6de0c9 | 2017-06-21 16:40:56 -0700 | [diff] [blame] | 1061 | #endif // OPENSSL_IS_BORINGSSL |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 1062 | if (ctx == nullptr) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1063 | unsigned long error = ERR_get_error(); // NOLINT: type used by OpenSSL. |
| 1064 | LOG(LS_WARNING) << "SSL_CTX creation failed: " |
| 1065 | << '"' << ERR_reason_error_string(error) << "\" " |
| 1066 | << "(error=" << error << ')'; |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 1067 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1068 | } |
| 1069 | if (!ConfigureTrustedRootCertificates(ctx)) { |
| 1070 | SSL_CTX_free(ctx); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 1071 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1072 | } |
| 1073 | |
tfarina | a41ab93 | 2015-10-30 16:08:48 -0700 | [diff] [blame] | 1074 | #if !defined(NDEBUG) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1075 | SSL_CTX_set_info_callback(ctx, SSLInfoCallback); |
| 1076 | #endif |
| 1077 | |
| 1078 | SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, SSLVerifyCallback); |
| 1079 | SSL_CTX_set_verify_depth(ctx, 4); |
Emad Omara | c6de0c9 | 2017-06-21 16:40:56 -0700 | [diff] [blame] | 1080 | // Use defaults, but disable HMAC-SHA256 and HMAC-SHA384 ciphers |
| 1081 | // (note that SHA256 and SHA384 only select legacy CBC ciphers). |
| 1082 | // Additionally disable HMAC-SHA1 ciphers in ECDSA. These are the remaining |
| 1083 | // CBC-mode ECDSA ciphers. |
| 1084 | SSL_CTX_set_cipher_list( |
| 1085 | ctx, "ALL:!SHA256:!SHA384:!aPSK:!ECDSA+SHA1:!ADH:!LOW:!EXP:!MD5"); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1086 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 1087 | if (mode == SSL_MODE_DTLS) { |
pthatcher@webrtc.org | a9b1ec0 | 2014-12-29 23:00:14 +0000 | [diff] [blame] | 1088 | SSL_CTX_set_read_ahead(ctx, 1); |
| 1089 | } |
| 1090 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 1091 | if (enable_cache) { |
| 1092 | SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_CLIENT); |
| 1093 | SSL_CTX_sess_set_new_cb(ctx, &OpenSSLAdapter::NewSSLSessionCallback); |
| 1094 | } |
| 1095 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1096 | return ctx; |
| 1097 | } |
| 1098 | |
Justin Uberti | 1d44550 | 2017-08-14 17:04:34 -0700 | [diff] [blame] | 1099 | ////////////////////////////////////////////////////////////////////// |
| 1100 | // OpenSSLAdapterFactory |
| 1101 | ////////////////////////////////////////////////////////////////////// |
| 1102 | |
| 1103 | OpenSSLAdapterFactory::OpenSSLAdapterFactory() |
| 1104 | : ssl_mode_(SSL_MODE_TLS), ssl_ctx_(nullptr) {} |
| 1105 | |
| 1106 | OpenSSLAdapterFactory::~OpenSSLAdapterFactory() { |
| 1107 | for (auto it : sessions_) { |
| 1108 | SSL_SESSION_free(it.second); |
| 1109 | } |
| 1110 | SSL_CTX_free(ssl_ctx_); |
| 1111 | } |
| 1112 | |
| 1113 | void OpenSSLAdapterFactory::SetMode(SSLMode mode) { |
| 1114 | RTC_DCHECK(!ssl_ctx_); |
| 1115 | ssl_mode_ = mode; |
| 1116 | } |
| 1117 | |
| 1118 | OpenSSLAdapter* OpenSSLAdapterFactory::CreateAdapter(AsyncSocket* socket) { |
| 1119 | if (!ssl_ctx_) { |
| 1120 | bool enable_cache = true; |
| 1121 | ssl_ctx_ = OpenSSLAdapter::CreateContext(ssl_mode_, enable_cache); |
| 1122 | if (!ssl_ctx_) { |
| 1123 | return nullptr; |
| 1124 | } |
| 1125 | } |
| 1126 | |
| 1127 | return new OpenSSLAdapter(socket, this); |
| 1128 | } |
| 1129 | |
| 1130 | SSL_SESSION* OpenSSLAdapterFactory::LookupSession(const std::string& hostname) { |
| 1131 | auto it = sessions_.find(hostname); |
| 1132 | return (it != sessions_.end()) ? it->second : nullptr; |
| 1133 | } |
| 1134 | |
| 1135 | void OpenSSLAdapterFactory::AddSession(const std::string& hostname, |
| 1136 | SSL_SESSION* new_session) { |
| 1137 | SSL_SESSION* old_session = LookupSession(hostname); |
| 1138 | SSL_SESSION_free(old_session); |
| 1139 | sessions_[hostname] = new_session; |
| 1140 | } |
| 1141 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1142 | } // namespace rtc |