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