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