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