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