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 |
Artem Titov | c374d11 | 2022-06-16 21:27:45 +0200 | [diff] [blame] | 42 | #include "api/task_queue/to_queued_task.h" |
Taylor Brandstetter | 165c618 | 2020-12-10 16:23:03 -0800 | [diff] [blame] | 43 | #include "rtc_base/openssl_utility.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 44 | #include "rtc_base/ssl_certificate.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 45 | #include "rtc_base/stream.h" |
Philipp Hancke | 117e692 | 2022-06-17 14:14:55 +0200 | [diff] [blame^] | 46 | #include "rtc_base/string_encode.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 47 | #include "rtc_base/thread.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 48 | #include "rtc_base/time_utils.h" |
Harald Alvestrand | 1379913 | 2020-03-09 19:39:36 +0100 | [diff] [blame] | 49 | #include "system_wrappers/include/field_trial.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 50 | |
Jiawei Ou | a9c94d5 | 2018-01-30 23:05:07 -0800 | [diff] [blame] | 51 | #if (OPENSSL_VERSION_NUMBER < 0x10100000L) |
| 52 | #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] | 53 | #endif |
| 54 | |
Benjamin Wright | 8e98c60 | 2019-02-28 17:25:01 -0800 | [diff] [blame] | 55 | // Defines for the TLS Cipher Suite Map. |
| 56 | #define DEFINE_CIPHER_ENTRY_SSL3(name) \ |
| 57 | { SSL3_CK_##name, "TLS_" #name } |
| 58 | #define DEFINE_CIPHER_ENTRY_TLS1(name) \ |
| 59 | { TLS1_CK_##name, "TLS_" #name } |
| 60 | |
| 61 | namespace rtc { |
| 62 | namespace { |
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 |
| 393 | for (const SslCipherMapEntry* entry = kSslCipherMap; entry->rfc_name; |
| 394 | ++entry) { |
| 395 | if (cipher_suite == static_cast<int>(entry->openssl_id)) { |
| 396 | return entry->rfc_name; |
| 397 | } |
| 398 | } |
| 399 | return std::string(); |
| 400 | #endif |
Guo-wei Shieh | 456696a | 2015-09-30 21:48:54 -0700 | [diff] [blame] | 401 | } |
pthatcher@webrtc.org | 3ee4fe5 | 2015-02-11 22:34:36 +0000 | [diff] [blame] | 402 | |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 403 | bool OpenSSLStreamAdapter::GetSslCipherSuite(int* cipher_suite) { |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 404 | if (state_ != SSL_CONNECTED) { |
pthatcher@webrtc.org | 3ee4fe5 | 2015-02-11 22:34:36 +0000 | [diff] [blame] | 405 | return false; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 406 | } |
pthatcher@webrtc.org | 3ee4fe5 | 2015-02-11 22:34:36 +0000 | [diff] [blame] | 407 | |
| 408 | const SSL_CIPHER* current_cipher = SSL_get_current_cipher(ssl_); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 409 | if (current_cipher == nullptr) { |
pthatcher@webrtc.org | 3ee4fe5 | 2015-02-11 22:34:36 +0000 | [diff] [blame] | 410 | return false; |
| 411 | } |
| 412 | |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 413 | *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] | 414 | return true; |
| 415 | } |
| 416 | |
Harald Alvestrand | 5cb7807 | 2019-10-28 09:51:17 +0100 | [diff] [blame] | 417 | SSLProtocolVersion OpenSSLStreamAdapter::GetSslVersion() const { |
Benjamin Wright | 8e98c60 | 2019-02-28 17:25:01 -0800 | [diff] [blame] | 418 | if (state_ != SSL_CONNECTED) { |
Harald Alvestrand | 5cb7807 | 2019-10-28 09:51:17 +0100 | [diff] [blame] | 419 | return SSL_PROTOCOL_NOT_GIVEN; |
Benjamin Wright | 8e98c60 | 2019-02-28 17:25:01 -0800 | [diff] [blame] | 420 | } |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 421 | |
| 422 | int ssl_version = SSL_version(ssl_); |
| 423 | if (ssl_mode_ == SSL_MODE_DTLS) { |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 424 | if (ssl_version == DTLS1_VERSION) { |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 425 | return SSL_PROTOCOL_DTLS_10; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 426 | } else if (ssl_version == DTLS1_2_VERSION) { |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 427 | return SSL_PROTOCOL_DTLS_12; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 428 | } |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 429 | } else { |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 430 | if (ssl_version == TLS1_VERSION) { |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 431 | return SSL_PROTOCOL_TLS_10; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 432 | } else if (ssl_version == TLS1_1_VERSION) { |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 433 | return SSL_PROTOCOL_TLS_11; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 434 | } else if (ssl_version == TLS1_2_VERSION) { |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 435 | return SSL_PROTOCOL_TLS_12; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 436 | } |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 437 | } |
| 438 | |
Harald Alvestrand | 5cb7807 | 2019-10-28 09:51:17 +0100 | [diff] [blame] | 439 | return SSL_PROTOCOL_NOT_GIVEN; |
| 440 | } |
| 441 | |
| 442 | bool OpenSSLStreamAdapter::GetSslVersionBytes(int* version) const { |
| 443 | if (state_ != SSL_CONNECTED) { |
| 444 | return false; |
| 445 | } |
| 446 | *version = SSL_version(ssl_); |
| 447 | return true; |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 448 | } |
| 449 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 450 | // Key Extractor interface |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 451 | bool OpenSSLStreamAdapter::ExportKeyingMaterial(absl::string_view label, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 452 | const uint8_t* context, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 453 | size_t context_len, |
| 454 | bool use_context, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 455 | uint8_t* result, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 456 | size_t result_len) { |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 457 | if (SSL_export_keying_material(ssl_, result, result_len, label.data(), |
| 458 | label.length(), context, context_len, |
| 459 | use_context) != 1) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 460 | return false; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 461 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 462 | return true; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 465 | bool OpenSSLStreamAdapter::SetDtlsSrtpCryptoSuites( |
| 466 | const std::vector<int>& ciphers) { |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 467 | if (state_ != SSL_NONE) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 468 | return false; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 469 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 470 | |
Benjamin Wright | 8e98c60 | 2019-02-28 17:25:01 -0800 | [diff] [blame] | 471 | std::string internal_ciphers; |
| 472 | for (const int cipher : ciphers) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 473 | bool found = false; |
Benjamin Wright | 8e98c60 | 2019-02-28 17:25:01 -0800 | [diff] [blame] | 474 | for (const auto& entry : kSrtpCipherMap) { |
| 475 | if (cipher == entry.id) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 476 | found = true; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 477 | if (!internal_ciphers.empty()) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 478 | internal_ciphers += ":"; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 479 | } |
Benjamin Wright | 8e98c60 | 2019-02-28 17:25:01 -0800 | [diff] [blame] | 480 | internal_ciphers += entry.internal_name; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 481 | break; |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | if (!found) { |
Benjamin Wright | 8e98c60 | 2019-02-28 17:25:01 -0800 | [diff] [blame] | 486 | RTC_LOG(LS_ERROR) << "Could not find cipher: " << cipher; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 487 | return false; |
| 488 | } |
| 489 | } |
| 490 | |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 491 | if (internal_ciphers.empty()) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 492 | return false; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 493 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 494 | |
| 495 | srtp_ciphers_ = internal_ciphers; |
| 496 | return true; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 499 | bool OpenSSLStreamAdapter::GetDtlsSrtpCryptoSuite(int* crypto_suite) { |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 500 | RTC_DCHECK(state_ == SSL_CONNECTED); |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 501 | if (state_ != SSL_CONNECTED) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 502 | return false; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 503 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 504 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 505 | const SRTP_PROTECTION_PROFILE* srtp_profile = |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 506 | SSL_get_selected_srtp_profile(ssl_); |
| 507 | |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 508 | if (!srtp_profile) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 509 | return false; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 510 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 511 | |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 512 | *crypto_suite = srtp_profile->id; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 513 | RTC_DCHECK(!SrtpCryptoSuiteToName(*crypto_suite).empty()); |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 514 | return true; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 515 | } |
| 516 | |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 517 | bool OpenSSLStreamAdapter::IsTlsConnected() { |
| 518 | return state_ == SSL_CONNECTED; |
| 519 | } |
| 520 | |
Taylor Brandstetter | c8762a8 | 2016-08-11 12:01:49 -0700 | [diff] [blame] | 521 | int OpenSSLStreamAdapter::StartSSL() { |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 522 | // Don't allow StartSSL to be called twice. |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 523 | if (state_ != SSL_NONE) { |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 524 | return -1; |
| 525 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 526 | |
Niels Möller | 0131a4d | 2021-04-16 09:16:21 +0200 | [diff] [blame] | 527 | if (stream_->GetState() != SS_OPEN) { |
Taylor Brandstetter | c8762a8 | 2016-08-11 12:01:49 -0700 | [diff] [blame] | 528 | state_ = SSL_WAIT; |
| 529 | return 0; |
| 530 | } |
| 531 | |
| 532 | state_ = SSL_CONNECTING; |
| 533 | if (int err = BeginSSL()) { |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 534 | Error("BeginSSL", err, 0, false); |
Taylor Brandstetter | c8762a8 | 2016-08-11 12:01:49 -0700 | [diff] [blame] | 535 | return err; |
| 536 | } |
| 537 | |
| 538 | return 0; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | void OpenSSLStreamAdapter::SetMode(SSLMode mode) { |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 542 | RTC_DCHECK(state_ == SSL_NONE); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 543 | ssl_mode_ = mode; |
| 544 | } |
| 545 | |
Joachim Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 546 | void OpenSSLStreamAdapter::SetMaxProtocolVersion(SSLProtocolVersion version) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 547 | RTC_DCHECK(ssl_ctx_ == nullptr); |
Joachim Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 548 | ssl_max_version_ = version; |
| 549 | } |
| 550 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 551 | void OpenSSLStreamAdapter::SetInitialRetransmissionTimeout(int timeout_ms) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 552 | RTC_DCHECK(ssl_ctx_ == nullptr); |
skvlad | d030912 | 2017-02-02 17:18:37 -0800 | [diff] [blame] | 553 | dtls_handshake_timeout_ms_ = timeout_ms; |
| 554 | } |
| 555 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 556 | // |
| 557 | // StreamInterface Implementation |
| 558 | // |
| 559 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 560 | StreamResult OpenSSLStreamAdapter::Write(const void* data, |
| 561 | size_t data_len, |
| 562 | size_t* written, |
| 563 | int* error) { |
Tomas Gunnarsson | d48ff45 | 2020-10-02 18:28:46 +0200 | [diff] [blame] | 564 | RTC_DLOG(LS_VERBOSE) << "OpenSSLStreamAdapter::Write(" << data_len << ")"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 565 | |
| 566 | switch (state_) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 567 | case SSL_NONE: |
| 568 | // pass-through in clear text |
Niels Möller | 0131a4d | 2021-04-16 09:16:21 +0200 | [diff] [blame] | 569 | return stream_->Write(data, data_len, written, error); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 570 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 571 | case SSL_WAIT: |
| 572 | case SSL_CONNECTING: |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 573 | return SR_BLOCK; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 574 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 575 | case SSL_CONNECTED: |
Benjamin Wright | 5d35554 | 2018-10-26 17:57:00 -0700 | [diff] [blame] | 576 | if (WaitingToVerifyPeerCertificate()) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 577 | return SR_BLOCK; |
| 578 | } |
| 579 | break; |
| 580 | |
| 581 | case SSL_ERROR: |
| 582 | case SSL_CLOSED: |
| 583 | default: |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 584 | if (error) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 585 | *error = ssl_error_code_; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 586 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 587 | return SR_ERROR; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | // OpenSSL will return an error if we try to write zero bytes |
| 591 | if (data_len == 0) { |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 592 | if (written) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 593 | *written = 0; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 594 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 595 | return SR_SUCCESS; |
| 596 | } |
| 597 | |
| 598 | ssl_write_needs_read_ = false; |
| 599 | |
henrike@webrtc.org | d89b69a | 2014-11-06 17:23:09 +0000 | [diff] [blame] | 600 | int code = SSL_write(ssl_, data, checked_cast<int>(data_len)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 601 | int ssl_error = SSL_get_error(ssl_, code); |
| 602 | switch (ssl_error) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 603 | case SSL_ERROR_NONE: |
Tomas Gunnarsson | d48ff45 | 2020-10-02 18:28:46 +0200 | [diff] [blame] | 604 | RTC_DLOG(LS_VERBOSE) << " -- success"; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 605 | RTC_DCHECK_GT(code, 0); |
| 606 | RTC_DCHECK_LE(code, data_len); |
| 607 | if (written) |
| 608 | *written = code; |
| 609 | return SR_SUCCESS; |
| 610 | case SSL_ERROR_WANT_READ: |
Tomas Gunnarsson | d48ff45 | 2020-10-02 18:28:46 +0200 | [diff] [blame] | 611 | RTC_DLOG(LS_VERBOSE) << " -- error want read"; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 612 | ssl_write_needs_read_ = true; |
| 613 | return SR_BLOCK; |
| 614 | case SSL_ERROR_WANT_WRITE: |
Tomas Gunnarsson | d48ff45 | 2020-10-02 18:28:46 +0200 | [diff] [blame] | 615 | RTC_DLOG(LS_VERBOSE) << " -- error want write"; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 616 | return SR_BLOCK; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 617 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 618 | case SSL_ERROR_ZERO_RETURN: |
| 619 | default: |
| 620 | Error("SSL_write", (ssl_error ? ssl_error : -1), 0, false); |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 621 | if (error) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 622 | *error = ssl_error_code_; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 623 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 624 | return SR_ERROR; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 625 | } |
| 626 | // not reached |
| 627 | } |
| 628 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 629 | StreamResult OpenSSLStreamAdapter::Read(void* data, |
| 630 | size_t data_len, |
| 631 | size_t* read, |
| 632 | int* error) { |
Tomas Gunnarsson | d48ff45 | 2020-10-02 18:28:46 +0200 | [diff] [blame] | 633 | RTC_DLOG(LS_VERBOSE) << "OpenSSLStreamAdapter::Read(" << data_len << ")"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 634 | switch (state_) { |
| 635 | case SSL_NONE: |
| 636 | // pass-through in clear text |
Niels Möller | 0131a4d | 2021-04-16 09:16:21 +0200 | [diff] [blame] | 637 | return stream_->Read(data, data_len, read, error); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 638 | case SSL_WAIT: |
| 639 | case SSL_CONNECTING: |
| 640 | return SR_BLOCK; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 641 | case SSL_CONNECTED: |
Benjamin Wright | 5d35554 | 2018-10-26 17:57:00 -0700 | [diff] [blame] | 642 | if (WaitingToVerifyPeerCertificate()) { |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 643 | return SR_BLOCK; |
| 644 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 645 | break; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 646 | case SSL_CLOSED: |
| 647 | return SR_EOS; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 648 | case SSL_ERROR: |
| 649 | default: |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 650 | if (error) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 651 | *error = ssl_error_code_; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 652 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 653 | return SR_ERROR; |
| 654 | } |
| 655 | |
| 656 | // Don't trust OpenSSL with zero byte reads |
| 657 | if (data_len == 0) { |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 658 | if (read) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 659 | *read = 0; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 660 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 661 | return SR_SUCCESS; |
| 662 | } |
| 663 | |
| 664 | ssl_read_needs_write_ = false; |
| 665 | |
Benjamin Wright | f54e30b | 2019-02-26 17:33:50 -0800 | [diff] [blame] | 666 | const int code = SSL_read(ssl_, data, checked_cast<int>(data_len)); |
| 667 | const int ssl_error = SSL_get_error(ssl_, code); |
| 668 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 669 | switch (ssl_error) { |
| 670 | case SSL_ERROR_NONE: |
Tomas Gunnarsson | d48ff45 | 2020-10-02 18:28:46 +0200 | [diff] [blame] | 671 | RTC_DLOG(LS_VERBOSE) << " -- success"; |
kwiberg | ee89e78 | 2017-08-09 17:22:01 -0700 | [diff] [blame] | 672 | RTC_DCHECK_GT(code, 0); |
| 673 | RTC_DCHECK_LE(code, data_len); |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 674 | if (read) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 675 | *read = code; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 676 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 677 | |
| 678 | if (ssl_mode_ == SSL_MODE_DTLS) { |
| 679 | // Enforce atomic reads -- this is a short read |
| 680 | unsigned int pending = SSL_pending(ssl_); |
| 681 | |
| 682 | if (pending) { |
Tomas Gunnarsson | d48ff45 | 2020-10-02 18:28:46 +0200 | [diff] [blame] | 683 | RTC_DLOG(LS_INFO) << " -- short DTLS read. flushing"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 684 | FlushInput(pending); |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 685 | if (error) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 686 | *error = SSE_MSG_TRUNC; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 687 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 688 | return SR_ERROR; |
| 689 | } |
| 690 | } |
| 691 | return SR_SUCCESS; |
| 692 | case SSL_ERROR_WANT_READ: |
Tomas Gunnarsson | d48ff45 | 2020-10-02 18:28:46 +0200 | [diff] [blame] | 693 | RTC_DLOG(LS_VERBOSE) << " -- error want read"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 694 | return SR_BLOCK; |
| 695 | case SSL_ERROR_WANT_WRITE: |
Tomas Gunnarsson | d48ff45 | 2020-10-02 18:28:46 +0200 | [diff] [blame] | 696 | RTC_DLOG(LS_VERBOSE) << " -- error want write"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 697 | ssl_read_needs_write_ = true; |
| 698 | return SR_BLOCK; |
| 699 | case SSL_ERROR_ZERO_RETURN: |
Tomas Gunnarsson | d48ff45 | 2020-10-02 18:28:46 +0200 | [diff] [blame] | 700 | RTC_DLOG(LS_VERBOSE) << " -- remote side closed"; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 701 | Close(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 702 | return SR_EOS; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 703 | default: |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 704 | Error("SSL_read", (ssl_error ? ssl_error : -1), 0, false); |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 705 | if (error) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 706 | *error = ssl_error_code_; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 707 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 708 | return SR_ERROR; |
| 709 | } |
| 710 | // not reached |
| 711 | } |
| 712 | |
| 713 | void OpenSSLStreamAdapter::FlushInput(unsigned int left) { |
| 714 | unsigned char buf[2048]; |
| 715 | |
| 716 | while (left) { |
| 717 | // This should always succeed |
Benjamin Wright | f54e30b | 2019-02-26 17:33:50 -0800 | [diff] [blame] | 718 | const int toread = (sizeof(buf) < left) ? sizeof(buf) : left; |
| 719 | const int code = SSL_read(ssl_, buf, toread); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 720 | |
Benjamin Wright | f54e30b | 2019-02-26 17:33:50 -0800 | [diff] [blame] | 721 | const int ssl_error = SSL_get_error(ssl_, code); |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 722 | RTC_DCHECK(ssl_error == SSL_ERROR_NONE); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 723 | |
| 724 | if (ssl_error != SSL_ERROR_NONE) { |
Jonas Olsson | addc380 | 2018-02-01 09:53:06 +0100 | [diff] [blame] | 725 | RTC_DLOG(LS_VERBOSE) << " -- error " << code; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 726 | Error("SSL_read", (ssl_error ? ssl_error : -1), 0, false); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 727 | return; |
| 728 | } |
| 729 | |
Tomas Gunnarsson | d48ff45 | 2020-10-02 18:28:46 +0200 | [diff] [blame] | 730 | RTC_DLOG(LS_VERBOSE) << " -- flushed " << code << " bytes"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 731 | left -= code; |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | void OpenSSLStreamAdapter::Close() { |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 736 | Cleanup(0); |
| 737 | RTC_DCHECK(state_ == SSL_CLOSED || state_ == SSL_ERROR); |
| 738 | // When we're closed at SSL layer, also close the stream level which |
| 739 | // performs necessary clean up. Otherwise, a new incoming packet after |
| 740 | // this could overflow the stream buffer. |
Niels Möller | 0131a4d | 2021-04-16 09:16:21 +0200 | [diff] [blame] | 741 | stream_->Close(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | StreamState OpenSSLStreamAdapter::GetState() const { |
| 745 | switch (state_) { |
| 746 | case SSL_WAIT: |
| 747 | case SSL_CONNECTING: |
| 748 | return SS_OPENING; |
| 749 | case SSL_CONNECTED: |
Benjamin Wright | 5d35554 | 2018-10-26 17:57:00 -0700 | [diff] [blame] | 750 | if (WaitingToVerifyPeerCertificate()) { |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 751 | return SS_OPENING; |
| 752 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 753 | return SS_OPEN; |
| 754 | default: |
| 755 | return SS_CLOSED; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 756 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 757 | // not reached |
| 758 | } |
| 759 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 760 | void OpenSSLStreamAdapter::OnEvent(StreamInterface* stream, |
| 761 | int events, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 762 | int err) { |
| 763 | int events_to_signal = 0; |
| 764 | int signal_error = 0; |
Niels Möller | 0131a4d | 2021-04-16 09:16:21 +0200 | [diff] [blame] | 765 | RTC_DCHECK(stream == stream_.get()); |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 766 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 767 | if ((events & SE_OPEN)) { |
Tomas Gunnarsson | d48ff45 | 2020-10-02 18:28:46 +0200 | [diff] [blame] | 768 | RTC_DLOG(LS_VERBOSE) << "OpenSSLStreamAdapter::OnEvent SE_OPEN"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 769 | if (state_ != SSL_WAIT) { |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 770 | RTC_DCHECK(state_ == SSL_NONE); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 771 | events_to_signal |= SE_OPEN; |
| 772 | } else { |
| 773 | state_ = SSL_CONNECTING; |
| 774 | if (int err = BeginSSL()) { |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 775 | Error("BeginSSL", err, 0, true); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 776 | return; |
| 777 | } |
| 778 | } |
| 779 | } |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 780 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 781 | if ((events & (SE_READ | SE_WRITE))) { |
Tomas Gunnarsson | d48ff45 | 2020-10-02 18:28:46 +0200 | [diff] [blame] | 782 | RTC_DLOG(LS_VERBOSE) << "OpenSSLStreamAdapter::OnEvent" |
| 783 | << ((events & SE_READ) ? " SE_READ" : "") |
| 784 | << ((events & SE_WRITE) ? " SE_WRITE" : ""); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 785 | if (state_ == SSL_NONE) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 786 | events_to_signal |= events & (SE_READ | SE_WRITE); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 787 | } else if (state_ == SSL_CONNECTING) { |
| 788 | if (int err = ContinueSSL()) { |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 789 | Error("ContinueSSL", err, 0, true); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 790 | return; |
| 791 | } |
| 792 | } else if (state_ == SSL_CONNECTED) { |
| 793 | if (((events & SE_READ) && ssl_write_needs_read_) || |
| 794 | (events & SE_WRITE)) { |
Tomas Gunnarsson | d48ff45 | 2020-10-02 18:28:46 +0200 | [diff] [blame] | 795 | RTC_DLOG(LS_VERBOSE) << " -- onStreamWriteable"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 796 | events_to_signal |= SE_WRITE; |
| 797 | } |
| 798 | if (((events & SE_WRITE) && ssl_read_needs_write_) || |
| 799 | (events & SE_READ)) { |
Tomas Gunnarsson | d48ff45 | 2020-10-02 18:28:46 +0200 | [diff] [blame] | 800 | RTC_DLOG(LS_VERBOSE) << " -- onStreamReadable"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 801 | events_to_signal |= SE_READ; |
| 802 | } |
| 803 | } |
| 804 | } |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 805 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 806 | if ((events & SE_CLOSE)) { |
Tomas Gunnarsson | d48ff45 | 2020-10-02 18:28:46 +0200 | [diff] [blame] | 807 | RTC_DLOG(LS_VERBOSE) << "OpenSSLStreamAdapter::OnEvent(SE_CLOSE, " << err |
| 808 | << ")"; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 809 | Cleanup(0); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 810 | events_to_signal |= SE_CLOSE; |
| 811 | // SE_CLOSE is the only event that uses the final parameter to OnEvent(). |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 812 | RTC_DCHECK(signal_error == 0); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 813 | signal_error = err; |
| 814 | } |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 815 | |
| 816 | if (events_to_signal) { |
Niels Möller | 0131a4d | 2021-04-16 09:16:21 +0200 | [diff] [blame] | 817 | // Note that the adapter presents itself as the origin of the stream events, |
| 818 | // since users of the adapter may not recognize the adapted object. |
| 819 | SignalEvent(this, events_to_signal, signal_error); |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 820 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 821 | } |
| 822 | |
Tommi | 0448298 | 2020-10-05 12:43:53 +0000 | [diff] [blame] | 823 | void OpenSSLStreamAdapter::PostEvent(int events, int err) { |
| 824 | owner_->PostTask(webrtc::ToQueuedTask( |
| 825 | task_safety_, [this, events, err]() { SignalEvent(this, events, err); })); |
| 826 | } |
| 827 | |
| 828 | void OpenSSLStreamAdapter::SetTimeout(int delay_ms) { |
| 829 | // We need to accept 0 delay here as well as >0 delay, because |
| 830 | // DTLSv1_get_timeout seems to frequently return 0 ms. |
| 831 | RTC_DCHECK_GE(delay_ms, 0); |
| 832 | RTC_DCHECK(!timeout_task_.Running()); |
| 833 | |
| 834 | timeout_task_ = webrtc::RepeatingTaskHandle::DelayedStart( |
| 835 | owner_, webrtc::TimeDelta::Millis(delay_ms), |
| 836 | [flag = task_safety_.flag(), this]() { |
| 837 | if (flag->alive()) { |
| 838 | RTC_DLOG(LS_INFO) << "DTLS timeout expired"; |
| 839 | timeout_task_.Stop(); |
Jonas Oreland | 808f494 | 2021-06-16 13:42:20 +0200 | [diff] [blame] | 840 | int res = DTLSv1_handle_timeout(ssl_); |
| 841 | if (res > 0) { |
| 842 | RTC_LOG(LS_INFO) << "DTLS retransmission"; |
| 843 | } else if (res < 0) { |
| 844 | RTC_LOG(LS_INFO) << "DTLSv1_handle_timeout() return -1"; |
Philipp Hancke | 9c83d9d | 2022-04-28 19:01:28 +0200 | [diff] [blame] | 845 | Error("DTLSv1_handle_timeout", res, -1, true); |
| 846 | return webrtc::TimeDelta::PlusInfinity(); |
Jonas Oreland | 808f494 | 2021-06-16 13:42:20 +0200 | [diff] [blame] | 847 | } |
Tommi | 0448298 | 2020-10-05 12:43:53 +0000 | [diff] [blame] | 848 | ContinueSSL(); |
| 849 | } else { |
Artem Titov | d325196 | 2021-11-15 16:57:07 +0100 | [diff] [blame] | 850 | RTC_DCHECK_NOTREACHED(); |
Tommi | 0448298 | 2020-10-05 12:43:53 +0000 | [diff] [blame] | 851 | } |
| 852 | // This callback will never run again (stopped above). |
| 853 | return webrtc::TimeDelta::PlusInfinity(); |
| 854 | }); |
| 855 | } |
| 856 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 857 | int OpenSSLStreamAdapter::BeginSSL() { |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 858 | RTC_DCHECK(state_ == SSL_CONNECTING); |
Taylor Brandstetter | c8762a8 | 2016-08-11 12:01:49 -0700 | [diff] [blame] | 859 | // The underlying stream has opened. |
Tomas Gunnarsson | d48ff45 | 2020-10-02 18:28:46 +0200 | [diff] [blame] | 860 | RTC_DLOG(LS_INFO) << "BeginSSL with peer."; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 861 | |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 862 | BIO* bio = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 863 | |
Taylor Brandstetter | c8762a8 | 2016-08-11 12:01:49 -0700 | [diff] [blame] | 864 | // First set up the context. |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 865 | RTC_DCHECK(ssl_ctx_ == nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 866 | ssl_ctx_ = SetupSSLContext(); |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 867 | if (!ssl_ctx_) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 868 | return -1; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 869 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 870 | |
Niels Möller | 0131a4d | 2021-04-16 09:16:21 +0200 | [diff] [blame] | 871 | bio = BIO_new_stream(stream_.get()); |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 872 | if (!bio) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 873 | return -1; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 874 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 875 | |
| 876 | ssl_ = SSL_new(ssl_ctx_); |
| 877 | if (!ssl_) { |
| 878 | BIO_free(bio); |
| 879 | return -1; |
| 880 | } |
| 881 | |
| 882 | SSL_set_app_data(ssl_, this); |
| 883 | |
| 884 | 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] | 885 | if (ssl_mode_ == SSL_MODE_DTLS) { |
Taylor Brandstetter | 4f0dfbd | 2016-06-15 17:15:23 -0700 | [diff] [blame] | 886 | #ifdef OPENSSL_IS_BORINGSSL |
skvlad | d030912 | 2017-02-02 17:18:37 -0800 | [diff] [blame] | 887 | DTLSv1_set_initial_timeout_duration(ssl_, dtls_handshake_timeout_ms_); |
Taylor Brandstetter | 4f0dfbd | 2016-06-15 17:15:23 -0700 | [diff] [blame] | 888 | #else |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 889 | // Enable read-ahead for DTLS so whole packets are read from internal BIO |
| 890 | // before parsing. This is done internally by BoringSSL for DTLS. |
| 891 | SSL_set_read_ahead(ssl_, 1); |
philipel | 49c0869 | 2016-05-24 01:49:43 -0700 | [diff] [blame] | 892 | #endif |
Taylor Brandstetter | 4f0dfbd | 2016-06-15 17:15:23 -0700 | [diff] [blame] | 893 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 894 | |
| 895 | SSL_set_mode(ssl_, SSL_MODE_ENABLE_PARTIAL_WRITE | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 896 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 897 | |
| 898 | // Do the connect |
| 899 | return ContinueSSL(); |
| 900 | } |
| 901 | |
| 902 | int OpenSSLStreamAdapter::ContinueSSL() { |
Tomas Gunnarsson | d48ff45 | 2020-10-02 18:28:46 +0200 | [diff] [blame] | 903 | RTC_DLOG(LS_VERBOSE) << "ContinueSSL"; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 904 | RTC_DCHECK(state_ == SSL_CONNECTING); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 905 | |
| 906 | // Clear the DTLS timer |
Tommi | 0448298 | 2020-10-05 12:43:53 +0000 | [diff] [blame] | 907 | timeout_task_.Stop(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 908 | |
Benjamin Wright | f54e30b | 2019-02-26 17:33:50 -0800 | [diff] [blame] | 909 | const int code = (role_ == SSL_CLIENT) ? SSL_connect(ssl_) : SSL_accept(ssl_); |
| 910 | const int ssl_error = SSL_get_error(ssl_, code); |
| 911 | |
| 912 | switch (ssl_error) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 913 | case SSL_ERROR_NONE: |
Tomas Gunnarsson | d48ff45 | 2020-10-02 18:28:46 +0200 | [diff] [blame] | 914 | RTC_DLOG(LS_VERBOSE) << " -- success"; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 915 | // By this point, OpenSSL should have given us a certificate, or errored |
| 916 | // out if one was missing. |
Benjamin Wright | b19b497 | 2018-10-25 10:46:49 -0700 | [diff] [blame] | 917 | RTC_DCHECK(peer_cert_chain_ || !GetClientAuthEnabled()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 918 | |
| 919 | state_ = SSL_CONNECTED; |
Benjamin Wright | 5d35554 | 2018-10-26 17:57:00 -0700 | [diff] [blame] | 920 | if (!WaitingToVerifyPeerCertificate()) { |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 921 | // We have everything we need to start the connection, so signal |
| 922 | // SE_OPEN. If we need a client certificate fingerprint and don't have |
| 923 | // it yet, we'll instead signal SE_OPEN in SetPeerCertificateDigest. |
| 924 | // |
Taylor Brandstetter | fe69a74 | 2016-09-30 17:34:32 -0700 | [diff] [blame] | 925 | // TODO(deadbeef): Post this event asynchronously to unwind the stack. |
| 926 | // The caller of ContinueSSL may be the same object listening for these |
| 927 | // events and may not be prepared for reentrancy. |
| 928 | // PostEvent(SE_OPEN | SE_READ | SE_WRITE, 0); |
Niels Möller | 0131a4d | 2021-04-16 09:16:21 +0200 | [diff] [blame] | 929 | SignalEvent(this, SE_OPEN | SE_READ | SE_WRITE, 0); |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 930 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 931 | break; |
| 932 | |
| 933 | case SSL_ERROR_WANT_READ: { |
Tomas Gunnarsson | d48ff45 | 2020-10-02 18:28:46 +0200 | [diff] [blame] | 934 | RTC_DLOG(LS_VERBOSE) << " -- error want read"; |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 935 | struct timeval timeout; |
| 936 | if (DTLSv1_get_timeout(ssl_, &timeout)) { |
| 937 | int delay = timeout.tv_sec * 1000 + timeout.tv_usec / 1000; |
Tommi | 0448298 | 2020-10-05 12:43:53 +0000 | [diff] [blame] | 938 | SetTimeout(delay); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 939 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 940 | } break; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 941 | |
| 942 | case SSL_ERROR_WANT_WRITE: |
Tomas Gunnarsson | d48ff45 | 2020-10-02 18:28:46 +0200 | [diff] [blame] | 943 | RTC_DLOG(LS_VERBOSE) << " -- error want write"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 944 | break; |
| 945 | |
| 946 | case SSL_ERROR_ZERO_RETURN: |
| 947 | default: |
zhihuang | d82eee0 | 2016-08-26 11:25:05 -0700 | [diff] [blame] | 948 | SSLHandshakeError ssl_handshake_err = SSLHandshakeError::UNKNOWN; |
| 949 | int err_code = ERR_peek_last_error(); |
| 950 | if (err_code != 0 && ERR_GET_REASON(err_code) == SSL_R_NO_SHARED_CIPHER) { |
| 951 | ssl_handshake_err = SSLHandshakeError::INCOMPATIBLE_CIPHERSUITE; |
| 952 | } |
Tomas Gunnarsson | d48ff45 | 2020-10-02 18:28:46 +0200 | [diff] [blame] | 953 | RTC_DLOG(LS_VERBOSE) << " -- error " << code << ", " << err_code << ", " |
| 954 | << ERR_GET_REASON(err_code); |
Sam Zackrisson | b298f74 | 2020-10-06 11:40:30 +0000 | [diff] [blame] | 955 | SignalSSLHandshakeError(ssl_handshake_err); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 956 | return (ssl_error != 0) ? ssl_error : -1; |
| 957 | } |
| 958 | |
| 959 | return 0; |
| 960 | } |
| 961 | |
Ali Tofigh | 2ab914c | 2022-04-13 12:55:15 +0200 | [diff] [blame] | 962 | void OpenSSLStreamAdapter::Error(absl::string_view context, |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 963 | int err, |
| 964 | uint8_t alert, |
| 965 | bool signal) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 966 | RTC_LOG(LS_WARNING) << "OpenSSLStreamAdapter::Error(" << context << ", " |
| 967 | << err << ", " << static_cast<int>(alert) << ")"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 968 | state_ = SSL_ERROR; |
| 969 | ssl_error_code_ = err; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 970 | Cleanup(alert); |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 971 | if (signal) { |
Niels Möller | 0131a4d | 2021-04-16 09:16:21 +0200 | [diff] [blame] | 972 | SignalEvent(this, SE_CLOSE, err); |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 973 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 974 | } |
| 975 | |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 976 | void OpenSSLStreamAdapter::Cleanup(uint8_t alert) { |
Tomas Gunnarsson | d48ff45 | 2020-10-02 18:28:46 +0200 | [diff] [blame] | 977 | RTC_DLOG(LS_INFO) << "Cleanup"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 978 | |
| 979 | if (state_ != SSL_ERROR) { |
| 980 | state_ = SSL_CLOSED; |
| 981 | ssl_error_code_ = 0; |
| 982 | } |
| 983 | |
| 984 | if (ssl_) { |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 985 | int ret; |
| 986 | // SSL_send_fatal_alert is only available in BoringSSL. |
| 987 | #ifdef OPENSSL_IS_BORINGSSL |
| 988 | if (alert) { |
| 989 | ret = SSL_send_fatal_alert(ssl_, alert); |
| 990 | if (ret < 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 991 | RTC_LOG(LS_WARNING) << "SSL_send_fatal_alert failed, error = " |
| 992 | << SSL_get_error(ssl_, ret); |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 993 | } |
| 994 | } else { |
| 995 | #endif |
| 996 | ret = SSL_shutdown(ssl_); |
| 997 | if (ret < 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 998 | RTC_LOG(LS_WARNING) |
| 999 | << "SSL_shutdown failed, error = " << SSL_get_error(ssl_, ret); |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 1000 | } |
| 1001 | #ifdef OPENSSL_IS_BORINGSSL |
jiayl@webrtc.org | f1d751c | 2014-09-25 16:38:46 +0000 | [diff] [blame] | 1002 | } |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 1003 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1004 | SSL_free(ssl_); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 1005 | ssl_ = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1006 | } |
| 1007 | if (ssl_ctx_) { |
| 1008 | SSL_CTX_free(ssl_ctx_); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 1009 | ssl_ctx_ = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1010 | } |
| 1011 | identity_.reset(); |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 1012 | peer_cert_chain_.reset(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1013 | |
| 1014 | // Clear the DTLS timer |
Tommi | 0448298 | 2020-10-05 12:43:53 +0000 | [diff] [blame] | 1015 | timeout_task_.Stop(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | SSL_CTX* OpenSSLStreamAdapter::SetupSSLContext() { |
Taylor Brandstetter | 165c618 | 2020-12-10 16:23:03 -0800 | [diff] [blame] | 1019 | #ifdef OPENSSL_IS_BORINGSSL |
| 1020 | // If X509 objects aren't used, we can use these methods to avoid |
| 1021 | // linking the sizable crypto/x509 code, using CRYPTO_BUFFER instead. |
| 1022 | SSL_CTX* ctx = |
| 1023 | SSL_CTX_new(ssl_mode_ == SSL_MODE_DTLS ? DTLS_with_buffers_method() |
| 1024 | : TLS_with_buffers_method()); |
| 1025 | #else |
David Benjamin | 170a4b3 | 2019-01-30 09:46:16 -0600 | [diff] [blame] | 1026 | SSL_CTX* ctx = |
| 1027 | SSL_CTX_new(ssl_mode_ == SSL_MODE_DTLS ? DTLS_method() : TLS_method()); |
Taylor Brandstetter | 165c618 | 2020-12-10 16:23:03 -0800 | [diff] [blame] | 1028 | #endif |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 1029 | if (ctx == nullptr) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 1030 | return nullptr; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 1031 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1032 | |
Harald Alvestrand | 1379913 | 2020-03-09 19:39:36 +0100 | [diff] [blame] | 1033 | if (support_legacy_tls_protocols_flag_) { |
| 1034 | // TODO(https://bugs.webrtc.org/10261): Completely remove this branch in |
| 1035 | // M84. |
| 1036 | SSL_CTX_set_min_proto_version( |
| 1037 | ctx, ssl_mode_ == SSL_MODE_DTLS ? DTLS1_VERSION : TLS1_VERSION); |
| 1038 | switch (ssl_max_version_) { |
| 1039 | case SSL_PROTOCOL_TLS_10: |
| 1040 | SSL_CTX_set_max_proto_version( |
| 1041 | ctx, ssl_mode_ == SSL_MODE_DTLS ? DTLS1_VERSION : TLS1_VERSION); |
| 1042 | break; |
| 1043 | case SSL_PROTOCOL_TLS_11: |
| 1044 | SSL_CTX_set_max_proto_version( |
| 1045 | ctx, ssl_mode_ == SSL_MODE_DTLS ? DTLS1_VERSION : TLS1_1_VERSION); |
| 1046 | break; |
| 1047 | case SSL_PROTOCOL_TLS_12: |
| 1048 | default: |
| 1049 | SSL_CTX_set_max_proto_version( |
| 1050 | ctx, ssl_mode_ == SSL_MODE_DTLS ? DTLS1_2_VERSION : TLS1_2_VERSION); |
| 1051 | break; |
| 1052 | } |
| 1053 | } else { |
| 1054 | // TODO(https://bugs.webrtc.org/10261): Make this the default in M84. |
| 1055 | SSL_CTX_set_min_proto_version( |
| 1056 | ctx, ssl_mode_ == SSL_MODE_DTLS ? DTLS1_2_VERSION : TLS1_2_VERSION); |
| 1057 | SSL_CTX_set_max_proto_version( |
| 1058 | ctx, ssl_mode_ == SSL_MODE_DTLS ? DTLS1_2_VERSION : TLS1_2_VERSION); |
Joachim Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 1059 | } |
Harald Alvestrand | 1379913 | 2020-03-09 19:39:36 +0100 | [diff] [blame] | 1060 | |
David Benjamin | 170a4b3 | 2019-01-30 09:46:16 -0600 | [diff] [blame] | 1061 | #ifdef OPENSSL_IS_BORINGSSL |
| 1062 | // SSL_CTX_set_current_time_cb is only supported in BoringSSL. |
deadbeef | 6cf94a0 | 2016-11-28 17:38:34 -0800 | [diff] [blame] | 1063 | if (g_use_time_callback_for_testing) { |
| 1064 | SSL_CTX_set_current_time_cb(ctx, &TimeCallbackForTesting); |
| 1065 | } |
Taylor Brandstetter | 165c618 | 2020-12-10 16:23:03 -0800 | [diff] [blame] | 1066 | SSL_CTX_set0_buffer_pool(ctx, openssl::GetBufferPool()); |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 1067 | #endif |
Joachim Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 1068 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1069 | if (identity_ && !identity_->ConfigureIdentity(ctx)) { |
| 1070 | SSL_CTX_free(ctx); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 1071 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1072 | } |
| 1073 | |
tfarina | a41ab93 | 2015-10-30 16:08:48 -0700 | [diff] [blame] | 1074 | #if !defined(NDEBUG) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1075 | SSL_CTX_set_info_callback(ctx, OpenSSLAdapter::SSLInfoCallback); |
| 1076 | #endif |
| 1077 | |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 1078 | int mode = SSL_VERIFY_PEER; |
Benjamin Wright | b19b497 | 2018-10-25 10:46:49 -0700 | [diff] [blame] | 1079 | if (GetClientAuthEnabled()) { |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 1080 | // Require a certificate from the client. |
| 1081 | // Note: Normally this is always true in production, but it may be disabled |
| 1082 | // for testing purposes (e.g. SSLAdapter unit tests). |
| 1083 | mode |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 1084 | } |
| 1085 | |
David Benjamin | dc24656 | 2017-09-29 12:14:08 -0400 | [diff] [blame] | 1086 | // Configure a custom certificate verification callback to check the peer |
Taylor Brandstetter | 165c618 | 2020-12-10 16:23:03 -0800 | [diff] [blame] | 1087 | // certificate digest. |
| 1088 | #ifdef OPENSSL_IS_BORINGSSL |
| 1089 | // Use CRYPTO_BUFFER version of the callback if building with BoringSSL. |
| 1090 | SSL_CTX_set_custom_verify(ctx, mode, SSLVerifyCallback); |
| 1091 | #else |
| 1092 | // Note the second argument to SSL_CTX_set_verify is to override individual |
| 1093 | // 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] | 1094 | SSL_CTX_set_verify(ctx, mode, nullptr); |
| 1095 | SSL_CTX_set_cert_verify_callback(ctx, SSLVerifyCallback, nullptr); |
Taylor Brandstetter | 165c618 | 2020-12-10 16:23:03 -0800 | [diff] [blame] | 1096 | #endif |
David Benjamin | dc24656 | 2017-09-29 12:14:08 -0400 | [diff] [blame] | 1097 | |
Joachim Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 1098 | // Select list of available ciphers. Note that !SHA256 and !SHA384 only |
| 1099 | // remove HMAC-SHA256 and HMAC-SHA384 cipher suites, not GCM cipher suites |
| 1100 | // with SHA256 or SHA384 as the handshake hash. |
David Benjamin | 7a46cc5 | 2021-08-17 16:56:20 -0400 | [diff] [blame] | 1101 | // This matches the list of SSLClientSocketImpl in Chromium. |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 1102 | SSL_CTX_set_cipher_list( |
David Benjamin | 7a46cc5 | 2021-08-17 16:56:20 -0400 | [diff] [blame] | 1103 | ctx, |
| 1104 | "DEFAULT:!NULL:!aNULL:!SHA256:!SHA384:!aECDH:!AESGCM+AES256:!aPSK:!3DES"); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1105 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1106 | if (!srtp_ciphers_.empty()) { |
| 1107 | if (SSL_CTX_set_tlsext_use_srtp(ctx, srtp_ciphers_.c_str())) { |
| 1108 | SSL_CTX_free(ctx); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 1109 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1110 | } |
| 1111 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1112 | |
| 1113 | return ctx; |
| 1114 | } |
| 1115 | |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 1116 | bool OpenSSLStreamAdapter::VerifyPeerCertificate() { |
Benjamin Wright | 5d35554 | 2018-10-26 17:57:00 -0700 | [diff] [blame] | 1117 | if (!HasPeerCertificateDigest() || !peer_cert_chain_ || |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 1118 | !peer_cert_chain_->GetSize()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1119 | RTC_LOG(LS_WARNING) << "Missing digest or peer certificate."; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 1120 | return false; |
| 1121 | } |
deadbeef | 81f6f4f | 2016-09-19 17:20:52 -0700 | [diff] [blame] | 1122 | |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 1123 | unsigned char digest[EVP_MAX_MD_SIZE]; |
| 1124 | size_t digest_length; |
Taylor Brandstetter | 165c618 | 2020-12-10 16:23:03 -0800 | [diff] [blame] | 1125 | if (!peer_cert_chain_->Get(0).ComputeDigest( |
| 1126 | peer_certificate_digest_algorithm_, digest, sizeof(digest), |
| 1127 | &digest_length)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1128 | RTC_LOG(LS_WARNING) << "Failed to compute peer cert digest."; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 1129 | return false; |
| 1130 | } |
| 1131 | |
| 1132 | Buffer computed_digest(digest, digest_length); |
| 1133 | if (computed_digest != peer_certificate_digest_value_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1134 | RTC_LOG(LS_WARNING) |
Philipp Hancke | 117e692 | 2022-06-17 14:14:55 +0200 | [diff] [blame^] | 1135 | << "Rejected peer certificate due to mismatched digest using " |
| 1136 | << peer_certificate_digest_algorithm_ << ". Expected " |
| 1137 | << rtc::hex_encode_with_delimiter(peer_certificate_digest_value_, ':') |
| 1138 | << " got " << rtc::hex_encode_with_delimiter(computed_digest, ':'); |
jbauch | f8f457b | 2017-03-09 16:24:57 -0800 | [diff] [blame] | 1139 | return false; |
deadbeef | 81f6f4f | 2016-09-19 17:20:52 -0700 | [diff] [blame] | 1140 | } |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 1141 | // Ignore any verification error if the digest matches, since there is no |
| 1142 | // value in checking the validity of a self-signed cert issued by untrusted |
| 1143 | // sources. |
Tomas Gunnarsson | d48ff45 | 2020-10-02 18:28:46 +0200 | [diff] [blame] | 1144 | RTC_DLOG(LS_INFO) << "Accepted peer certificate."; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 1145 | peer_certificate_verified_ = true; |
| 1146 | return true; |
| 1147 | } |
| 1148 | |
Jian Cui | 0a8798b | 2017-11-16 16:58:02 -0800 | [diff] [blame] | 1149 | std::unique_ptr<SSLCertChain> OpenSSLStreamAdapter::GetPeerSSLCertChain() |
| 1150 | const { |
Steve Anton | f25303e | 2018-10-16 15:23:31 -0700 | [diff] [blame] | 1151 | return peer_cert_chain_ ? peer_cert_chain_->Clone() : nullptr; |
Jian Cui | 0a8798b | 2017-11-16 16:58:02 -0800 | [diff] [blame] | 1152 | } |
| 1153 | |
Taylor Brandstetter | 165c618 | 2020-12-10 16:23:03 -0800 | [diff] [blame] | 1154 | #ifdef OPENSSL_IS_BORINGSSL |
| 1155 | enum ssl_verify_result_t OpenSSLStreamAdapter::SSLVerifyCallback( |
| 1156 | SSL* ssl, |
| 1157 | uint8_t* out_alert) { |
| 1158 | // Get our OpenSSLStreamAdapter from the context. |
| 1159 | OpenSSLStreamAdapter* stream = |
| 1160 | reinterpret_cast<OpenSSLStreamAdapter*>(SSL_get_app_data(ssl)); |
| 1161 | const STACK_OF(CRYPTO_BUFFER)* chain = SSL_get0_peer_certificates(ssl); |
| 1162 | // Creates certificate chain. |
| 1163 | std::vector<std::unique_ptr<SSLCertificate>> cert_chain; |
| 1164 | for (CRYPTO_BUFFER* cert : chain) { |
| 1165 | cert_chain.emplace_back(new BoringSSLCertificate(bssl::UpRef(cert))); |
| 1166 | } |
| 1167 | stream->peer_cert_chain_.reset(new SSLCertChain(std::move(cert_chain))); |
| 1168 | |
| 1169 | // If the peer certificate digest isn't known yet, we'll wait to verify |
| 1170 | // until it's known, and for now just return a success status. |
| 1171 | if (stream->peer_certificate_digest_algorithm_.empty()) { |
| 1172 | RTC_LOG(LS_INFO) << "Waiting to verify certificate until digest is known."; |
| 1173 | // TODO(deadbeef): Use ssl_verify_retry? |
| 1174 | return ssl_verify_ok; |
| 1175 | } |
| 1176 | |
| 1177 | if (!stream->VerifyPeerCertificate()) { |
| 1178 | return ssl_verify_invalid; |
| 1179 | } |
| 1180 | |
| 1181 | return ssl_verify_ok; |
| 1182 | } |
| 1183 | #else // OPENSSL_IS_BORINGSSL |
David Benjamin | dc24656 | 2017-09-29 12:14:08 -0400 | [diff] [blame] | 1184 | int OpenSSLStreamAdapter::SSLVerifyCallback(X509_STORE_CTX* store, void* arg) { |
| 1185 | // Get our SSL structure and OpenSSLStreamAdapter from the store. |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 1186 | SSL* ssl = reinterpret_cast<SSL*>( |
| 1187 | 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] | 1188 | OpenSSLStreamAdapter* stream = |
| 1189 | reinterpret_cast<OpenSSLStreamAdapter*>(SSL_get_app_data(ssl)); |
henrike@webrtc.org | 4e5f65a | 2014-06-05 20:40:11 +0000 | [diff] [blame] | 1190 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1191 | // Record the peer's certificate. |
Jiawei Ou | 9d4e840 | 2018-05-23 15:44:20 -0700 | [diff] [blame] | 1192 | X509* cert = X509_STORE_CTX_get0_cert(store); |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 1193 | stream->peer_cert_chain_.reset( |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 1194 | new SSLCertChain(std::make_unique<OpenSSLCertificate>(cert))); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1195 | |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 1196 | // If the peer certificate digest isn't known yet, we'll wait to verify |
| 1197 | // until it's known, and for now just return a success status. |
| 1198 | if (stream->peer_certificate_digest_algorithm_.empty()) { |
Tomas Gunnarsson | d48ff45 | 2020-10-02 18:28:46 +0200 | [diff] [blame] | 1199 | RTC_DLOG(LS_INFO) << "Waiting to verify certificate until digest is known."; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 1200 | return 1; |
| 1201 | } |
| 1202 | |
David Benjamin | dc24656 | 2017-09-29 12:14:08 -0400 | [diff] [blame] | 1203 | if (!stream->VerifyPeerCertificate()) { |
| 1204 | X509_STORE_CTX_set_error(store, X509_V_ERR_CERT_REJECTED); |
| 1205 | return 0; |
| 1206 | } |
| 1207 | |
| 1208 | return 1; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1209 | } |
Taylor Brandstetter | 165c618 | 2020-12-10 16:23:03 -0800 | [diff] [blame] | 1210 | #endif // !OPENSSL_IS_BORINGSSL |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1211 | |
Taylor Brandstetter | 4f0dfbd | 2016-06-15 17:15:23 -0700 | [diff] [blame] | 1212 | bool OpenSSLStreamAdapter::IsBoringSsl() { |
| 1213 | #ifdef OPENSSL_IS_BORINGSSL |
| 1214 | return true; |
| 1215 | #else |
| 1216 | return false; |
| 1217 | #endif |
| 1218 | } |
| 1219 | |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1220 | #define CDEF(X) \ |
| 1221 | { static_cast<uint16_t>(TLS1_CK_##X & 0xffff), "TLS_" #X } |
| 1222 | |
| 1223 | struct cipher_list { |
| 1224 | uint16_t cipher; |
| 1225 | const char* cipher_str; |
| 1226 | }; |
| 1227 | |
| 1228 | // TODO(torbjorng): Perhaps add more cipher suites to these lists. |
| 1229 | static const cipher_list OK_RSA_ciphers[] = { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1230 | CDEF(ECDHE_RSA_WITH_AES_128_CBC_SHA), |
| 1231 | CDEF(ECDHE_RSA_WITH_AES_256_CBC_SHA), |
| 1232 | CDEF(ECDHE_RSA_WITH_AES_128_GCM_SHA256), |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1233 | #ifdef TLS1_CK_ECDHE_RSA_WITH_AES_256_GCM_SHA256 |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1234 | CDEF(ECDHE_RSA_WITH_AES_256_GCM_SHA256), |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1235 | #endif |
torbjorng | aad6780 | 2016-04-07 08:55:28 -0700 | [diff] [blame] | 1236 | #ifdef TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1237 | CDEF(ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256), |
torbjorng | aad6780 | 2016-04-07 08:55:28 -0700 | [diff] [blame] | 1238 | #endif |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1239 | }; |
| 1240 | |
| 1241 | static const cipher_list OK_ECDSA_ciphers[] = { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1242 | CDEF(ECDHE_ECDSA_WITH_AES_128_CBC_SHA), |
| 1243 | CDEF(ECDHE_ECDSA_WITH_AES_256_CBC_SHA), |
| 1244 | CDEF(ECDHE_ECDSA_WITH_AES_128_GCM_SHA256), |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1245 | #ifdef TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA256 |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1246 | CDEF(ECDHE_ECDSA_WITH_AES_256_GCM_SHA256), |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1247 | #endif |
torbjorng | aad6780 | 2016-04-07 08:55:28 -0700 | [diff] [blame] | 1248 | #ifdef TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1249 | CDEF(ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256), |
torbjorng | aad6780 | 2016-04-07 08:55:28 -0700 | [diff] [blame] | 1250 | #endif |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1251 | }; |
| 1252 | #undef CDEF |
| 1253 | |
| 1254 | bool OpenSSLStreamAdapter::IsAcceptableCipher(int cipher, KeyType key_type) { |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 1255 | if (key_type == KT_RSA) { |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1256 | for (const cipher_list& c : OK_RSA_ciphers) { |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 1257 | if (cipher == c.cipher) { |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1258 | return true; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 1259 | } |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 1260 | } |
Joachim Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 1261 | } |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1262 | |
| 1263 | if (key_type == KT_ECDSA) { |
| 1264 | for (const cipher_list& c : OK_ECDSA_ciphers) { |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 1265 | if (cipher == c.cipher) { |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1266 | return true; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 1267 | } |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1268 | } |
| 1269 | } |
| 1270 | |
| 1271 | return false; |
| 1272 | } |
| 1273 | |
Ali Tofigh | 7fa9057 | 2022-03-17 15:47:49 +0100 | [diff] [blame] | 1274 | bool OpenSSLStreamAdapter::IsAcceptableCipher(absl::string_view cipher, |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1275 | KeyType key_type) { |
| 1276 | if (key_type == KT_RSA) { |
| 1277 | for (const cipher_list& c : OK_RSA_ciphers) { |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 1278 | if (cipher == c.cipher_str) { |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1279 | return true; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 1280 | } |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1281 | } |
| 1282 | } |
| 1283 | |
| 1284 | if (key_type == KT_ECDSA) { |
| 1285 | for (const cipher_list& c : OK_ECDSA_ciphers) { |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 1286 | if (cipher == c.cipher_str) { |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1287 | return true; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 1288 | } |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1289 | } |
| 1290 | } |
| 1291 | |
| 1292 | return false; |
pthatcher@webrtc.org | 3ee4fe5 | 2015-02-11 22:34:36 +0000 | [diff] [blame] | 1293 | } |
| 1294 | |
Benjamin Wright | b19b497 | 2018-10-25 10:46:49 -0700 | [diff] [blame] | 1295 | void OpenSSLStreamAdapter::EnableTimeCallbackForTesting() { |
Joachim Reiersen | 637bed5 | 2019-04-25 10:41:35 -0700 | [diff] [blame] | 1296 | #ifdef OPENSSL_IS_BORINGSSL |
deadbeef | 6cf94a0 | 2016-11-28 17:38:34 -0800 | [diff] [blame] | 1297 | g_use_time_callback_for_testing = true; |
Joachim Reiersen | 637bed5 | 2019-04-25 10:41:35 -0700 | [diff] [blame] | 1298 | #endif |
deadbeef | 6cf94a0 | 2016-11-28 17:38:34 -0800 | [diff] [blame] | 1299 | } |
| 1300 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1301 | } // namespace rtc |