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