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