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