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