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