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