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 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 24 | #include <memory> |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 25 | #include <utility> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 26 | #include <vector> |
| 27 | |
Sergey Sablin | 3c119fb | 2019-02-12 18:30:45 -0800 | [diff] [blame] | 28 | #include "absl/memory/memory.h" |
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" |
Benjamin Wright | 7276b97 | 2019-03-06 11:51:34 -0800 | [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 { |
| 54 | |
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 | |
| 269 | OpenSSLStreamAdapter::OpenSSLStreamAdapter(StreamInterface* stream) |
| 270 | : SSLStreamAdapter(stream), |
| 271 | state_(SSL_NONE), |
| 272 | role_(SSL_CLIENT), |
Guo-wei Shieh | a7446d2 | 2016-01-11 15:27:03 -0800 | [diff] [blame] | 273 | ssl_read_needs_write_(false), |
| 274 | ssl_write_needs_read_(false), |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 275 | ssl_(nullptr), |
| 276 | ssl_ctx_(nullptr), |
Joachim Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 277 | ssl_mode_(SSL_MODE_TLS), |
Benjamin Wright | 7276b97 | 2019-03-06 11:51:34 -0800 | [diff] [blame] | 278 | ssl_max_version_(SSL_PROTOCOL_TLS_12), |
| 279 | support_legacy_tls_protocols_flag_( |
| 280 | webrtc::field_trial::IsEnabled("WebRTC-LegacyTlsProtocols")) {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 281 | |
| 282 | OpenSSLStreamAdapter::~OpenSSLStreamAdapter() { |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 283 | Cleanup(0); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | void OpenSSLStreamAdapter::SetIdentity(SSLIdentity* identity) { |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 287 | RTC_DCHECK(!identity_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 288 | identity_.reset(static_cast<OpenSSLIdentity*>(identity)); |
| 289 | } |
| 290 | |
| 291 | void OpenSSLStreamAdapter::SetServerRole(SSLRole role) { |
| 292 | role_ = role; |
| 293 | } |
| 294 | |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 295 | bool OpenSSLStreamAdapter::SetPeerCertificateDigest( |
| 296 | const std::string& digest_alg, |
| 297 | const unsigned char* digest_val, |
| 298 | size_t digest_len, |
| 299 | SSLPeerCertificateDigestError* error) { |
| 300 | RTC_DCHECK(!peer_certificate_verified_); |
Benjamin Wright | 5d35554 | 2018-10-26 17:57:00 -0700 | [diff] [blame] | 301 | RTC_DCHECK(!HasPeerCertificateDigest()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 302 | size_t expected_len; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 303 | if (error) { |
| 304 | *error = SSLPeerCertificateDigestError::NONE; |
| 305 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 306 | |
| 307 | if (!OpenSSLDigest::GetDigestSize(digest_alg, &expected_len)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 308 | RTC_LOG(LS_WARNING) << "Unknown digest algorithm: " << digest_alg; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 309 | if (error) { |
| 310 | *error = SSLPeerCertificateDigestError::UNKNOWN_ALGORITHM; |
| 311 | } |
deadbeef | 81f6f4f | 2016-09-19 17:20:52 -0700 | [diff] [blame] | 312 | return false; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 313 | } |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 314 | if (expected_len != digest_len) { |
| 315 | if (error) { |
| 316 | *error = SSLPeerCertificateDigestError::INVALID_LENGTH; |
| 317 | } |
deadbeef | 81f6f4f | 2016-09-19 17:20:52 -0700 | [diff] [blame] | 318 | return false; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 319 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 320 | |
| 321 | peer_certificate_digest_value_.SetData(digest_val, digest_len); |
| 322 | peer_certificate_digest_algorithm_ = digest_alg; |
| 323 | |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 324 | if (!peer_cert_chain_) { |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 325 | // Normal case, where the digest is set before we obtain the certificate |
| 326 | // from the handshake. |
| 327 | return true; |
| 328 | } |
| 329 | |
| 330 | if (!VerifyPeerCertificate()) { |
| 331 | Error("SetPeerCertificateDigest", -1, SSL_AD_BAD_CERTIFICATE, false); |
| 332 | if (error) { |
| 333 | *error = SSLPeerCertificateDigestError::VERIFICATION_FAILED; |
| 334 | } |
| 335 | return false; |
| 336 | } |
| 337 | |
| 338 | if (state_ == SSL_CONNECTED) { |
| 339 | // Post the event asynchronously to unwind the stack. The caller |
| 340 | // of ContinueSSL may be the same object listening for these |
| 341 | // events and may not be prepared for reentrancy. |
| 342 | PostEvent(SE_OPEN | SE_READ | SE_WRITE, 0); |
| 343 | } |
| 344 | |
deadbeef | 81f6f4f | 2016-09-19 17:20:52 -0700 | [diff] [blame] | 345 | return true; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 348 | std::string OpenSSLStreamAdapter::SslCipherSuiteToName(int cipher_suite) { |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 349 | #ifdef OPENSSL_IS_BORINGSSL |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 350 | 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] | 351 | if (!ssl_cipher) { |
| 352 | return std::string(); |
| 353 | } |
David Benjamin | a8f7376 | 2017-09-28 15:49:42 -0400 | [diff] [blame] | 354 | return SSL_CIPHER_standard_name(ssl_cipher); |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 355 | #else |
| 356 | for (const SslCipherMapEntry* entry = kSslCipherMap; entry->rfc_name; |
| 357 | ++entry) { |
| 358 | if (cipher_suite == static_cast<int>(entry->openssl_id)) { |
| 359 | return entry->rfc_name; |
| 360 | } |
| 361 | } |
| 362 | return std::string(); |
| 363 | #endif |
Guo-wei Shieh | 456696a | 2015-09-30 21:48:54 -0700 | [diff] [blame] | 364 | } |
pthatcher@webrtc.org | 3ee4fe5 | 2015-02-11 22:34:36 +0000 | [diff] [blame] | 365 | |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 366 | bool OpenSSLStreamAdapter::GetSslCipherSuite(int* cipher_suite) { |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 367 | if (state_ != SSL_CONNECTED) { |
pthatcher@webrtc.org | 3ee4fe5 | 2015-02-11 22:34:36 +0000 | [diff] [blame] | 368 | return false; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 369 | } |
pthatcher@webrtc.org | 3ee4fe5 | 2015-02-11 22:34:36 +0000 | [diff] [blame] | 370 | |
| 371 | const SSL_CIPHER* current_cipher = SSL_get_current_cipher(ssl_); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 372 | if (current_cipher == nullptr) { |
pthatcher@webrtc.org | 3ee4fe5 | 2015-02-11 22:34:36 +0000 | [diff] [blame] | 373 | return false; |
| 374 | } |
| 375 | |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 376 | *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] | 377 | return true; |
| 378 | } |
| 379 | |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 380 | int OpenSSLStreamAdapter::GetSslVersion() const { |
Benjamin Wright | 8e98c60 | 2019-02-28 17:25:01 -0800 | [diff] [blame] | 381 | if (state_ != SSL_CONNECTED) { |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 382 | return -1; |
Benjamin Wright | 8e98c60 | 2019-02-28 17:25:01 -0800 | [diff] [blame] | 383 | } |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 384 | |
| 385 | int ssl_version = SSL_version(ssl_); |
| 386 | if (ssl_mode_ == SSL_MODE_DTLS) { |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 387 | if (ssl_version == DTLS1_VERSION) { |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 388 | return SSL_PROTOCOL_DTLS_10; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 389 | } else if (ssl_version == DTLS1_2_VERSION) { |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 390 | return SSL_PROTOCOL_DTLS_12; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 391 | } |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 392 | } else { |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 393 | if (ssl_version == TLS1_VERSION) { |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 394 | return SSL_PROTOCOL_TLS_10; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 395 | } else if (ssl_version == TLS1_1_VERSION) { |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 396 | return SSL_PROTOCOL_TLS_11; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 397 | } else if (ssl_version == TLS1_2_VERSION) { |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 398 | return SSL_PROTOCOL_TLS_12; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 399 | } |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | return -1; |
| 403 | } |
| 404 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 405 | // Key Extractor interface |
| 406 | bool OpenSSLStreamAdapter::ExportKeyingMaterial(const std::string& label, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 407 | const uint8_t* context, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 408 | size_t context_len, |
| 409 | bool use_context, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 410 | uint8_t* result, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 411 | size_t result_len) { |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 412 | 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] | 413 | label.length(), const_cast<uint8_t*>(context), |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 414 | context_len, use_context) != 1) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 415 | return false; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 416 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 417 | return true; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 420 | bool OpenSSLStreamAdapter::SetDtlsSrtpCryptoSuites( |
| 421 | const std::vector<int>& ciphers) { |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 422 | if (state_ != SSL_NONE) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 423 | return false; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 424 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 425 | |
Benjamin Wright | 8e98c60 | 2019-02-28 17:25:01 -0800 | [diff] [blame] | 426 | std::string internal_ciphers; |
| 427 | for (const int cipher : ciphers) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 428 | bool found = false; |
Benjamin Wright | 8e98c60 | 2019-02-28 17:25:01 -0800 | [diff] [blame] | 429 | for (const auto& entry : kSrtpCipherMap) { |
| 430 | if (cipher == entry.id) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 431 | found = true; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 432 | if (!internal_ciphers.empty()) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 433 | internal_ciphers += ":"; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 434 | } |
Benjamin Wright | 8e98c60 | 2019-02-28 17:25:01 -0800 | [diff] [blame] | 435 | internal_ciphers += entry.internal_name; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 436 | break; |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | if (!found) { |
Benjamin Wright | 8e98c60 | 2019-02-28 17:25:01 -0800 | [diff] [blame] | 441 | RTC_LOG(LS_ERROR) << "Could not find cipher: " << cipher; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 442 | return false; |
| 443 | } |
| 444 | } |
| 445 | |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 446 | if (internal_ciphers.empty()) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 447 | return false; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 448 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 449 | |
| 450 | srtp_ciphers_ = internal_ciphers; |
| 451 | return true; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 452 | } |
| 453 | |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 454 | bool OpenSSLStreamAdapter::GetDtlsSrtpCryptoSuite(int* crypto_suite) { |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 455 | RTC_DCHECK(state_ == SSL_CONNECTED); |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 456 | if (state_ != SSL_CONNECTED) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 457 | return false; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 458 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 459 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 460 | const SRTP_PROTECTION_PROFILE* srtp_profile = |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 461 | SSL_get_selected_srtp_profile(ssl_); |
| 462 | |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 463 | if (!srtp_profile) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 464 | return false; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 465 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 466 | |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 467 | *crypto_suite = srtp_profile->id; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 468 | RTC_DCHECK(!SrtpCryptoSuiteToName(*crypto_suite).empty()); |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 469 | return true; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 470 | } |
| 471 | |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 472 | bool OpenSSLStreamAdapter::IsTlsConnected() { |
| 473 | return state_ == SSL_CONNECTED; |
| 474 | } |
| 475 | |
Taylor Brandstetter | c8762a8 | 2016-08-11 12:01:49 -0700 | [diff] [blame] | 476 | int OpenSSLStreamAdapter::StartSSL() { |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 477 | // Don't allow StartSSL to be called twice. |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 478 | if (state_ != SSL_NONE) { |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 479 | return -1; |
| 480 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 481 | |
Taylor Brandstetter | c8762a8 | 2016-08-11 12:01:49 -0700 | [diff] [blame] | 482 | if (StreamAdapterInterface::GetState() != SS_OPEN) { |
| 483 | state_ = SSL_WAIT; |
| 484 | return 0; |
| 485 | } |
| 486 | |
| 487 | state_ = SSL_CONNECTING; |
| 488 | if (int err = BeginSSL()) { |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 489 | Error("BeginSSL", err, 0, false); |
Taylor Brandstetter | c8762a8 | 2016-08-11 12:01:49 -0700 | [diff] [blame] | 490 | return err; |
| 491 | } |
| 492 | |
| 493 | return 0; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | void OpenSSLStreamAdapter::SetMode(SSLMode mode) { |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 497 | RTC_DCHECK(state_ == SSL_NONE); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 498 | ssl_mode_ = mode; |
| 499 | } |
| 500 | |
Joachim Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 501 | void OpenSSLStreamAdapter::SetMaxProtocolVersion(SSLProtocolVersion version) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 502 | RTC_DCHECK(ssl_ctx_ == nullptr); |
Joachim Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 503 | ssl_max_version_ = version; |
| 504 | } |
| 505 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 506 | void OpenSSLStreamAdapter::SetInitialRetransmissionTimeout(int timeout_ms) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 507 | RTC_DCHECK(ssl_ctx_ == nullptr); |
skvlad | d030912 | 2017-02-02 17:18:37 -0800 | [diff] [blame] | 508 | dtls_handshake_timeout_ms_ = timeout_ms; |
| 509 | } |
| 510 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 511 | // |
| 512 | // StreamInterface Implementation |
| 513 | // |
| 514 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 515 | StreamResult OpenSSLStreamAdapter::Write(const void* data, |
| 516 | size_t data_len, |
| 517 | size_t* written, |
| 518 | int* error) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 519 | RTC_LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::Write(" << data_len << ")"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 520 | |
| 521 | switch (state_) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 522 | case SSL_NONE: |
| 523 | // pass-through in clear text |
| 524 | return StreamAdapterInterface::Write(data, data_len, written, error); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 525 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 526 | case SSL_WAIT: |
| 527 | case SSL_CONNECTING: |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 528 | return SR_BLOCK; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 529 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 530 | case SSL_CONNECTED: |
Benjamin Wright | 5d35554 | 2018-10-26 17:57:00 -0700 | [diff] [blame] | 531 | if (WaitingToVerifyPeerCertificate()) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 532 | return SR_BLOCK; |
| 533 | } |
| 534 | break; |
| 535 | |
| 536 | case SSL_ERROR: |
| 537 | case SSL_CLOSED: |
| 538 | default: |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 539 | if (error) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 540 | *error = ssl_error_code_; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 541 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 542 | return SR_ERROR; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | // OpenSSL will return an error if we try to write zero bytes |
| 546 | if (data_len == 0) { |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 547 | if (written) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 548 | *written = 0; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 549 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 550 | return SR_SUCCESS; |
| 551 | } |
| 552 | |
| 553 | ssl_write_needs_read_ = false; |
| 554 | |
henrike@webrtc.org | d89b69a | 2014-11-06 17:23:09 +0000 | [diff] [blame] | 555 | int code = SSL_write(ssl_, data, checked_cast<int>(data_len)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 556 | int ssl_error = SSL_get_error(ssl_, code); |
| 557 | switch (ssl_error) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 558 | case SSL_ERROR_NONE: |
| 559 | RTC_LOG(LS_VERBOSE) << " -- success"; |
| 560 | RTC_DCHECK_GT(code, 0); |
| 561 | RTC_DCHECK_LE(code, data_len); |
| 562 | if (written) |
| 563 | *written = code; |
| 564 | return SR_SUCCESS; |
| 565 | case SSL_ERROR_WANT_READ: |
| 566 | RTC_LOG(LS_VERBOSE) << " -- error want read"; |
| 567 | ssl_write_needs_read_ = true; |
| 568 | return SR_BLOCK; |
| 569 | case SSL_ERROR_WANT_WRITE: |
| 570 | RTC_LOG(LS_VERBOSE) << " -- error want write"; |
| 571 | return SR_BLOCK; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 572 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 573 | case SSL_ERROR_ZERO_RETURN: |
| 574 | default: |
| 575 | Error("SSL_write", (ssl_error ? ssl_error : -1), 0, false); |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 576 | if (error) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 577 | *error = ssl_error_code_; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 578 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 579 | return SR_ERROR; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 580 | } |
| 581 | // not reached |
| 582 | } |
| 583 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 584 | StreamResult OpenSSLStreamAdapter::Read(void* data, |
| 585 | size_t data_len, |
| 586 | size_t* read, |
| 587 | int* error) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 588 | RTC_LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::Read(" << data_len << ")"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 589 | switch (state_) { |
| 590 | case SSL_NONE: |
| 591 | // pass-through in clear text |
| 592 | return StreamAdapterInterface::Read(data, data_len, read, error); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 593 | case SSL_WAIT: |
| 594 | case SSL_CONNECTING: |
| 595 | return SR_BLOCK; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 596 | case SSL_CONNECTED: |
Benjamin Wright | 5d35554 | 2018-10-26 17:57:00 -0700 | [diff] [blame] | 597 | if (WaitingToVerifyPeerCertificate()) { |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 598 | return SR_BLOCK; |
| 599 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 600 | break; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 601 | case SSL_CLOSED: |
| 602 | return SR_EOS; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 603 | case SSL_ERROR: |
| 604 | default: |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 605 | if (error) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 606 | *error = ssl_error_code_; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 607 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 608 | return SR_ERROR; |
| 609 | } |
| 610 | |
| 611 | // Don't trust OpenSSL with zero byte reads |
| 612 | if (data_len == 0) { |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 613 | if (read) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 614 | *read = 0; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 615 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 616 | return SR_SUCCESS; |
| 617 | } |
| 618 | |
| 619 | ssl_read_needs_write_ = false; |
| 620 | |
Benjamin Wright | f54e30b | 2019-02-26 17:33:50 -0800 | [diff] [blame] | 621 | const int code = SSL_read(ssl_, data, checked_cast<int>(data_len)); |
| 622 | const int ssl_error = SSL_get_error(ssl_, code); |
| 623 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 624 | switch (ssl_error) { |
| 625 | case SSL_ERROR_NONE: |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 626 | RTC_LOG(LS_VERBOSE) << " -- success"; |
kwiberg | ee89e78 | 2017-08-09 17:22:01 -0700 | [diff] [blame] | 627 | RTC_DCHECK_GT(code, 0); |
| 628 | RTC_DCHECK_LE(code, data_len); |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 629 | if (read) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 630 | *read = code; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 631 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 632 | |
| 633 | if (ssl_mode_ == SSL_MODE_DTLS) { |
| 634 | // Enforce atomic reads -- this is a short read |
| 635 | unsigned int pending = SSL_pending(ssl_); |
| 636 | |
| 637 | if (pending) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 638 | RTC_LOG(LS_INFO) << " -- short DTLS read. flushing"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 639 | FlushInput(pending); |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 640 | if (error) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 641 | *error = SSE_MSG_TRUNC; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 642 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 643 | return SR_ERROR; |
| 644 | } |
| 645 | } |
| 646 | return SR_SUCCESS; |
| 647 | case SSL_ERROR_WANT_READ: |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 648 | RTC_LOG(LS_VERBOSE) << " -- error want read"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 649 | return SR_BLOCK; |
| 650 | case SSL_ERROR_WANT_WRITE: |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 651 | RTC_LOG(LS_VERBOSE) << " -- error want write"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 652 | ssl_read_needs_write_ = true; |
| 653 | return SR_BLOCK; |
| 654 | case SSL_ERROR_ZERO_RETURN: |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 655 | RTC_LOG(LS_VERBOSE) << " -- remote side closed"; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 656 | Close(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 657 | return SR_EOS; |
| 658 | break; |
| 659 | default: |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 660 | Error("SSL_read", (ssl_error ? ssl_error : -1), 0, false); |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 661 | if (error) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 662 | *error = ssl_error_code_; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 663 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 664 | return SR_ERROR; |
| 665 | } |
| 666 | // not reached |
| 667 | } |
| 668 | |
| 669 | void OpenSSLStreamAdapter::FlushInput(unsigned int left) { |
| 670 | unsigned char buf[2048]; |
| 671 | |
| 672 | while (left) { |
| 673 | // This should always succeed |
Benjamin Wright | f54e30b | 2019-02-26 17:33:50 -0800 | [diff] [blame] | 674 | const int toread = (sizeof(buf) < left) ? sizeof(buf) : left; |
| 675 | const int code = SSL_read(ssl_, buf, toread); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 676 | |
Benjamin Wright | f54e30b | 2019-02-26 17:33:50 -0800 | [diff] [blame] | 677 | const int ssl_error = SSL_get_error(ssl_, code); |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 678 | RTC_DCHECK(ssl_error == SSL_ERROR_NONE); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 679 | |
| 680 | if (ssl_error != SSL_ERROR_NONE) { |
Jonas Olsson | addc380 | 2018-02-01 09:53:06 +0100 | [diff] [blame] | 681 | RTC_DLOG(LS_VERBOSE) << " -- error " << code; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 682 | Error("SSL_read", (ssl_error ? ssl_error : -1), 0, false); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 683 | return; |
| 684 | } |
| 685 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 686 | RTC_LOG(LS_VERBOSE) << " -- flushed " << code << " bytes"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 687 | left -= code; |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | void OpenSSLStreamAdapter::Close() { |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 692 | Cleanup(0); |
| 693 | RTC_DCHECK(state_ == SSL_CLOSED || state_ == SSL_ERROR); |
| 694 | // When we're closed at SSL layer, also close the stream level which |
| 695 | // performs necessary clean up. Otherwise, a new incoming packet after |
| 696 | // this could overflow the stream buffer. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 697 | StreamAdapterInterface::Close(); |
| 698 | } |
| 699 | |
| 700 | StreamState OpenSSLStreamAdapter::GetState() const { |
| 701 | switch (state_) { |
| 702 | case SSL_WAIT: |
| 703 | case SSL_CONNECTING: |
| 704 | return SS_OPENING; |
| 705 | case SSL_CONNECTED: |
Benjamin Wright | 5d35554 | 2018-10-26 17:57:00 -0700 | [diff] [blame] | 706 | if (WaitingToVerifyPeerCertificate()) { |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 707 | return SS_OPENING; |
| 708 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 709 | return SS_OPEN; |
| 710 | default: |
| 711 | return SS_CLOSED; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 712 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 713 | // not reached |
| 714 | } |
| 715 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 716 | void OpenSSLStreamAdapter::OnEvent(StreamInterface* stream, |
| 717 | int events, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 718 | int err) { |
| 719 | int events_to_signal = 0; |
| 720 | int signal_error = 0; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 721 | RTC_DCHECK(stream == this->stream()); |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 722 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 723 | if ((events & SE_OPEN)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 724 | RTC_LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::OnEvent SE_OPEN"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 725 | if (state_ != SSL_WAIT) { |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 726 | RTC_DCHECK(state_ == SSL_NONE); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 727 | events_to_signal |= SE_OPEN; |
| 728 | } else { |
| 729 | state_ = SSL_CONNECTING; |
| 730 | if (int err = BeginSSL()) { |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 731 | Error("BeginSSL", err, 0, true); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 732 | return; |
| 733 | } |
| 734 | } |
| 735 | } |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 736 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 737 | if ((events & (SE_READ | SE_WRITE))) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 738 | RTC_LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::OnEvent" |
| 739 | << ((events & SE_READ) ? " SE_READ" : "") |
| 740 | << ((events & SE_WRITE) ? " SE_WRITE" : ""); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 741 | if (state_ == SSL_NONE) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 742 | events_to_signal |= events & (SE_READ | SE_WRITE); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 743 | } else if (state_ == SSL_CONNECTING) { |
| 744 | if (int err = ContinueSSL()) { |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 745 | Error("ContinueSSL", err, 0, true); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 746 | return; |
| 747 | } |
| 748 | } else if (state_ == SSL_CONNECTED) { |
| 749 | if (((events & SE_READ) && ssl_write_needs_read_) || |
| 750 | (events & SE_WRITE)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 751 | RTC_LOG(LS_VERBOSE) << " -- onStreamWriteable"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 752 | events_to_signal |= SE_WRITE; |
| 753 | } |
| 754 | if (((events & SE_WRITE) && ssl_read_needs_write_) || |
| 755 | (events & SE_READ)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 756 | RTC_LOG(LS_VERBOSE) << " -- onStreamReadable"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 757 | events_to_signal |= SE_READ; |
| 758 | } |
| 759 | } |
| 760 | } |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 761 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 762 | if ((events & SE_CLOSE)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 763 | RTC_LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::OnEvent(SE_CLOSE, " << err |
| 764 | << ")"; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 765 | Cleanup(0); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 766 | events_to_signal |= SE_CLOSE; |
| 767 | // SE_CLOSE is the only event that uses the final parameter to OnEvent(). |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 768 | RTC_DCHECK(signal_error == 0); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 769 | signal_error = err; |
| 770 | } |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 771 | |
| 772 | if (events_to_signal) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 773 | StreamAdapterInterface::OnEvent(stream, events_to_signal, signal_error); |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 774 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 775 | } |
| 776 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 777 | int OpenSSLStreamAdapter::BeginSSL() { |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 778 | RTC_DCHECK(state_ == SSL_CONNECTING); |
Taylor Brandstetter | c8762a8 | 2016-08-11 12:01:49 -0700 | [diff] [blame] | 779 | // The underlying stream has opened. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 780 | RTC_LOG(LS_INFO) << "BeginSSL with peer."; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 781 | |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 782 | BIO* bio = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 783 | |
Taylor Brandstetter | c8762a8 | 2016-08-11 12:01:49 -0700 | [diff] [blame] | 784 | // First set up the context. |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 785 | RTC_DCHECK(ssl_ctx_ == nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 786 | ssl_ctx_ = SetupSSLContext(); |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 787 | if (!ssl_ctx_) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 788 | return -1; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 789 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 790 | |
| 791 | bio = BIO_new_stream(static_cast<StreamInterface*>(stream())); |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 792 | if (!bio) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 793 | return -1; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 794 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 795 | |
| 796 | ssl_ = SSL_new(ssl_ctx_); |
| 797 | if (!ssl_) { |
| 798 | BIO_free(bio); |
| 799 | return -1; |
| 800 | } |
| 801 | |
| 802 | SSL_set_app_data(ssl_, this); |
| 803 | |
| 804 | 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] | 805 | if (ssl_mode_ == SSL_MODE_DTLS) { |
Taylor Brandstetter | 4f0dfbd | 2016-06-15 17:15:23 -0700 | [diff] [blame] | 806 | #ifdef OPENSSL_IS_BORINGSSL |
skvlad | d030912 | 2017-02-02 17:18:37 -0800 | [diff] [blame] | 807 | DTLSv1_set_initial_timeout_duration(ssl_, dtls_handshake_timeout_ms_); |
Taylor Brandstetter | 4f0dfbd | 2016-06-15 17:15:23 -0700 | [diff] [blame] | 808 | #else |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 809 | // Enable read-ahead for DTLS so whole packets are read from internal BIO |
| 810 | // before parsing. This is done internally by BoringSSL for DTLS. |
| 811 | SSL_set_read_ahead(ssl_, 1); |
philipel | 49c0869 | 2016-05-24 01:49:43 -0700 | [diff] [blame] | 812 | #endif |
Taylor Brandstetter | 4f0dfbd | 2016-06-15 17:15:23 -0700 | [diff] [blame] | 813 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 814 | |
| 815 | SSL_set_mode(ssl_, SSL_MODE_ENABLE_PARTIAL_WRITE | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 816 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 817 | |
| 818 | // Do the connect |
| 819 | return ContinueSSL(); |
| 820 | } |
| 821 | |
| 822 | int OpenSSLStreamAdapter::ContinueSSL() { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 823 | RTC_LOG(LS_VERBOSE) << "ContinueSSL"; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 824 | RTC_DCHECK(state_ == SSL_CONNECTING); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 825 | |
| 826 | // Clear the DTLS timer |
| 827 | Thread::Current()->Clear(this, MSG_TIMEOUT); |
| 828 | |
Benjamin Wright | f54e30b | 2019-02-26 17:33:50 -0800 | [diff] [blame] | 829 | const int code = (role_ == SSL_CLIENT) ? SSL_connect(ssl_) : SSL_accept(ssl_); |
| 830 | const int ssl_error = SSL_get_error(ssl_, code); |
| 831 | |
| 832 | switch (ssl_error) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 833 | case SSL_ERROR_NONE: |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 834 | RTC_LOG(LS_VERBOSE) << " -- success"; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 835 | // By this point, OpenSSL should have given us a certificate, or errored |
| 836 | // out if one was missing. |
Benjamin Wright | b19b497 | 2018-10-25 10:46:49 -0700 | [diff] [blame] | 837 | RTC_DCHECK(peer_cert_chain_ || !GetClientAuthEnabled()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 838 | |
| 839 | state_ = SSL_CONNECTED; |
Benjamin Wright | 5d35554 | 2018-10-26 17:57:00 -0700 | [diff] [blame] | 840 | if (!WaitingToVerifyPeerCertificate()) { |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 841 | // We have everything we need to start the connection, so signal |
| 842 | // SE_OPEN. If we need a client certificate fingerprint and don't have |
| 843 | // it yet, we'll instead signal SE_OPEN in SetPeerCertificateDigest. |
| 844 | // |
Taylor Brandstetter | fe69a74 | 2016-09-30 17:34:32 -0700 | [diff] [blame] | 845 | // TODO(deadbeef): Post this event asynchronously to unwind the stack. |
| 846 | // The caller of ContinueSSL may be the same object listening for these |
| 847 | // events and may not be prepared for reentrancy. |
| 848 | // PostEvent(SE_OPEN | SE_READ | SE_WRITE, 0); |
| 849 | StreamAdapterInterface::OnEvent(stream(), SE_OPEN | SE_READ | SE_WRITE, |
| 850 | 0); |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 851 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 852 | break; |
| 853 | |
| 854 | case SSL_ERROR_WANT_READ: { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 855 | RTC_LOG(LS_VERBOSE) << " -- error want read"; |
| 856 | struct timeval timeout; |
| 857 | if (DTLSv1_get_timeout(ssl_, &timeout)) { |
| 858 | int delay = timeout.tv_sec * 1000 + timeout.tv_usec / 1000; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 859 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 860 | Thread::Current()->PostDelayed(RTC_FROM_HERE, delay, this, MSG_TIMEOUT, |
| 861 | 0); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 862 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 863 | } break; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 864 | |
| 865 | case SSL_ERROR_WANT_WRITE: |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 866 | RTC_LOG(LS_VERBOSE) << " -- error want write"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 867 | break; |
| 868 | |
| 869 | case SSL_ERROR_ZERO_RETURN: |
| 870 | default: |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 871 | RTC_LOG(LS_VERBOSE) << " -- error " << code; |
zhihuang | d82eee0 | 2016-08-26 11:25:05 -0700 | [diff] [blame] | 872 | SSLHandshakeError ssl_handshake_err = SSLHandshakeError::UNKNOWN; |
| 873 | int err_code = ERR_peek_last_error(); |
| 874 | if (err_code != 0 && ERR_GET_REASON(err_code) == SSL_R_NO_SHARED_CIPHER) { |
| 875 | ssl_handshake_err = SSLHandshakeError::INCOMPATIBLE_CIPHERSUITE; |
| 876 | } |
| 877 | SignalSSLHandshakeError(ssl_handshake_err); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 878 | return (ssl_error != 0) ? ssl_error : -1; |
| 879 | } |
| 880 | |
| 881 | return 0; |
| 882 | } |
| 883 | |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 884 | void OpenSSLStreamAdapter::Error(const char* context, |
| 885 | int err, |
| 886 | uint8_t alert, |
| 887 | bool signal) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 888 | RTC_LOG(LS_WARNING) << "OpenSSLStreamAdapter::Error(" << context << ", " |
| 889 | << err << ", " << static_cast<int>(alert) << ")"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 890 | state_ = SSL_ERROR; |
| 891 | ssl_error_code_ = err; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 892 | Cleanup(alert); |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 893 | if (signal) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 894 | StreamAdapterInterface::OnEvent(stream(), SE_CLOSE, err); |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 895 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 896 | } |
| 897 | |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 898 | void OpenSSLStreamAdapter::Cleanup(uint8_t alert) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 899 | RTC_LOG(LS_INFO) << "Cleanup"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 900 | |
| 901 | if (state_ != SSL_ERROR) { |
| 902 | state_ = SSL_CLOSED; |
| 903 | ssl_error_code_ = 0; |
| 904 | } |
| 905 | |
| 906 | if (ssl_) { |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 907 | int ret; |
| 908 | // SSL_send_fatal_alert is only available in BoringSSL. |
| 909 | #ifdef OPENSSL_IS_BORINGSSL |
| 910 | if (alert) { |
| 911 | ret = SSL_send_fatal_alert(ssl_, alert); |
| 912 | if (ret < 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 913 | RTC_LOG(LS_WARNING) << "SSL_send_fatal_alert failed, error = " |
| 914 | << SSL_get_error(ssl_, ret); |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 915 | } |
| 916 | } else { |
| 917 | #endif |
| 918 | ret = SSL_shutdown(ssl_); |
| 919 | if (ret < 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 920 | RTC_LOG(LS_WARNING) |
| 921 | << "SSL_shutdown failed, error = " << SSL_get_error(ssl_, ret); |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 922 | } |
| 923 | #ifdef OPENSSL_IS_BORINGSSL |
jiayl@webrtc.org | f1d751c | 2014-09-25 16:38:46 +0000 | [diff] [blame] | 924 | } |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 925 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 926 | SSL_free(ssl_); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 927 | ssl_ = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 928 | } |
| 929 | if (ssl_ctx_) { |
| 930 | SSL_CTX_free(ssl_ctx_); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 931 | ssl_ctx_ = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 932 | } |
| 933 | identity_.reset(); |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 934 | peer_cert_chain_.reset(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 935 | |
| 936 | // Clear the DTLS timer |
| 937 | Thread::Current()->Clear(this, MSG_TIMEOUT); |
| 938 | } |
| 939 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 940 | void OpenSSLStreamAdapter::OnMessage(Message* msg) { |
| 941 | // Process our own messages and then pass others to the superclass |
| 942 | if (MSG_TIMEOUT == msg->message_id) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 943 | RTC_LOG(LS_INFO) << "DTLS timeout expired"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 944 | DTLSv1_handle_timeout(ssl_); |
| 945 | ContinueSSL(); |
| 946 | } else { |
| 947 | StreamInterface::OnMessage(msg); |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | SSL_CTX* OpenSSLStreamAdapter::SetupSSLContext() { |
David Benjamin | 170a4b3 | 2019-01-30 09:46:16 -0600 | [diff] [blame] | 952 | SSL_CTX* ctx = |
| 953 | SSL_CTX_new(ssl_mode_ == SSL_MODE_DTLS ? DTLS_method() : TLS_method()); |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 954 | if (ctx == nullptr) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 955 | return nullptr; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 956 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 957 | |
Benjamin Wright | 7276b97 | 2019-03-06 11:51:34 -0800 | [diff] [blame] | 958 | if (support_legacy_tls_protocols_flag_) { |
| 959 | // TODO(https://bugs.webrtc.org/10261): Completely remove this branch in |
| 960 | // M75. |
| 961 | SSL_CTX_set_min_proto_version( |
| 962 | ctx, ssl_mode_ == SSL_MODE_DTLS ? DTLS1_VERSION : TLS1_VERSION); |
| 963 | switch (ssl_max_version_) { |
| 964 | case SSL_PROTOCOL_TLS_10: |
| 965 | SSL_CTX_set_max_proto_version( |
| 966 | ctx, ssl_mode_ == SSL_MODE_DTLS ? DTLS1_VERSION : TLS1_VERSION); |
| 967 | break; |
| 968 | case SSL_PROTOCOL_TLS_11: |
| 969 | SSL_CTX_set_max_proto_version( |
| 970 | ctx, ssl_mode_ == SSL_MODE_DTLS ? DTLS1_VERSION : TLS1_1_VERSION); |
| 971 | break; |
| 972 | case SSL_PROTOCOL_TLS_12: |
| 973 | default: |
| 974 | SSL_CTX_set_max_proto_version( |
| 975 | ctx, ssl_mode_ == SSL_MODE_DTLS ? DTLS1_2_VERSION : TLS1_2_VERSION); |
| 976 | break; |
| 977 | } |
| 978 | } else { |
| 979 | // TODO(https://bugs.webrtc.org/10261): Make this the default in M75. |
| 980 | SSL_CTX_set_min_proto_version( |
| 981 | ctx, ssl_mode_ == SSL_MODE_DTLS ? DTLS1_2_VERSION : TLS1_2_VERSION); |
| 982 | SSL_CTX_set_max_proto_version( |
| 983 | ctx, ssl_mode_ == SSL_MODE_DTLS ? DTLS1_2_VERSION : TLS1_2_VERSION); |
Joachim Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 984 | } |
Benjamin Wright | 7276b97 | 2019-03-06 11:51:34 -0800 | [diff] [blame] | 985 | |
David Benjamin | 170a4b3 | 2019-01-30 09:46:16 -0600 | [diff] [blame] | 986 | #ifdef OPENSSL_IS_BORINGSSL |
| 987 | // SSL_CTX_set_current_time_cb is only supported in BoringSSL. |
deadbeef | 6cf94a0 | 2016-11-28 17:38:34 -0800 | [diff] [blame] | 988 | if (g_use_time_callback_for_testing) { |
| 989 | SSL_CTX_set_current_time_cb(ctx, &TimeCallbackForTesting); |
| 990 | } |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 991 | #endif |
Joachim Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 992 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 993 | if (identity_ && !identity_->ConfigureIdentity(ctx)) { |
| 994 | SSL_CTX_free(ctx); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 995 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 996 | } |
| 997 | |
tfarina | a41ab93 | 2015-10-30 16:08:48 -0700 | [diff] [blame] | 998 | #if !defined(NDEBUG) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 999 | SSL_CTX_set_info_callback(ctx, OpenSSLAdapter::SSLInfoCallback); |
| 1000 | #endif |
| 1001 | |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 1002 | int mode = SSL_VERIFY_PEER; |
Benjamin Wright | b19b497 | 2018-10-25 10:46:49 -0700 | [diff] [blame] | 1003 | if (GetClientAuthEnabled()) { |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 1004 | // Require a certificate from the client. |
| 1005 | // Note: Normally this is always true in production, but it may be disabled |
| 1006 | // for testing purposes (e.g. SSLAdapter unit tests). |
| 1007 | mode |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 1008 | } |
| 1009 | |
David Benjamin | dc24656 | 2017-09-29 12:14:08 -0400 | [diff] [blame] | 1010 | // Configure a custom certificate verification callback to check the peer |
| 1011 | // certificate digest. Note the second argument to SSL_CTX_set_verify is to |
| 1012 | // override individual errors in the default verification logic, which is not |
| 1013 | // what we want here. |
| 1014 | SSL_CTX_set_verify(ctx, mode, nullptr); |
| 1015 | SSL_CTX_set_cert_verify_callback(ctx, SSLVerifyCallback, nullptr); |
| 1016 | |
Joachim Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 1017 | // Select list of available ciphers. Note that !SHA256 and !SHA384 only |
| 1018 | // remove HMAC-SHA256 and HMAC-SHA384 cipher suites, not GCM cipher suites |
| 1019 | // with SHA256 or SHA384 as the handshake hash. |
| 1020 | // This matches the list of SSLClientSocketOpenSSL in Chromium. |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 1021 | SSL_CTX_set_cipher_list( |
| 1022 | ctx, "DEFAULT:!NULL:!aNULL:!SHA256:!SHA384:!aECDH:!AESGCM+AES256:!aPSK"); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1023 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1024 | if (!srtp_ciphers_.empty()) { |
| 1025 | if (SSL_CTX_set_tlsext_use_srtp(ctx, srtp_ciphers_.c_str())) { |
| 1026 | SSL_CTX_free(ctx); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 1027 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1028 | } |
| 1029 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1030 | |
| 1031 | return ctx; |
| 1032 | } |
| 1033 | |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 1034 | bool OpenSSLStreamAdapter::VerifyPeerCertificate() { |
Benjamin Wright | 5d35554 | 2018-10-26 17:57:00 -0700 | [diff] [blame] | 1035 | if (!HasPeerCertificateDigest() || !peer_cert_chain_ || |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 1036 | !peer_cert_chain_->GetSize()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1037 | RTC_LOG(LS_WARNING) << "Missing digest or peer certificate."; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 1038 | return false; |
| 1039 | } |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 1040 | const OpenSSLCertificate* leaf_cert = |
| 1041 | static_cast<const OpenSSLCertificate*>(&peer_cert_chain_->Get(0)); |
deadbeef | 81f6f4f | 2016-09-19 17:20:52 -0700 | [diff] [blame] | 1042 | |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 1043 | unsigned char digest[EVP_MAX_MD_SIZE]; |
| 1044 | size_t digest_length; |
| 1045 | if (!OpenSSLCertificate::ComputeDigest( |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 1046 | leaf_cert->x509(), peer_certificate_digest_algorithm_, digest, |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 1047 | sizeof(digest), &digest_length)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1048 | RTC_LOG(LS_WARNING) << "Failed to compute peer cert digest."; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 1049 | return false; |
| 1050 | } |
| 1051 | |
| 1052 | Buffer computed_digest(digest, digest_length); |
| 1053 | if (computed_digest != peer_certificate_digest_value_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1054 | RTC_LOG(LS_WARNING) |
| 1055 | << "Rejected peer certificate due to mismatched digest."; |
jbauch | f8f457b | 2017-03-09 16:24:57 -0800 | [diff] [blame] | 1056 | return false; |
deadbeef | 81f6f4f | 2016-09-19 17:20:52 -0700 | [diff] [blame] | 1057 | } |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 1058 | // Ignore any verification error if the digest matches, since there is no |
| 1059 | // value in checking the validity of a self-signed cert issued by untrusted |
| 1060 | // sources. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1061 | RTC_LOG(LS_INFO) << "Accepted peer certificate."; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 1062 | peer_certificate_verified_ = true; |
| 1063 | return true; |
| 1064 | } |
| 1065 | |
Jian Cui | 0a8798b | 2017-11-16 16:58:02 -0800 | [diff] [blame] | 1066 | std::unique_ptr<SSLCertChain> OpenSSLStreamAdapter::GetPeerSSLCertChain() |
| 1067 | const { |
Steve Anton | f25303e | 2018-10-16 15:23:31 -0700 | [diff] [blame] | 1068 | return peer_cert_chain_ ? peer_cert_chain_->Clone() : nullptr; |
Jian Cui | 0a8798b | 2017-11-16 16:58:02 -0800 | [diff] [blame] | 1069 | } |
| 1070 | |
David Benjamin | dc24656 | 2017-09-29 12:14:08 -0400 | [diff] [blame] | 1071 | int OpenSSLStreamAdapter::SSLVerifyCallback(X509_STORE_CTX* store, void* arg) { |
| 1072 | // Get our SSL structure and OpenSSLStreamAdapter from the store. |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 1073 | SSL* ssl = reinterpret_cast<SSL*>( |
| 1074 | 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] | 1075 | OpenSSLStreamAdapter* stream = |
| 1076 | reinterpret_cast<OpenSSLStreamAdapter*>(SSL_get_app_data(ssl)); |
henrike@webrtc.org | 4e5f65a | 2014-06-05 20:40:11 +0000 | [diff] [blame] | 1077 | |
Jian Cui | 0a8798b | 2017-11-16 16:58:02 -0800 | [diff] [blame] | 1078 | #if defined(OPENSSL_IS_BORINGSSL) |
| 1079 | STACK_OF(X509)* chain = SSL_get_peer_full_cert_chain(ssl); |
Jian Cui | 0a8798b | 2017-11-16 16:58:02 -0800 | [diff] [blame] | 1080 | // Creates certificate chain. |
| 1081 | std::vector<std::unique_ptr<SSLCertificate>> cert_chain; |
| 1082 | for (X509* cert : chain) { |
| 1083 | cert_chain.emplace_back(new OpenSSLCertificate(cert)); |
| 1084 | } |
| 1085 | stream->peer_cert_chain_.reset(new SSLCertChain(std::move(cert_chain))); |
| 1086 | #else |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1087 | // Record the peer's certificate. |
Jiawei Ou | 9d4e840 | 2018-05-23 15:44:20 -0700 | [diff] [blame] | 1088 | X509* cert = X509_STORE_CTX_get0_cert(store); |
Taylor Brandstetter | c392866 | 2018-02-23 13:04:51 -0800 | [diff] [blame] | 1089 | stream->peer_cert_chain_.reset( |
Sergey Sablin | 3c119fb | 2019-02-12 18:30:45 -0800 | [diff] [blame] | 1090 | new SSLCertChain(absl::make_unique<OpenSSLCertificate>(cert))); |
Jian Cui | 0a8798b | 2017-11-16 16:58:02 -0800 | [diff] [blame] | 1091 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1092 | |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 1093 | // If the peer certificate digest isn't known yet, we'll wait to verify |
| 1094 | // until it's known, and for now just return a success status. |
| 1095 | if (stream->peer_certificate_digest_algorithm_.empty()) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1096 | RTC_LOG(LS_INFO) << "Waiting to verify certificate until digest is known."; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 1097 | return 1; |
| 1098 | } |
| 1099 | |
David Benjamin | dc24656 | 2017-09-29 12:14:08 -0400 | [diff] [blame] | 1100 | if (!stream->VerifyPeerCertificate()) { |
| 1101 | X509_STORE_CTX_set_error(store, X509_V_ERR_CERT_REJECTED); |
| 1102 | return 0; |
| 1103 | } |
| 1104 | |
| 1105 | return 1; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1106 | } |
| 1107 | |
Taylor Brandstetter | 4f0dfbd | 2016-06-15 17:15:23 -0700 | [diff] [blame] | 1108 | bool OpenSSLStreamAdapter::IsBoringSsl() { |
| 1109 | #ifdef OPENSSL_IS_BORINGSSL |
| 1110 | return true; |
| 1111 | #else |
| 1112 | return false; |
| 1113 | #endif |
| 1114 | } |
| 1115 | |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1116 | #define CDEF(X) \ |
| 1117 | { static_cast<uint16_t>(TLS1_CK_##X & 0xffff), "TLS_" #X } |
| 1118 | |
| 1119 | struct cipher_list { |
| 1120 | uint16_t cipher; |
| 1121 | const char* cipher_str; |
| 1122 | }; |
| 1123 | |
| 1124 | // TODO(torbjorng): Perhaps add more cipher suites to these lists. |
| 1125 | static const cipher_list OK_RSA_ciphers[] = { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1126 | CDEF(ECDHE_RSA_WITH_AES_128_CBC_SHA), |
| 1127 | CDEF(ECDHE_RSA_WITH_AES_256_CBC_SHA), |
| 1128 | CDEF(ECDHE_RSA_WITH_AES_128_GCM_SHA256), |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1129 | #ifdef TLS1_CK_ECDHE_RSA_WITH_AES_256_GCM_SHA256 |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1130 | CDEF(ECDHE_RSA_WITH_AES_256_GCM_SHA256), |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1131 | #endif |
torbjorng | aad6780 | 2016-04-07 08:55:28 -0700 | [diff] [blame] | 1132 | #ifdef TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1133 | CDEF(ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256), |
torbjorng | aad6780 | 2016-04-07 08:55:28 -0700 | [diff] [blame] | 1134 | #endif |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1135 | }; |
| 1136 | |
| 1137 | static const cipher_list OK_ECDSA_ciphers[] = { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1138 | CDEF(ECDHE_ECDSA_WITH_AES_128_CBC_SHA), |
| 1139 | CDEF(ECDHE_ECDSA_WITH_AES_256_CBC_SHA), |
| 1140 | CDEF(ECDHE_ECDSA_WITH_AES_128_GCM_SHA256), |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1141 | #ifdef TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA256 |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1142 | CDEF(ECDHE_ECDSA_WITH_AES_256_GCM_SHA256), |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1143 | #endif |
torbjorng | aad6780 | 2016-04-07 08:55:28 -0700 | [diff] [blame] | 1144 | #ifdef TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 1145 | CDEF(ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256), |
torbjorng | aad6780 | 2016-04-07 08:55:28 -0700 | [diff] [blame] | 1146 | #endif |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1147 | }; |
| 1148 | #undef CDEF |
| 1149 | |
| 1150 | bool OpenSSLStreamAdapter::IsAcceptableCipher(int cipher, KeyType key_type) { |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 1151 | if (key_type == KT_RSA) { |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1152 | for (const cipher_list& c : OK_RSA_ciphers) { |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 1153 | if (cipher == c.cipher) { |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1154 | return true; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 1155 | } |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 1156 | } |
Joachim Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 1157 | } |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1158 | |
| 1159 | if (key_type == KT_ECDSA) { |
| 1160 | for (const cipher_list& c : OK_ECDSA_ciphers) { |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 1161 | if (cipher == c.cipher) { |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1162 | return true; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 1163 | } |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1164 | } |
| 1165 | } |
| 1166 | |
| 1167 | return false; |
| 1168 | } |
| 1169 | |
| 1170 | bool OpenSSLStreamAdapter::IsAcceptableCipher(const std::string& cipher, |
| 1171 | KeyType key_type) { |
| 1172 | if (key_type == KT_RSA) { |
| 1173 | for (const cipher_list& c : OK_RSA_ciphers) { |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 1174 | if (cipher == c.cipher_str) { |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1175 | return true; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 1176 | } |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1177 | } |
| 1178 | } |
| 1179 | |
| 1180 | if (key_type == KT_ECDSA) { |
| 1181 | for (const cipher_list& c : OK_ECDSA_ciphers) { |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 1182 | if (cipher == c.cipher_str) { |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1183 | return true; |
Benjamin Wright | d4d5f8a | 2018-10-15 11:19:39 -0700 | [diff] [blame] | 1184 | } |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1185 | } |
| 1186 | } |
| 1187 | |
| 1188 | return false; |
pthatcher@webrtc.org | 3ee4fe5 | 2015-02-11 22:34:36 +0000 | [diff] [blame] | 1189 | } |
| 1190 | |
Benjamin Wright | b19b497 | 2018-10-25 10:46:49 -0700 | [diff] [blame] | 1191 | void OpenSSLStreamAdapter::EnableTimeCallbackForTesting() { |
deadbeef | 6cf94a0 | 2016-11-28 17:38:34 -0800 | [diff] [blame] | 1192 | g_use_time_callback_for_testing = true; |
| 1193 | } |
| 1194 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1195 | } // namespace rtc |