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