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