henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 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_stream_adapter.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 12 | |
| 13 | #include <openssl/bio.h> |
| 14 | #include <openssl/crypto.h> |
| 15 | #include <openssl/err.h> |
| 16 | #include <openssl/rand.h> |
pthatcher@webrtc.org | 3ee4fe5 | 2015-02-11 22:34:36 +0000 | [diff] [blame] | 17 | #include <openssl/tls1.h> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 18 | #include <openssl/x509v3.h> |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 19 | |
| 20 | #include "absl/strings/string_view.h" |
torbjorng | aad6780 | 2016-04-07 08:55:28 -0700 | [diff] [blame] | 21 | #ifndef OPENSSL_IS_BORINGSSL |
| 22 | #include <openssl/dtls1.h> |
ssaroha | bbfed52 | 2016-12-11 18:42:07 -0800 | [diff] [blame] | 23 | #include <openssl/ssl.h> |
torbjorng | aad6780 | 2016-04-07 08:55:28 -0700 | [diff] [blame] | 24 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 25 | |
Guido Urdaneta | 14bba6e | 2020-09-25 16:00:51 +0200 | [diff] [blame] | 26 | #include <atomic> |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 27 | #include <memory> |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 28 | #include <utility> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 29 | #include <vector> |
| 30 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 31 | #include "rtc_base/checks.h" |
| 32 | #include "rtc_base/logging.h" |
Karl Wiberg | e40468b | 2017-11-22 10:42:26 +0100 | [diff] [blame] | 33 | #include "rtc_base/numerics/safe_conversions.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 34 | #include "rtc_base/openssl.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 35 | #include "rtc_base/openssl_adapter.h" |
| 36 | #include "rtc_base/openssl_digest.h" |
Taylor Brandstetter | 165c618 | 2020-12-10 16:23:03 -0800 | [diff] [blame] | 37 | #ifdef OPENSSL_IS_BORINGSSL |
| 38 | #include "rtc_base/boringssl_identity.h" |
| 39 | #else |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 40 | #include "rtc_base/openssl_identity.h" |
Taylor Brandstetter | 165c618 | 2020-12-10 16:23:03 -0800 | [diff] [blame] | 41 | #endif |
| 42 | #include "rtc_base/openssl_utility.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 43 | #include "rtc_base/ssl_certificate.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 44 | #include "rtc_base/stream.h" |
Philipp Hancke | 117e692 | 2022-06-17 14:14:55 +0200 | [diff] [blame] | 45 | #include "rtc_base/string_encode.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 46 | #include "rtc_base/thread.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 47 | #include "rtc_base/time_utils.h" |
Harald Alvestrand | 1379913 | 2020-03-09 19:39:36 +0100 | [diff] [blame] | 48 | #include "system_wrappers/include/field_trial.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 49 | |
Jiawei Ou | a9c94d5 | 2018-01-30 23:05:07 -0800 | [diff] [blame] | 50 | #if (OPENSSL_VERSION_NUMBER < 0x10100000L) |
| 51 | #error "webrtc requires at least OpenSSL version 1.1.0, to support DTLS-SRTP" |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 52 | #endif |
| 53 | |
Benjamin Wright | 8e98c60 | 2019-02-28 17:25:01 -0800 | [diff] [blame] | 54 | // Defines for the TLS Cipher Suite Map. |
| 55 | #define DEFINE_CIPHER_ENTRY_SSL3(name) \ |
| 56 | { SSL3_CK_##name, "TLS_" #name } |
| 57 | #define DEFINE_CIPHER_ENTRY_TLS1(name) \ |
| 58 | { TLS1_CK_##name, "TLS_" #name } |
| 59 | |
| 60 | namespace rtc { |
| 61 | namespace { |
Danil Chapovalov | 5286dcf | 2022-07-18 17:04:56 +0200 | [diff] [blame] | 62 | using ::webrtc::SafeTask; |
Artem Titov | 96e3b99 | 2021-07-26 16:03:14 +0200 | [diff] [blame] | 63 | // SRTP cipher suite table. `internal_name` is used to construct a |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 64 | // colon-separated profile strings which is needed by |
| 65 | // SSL_CTX_set_tlsext_use_srtp(). |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 66 | struct SrtpCipherMapEntry { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 67 | const char* internal_name; |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 68 | const int id; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 69 | }; |
| 70 | |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 71 | // Cipher name table. Maps internal OpenSSL cipher ids to the RFC name. |
| 72 | struct SslCipherMapEntry { |
| 73 | uint32_t openssl_id; |
| 74 | const char* rfc_name; |
| 75 | }; |
| 76 | |
Benjamin Wright | 8e98c60 | 2019-02-28 17:25:01 -0800 | [diff] [blame] | 77 | // This isn't elegant, but it's better than an external reference |
| 78 | constexpr SrtpCipherMapEntry kSrtpCipherMap[] = { |
Mirko Bonadei | 7750d80 | 2021-07-26 17:27:42 +0200 | [diff] [blame] | 79 | {"SRTP_AES128_CM_SHA1_80", kSrtpAes128CmSha1_80}, |
| 80 | {"SRTP_AES128_CM_SHA1_32", kSrtpAes128CmSha1_32}, |
| 81 | {"SRTP_AEAD_AES_128_GCM", kSrtpAeadAes128Gcm}, |
| 82 | {"SRTP_AEAD_AES_256_GCM", kSrtpAeadAes256Gcm}}; |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 83 | |
Benjamin Wright | 8e98c60 | 2019-02-28 17:25:01 -0800 | [diff] [blame] | 84 | #ifndef OPENSSL_IS_BORINGSSL |
David Benjamin | 3c1f05d | 2017-12-01 17:28:03 -0500 | [diff] [blame] | 85 | // The "SSL_CIPHER_standard_name" function is only available in OpenSSL when |
| 86 | // compiled with tracing, so we need to define the mapping manually here. |
Benjamin Wright | 8e98c60 | 2019-02-28 17:25:01 -0800 | [diff] [blame] | 87 | constexpr SslCipherMapEntry kSslCipherMap[] = { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 88 | // TLS v1.0 ciphersuites from RFC2246. |
| 89 | DEFINE_CIPHER_ENTRY_SSL3(RSA_RC4_128_SHA), |
| 90 | {SSL3_CK_RSA_DES_192_CBC3_SHA, "TLS_RSA_WITH_3DES_EDE_CBC_SHA"}, |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 91 | |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 92 | // AES ciphersuites from RFC3268. |
| 93 | {TLS1_CK_RSA_WITH_AES_128_SHA, "TLS_RSA_WITH_AES_128_CBC_SHA"}, |
| 94 | {TLS1_CK_DHE_RSA_WITH_AES_128_SHA, "TLS_DHE_RSA_WITH_AES_128_CBC_SHA"}, |
| 95 | {TLS1_CK_RSA_WITH_AES_256_SHA, "TLS_RSA_WITH_AES_256_CBC_SHA"}, |
| 96 | {TLS1_CK_DHE_RSA_WITH_AES_256_SHA, "TLS_DHE_RSA_WITH_AES_256_CBC_SHA"}, |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 97 | |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 98 | // ECC ciphersuites from RFC4492. |
| 99 | DEFINE_CIPHER_ENTRY_TLS1(ECDHE_ECDSA_WITH_RC4_128_SHA), |
| 100 | {TLS1_CK_ECDHE_ECDSA_WITH_DES_192_CBC3_SHA, |
| 101 | "TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA"}, |
| 102 | DEFINE_CIPHER_ENTRY_TLS1(ECDHE_ECDSA_WITH_AES_128_CBC_SHA), |
| 103 | DEFINE_CIPHER_ENTRY_TLS1(ECDHE_ECDSA_WITH_AES_256_CBC_SHA), |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 104 | |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 105 | DEFINE_CIPHER_ENTRY_TLS1(ECDHE_RSA_WITH_RC4_128_SHA), |
| 106 | {TLS1_CK_ECDHE_RSA_WITH_DES_192_CBC3_SHA, |
| 107 | "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA"}, |
| 108 | DEFINE_CIPHER_ENTRY_TLS1(ECDHE_RSA_WITH_AES_128_CBC_SHA), |
| 109 | DEFINE_CIPHER_ENTRY_TLS1(ECDHE_RSA_WITH_AES_256_CBC_SHA), |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 110 | |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 111 | // TLS v1.2 ciphersuites. |
| 112 | {TLS1_CK_RSA_WITH_AES_128_SHA256, "TLS_RSA_WITH_AES_128_CBC_SHA256"}, |
| 113 | {TLS1_CK_RSA_WITH_AES_256_SHA256, "TLS_RSA_WITH_AES_256_CBC_SHA256"}, |
| 114 | {TLS1_CK_DHE_RSA_WITH_AES_128_SHA256, |
| 115 | "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256"}, |
| 116 | {TLS1_CK_DHE_RSA_WITH_AES_256_SHA256, |
| 117 | "TLS_DHE_RSA_WITH_AES_256_CBC_SHA256"}, |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 118 | |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 119 | // TLS v1.2 GCM ciphersuites from RFC5288. |
| 120 | DEFINE_CIPHER_ENTRY_TLS1(RSA_WITH_AES_128_GCM_SHA256), |
| 121 | DEFINE_CIPHER_ENTRY_TLS1(RSA_WITH_AES_256_GCM_SHA384), |
| 122 | DEFINE_CIPHER_ENTRY_TLS1(DHE_RSA_WITH_AES_128_GCM_SHA256), |
| 123 | DEFINE_CIPHER_ENTRY_TLS1(DHE_RSA_WITH_AES_256_GCM_SHA384), |
| 124 | DEFINE_CIPHER_ENTRY_TLS1(DH_RSA_WITH_AES_128_GCM_SHA256), |
| 125 | DEFINE_CIPHER_ENTRY_TLS1(DH_RSA_WITH_AES_256_GCM_SHA384), |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 126 | |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 127 | // ECDH HMAC based ciphersuites from RFC5289. |
| 128 | {TLS1_CK_ECDHE_ECDSA_WITH_AES_128_SHA256, |
| 129 | "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256"}, |
| 130 | {TLS1_CK_ECDHE_ECDSA_WITH_AES_256_SHA384, |
| 131 | "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384"}, |
| 132 | {TLS1_CK_ECDHE_RSA_WITH_AES_128_SHA256, |
| 133 | "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256"}, |
| 134 | {TLS1_CK_ECDHE_RSA_WITH_AES_256_SHA384, |
| 135 | "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384"}, |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 136 | |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 137 | // ECDH GCM based ciphersuites from RFC5289. |
| 138 | DEFINE_CIPHER_ENTRY_TLS1(ECDHE_ECDSA_WITH_AES_128_GCM_SHA256), |
| 139 | DEFINE_CIPHER_ENTRY_TLS1(ECDHE_ECDSA_WITH_AES_256_GCM_SHA384), |
| 140 | DEFINE_CIPHER_ENTRY_TLS1(ECDHE_RSA_WITH_AES_128_GCM_SHA256), |
| 141 | DEFINE_CIPHER_ENTRY_TLS1(ECDHE_RSA_WITH_AES_256_GCM_SHA384), |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 142 | |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 143 | {0, nullptr}}; |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 144 | #endif // #ifndef OPENSSL_IS_BORINGSSL |
pthatcher@webrtc.org | 3ee4fe5 | 2015-02-11 22:34:36 +0000 | [diff] [blame] | 145 | |
Benjamin Wright | 8e98c60 | 2019-02-28 17:25:01 -0800 | [diff] [blame] | 146 | #ifdef OPENSSL_IS_BORINGSSL |
| 147 | // Enabled by EnableTimeCallbackForTesting. Should never be set in production |
| 148 | // code. |
| 149 | bool g_use_time_callback_for_testing = false; |
| 150 | // Not used in production code. Actual time should be relative to Jan 1, 1970. |
| 151 | void TimeCallbackForTesting(const SSL* ssl, struct timeval* out_clock) { |
| 152 | int64_t time = TimeNanos(); |
| 153 | out_clock->tv_sec = time / kNumNanosecsPerSec; |
| 154 | out_clock->tv_usec = (time % kNumNanosecsPerSec) / kNumNanosecsPerMicrosec; |
| 155 | } |
| 156 | #endif |
Guo-wei Shieh | 456696a | 2015-09-30 21:48:54 -0700 | [diff] [blame] | 157 | |
Benjamin Wright | 8e98c60 | 2019-02-28 17:25:01 -0800 | [diff] [blame] | 158 | } // namespace |
Guo-wei Shieh | 456696a | 2015-09-30 21:48:54 -0700 | [diff] [blame] | 159 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 160 | ////////////////////////////////////////////////////////////////////// |
| 161 | // StreamBIO |
| 162 | ////////////////////////////////////////////////////////////////////// |
| 163 | |
| 164 | static int stream_write(BIO* h, const char* buf, int num); |
| 165 | static int stream_read(BIO* h, char* buf, int size); |
| 166 | static int stream_puts(BIO* h, const char* str); |
| 167 | static long stream_ctrl(BIO* h, int cmd, long arg1, void* arg2); |
| 168 | static int stream_new(BIO* h); |
| 169 | static int stream_free(BIO* data); |
| 170 | |
Jiawei Ou | eb0df08 | 2018-02-02 14:51:18 -0800 | [diff] [blame] | 171 | static BIO_METHOD* BIO_stream_method() { |
| 172 | static BIO_METHOD* method = [] { |
| 173 | BIO_METHOD* method = BIO_meth_new(BIO_TYPE_BIO, "stream"); |
| 174 | BIO_meth_set_write(method, stream_write); |
| 175 | BIO_meth_set_read(method, stream_read); |
| 176 | BIO_meth_set_puts(method, stream_puts); |
| 177 | BIO_meth_set_ctrl(method, stream_ctrl); |
| 178 | BIO_meth_set_create(method, stream_new); |
| 179 | BIO_meth_set_destroy(method, stream_free); |
| 180 | return method; |
| 181 | }(); |
| 182 | return method; |
| 183 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 184 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 185 | static BIO* BIO_new_stream(StreamInterface* stream) { |
Jiawei Ou | eb0df08 | 2018-02-02 14:51:18 -0800 | [diff] [blame] | 186 | BIO* ret = BIO_new(BIO_stream_method()); |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 187 | if (ret == nullptr) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 188 | return nullptr; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 189 | } |
Jiawei Ou | eb0df08 | 2018-02-02 14:51:18 -0800 | [diff] [blame] | 190 | BIO_set_data(ret, stream); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 191 | return ret; |
| 192 | } |
| 193 | |
| 194 | // bio methods return 1 (or at least non-zero) on success and 0 on failure. |
| 195 | |
| 196 | static int stream_new(BIO* b) { |
Jiawei Ou | eb0df08 | 2018-02-02 14:51:18 -0800 | [diff] [blame] | 197 | BIO_set_shutdown(b, 0); |
| 198 | BIO_set_init(b, 1); |
| 199 | BIO_set_data(b, 0); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 200 | return 1; |
| 201 | } |
| 202 | |
| 203 | static int stream_free(BIO* b) { |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 204 | if (b == nullptr) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 205 | return 0; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 206 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 207 | return 1; |
| 208 | } |
| 209 | |
| 210 | static int stream_read(BIO* b, char* out, int outl) { |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 211 | if (!out) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 212 | return -1; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 213 | } |
Jiawei Ou | eb0df08 | 2018-02-02 14:51:18 -0800 | [diff] [blame] | 214 | StreamInterface* stream = static_cast<StreamInterface*>(BIO_get_data(b)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 215 | BIO_clear_retry_flags(b); |
| 216 | size_t read; |
| 217 | int error; |
| 218 | StreamResult result = stream->Read(out, outl, &read, &error); |
| 219 | if (result == SR_SUCCESS) { |
henrike@webrtc.org | d89b69a | 2014-11-06 17:23:09 +0000 | [diff] [blame] | 220 | return checked_cast<int>(read); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 221 | } else if (result == SR_BLOCK) { |
| 222 | BIO_set_retry_read(b); |
| 223 | } |
| 224 | return -1; |
| 225 | } |
| 226 | |
| 227 | static int stream_write(BIO* b, const char* in, int inl) { |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 228 | if (!in) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 229 | return -1; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 230 | } |
Jiawei Ou | eb0df08 | 2018-02-02 14:51:18 -0800 | [diff] [blame] | 231 | StreamInterface* stream = static_cast<StreamInterface*>(BIO_get_data(b)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 232 | BIO_clear_retry_flags(b); |
| 233 | size_t written; |
| 234 | int error; |
| 235 | StreamResult result = stream->Write(in, inl, &written, &error); |
| 236 | if (result == SR_SUCCESS) { |
henrike@webrtc.org | d89b69a | 2014-11-06 17:23:09 +0000 | [diff] [blame] | 237 | return checked_cast<int>(written); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 238 | } else if (result == SR_BLOCK) { |
| 239 | BIO_set_retry_write(b); |
| 240 | } |
| 241 | return -1; |
| 242 | } |
| 243 | |
| 244 | static int stream_puts(BIO* b, const char* str) { |
henrike@webrtc.org | d89b69a | 2014-11-06 17:23:09 +0000 | [diff] [blame] | 245 | return stream_write(b, str, checked_cast<int>(strlen(str))); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | static long stream_ctrl(BIO* b, int cmd, long num, void* ptr) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 249 | switch (cmd) { |
| 250 | case BIO_CTRL_RESET: |
| 251 | return 0; |
Jiawei Ou | 018dd6e | 2018-01-30 12:13:48 -0800 | [diff] [blame] | 252 | case BIO_CTRL_EOF: { |
| 253 | StreamInterface* stream = static_cast<StreamInterface*>(ptr); |
| 254 | // 1 means end-of-stream. |
| 255 | return (stream->GetState() == SS_CLOSED) ? 1 : 0; |
| 256 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 257 | case BIO_CTRL_WPENDING: |
| 258 | case BIO_CTRL_PENDING: |
| 259 | return 0; |
| 260 | case BIO_CTRL_FLUSH: |
| 261 | return 1; |
Henrik Lundin | f4baca5 | 2015-06-10 09:45:58 +0200 | [diff] [blame] | 262 | case BIO_CTRL_DGRAM_QUERY_MTU: |
| 263 | // openssl defaults to mtu=256 unless we return something here. |
| 264 | // The handshake doesn't actually need to send packets above 1k, |
| 265 | // so this seems like a sensible value that should work in most cases. |
| 266 | // Webrtc uses the same value for video packets. |
| 267 | return 1200; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 268 | default: |
| 269 | return 0; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | ///////////////////////////////////////////////////////////////////////////// |
| 274 | // OpenSSLStreamAdapter |
| 275 | ///////////////////////////////////////////////////////////////////////////// |
| 276 | |
Guido Urdaneta | 14bba6e | 2020-09-25 16:00:51 +0200 | [diff] [blame] | 277 | static std::atomic<bool> g_use_legacy_tls_protocols_override(false); |
| 278 | static std::atomic<bool> g_allow_legacy_tls_protocols(false); |
| 279 | |
| 280 | void SetAllowLegacyTLSProtocols(const absl::optional<bool>& allow) { |
| 281 | g_use_legacy_tls_protocols_override.store(allow.has_value()); |
| 282 | if (allow.has_value()) |
| 283 | g_allow_legacy_tls_protocols.store(allow.value()); |
| 284 | } |
| 285 | |
| 286 | bool ShouldAllowLegacyTLSProtocols() { |
| 287 | return g_use_legacy_tls_protocols_override.load() |
| 288 | ? g_allow_legacy_tls_protocols.load() |
Guido Urdaneta | ae2e864 | 2020-10-26 09:55:26 +0100 | [diff] [blame] | 289 | : webrtc::field_trial::IsEnabled("WebRTC-LegacyTlsProtocols"); |
Guido Urdaneta | 14bba6e | 2020-09-25 16:00:51 +0200 | [diff] [blame] | 290 | } |
| 291 | |
Harald Alvestrand | 8515d5a | 2020-03-20 22:51:32 +0100 | [diff] [blame] | 292 | OpenSSLStreamAdapter::OpenSSLStreamAdapter( |
| 293 | std::unique_ptr<StreamInterface> stream) |
Niels Möller | 0131a4d | 2021-04-16 09:16:21 +0200 | [diff] [blame] | 294 | : stream_(std::move(stream)), |
Tommi | 0448298 | 2020-10-05 12:43:53 +0000 | [diff] [blame] | 295 | owner_(rtc::Thread::Current()), |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 296 | state_(SSL_NONE), |
| 297 | role_(SSL_CLIENT), |
Guo-wei Shieh | a7446d2 | 2016-01-11 15:27:03 -0800 | [diff] [blame] | 298 | ssl_read_needs_write_(false), |
| 299 | ssl_write_needs_read_(false), |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 300 | ssl_(nullptr), |
| 301 | ssl_ctx_(nullptr), |
Joachim Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 302 | ssl_mode_(SSL_MODE_TLS), |
Harald Alvestrand | 1379913 | 2020-03-09 19:39:36 +0100 | [diff] [blame] | 303 | ssl_max_version_(SSL_PROTOCOL_TLS_12), |
| 304 | // Default is to support legacy TLS protocols. |
| 305 | // This will be changed to default non-support in M82 or M83. |
Niels Möller | 0131a4d | 2021-04-16 09:16:21 +0200 | [diff] [blame] | 306 | support_legacy_tls_protocols_flag_(ShouldAllowLegacyTLSProtocols()) { |
| 307 | stream_->SignalEvent.connect(this, &OpenSSLStreamAdapter::OnEvent); |
| 308 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 309 | |
| 310 | OpenSSLStreamAdapter::~OpenSSLStreamAdapter() { |
Tommi | 0448298 | 2020-10-05 12:43:53 +0000 | [diff] [blame] | 311 | timeout_task_.Stop(); |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 312 | Cleanup(0); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Harald Alvestrand | 8515d5a | 2020-03-20 22:51:32 +0100 | [diff] [blame] | 315 | void OpenSSLStreamAdapter::SetIdentity(std::unique_ptr<SSLIdentity> identity) { |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 316 | RTC_DCHECK(!identity_); |
Taylor Brandstetter | 165c618 | 2020-12-10 16:23:03 -0800 | [diff] [blame] | 317 | #ifdef OPENSSL_IS_BORINGSSL |
| 318 | identity_.reset(static_cast<BoringSSLIdentity*>(identity.release())); |
| 319 | #else |
Harald Alvestrand | 8515d5a | 2020-03-20 22:51:32 +0100 | [diff] [blame] | 320 | identity_.reset(static_cast<OpenSSLIdentity*>(identity.release())); |
Taylor Brandstetter | 165c618 | 2020-12-10 16:23:03 -0800 | [diff] [blame] | 321 | #endif |
Harald Alvestrand | 8515d5a | 2020-03-20 22:51:32 +0100 | [diff] [blame] | 322 | } |
| 323 | |
Taylor Brandstetter | 165c618 | 2020-12-10 16:23:03 -0800 | [diff] [blame] | 324 | SSLIdentity* OpenSSLStreamAdapter::GetIdentityForTesting() const { |
Harald Alvestrand | 8515d5a | 2020-03-20 22:51:32 +0100 | [diff] [blame] | 325 | return identity_.get(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | void OpenSSLStreamAdapter::SetServerRole(SSLRole role) { |
| 329 | role_ = role; |
| 330 | } |
| 331 | |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 332 | bool OpenSSLStreamAdapter::SetPeerCertificateDigest( |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 333 | absl::string_view digest_alg, |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 334 | const unsigned char* digest_val, |
| 335 | size_t digest_len, |
| 336 | SSLPeerCertificateDigestError* error) { |
| 337 | RTC_DCHECK(!peer_certificate_verified_); |
Benjamin Wright | 5d35554 | 2018-10-26 17:57:00 -0700 | [diff] [blame] | 338 | RTC_DCHECK(!HasPeerCertificateDigest()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 339 | size_t expected_len; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 340 | if (error) { |
| 341 | *error = SSLPeerCertificateDigestError::NONE; |
| 342 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 343 | |
| 344 | if (!OpenSSLDigest::GetDigestSize(digest_alg, &expected_len)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 345 | RTC_LOG(LS_WARNING) << "Unknown digest algorithm: " << digest_alg; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 346 | if (error) { |
| 347 | *error = SSLPeerCertificateDigestError::UNKNOWN_ALGORITHM; |
| 348 | } |
deadbeef | 81f6f4f | 2016-09-19 17:20:52 -0700 | [diff] [blame] | 349 | return false; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 350 | } |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 351 | if (expected_len != digest_len) { |
| 352 | if (error) { |
| 353 | *error = SSLPeerCertificateDigestError::INVALID_LENGTH; |
| 354 | } |
deadbeef | 81f6f4f | 2016-09-19 17:20:52 -0700 | [diff] [blame] | 355 | return false; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 356 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 357 | |
| 358 | peer_certificate_digest_value_.SetData(digest_val, digest_len); |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 359 | peer_certificate_digest_algorithm_ = std::string(digest_alg); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 360 | |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 361 | if (!peer_cert_chain_) { |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 362 | // Normal case, where the digest is set before we obtain the certificate |
| 363 | // from the handshake. |
| 364 | return true; |
| 365 | } |
| 366 | |
| 367 | if (!VerifyPeerCertificate()) { |
| 368 | Error("SetPeerCertificateDigest", -1, SSL_AD_BAD_CERTIFICATE, false); |
| 369 | if (error) { |
| 370 | *error = SSLPeerCertificateDigestError::VERIFICATION_FAILED; |
| 371 | } |
| 372 | return false; |
| 373 | } |
| 374 | |
| 375 | if (state_ == SSL_CONNECTED) { |
| 376 | // Post the event asynchronously to unwind the stack. The caller |
| 377 | // of ContinueSSL may be the same object listening for these |
| 378 | // events and may not be prepared for reentrancy. |
| 379 | PostEvent(SE_OPEN | SE_READ | SE_WRITE, 0); |
| 380 | } |
| 381 | |
deadbeef | 81f6f4f | 2016-09-19 17:20:52 -0700 | [diff] [blame] | 382 | return true; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 383 | } |
| 384 | |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 385 | std::string OpenSSLStreamAdapter::SslCipherSuiteToName(int cipher_suite) { |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 386 | #ifdef OPENSSL_IS_BORINGSSL |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 387 | const SSL_CIPHER* ssl_cipher = SSL_get_cipher_by_value(cipher_suite); |
Guo-wei Shieh | 456696a | 2015-09-30 21:48:54 -0700 | [diff] [blame] | 388 | if (!ssl_cipher) { |
| 389 | return std::string(); |
| 390 | } |
David Benjamin | a8f7376 | 2017-09-28 15:49:42 -0400 | [diff] [blame] | 391 | return SSL_CIPHER_standard_name(ssl_cipher); |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 392 | #else |
Joachim Reiersen | e457e43 | 2022-10-21 12:08:22 -0700 | [diff] [blame] | 393 | const int openssl_cipher_id = 0x03000000L | cipher_suite; |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 394 | for (const SslCipherMapEntry* entry = kSslCipherMap; entry->rfc_name; |
| 395 | ++entry) { |
Joachim Reiersen | e457e43 | 2022-10-21 12:08:22 -0700 | [diff] [blame] | 396 | if (openssl_cipher_id == static_cast<int>(entry->openssl_id)) { |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 397 | return entry->rfc_name; |
| 398 | } |
| 399 | } |
| 400 | return std::string(); |
| 401 | #endif |
Guo-wei Shieh | 456696a | 2015-09-30 21:48:54 -0700 | [diff] [blame] | 402 | } |
pthatcher@webrtc.org | 3ee4fe5 | 2015-02-11 22:34:36 +0000 | [diff] [blame] | 403 | |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 404 | bool OpenSSLStreamAdapter::GetSslCipherSuite(int* cipher_suite) { |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 405 | if (state_ != SSL_CONNECTED) { |
pthatcher@webrtc.org | 3ee4fe5 | 2015-02-11 22:34:36 +0000 | [diff] [blame] | 406 | return false; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 407 | } |
pthatcher@webrtc.org | 3ee4fe5 | 2015-02-11 22:34:36 +0000 | [diff] [blame] | 408 | |
| 409 | const SSL_CIPHER* current_cipher = SSL_get_current_cipher(ssl_); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 410 | if (current_cipher == nullptr) { |
pthatcher@webrtc.org | 3ee4fe5 | 2015-02-11 22:34:36 +0000 | [diff] [blame] | 411 | return false; |
| 412 | } |
| 413 | |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 414 | *cipher_suite = static_cast<uint16_t>(SSL_CIPHER_get_id(current_cipher)); |
pthatcher@webrtc.org | 3ee4fe5 | 2015-02-11 22:34:36 +0000 | [diff] [blame] | 415 | return true; |
| 416 | } |
| 417 | |
Harald Alvestrand | 5cb7807 | 2019-10-28 09:51:17 +0100 | [diff] [blame] | 418 | SSLProtocolVersion OpenSSLStreamAdapter::GetSslVersion() const { |
Benjamin Wright | 8e98c60 | 2019-02-28 17:25:01 -0800 | [diff] [blame] | 419 | if (state_ != SSL_CONNECTED) { |
Harald Alvestrand | 5cb7807 | 2019-10-28 09:51:17 +0100 | [diff] [blame] | 420 | return SSL_PROTOCOL_NOT_GIVEN; |
Benjamin Wright | 8e98c60 | 2019-02-28 17:25:01 -0800 | [diff] [blame] | 421 | } |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 422 | |
| 423 | int ssl_version = SSL_version(ssl_); |
| 424 | if (ssl_mode_ == SSL_MODE_DTLS) { |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 425 | if (ssl_version == DTLS1_VERSION) { |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 426 | return SSL_PROTOCOL_DTLS_10; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 427 | } else if (ssl_version == DTLS1_2_VERSION) { |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 428 | return SSL_PROTOCOL_DTLS_12; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 429 | } |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 430 | } else { |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 431 | if (ssl_version == TLS1_VERSION) { |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 432 | return SSL_PROTOCOL_TLS_10; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 433 | } else if (ssl_version == TLS1_1_VERSION) { |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 434 | return SSL_PROTOCOL_TLS_11; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 435 | } else if (ssl_version == TLS1_2_VERSION) { |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 436 | return SSL_PROTOCOL_TLS_12; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 437 | } |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 438 | } |
| 439 | |
Harald Alvestrand | 5cb7807 | 2019-10-28 09:51:17 +0100 | [diff] [blame] | 440 | return SSL_PROTOCOL_NOT_GIVEN; |
| 441 | } |
| 442 | |
| 443 | bool OpenSSLStreamAdapter::GetSslVersionBytes(int* version) const { |
| 444 | if (state_ != SSL_CONNECTED) { |
| 445 | return false; |
| 446 | } |
| 447 | *version = SSL_version(ssl_); |
| 448 | return true; |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 449 | } |
| 450 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 451 | // Key Extractor interface |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 452 | bool OpenSSLStreamAdapter::ExportKeyingMaterial(absl::string_view label, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 453 | const uint8_t* context, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 454 | size_t context_len, |
| 455 | bool use_context, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 456 | uint8_t* result, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 457 | size_t result_len) { |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 458 | if (SSL_export_keying_material(ssl_, result, result_len, label.data(), |
| 459 | label.length(), context, context_len, |
| 460 | use_context) != 1) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 461 | return false; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 462 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 463 | return true; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 464 | } |
| 465 | |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 466 | bool OpenSSLStreamAdapter::SetDtlsSrtpCryptoSuites( |
| 467 | const std::vector<int>& ciphers) { |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 468 | if (state_ != SSL_NONE) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 469 | return false; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 470 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 471 | |
Benjamin Wright | 8e98c60 | 2019-02-28 17:25:01 -0800 | [diff] [blame] | 472 | std::string internal_ciphers; |
| 473 | for (const int cipher : ciphers) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 474 | bool found = false; |
Benjamin Wright | 8e98c60 | 2019-02-28 17:25:01 -0800 | [diff] [blame] | 475 | for (const auto& entry : kSrtpCipherMap) { |
| 476 | if (cipher == entry.id) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 477 | found = true; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 478 | if (!internal_ciphers.empty()) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 479 | internal_ciphers += ":"; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 480 | } |
Benjamin Wright | 8e98c60 | 2019-02-28 17:25:01 -0800 | [diff] [blame] | 481 | internal_ciphers += entry.internal_name; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 482 | break; |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | if (!found) { |
Benjamin Wright | 8e98c60 | 2019-02-28 17:25:01 -0800 | [diff] [blame] | 487 | RTC_LOG(LS_ERROR) << "Could not find cipher: " << cipher; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 488 | return false; |
| 489 | } |
| 490 | } |
| 491 | |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 492 | if (internal_ciphers.empty()) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 493 | return false; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 494 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 495 | |
| 496 | srtp_ciphers_ = internal_ciphers; |
| 497 | return true; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 500 | bool OpenSSLStreamAdapter::GetDtlsSrtpCryptoSuite(int* crypto_suite) { |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 501 | RTC_DCHECK(state_ == SSL_CONNECTED); |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 502 | if (state_ != SSL_CONNECTED) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 503 | return false; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 504 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 505 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 506 | const SRTP_PROTECTION_PROFILE* srtp_profile = |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 507 | SSL_get_selected_srtp_profile(ssl_); |
| 508 | |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 509 | if (!srtp_profile) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 510 | return false; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 511 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 512 | |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 513 | *crypto_suite = srtp_profile->id; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 514 | RTC_DCHECK(!SrtpCryptoSuiteToName(*crypto_suite).empty()); |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 515 | return true; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 516 | } |
| 517 | |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 518 | bool OpenSSLStreamAdapter::IsTlsConnected() { |
| 519 | return state_ == SSL_CONNECTED; |
| 520 | } |
| 521 | |
Taylor Brandstetter | c8762a8 | 2016-08-11 12:01:49 -0700 | [diff] [blame] | 522 | int OpenSSLStreamAdapter::StartSSL() { |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 523 | // Don't allow StartSSL to be called twice. |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 524 | if (state_ != SSL_NONE) { |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 525 | return -1; |
| 526 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 527 | |
Niels Möller | 0131a4d | 2021-04-16 09:16:21 +0200 | [diff] [blame] | 528 | if (stream_->GetState() != SS_OPEN) { |
Taylor Brandstetter | c8762a8 | 2016-08-11 12:01:49 -0700 | [diff] [blame] | 529 | state_ = SSL_WAIT; |
| 530 | return 0; |
| 531 | } |
| 532 | |
| 533 | state_ = SSL_CONNECTING; |
| 534 | if (int err = BeginSSL()) { |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 535 | Error("BeginSSL", err, 0, false); |
Taylor Brandstetter | c8762a8 | 2016-08-11 12:01:49 -0700 | [diff] [blame] | 536 | return err; |
| 537 | } |
| 538 | |
| 539 | return 0; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | void OpenSSLStreamAdapter::SetMode(SSLMode mode) { |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 543 | RTC_DCHECK(state_ == SSL_NONE); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 544 | ssl_mode_ = mode; |
| 545 | } |
| 546 | |
Joachim Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 547 | void OpenSSLStreamAdapter::SetMaxProtocolVersion(SSLProtocolVersion version) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 548 | RTC_DCHECK(ssl_ctx_ == nullptr); |
Joachim Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 549 | ssl_max_version_ = version; |
| 550 | } |
| 551 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 552 | void OpenSSLStreamAdapter::SetInitialRetransmissionTimeout(int timeout_ms) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 553 | RTC_DCHECK(ssl_ctx_ == nullptr); |
skvlad | d030912 | 2017-02-02 17:18:37 -0800 | [diff] [blame] | 554 | dtls_handshake_timeout_ms_ = timeout_ms; |
| 555 | } |
| 556 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 557 | // |
| 558 | // StreamInterface Implementation |
| 559 | // |
| 560 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 561 | StreamResult OpenSSLStreamAdapter::Write(const void* data, |
| 562 | size_t data_len, |
| 563 | size_t* written, |
| 564 | int* error) { |
Tomas Gunnarsson | d48ff45 | 2020-10-02 18:28:46 +0200 | [diff] [blame] | 565 | RTC_DLOG(LS_VERBOSE) << "OpenSSLStreamAdapter::Write(" << data_len << ")"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 566 | |
| 567 | switch (state_) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 568 | case SSL_NONE: |
| 569 | // pass-through in clear text |
Niels Möller | 0131a4d | 2021-04-16 09:16:21 +0200 | [diff] [blame] | 570 | return stream_->Write(data, data_len, written, error); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 571 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 572 | case SSL_WAIT: |
| 573 | case SSL_CONNECTING: |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 574 | return SR_BLOCK; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 575 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 576 | case SSL_CONNECTED: |
Benjamin Wright | 5d35554 | 2018-10-26 17:57:00 -0700 | [diff] [blame] | 577 | if (WaitingToVerifyPeerCertificate()) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 578 | return SR_BLOCK; |
| 579 | } |
| 580 | break; |
| 581 | |
| 582 | case SSL_ERROR: |
| 583 | case SSL_CLOSED: |
| 584 | default: |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 585 | if (error) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 586 | *error = ssl_error_code_; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 587 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 588 | return SR_ERROR; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | // OpenSSL will return an error if we try to write zero bytes |
| 592 | if (data_len == 0) { |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 593 | if (written) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 594 | *written = 0; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 595 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 596 | return SR_SUCCESS; |
| 597 | } |
| 598 | |
| 599 | ssl_write_needs_read_ = false; |
| 600 | |
henrike@webrtc.org | d89b69a | 2014-11-06 17:23:09 +0000 | [diff] [blame] | 601 | int code = SSL_write(ssl_, data, checked_cast<int>(data_len)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 602 | int ssl_error = SSL_get_error(ssl_, code); |
| 603 | switch (ssl_error) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 604 | case SSL_ERROR_NONE: |
Tomas Gunnarsson | d48ff45 | 2020-10-02 18:28:46 +0200 | [diff] [blame] | 605 | RTC_DLOG(LS_VERBOSE) << " -- success"; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 606 | RTC_DCHECK_GT(code, 0); |
| 607 | RTC_DCHECK_LE(code, data_len); |
| 608 | if (written) |
| 609 | *written = code; |
| 610 | return SR_SUCCESS; |
| 611 | case SSL_ERROR_WANT_READ: |
Tomas Gunnarsson | d48ff45 | 2020-10-02 18:28:46 +0200 | [diff] [blame] | 612 | RTC_DLOG(LS_VERBOSE) << " -- error want read"; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 613 | ssl_write_needs_read_ = true; |
| 614 | return SR_BLOCK; |
| 615 | case SSL_ERROR_WANT_WRITE: |
Tomas Gunnarsson | d48ff45 | 2020-10-02 18:28:46 +0200 | [diff] [blame] | 616 | RTC_DLOG(LS_VERBOSE) << " -- error want write"; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 617 | return SR_BLOCK; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 618 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 619 | case SSL_ERROR_ZERO_RETURN: |
| 620 | default: |
| 621 | Error("SSL_write", (ssl_error ? ssl_error : -1), 0, false); |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 622 | if (error) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 623 | *error = ssl_error_code_; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 624 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 625 | return SR_ERROR; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 626 | } |
| 627 | // not reached |
| 628 | } |
| 629 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 630 | StreamResult OpenSSLStreamAdapter::Read(void* data, |
| 631 | size_t data_len, |
| 632 | size_t* read, |
| 633 | int* error) { |
Tomas Gunnarsson | d48ff45 | 2020-10-02 18:28:46 +0200 | [diff] [blame] | 634 | RTC_DLOG(LS_VERBOSE) << "OpenSSLStreamAdapter::Read(" << data_len << ")"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 635 | switch (state_) { |
| 636 | case SSL_NONE: |
| 637 | // pass-through in clear text |
Niels Möller | 0131a4d | 2021-04-16 09:16:21 +0200 | [diff] [blame] | 638 | return stream_->Read(data, data_len, read, error); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 639 | case SSL_WAIT: |
| 640 | case SSL_CONNECTING: |
| 641 | return SR_BLOCK; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 642 | case SSL_CONNECTED: |
Benjamin Wright | 5d35554 | 2018-10-26 17:57:00 -0700 | [diff] [blame] | 643 | if (WaitingToVerifyPeerCertificate()) { |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 644 | return SR_BLOCK; |
| 645 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 646 | break; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 647 | case SSL_CLOSED: |
| 648 | return SR_EOS; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 649 | case SSL_ERROR: |
| 650 | default: |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 651 | if (error) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 652 | *error = ssl_error_code_; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 653 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 654 | return SR_ERROR; |
| 655 | } |
| 656 | |
| 657 | // Don't trust OpenSSL with zero byte reads |
| 658 | if (data_len == 0) { |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 659 | if (read) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 660 | *read = 0; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 661 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 662 | return SR_SUCCESS; |
| 663 | } |
| 664 | |
| 665 | ssl_read_needs_write_ = false; |
| 666 | |
Benjamin Wright | f54e30b | 2019-02-26 17:33:50 -0800 | [diff] [blame] | 667 | const int code = SSL_read(ssl_, data, checked_cast<int>(data_len)); |
| 668 | const int ssl_error = SSL_get_error(ssl_, code); |
| 669 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 670 | switch (ssl_error) { |
| 671 | case SSL_ERROR_NONE: |
Tomas Gunnarsson | d48ff45 | 2020-10-02 18:28:46 +0200 | [diff] [blame] | 672 | RTC_DLOG(LS_VERBOSE) << " -- success"; |
kwiberg | ee89e78 | 2017-08-09 17:22:01 -0700 | [diff] [blame] | 673 | RTC_DCHECK_GT(code, 0); |
| 674 | RTC_DCHECK_LE(code, data_len); |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 675 | if (read) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 676 | *read = code; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 677 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 678 | |
| 679 | if (ssl_mode_ == SSL_MODE_DTLS) { |
| 680 | // Enforce atomic reads -- this is a short read |
| 681 | unsigned int pending = SSL_pending(ssl_); |
| 682 | |
| 683 | if (pending) { |
Tomas Gunnarsson | d48ff45 | 2020-10-02 18:28:46 +0200 | [diff] [blame] | 684 | RTC_DLOG(LS_INFO) << " -- short DTLS read. flushing"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 685 | FlushInput(pending); |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 686 | if (error) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 687 | *error = SSE_MSG_TRUNC; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 688 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 689 | return SR_ERROR; |
| 690 | } |
| 691 | } |
| 692 | return SR_SUCCESS; |
| 693 | case SSL_ERROR_WANT_READ: |
Tomas Gunnarsson | d48ff45 | 2020-10-02 18:28:46 +0200 | [diff] [blame] | 694 | RTC_DLOG(LS_VERBOSE) << " -- error want read"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 695 | return SR_BLOCK; |
| 696 | case SSL_ERROR_WANT_WRITE: |
Tomas Gunnarsson | d48ff45 | 2020-10-02 18:28:46 +0200 | [diff] [blame] | 697 | RTC_DLOG(LS_VERBOSE) << " -- error want write"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 698 | ssl_read_needs_write_ = true; |
| 699 | return SR_BLOCK; |
| 700 | case SSL_ERROR_ZERO_RETURN: |
Tomas Gunnarsson | d48ff45 | 2020-10-02 18:28:46 +0200 | [diff] [blame] | 701 | RTC_DLOG(LS_VERBOSE) << " -- remote side closed"; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 702 | Close(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 703 | return SR_EOS; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 704 | default: |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 705 | Error("SSL_read", (ssl_error ? ssl_error : -1), 0, false); |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 706 | if (error) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 707 | *error = ssl_error_code_; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 708 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 709 | return SR_ERROR; |
| 710 | } |
| 711 | // not reached |
| 712 | } |
| 713 | |
| 714 | void OpenSSLStreamAdapter::FlushInput(unsigned int left) { |
| 715 | unsigned char buf[2048]; |
| 716 | |
| 717 | while (left) { |
| 718 | // This should always succeed |
Benjamin Wright | f54e30b | 2019-02-26 17:33:50 -0800 | [diff] [blame] | 719 | const int toread = (sizeof(buf) < left) ? sizeof(buf) : left; |
| 720 | const int code = SSL_read(ssl_, buf, toread); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 721 | |
Benjamin Wright | f54e30b | 2019-02-26 17:33:50 -0800 | [diff] [blame] | 722 | const int ssl_error = SSL_get_error(ssl_, code); |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 723 | RTC_DCHECK(ssl_error == SSL_ERROR_NONE); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 724 | |
| 725 | if (ssl_error != SSL_ERROR_NONE) { |
Jonas Olsson | addc380 | 2018-02-01 09:53:06 +0100 | [diff] [blame] | 726 | RTC_DLOG(LS_VERBOSE) << " -- error " << code; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 727 | Error("SSL_read", (ssl_error ? ssl_error : -1), 0, false); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 728 | return; |
| 729 | } |
| 730 | |
Tomas Gunnarsson | d48ff45 | 2020-10-02 18:28:46 +0200 | [diff] [blame] | 731 | RTC_DLOG(LS_VERBOSE) << " -- flushed " << code << " bytes"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 732 | left -= code; |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | void OpenSSLStreamAdapter::Close() { |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 737 | Cleanup(0); |
| 738 | RTC_DCHECK(state_ == SSL_CLOSED || state_ == SSL_ERROR); |
| 739 | // When we're closed at SSL layer, also close the stream level which |
| 740 | // performs necessary clean up. Otherwise, a new incoming packet after |
| 741 | // this could overflow the stream buffer. |
Niels Möller | 0131a4d | 2021-04-16 09:16:21 +0200 | [diff] [blame] | 742 | stream_->Close(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | StreamState OpenSSLStreamAdapter::GetState() const { |
| 746 | switch (state_) { |
| 747 | case SSL_WAIT: |
| 748 | case SSL_CONNECTING: |
| 749 | return SS_OPENING; |
| 750 | case SSL_CONNECTED: |
Benjamin Wright | 5d35554 | 2018-10-26 17:57:00 -0700 | [diff] [blame] | 751 | if (WaitingToVerifyPeerCertificate()) { |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 752 | return SS_OPENING; |
| 753 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 754 | return SS_OPEN; |
| 755 | default: |
| 756 | return SS_CLOSED; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 757 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 758 | // not reached |
| 759 | } |
| 760 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 761 | void OpenSSLStreamAdapter::OnEvent(StreamInterface* stream, |
| 762 | int events, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 763 | int err) { |
| 764 | int events_to_signal = 0; |
| 765 | int signal_error = 0; |
Niels Möller | 0131a4d | 2021-04-16 09:16:21 +0200 | [diff] [blame] | 766 | RTC_DCHECK(stream == stream_.get()); |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 767 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 768 | if ((events & SE_OPEN)) { |
Tomas Gunnarsson | d48ff45 | 2020-10-02 18:28:46 +0200 | [diff] [blame] | 769 | RTC_DLOG(LS_VERBOSE) << "OpenSSLStreamAdapter::OnEvent SE_OPEN"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 770 | if (state_ != SSL_WAIT) { |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 771 | RTC_DCHECK(state_ == SSL_NONE); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 772 | events_to_signal |= SE_OPEN; |
| 773 | } else { |
| 774 | state_ = SSL_CONNECTING; |
| 775 | if (int err = BeginSSL()) { |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 776 | Error("BeginSSL", err, 0, true); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 777 | return; |
| 778 | } |
| 779 | } |
| 780 | } |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 781 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 782 | if ((events & (SE_READ | SE_WRITE))) { |
Tomas Gunnarsson | d48ff45 | 2020-10-02 18:28:46 +0200 | [diff] [blame] | 783 | RTC_DLOG(LS_VERBOSE) << "OpenSSLStreamAdapter::OnEvent" |
| 784 | << ((events & SE_READ) ? " SE_READ" : "") |
| 785 | << ((events & SE_WRITE) ? " SE_WRITE" : ""); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 786 | if (state_ == SSL_NONE) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 787 | events_to_signal |= events & (SE_READ | SE_WRITE); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 788 | } else if (state_ == SSL_CONNECTING) { |
| 789 | if (int err = ContinueSSL()) { |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 790 | Error("ContinueSSL", err, 0, true); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 791 | return; |
| 792 | } |
| 793 | } else if (state_ == SSL_CONNECTED) { |
| 794 | if (((events & SE_READ) && ssl_write_needs_read_) || |
| 795 | (events & SE_WRITE)) { |
Tomas Gunnarsson | d48ff45 | 2020-10-02 18:28:46 +0200 | [diff] [blame] | 796 | RTC_DLOG(LS_VERBOSE) << " -- onStreamWriteable"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 797 | events_to_signal |= SE_WRITE; |
| 798 | } |
| 799 | if (((events & SE_WRITE) && ssl_read_needs_write_) || |
| 800 | (events & SE_READ)) { |
Tomas Gunnarsson | d48ff45 | 2020-10-02 18:28:46 +0200 | [diff] [blame] | 801 | RTC_DLOG(LS_VERBOSE) << " -- onStreamReadable"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 802 | events_to_signal |= SE_READ; |
| 803 | } |
| 804 | } |
| 805 | } |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 806 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 807 | if ((events & SE_CLOSE)) { |
Tomas Gunnarsson | d48ff45 | 2020-10-02 18:28:46 +0200 | [diff] [blame] | 808 | RTC_DLOG(LS_VERBOSE) << "OpenSSLStreamAdapter::OnEvent(SE_CLOSE, " << err |
| 809 | << ")"; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 810 | Cleanup(0); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 811 | events_to_signal |= SE_CLOSE; |
| 812 | // SE_CLOSE is the only event that uses the final parameter to OnEvent(). |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 813 | RTC_DCHECK(signal_error == 0); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 814 | signal_error = err; |
| 815 | } |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 816 | |
| 817 | if (events_to_signal) { |
Niels Möller | 0131a4d | 2021-04-16 09:16:21 +0200 | [diff] [blame] | 818 | // Note that the adapter presents itself as the origin of the stream events, |
| 819 | // since users of the adapter may not recognize the adapted object. |
| 820 | SignalEvent(this, events_to_signal, signal_error); |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 821 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 822 | } |
| 823 | |
Tommi | 0448298 | 2020-10-05 12:43:53 +0000 | [diff] [blame] | 824 | void OpenSSLStreamAdapter::PostEvent(int events, int err) { |
Danil Chapovalov | 5286dcf | 2022-07-18 17:04:56 +0200 | [diff] [blame] | 825 | owner_->PostTask(SafeTask(task_safety_.flag(), [this, events, err]() { |
| 826 | SignalEvent(this, events, err); |
| 827 | })); |
Tommi | 0448298 | 2020-10-05 12:43:53 +0000 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | void OpenSSLStreamAdapter::SetTimeout(int delay_ms) { |
| 831 | // We need to accept 0 delay here as well as >0 delay, because |
| 832 | // DTLSv1_get_timeout seems to frequently return 0 ms. |
| 833 | RTC_DCHECK_GE(delay_ms, 0); |
| 834 | RTC_DCHECK(!timeout_task_.Running()); |
| 835 | |
| 836 | timeout_task_ = webrtc::RepeatingTaskHandle::DelayedStart( |
| 837 | owner_, webrtc::TimeDelta::Millis(delay_ms), |
| 838 | [flag = task_safety_.flag(), this]() { |
| 839 | if (flag->alive()) { |
| 840 | RTC_DLOG(LS_INFO) << "DTLS timeout expired"; |
| 841 | timeout_task_.Stop(); |
Jonas Oreland | 808f494 | 2021-06-16 13:42:20 +0200 | [diff] [blame] | 842 | int res = DTLSv1_handle_timeout(ssl_); |
| 843 | if (res > 0) { |
| 844 | RTC_LOG(LS_INFO) << "DTLS retransmission"; |
| 845 | } else if (res < 0) { |
| 846 | RTC_LOG(LS_INFO) << "DTLSv1_handle_timeout() return -1"; |
Philipp Hancke | 9c83d9d | 2022-04-28 19:01:28 +0200 | [diff] [blame] | 847 | Error("DTLSv1_handle_timeout", res, -1, true); |
| 848 | return webrtc::TimeDelta::PlusInfinity(); |
Jonas Oreland | 808f494 | 2021-06-16 13:42:20 +0200 | [diff] [blame] | 849 | } |
Tommi | 0448298 | 2020-10-05 12:43:53 +0000 | [diff] [blame] | 850 | ContinueSSL(); |
| 851 | } else { |
Artem Titov | d325196 | 2021-11-15 16:57:07 +0100 | [diff] [blame] | 852 | RTC_DCHECK_NOTREACHED(); |
Tommi | 0448298 | 2020-10-05 12:43:53 +0000 | [diff] [blame] | 853 | } |
| 854 | // This callback will never run again (stopped above). |
| 855 | return webrtc::TimeDelta::PlusInfinity(); |
| 856 | }); |
| 857 | } |
| 858 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 859 | int OpenSSLStreamAdapter::BeginSSL() { |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 860 | RTC_DCHECK(state_ == SSL_CONNECTING); |
Taylor Brandstetter | c8762a8 | 2016-08-11 12:01:49 -0700 | [diff] [blame] | 861 | // The underlying stream has opened. |
Tomas Gunnarsson | d48ff45 | 2020-10-02 18:28:46 +0200 | [diff] [blame] | 862 | RTC_DLOG(LS_INFO) << "BeginSSL with peer."; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 863 | |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 864 | BIO* bio = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 865 | |
Taylor Brandstetter | c8762a8 | 2016-08-11 12:01:49 -0700 | [diff] [blame] | 866 | // First set up the context. |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 867 | RTC_DCHECK(ssl_ctx_ == nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 868 | ssl_ctx_ = SetupSSLContext(); |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 869 | if (!ssl_ctx_) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 870 | return -1; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 871 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 872 | |
Niels Möller | 0131a4d | 2021-04-16 09:16:21 +0200 | [diff] [blame] | 873 | bio = BIO_new_stream(stream_.get()); |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 874 | if (!bio) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 875 | return -1; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 876 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 877 | |
| 878 | ssl_ = SSL_new(ssl_ctx_); |
| 879 | if (!ssl_) { |
| 880 | BIO_free(bio); |
| 881 | return -1; |
| 882 | } |
| 883 | |
| 884 | SSL_set_app_data(ssl_, this); |
| 885 | |
| 886 | SSL_set_bio(ssl_, bio, bio); // the SSL object owns the bio now. |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 887 | if (ssl_mode_ == SSL_MODE_DTLS) { |
Taylor Brandstetter | 4f0dfbd | 2016-06-15 17:15:23 -0700 | [diff] [blame] | 888 | #ifdef OPENSSL_IS_BORINGSSL |
skvlad | d030912 | 2017-02-02 17:18:37 -0800 | [diff] [blame] | 889 | DTLSv1_set_initial_timeout_duration(ssl_, dtls_handshake_timeout_ms_); |
Taylor Brandstetter | 4f0dfbd | 2016-06-15 17:15:23 -0700 | [diff] [blame] | 890 | #else |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 891 | // Enable read-ahead for DTLS so whole packets are read from internal BIO |
| 892 | // before parsing. This is done internally by BoringSSL for DTLS. |
| 893 | SSL_set_read_ahead(ssl_, 1); |
philipel | 49c0869 | 2016-05-24 01:49:43 -0700 | [diff] [blame] | 894 | #endif |
Taylor Brandstetter | 4f0dfbd | 2016-06-15 17:15:23 -0700 | [diff] [blame] | 895 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 896 | |
| 897 | SSL_set_mode(ssl_, SSL_MODE_ENABLE_PARTIAL_WRITE | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 898 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 899 | |
| 900 | // Do the connect |
| 901 | return ContinueSSL(); |
| 902 | } |
| 903 | |
| 904 | int OpenSSLStreamAdapter::ContinueSSL() { |
Tomas Gunnarsson | d48ff45 | 2020-10-02 18:28:46 +0200 | [diff] [blame] | 905 | RTC_DLOG(LS_VERBOSE) << "ContinueSSL"; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 906 | RTC_DCHECK(state_ == SSL_CONNECTING); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 907 | |
| 908 | // Clear the DTLS timer |
Tommi | 0448298 | 2020-10-05 12:43:53 +0000 | [diff] [blame] | 909 | timeout_task_.Stop(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 910 | |
Benjamin Wright | f54e30b | 2019-02-26 17:33:50 -0800 | [diff] [blame] | 911 | const int code = (role_ == SSL_CLIENT) ? SSL_connect(ssl_) : SSL_accept(ssl_); |
| 912 | const int ssl_error = SSL_get_error(ssl_, code); |
| 913 | |
| 914 | switch (ssl_error) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 915 | case SSL_ERROR_NONE: |
Tomas Gunnarsson | d48ff45 | 2020-10-02 18:28:46 +0200 | [diff] [blame] | 916 | RTC_DLOG(LS_VERBOSE) << " -- success"; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 917 | // By this point, OpenSSL should have given us a certificate, or errored |
| 918 | // out if one was missing. |
Benjamin Wright | b19b497 | 2018-10-25 10:46:49 -0700 | [diff] [blame] | 919 | RTC_DCHECK(peer_cert_chain_ || !GetClientAuthEnabled()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 920 | |
| 921 | state_ = SSL_CONNECTED; |
Benjamin Wright | 5d35554 | 2018-10-26 17:57:00 -0700 | [diff] [blame] | 922 | if (!WaitingToVerifyPeerCertificate()) { |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 923 | // We have everything we need to start the connection, so signal |
| 924 | // SE_OPEN. If we need a client certificate fingerprint and don't have |
| 925 | // it yet, we'll instead signal SE_OPEN in SetPeerCertificateDigest. |
| 926 | // |
Taylor Brandstetter | fe69a74 | 2016-09-30 17:34:32 -0700 | [diff] [blame] | 927 | // TODO(deadbeef): Post this event asynchronously to unwind the stack. |
| 928 | // The caller of ContinueSSL may be the same object listening for these |
| 929 | // events and may not be prepared for reentrancy. |
| 930 | // PostEvent(SE_OPEN | SE_READ | SE_WRITE, 0); |
Niels Möller | 0131a4d | 2021-04-16 09:16:21 +0200 | [diff] [blame] | 931 | SignalEvent(this, SE_OPEN | SE_READ | SE_WRITE, 0); |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 932 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 933 | break; |
| 934 | |
| 935 | case SSL_ERROR_WANT_READ: { |
Tomas Gunnarsson | d48ff45 | 2020-10-02 18:28:46 +0200 | [diff] [blame] | 936 | RTC_DLOG(LS_VERBOSE) << " -- error want read"; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 937 | struct timeval timeout; |
| 938 | if (DTLSv1_get_timeout(ssl_, &timeout)) { |
| 939 | int delay = timeout.tv_sec * 1000 + timeout.tv_usec / 1000; |
Tommi | 0448298 | 2020-10-05 12:43:53 +0000 | [diff] [blame] | 940 | SetTimeout(delay); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 941 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 942 | } break; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 943 | |
| 944 | case SSL_ERROR_WANT_WRITE: |
Tomas Gunnarsson | d48ff45 | 2020-10-02 18:28:46 +0200 | [diff] [blame] | 945 | RTC_DLOG(LS_VERBOSE) << " -- error want write"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 946 | break; |
| 947 | |
| 948 | case SSL_ERROR_ZERO_RETURN: |
| 949 | default: |
zhihuang | d82eee0 | 2016-08-26 11:25:05 -0700 | [diff] [blame] | 950 | SSLHandshakeError ssl_handshake_err = SSLHandshakeError::UNKNOWN; |
| 951 | int err_code = ERR_peek_last_error(); |
| 952 | if (err_code != 0 && ERR_GET_REASON(err_code) == SSL_R_NO_SHARED_CIPHER) { |
| 953 | ssl_handshake_err = SSLHandshakeError::INCOMPATIBLE_CIPHERSUITE; |
| 954 | } |
Tomas Gunnarsson | d48ff45 | 2020-10-02 18:28:46 +0200 | [diff] [blame] | 955 | RTC_DLOG(LS_VERBOSE) << " -- error " << code << ", " << err_code << ", " |
| 956 | << ERR_GET_REASON(err_code); |
Sam Zackrisson | b298f74 | 2020-10-06 11:40:30 +0000 | [diff] [blame] | 957 | SignalSSLHandshakeError(ssl_handshake_err); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 958 | return (ssl_error != 0) ? ssl_error : -1; |
| 959 | } |
| 960 | |
| 961 | return 0; |
| 962 | } |
| 963 | |
Ali Tofigh | 2ab914c | 2022-04-13 12:55:15 +0200 | [diff] [blame] | 964 | void OpenSSLStreamAdapter::Error(absl::string_view context, |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 965 | int err, |
| 966 | uint8_t alert, |
| 967 | bool signal) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 968 | RTC_LOG(LS_WARNING) << "OpenSSLStreamAdapter::Error(" << context << ", " |
| 969 | << err << ", " << static_cast<int>(alert) << ")"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 970 | state_ = SSL_ERROR; |
| 971 | ssl_error_code_ = err; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 972 | Cleanup(alert); |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 973 | if (signal) { |
Niels Möller | 0131a4d | 2021-04-16 09:16:21 +0200 | [diff] [blame] | 974 | SignalEvent(this, SE_CLOSE, err); |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 975 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 976 | } |
| 977 | |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 978 | void OpenSSLStreamAdapter::Cleanup(uint8_t alert) { |
Tomas Gunnarsson | d48ff45 | 2020-10-02 18:28:46 +0200 | [diff] [blame] | 979 | RTC_DLOG(LS_INFO) << "Cleanup"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 980 | |
| 981 | if (state_ != SSL_ERROR) { |
| 982 | state_ = SSL_CLOSED; |
| 983 | ssl_error_code_ = 0; |
| 984 | } |
| 985 | |
| 986 | if (ssl_) { |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 987 | int ret; |
| 988 | // SSL_send_fatal_alert is only available in BoringSSL. |
| 989 | #ifdef OPENSSL_IS_BORINGSSL |
| 990 | if (alert) { |
| 991 | ret = SSL_send_fatal_alert(ssl_, alert); |
| 992 | if (ret < 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 993 | RTC_LOG(LS_WARNING) << "SSL_send_fatal_alert failed, error = " |
| 994 | << SSL_get_error(ssl_, ret); |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 995 | } |
| 996 | } else { |
| 997 | #endif |
| 998 | ret = SSL_shutdown(ssl_); |
| 999 | if (ret < 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1000 | RTC_LOG(LS_WARNING) |
| 1001 | << "SSL_shutdown failed, error = " << SSL_get_error(ssl_, ret); |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 1002 | } |
| 1003 | #ifdef OPENSSL_IS_BORINGSSL |
jiayl@webrtc.org | f1d751c | 2014-09-25 16:38:46 +0000 | [diff] [blame] | 1004 | } |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 1005 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1006 | SSL_free(ssl_); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 1007 | ssl_ = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1008 | } |
| 1009 | if (ssl_ctx_) { |
| 1010 | SSL_CTX_free(ssl_ctx_); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 1011 | ssl_ctx_ = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1012 | } |
| 1013 | identity_.reset(); |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 1014 | peer_cert_chain_.reset(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1015 | |
| 1016 | // Clear the DTLS timer |
Tommi | 0448298 | 2020-10-05 12:43:53 +0000 | [diff] [blame] | 1017 | timeout_task_.Stop(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | SSL_CTX* OpenSSLStreamAdapter::SetupSSLContext() { |
Taylor Brandstetter | 165c618 | 2020-12-10 16:23:03 -0800 | [diff] [blame] | 1021 | #ifdef OPENSSL_IS_BORINGSSL |
| 1022 | // If X509 objects aren't used, we can use these methods to avoid |
| 1023 | // linking the sizable crypto/x509 code, using CRYPTO_BUFFER instead. |
| 1024 | SSL_CTX* ctx = |
| 1025 | SSL_CTX_new(ssl_mode_ == SSL_MODE_DTLS ? DTLS_with_buffers_method() |
| 1026 | : TLS_with_buffers_method()); |
| 1027 | #else |
David Benjamin | 170a4b3 | 2019-01-30 09:46:16 -0600 | [diff] [blame] | 1028 | SSL_CTX* ctx = |
| 1029 | SSL_CTX_new(ssl_mode_ == SSL_MODE_DTLS ? DTLS_method() : TLS_method()); |
Taylor Brandstetter | 165c618 | 2020-12-10 16:23:03 -0800 | [diff] [blame] | 1030 | #endif |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 1031 | if (ctx == nullptr) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 1032 | return nullptr; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 1033 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1034 | |
Harald Alvestrand | 1379913 | 2020-03-09 19:39:36 +0100 | [diff] [blame] | 1035 | if (support_legacy_tls_protocols_flag_) { |
| 1036 | // TODO(https://bugs.webrtc.org/10261): Completely remove this branch in |
| 1037 | // M84. |
| 1038 | SSL_CTX_set_min_proto_version( |
| 1039 | ctx, ssl_mode_ == SSL_MODE_DTLS ? DTLS1_VERSION : TLS1_VERSION); |
| 1040 | switch (ssl_max_version_) { |
| 1041 | case SSL_PROTOCOL_TLS_10: |
| 1042 | SSL_CTX_set_max_proto_version( |
| 1043 | ctx, ssl_mode_ == SSL_MODE_DTLS ? DTLS1_VERSION : TLS1_VERSION); |
| 1044 | break; |
| 1045 | case SSL_PROTOCOL_TLS_11: |
| 1046 | SSL_CTX_set_max_proto_version( |
| 1047 | ctx, ssl_mode_ == SSL_MODE_DTLS ? DTLS1_VERSION : TLS1_1_VERSION); |
| 1048 | break; |
| 1049 | case SSL_PROTOCOL_TLS_12: |
| 1050 | default: |
| 1051 | SSL_CTX_set_max_proto_version( |
| 1052 | ctx, ssl_mode_ == SSL_MODE_DTLS ? DTLS1_2_VERSION : TLS1_2_VERSION); |
| 1053 | break; |
| 1054 | } |
| 1055 | } else { |
| 1056 | // TODO(https://bugs.webrtc.org/10261): Make this the default in M84. |
| 1057 | SSL_CTX_set_min_proto_version( |
| 1058 | ctx, ssl_mode_ == SSL_MODE_DTLS ? DTLS1_2_VERSION : TLS1_2_VERSION); |
| 1059 | SSL_CTX_set_max_proto_version( |
| 1060 | ctx, ssl_mode_ == SSL_MODE_DTLS ? DTLS1_2_VERSION : TLS1_2_VERSION); |
Joachim Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 1061 | } |
Harald Alvestrand | 1379913 | 2020-03-09 19:39:36 +0100 | [diff] [blame] | 1062 | |
David Benjamin | 170a4b3 | 2019-01-30 09:46:16 -0600 | [diff] [blame] | 1063 | #ifdef OPENSSL_IS_BORINGSSL |
| 1064 | // SSL_CTX_set_current_time_cb is only supported in BoringSSL. |
deadbeef | 6cf94a0 | 2016-11-28 17:38:34 -0800 | [diff] [blame] | 1065 | if (g_use_time_callback_for_testing) { |
| 1066 | SSL_CTX_set_current_time_cb(ctx, &TimeCallbackForTesting); |
| 1067 | } |
Taylor Brandstetter | 165c618 | 2020-12-10 16:23:03 -0800 | [diff] [blame] | 1068 | SSL_CTX_set0_buffer_pool(ctx, openssl::GetBufferPool()); |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 1069 | #endif |
Joachim Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 1070 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1071 | if (identity_ && !identity_->ConfigureIdentity(ctx)) { |
| 1072 | SSL_CTX_free(ctx); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 1073 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
tfarina | a41ab93 | 2015-10-30 16:08:48 -0700 | [diff] [blame] | 1076 | #if !defined(NDEBUG) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1077 | SSL_CTX_set_info_callback(ctx, OpenSSLAdapter::SSLInfoCallback); |
| 1078 | #endif |
| 1079 | |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 1080 | int mode = SSL_VERIFY_PEER; |
Benjamin Wright | b19b497 | 2018-10-25 10:46:49 -0700 | [diff] [blame] | 1081 | if (GetClientAuthEnabled()) { |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 1082 | // Require a certificate from the client. |
| 1083 | // Note: Normally this is always true in production, but it may be disabled |
| 1084 | // for testing purposes (e.g. SSLAdapter unit tests). |
| 1085 | mode |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 1086 | } |
| 1087 | |
David Benjamin | dc24656 | 2017-09-29 12:14:08 -0400 | [diff] [blame] | 1088 | // Configure a custom certificate verification callback to check the peer |
Taylor Brandstetter | 165c618 | 2020-12-10 16:23:03 -0800 | [diff] [blame] | 1089 | // certificate digest. |
| 1090 | #ifdef OPENSSL_IS_BORINGSSL |
| 1091 | // Use CRYPTO_BUFFER version of the callback if building with BoringSSL. |
| 1092 | SSL_CTX_set_custom_verify(ctx, mode, SSLVerifyCallback); |
| 1093 | #else |
| 1094 | // Note the second argument to SSL_CTX_set_verify is to override individual |
| 1095 | // errors in the default verification logic, which is not what we want here. |
David Benjamin | dc24656 | 2017-09-29 12:14:08 -0400 | [diff] [blame] | 1096 | SSL_CTX_set_verify(ctx, mode, nullptr); |
| 1097 | SSL_CTX_set_cert_verify_callback(ctx, SSLVerifyCallback, nullptr); |
Taylor Brandstetter | 165c618 | 2020-12-10 16:23:03 -0800 | [diff] [blame] | 1098 | #endif |
David Benjamin | dc24656 | 2017-09-29 12:14:08 -0400 | [diff] [blame] | 1099 | |
Joachim Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 1100 | // Select list of available ciphers. Note that !SHA256 and !SHA384 only |
| 1101 | // remove HMAC-SHA256 and HMAC-SHA384 cipher suites, not GCM cipher suites |
| 1102 | // with SHA256 or SHA384 as the handshake hash. |
David Benjamin | 7a46cc5 | 2021-08-17 16:56:20 -0400 | [diff] [blame] | 1103 | // This matches the list of SSLClientSocketImpl in Chromium. |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 1104 | SSL_CTX_set_cipher_list( |
David Benjamin | 7a46cc5 | 2021-08-17 16:56:20 -0400 | [diff] [blame] | 1105 | ctx, |
| 1106 | "DEFAULT:!NULL:!aNULL:!SHA256:!SHA384:!aECDH:!AESGCM+AES256:!aPSK:!3DES"); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1107 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1108 | if (!srtp_ciphers_.empty()) { |
| 1109 | if (SSL_CTX_set_tlsext_use_srtp(ctx, srtp_ciphers_.c_str())) { |
| 1110 | SSL_CTX_free(ctx); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 1111 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1112 | } |
| 1113 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1114 | |
| 1115 | return ctx; |
| 1116 | } |
| 1117 | |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 1118 | bool OpenSSLStreamAdapter::VerifyPeerCertificate() { |
Benjamin Wright | 5d35554 | 2018-10-26 17:57:00 -0700 | [diff] [blame] | 1119 | if (!HasPeerCertificateDigest() || !peer_cert_chain_ || |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 1120 | !peer_cert_chain_->GetSize()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1121 | RTC_LOG(LS_WARNING) << "Missing digest or peer certificate."; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 1122 | return false; |
| 1123 | } |
deadbeef | 81f6f4f | 2016-09-19 17:20:52 -0700 | [diff] [blame] | 1124 | |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 1125 | unsigned char digest[EVP_MAX_MD_SIZE]; |
| 1126 | size_t digest_length; |
Taylor Brandstetter | 165c618 | 2020-12-10 16:23:03 -0800 | [diff] [blame] | 1127 | if (!peer_cert_chain_->Get(0).ComputeDigest( |
| 1128 | peer_certificate_digest_algorithm_, digest, sizeof(digest), |
| 1129 | &digest_length)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1130 | RTC_LOG(LS_WARNING) << "Failed to compute peer cert digest."; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 1131 | return false; |
| 1132 | } |
| 1133 | |
| 1134 | Buffer computed_digest(digest, digest_length); |
| 1135 | if (computed_digest != peer_certificate_digest_value_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1136 | RTC_LOG(LS_WARNING) |
Philipp Hancke | 117e692 | 2022-06-17 14:14:55 +0200 | [diff] [blame] | 1137 | << "Rejected peer certificate due to mismatched digest using " |
| 1138 | << peer_certificate_digest_algorithm_ << ". Expected " |
| 1139 | << rtc::hex_encode_with_delimiter(peer_certificate_digest_value_, ':') |
| 1140 | << " got " << rtc::hex_encode_with_delimiter(computed_digest, ':'); |
jbauch | f8f457b | 2017-03-09 16:24:57 -0800 | [diff] [blame] | 1141 | return false; |
deadbeef | 81f6f4f | 2016-09-19 17:20:52 -0700 | [diff] [blame] | 1142 | } |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 1143 | // Ignore any verification error if the digest matches, since there is no |
| 1144 | // value in checking the validity of a self-signed cert issued by untrusted |
| 1145 | // sources. |
Tomas Gunnarsson | d48ff45 | 2020-10-02 18:28:46 +0200 | [diff] [blame] | 1146 | RTC_DLOG(LS_INFO) << "Accepted peer certificate."; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 1147 | peer_certificate_verified_ = true; |
| 1148 | return true; |
| 1149 | } |
| 1150 | |
Jian Cui | 0a8798b | 2017-11-16 16:58:02 -0800 | [diff] [blame] | 1151 | std::unique_ptr<SSLCertChain> OpenSSLStreamAdapter::GetPeerSSLCertChain() |
| 1152 | const { |
Steve Anton | f25303e | 2018-10-16 15:23:31 -0700 | [diff] [blame] | 1153 | return peer_cert_chain_ ? peer_cert_chain_->Clone() : nullptr; |
Jian Cui | 0a8798b | 2017-11-16 16:58:02 -0800 | [diff] [blame] | 1154 | } |
| 1155 | |
Taylor Brandstetter | 165c618 | 2020-12-10 16:23:03 -0800 | [diff] [blame] | 1156 | #ifdef OPENSSL_IS_BORINGSSL |
| 1157 | enum ssl_verify_result_t OpenSSLStreamAdapter::SSLVerifyCallback( |
| 1158 | SSL* ssl, |
| 1159 | uint8_t* out_alert) { |
| 1160 | // Get our OpenSSLStreamAdapter from the context. |
| 1161 | OpenSSLStreamAdapter* stream = |
| 1162 | reinterpret_cast<OpenSSLStreamAdapter*>(SSL_get_app_data(ssl)); |
| 1163 | const STACK_OF(CRYPTO_BUFFER)* chain = SSL_get0_peer_certificates(ssl); |
| 1164 | // Creates certificate chain. |
| 1165 | std::vector<std::unique_ptr<SSLCertificate>> cert_chain; |
| 1166 | for (CRYPTO_BUFFER* cert : chain) { |
| 1167 | cert_chain.emplace_back(new BoringSSLCertificate(bssl::UpRef(cert))); |
| 1168 | } |
| 1169 | stream->peer_cert_chain_.reset(new SSLCertChain(std::move(cert_chain))); |
| 1170 | |
| 1171 | // If the peer certificate digest isn't known yet, we'll wait to verify |
| 1172 | // until it's known, and for now just return a success status. |
| 1173 | if (stream->peer_certificate_digest_algorithm_.empty()) { |
| 1174 | RTC_LOG(LS_INFO) << "Waiting to verify certificate until digest is known."; |
| 1175 | // TODO(deadbeef): Use ssl_verify_retry? |
| 1176 | return ssl_verify_ok; |
| 1177 | } |
| 1178 | |
| 1179 | if (!stream->VerifyPeerCertificate()) { |
| 1180 | return ssl_verify_invalid; |
| 1181 | } |
| 1182 | |
| 1183 | return ssl_verify_ok; |
| 1184 | } |
| 1185 | #else // OPENSSL_IS_BORINGSSL |
David Benjamin | dc24656 | 2017-09-29 12:14:08 -0400 | [diff] [blame] | 1186 | int OpenSSLStreamAdapter::SSLVerifyCallback(X509_STORE_CTX* store, void* arg) { |
| 1187 | // Get our SSL structure and OpenSSLStreamAdapter from the store. |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 1188 | SSL* ssl = reinterpret_cast<SSL*>( |
| 1189 | X509_STORE_CTX_get_ex_data(store, SSL_get_ex_data_X509_STORE_CTX_idx())); |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 1190 | OpenSSLStreamAdapter* stream = |
| 1191 | reinterpret_cast<OpenSSLStreamAdapter*>(SSL_get_app_data(ssl)); |
henrike@webrtc.org | 4e5f65a | 2014-06-05 20:40:11 +0000 | [diff] [blame] | 1192 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1193 | // Record the peer's certificate. |
Jiawei Ou | 9d4e840 | 2018-05-23 15:44:20 -0700 | [diff] [blame] | 1194 | X509* cert = X509_STORE_CTX_get0_cert(store); |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 1195 | stream->peer_cert_chain_.reset( |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 1196 | new SSLCertChain(std::make_unique<OpenSSLCertificate>(cert))); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1197 | |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 1198 | // If the peer certificate digest isn't known yet, we'll wait to verify |
| 1199 | // until it's known, and for now just return a success status. |
| 1200 | if (stream->peer_certificate_digest_algorithm_.empty()) { |
Tomas Gunnarsson | d48ff45 | 2020-10-02 18:28:46 +0200 | [diff] [blame] | 1201 | RTC_DLOG(LS_INFO) << "Waiting to verify certificate until digest is known."; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 1202 | return 1; |
| 1203 | } |
| 1204 | |
David Benjamin | dc24656 | 2017-09-29 12:14:08 -0400 | [diff] [blame] | 1205 | if (!stream->VerifyPeerCertificate()) { |
| 1206 | X509_STORE_CTX_set_error(store, X509_V_ERR_CERT_REJECTED); |
| 1207 | return 0; |
| 1208 | } |
| 1209 | |
| 1210 | return 1; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1211 | } |
Taylor Brandstetter | 165c618 | 2020-12-10 16:23:03 -0800 | [diff] [blame] | 1212 | #endif // !OPENSSL_IS_BORINGSSL |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1213 | |
Taylor Brandstetter | 4f0dfbd | 2016-06-15 17:15:23 -0700 | [diff] [blame] | 1214 | bool OpenSSLStreamAdapter::IsBoringSsl() { |
| 1215 | #ifdef OPENSSL_IS_BORINGSSL |
| 1216 | return true; |
| 1217 | #else |
| 1218 | return false; |
| 1219 | #endif |
| 1220 | } |
| 1221 | |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1222 | #define CDEF(X) \ |
| 1223 | { static_cast<uint16_t>(TLS1_CK_##X & 0xffff), "TLS_" #X } |
| 1224 | |
| 1225 | struct cipher_list { |
| 1226 | uint16_t cipher; |
| 1227 | const char* cipher_str; |
| 1228 | }; |
| 1229 | |
| 1230 | // TODO(torbjorng): Perhaps add more cipher suites to these lists. |
| 1231 | static const cipher_list OK_RSA_ciphers[] = { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1232 | CDEF(ECDHE_RSA_WITH_AES_128_CBC_SHA), |
| 1233 | CDEF(ECDHE_RSA_WITH_AES_256_CBC_SHA), |
| 1234 | CDEF(ECDHE_RSA_WITH_AES_128_GCM_SHA256), |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1235 | #ifdef TLS1_CK_ECDHE_RSA_WITH_AES_256_GCM_SHA256 |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1236 | CDEF(ECDHE_RSA_WITH_AES_256_GCM_SHA256), |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1237 | #endif |
torbjorng | aad6780 | 2016-04-07 08:55:28 -0700 | [diff] [blame] | 1238 | #ifdef TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1239 | CDEF(ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256), |
torbjorng | aad6780 | 2016-04-07 08:55:28 -0700 | [diff] [blame] | 1240 | #endif |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1241 | }; |
| 1242 | |
| 1243 | static const cipher_list OK_ECDSA_ciphers[] = { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1244 | CDEF(ECDHE_ECDSA_WITH_AES_128_CBC_SHA), |
| 1245 | CDEF(ECDHE_ECDSA_WITH_AES_256_CBC_SHA), |
| 1246 | CDEF(ECDHE_ECDSA_WITH_AES_128_GCM_SHA256), |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1247 | #ifdef TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA256 |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1248 | CDEF(ECDHE_ECDSA_WITH_AES_256_GCM_SHA256), |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1249 | #endif |
torbjorng | aad6780 | 2016-04-07 08:55:28 -0700 | [diff] [blame] | 1250 | #ifdef TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1251 | CDEF(ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256), |
torbjorng | aad6780 | 2016-04-07 08:55:28 -0700 | [diff] [blame] | 1252 | #endif |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1253 | }; |
| 1254 | #undef CDEF |
| 1255 | |
| 1256 | bool OpenSSLStreamAdapter::IsAcceptableCipher(int cipher, KeyType key_type) { |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 1257 | if (key_type == KT_RSA) { |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1258 | for (const cipher_list& c : OK_RSA_ciphers) { |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 1259 | if (cipher == c.cipher) { |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1260 | return true; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 1261 | } |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 1262 | } |
Joachim Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 1263 | } |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1264 | |
| 1265 | if (key_type == KT_ECDSA) { |
| 1266 | for (const cipher_list& c : OK_ECDSA_ciphers) { |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 1267 | if (cipher == c.cipher) { |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1268 | return true; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 1269 | } |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1270 | } |
| 1271 | } |
| 1272 | |
| 1273 | return false; |
| 1274 | } |
| 1275 | |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 1276 | bool OpenSSLStreamAdapter::IsAcceptableCipher(absl::string_view cipher, |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1277 | KeyType key_type) { |
| 1278 | if (key_type == KT_RSA) { |
| 1279 | for (const cipher_list& c : OK_RSA_ciphers) { |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 1280 | if (cipher == c.cipher_str) { |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1281 | return true; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 1282 | } |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1283 | } |
| 1284 | } |
| 1285 | |
| 1286 | if (key_type == KT_ECDSA) { |
| 1287 | for (const cipher_list& c : OK_ECDSA_ciphers) { |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 1288 | if (cipher == c.cipher_str) { |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1289 | return true; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 1290 | } |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1291 | } |
| 1292 | } |
| 1293 | |
| 1294 | return false; |
pthatcher@webrtc.org | 3ee4fe5 | 2015-02-11 22:34:36 +0000 | [diff] [blame] | 1295 | } |
| 1296 | |
Benjamin Wright | b19b497 | 2018-10-25 10:46:49 -0700 | [diff] [blame] | 1297 | void OpenSSLStreamAdapter::EnableTimeCallbackForTesting() { |
Joachim Reiersen | 637bed5 | 2019-04-25 10:41:35 -0700 | [diff] [blame] | 1298 | #ifdef OPENSSL_IS_BORINGSSL |
deadbeef | 6cf94a0 | 2016-11-28 17:38:34 -0800 | [diff] [blame] | 1299 | g_use_time_callback_for_testing = true; |
Joachim Reiersen | 637bed5 | 2019-04-25 10:41:35 -0700 | [diff] [blame] | 1300 | #endif |
deadbeef | 6cf94a0 | 2016-11-28 17:38:34 -0800 | [diff] [blame] | 1301 | } |
| 1302 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1303 | } // namespace rtc |