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