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