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