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" |
| 29 | #include "rtc_base/openssl.h" |
| 30 | #include "rtc_base/openssladapter.h" |
| 31 | #include "rtc_base/openssldigest.h" |
| 32 | #include "rtc_base/opensslidentity.h" |
| 33 | #include "rtc_base/safe_conversions.h" |
| 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 | |
deadbeef | 1b54a5f | 2017-01-23 19:39:57 -0800 | [diff] [blame] | 45 | #if (OPENSSL_VERSION_NUMBER < 0x10001000L) |
| 46 | #error "webrtc requires at least OpenSSL version 1.0.1, 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 | |
| 83 | // There currently is no method available to get a RFC-compliant name for a |
| 84 | // cipher suite from BoringSSL, so we need to define the mapping manually here. |
| 85 | // This should go away once BoringSSL supports "SSL_CIPHER_standard_name" |
| 86 | // (as available in OpenSSL if compiled with tracing enabled) or a similar |
| 87 | // method. |
| 88 | static const SslCipherMapEntry kSslCipherMap[] = { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 89 | // TLS v1.0 ciphersuites from RFC2246. |
| 90 | DEFINE_CIPHER_ENTRY_SSL3(RSA_RC4_128_SHA), |
| 91 | {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] | 92 | |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 93 | // AES ciphersuites from RFC3268. |
| 94 | {TLS1_CK_RSA_WITH_AES_128_SHA, "TLS_RSA_WITH_AES_128_CBC_SHA"}, |
| 95 | {TLS1_CK_DHE_RSA_WITH_AES_128_SHA, "TLS_DHE_RSA_WITH_AES_128_CBC_SHA"}, |
| 96 | {TLS1_CK_RSA_WITH_AES_256_SHA, "TLS_RSA_WITH_AES_256_CBC_SHA"}, |
| 97 | {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] | 98 | |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 99 | // ECC ciphersuites from RFC4492. |
| 100 | DEFINE_CIPHER_ENTRY_TLS1(ECDHE_ECDSA_WITH_RC4_128_SHA), |
| 101 | {TLS1_CK_ECDHE_ECDSA_WITH_DES_192_CBC3_SHA, |
| 102 | "TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA"}, |
| 103 | DEFINE_CIPHER_ENTRY_TLS1(ECDHE_ECDSA_WITH_AES_128_CBC_SHA), |
| 104 | DEFINE_CIPHER_ENTRY_TLS1(ECDHE_ECDSA_WITH_AES_256_CBC_SHA), |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 105 | |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 106 | DEFINE_CIPHER_ENTRY_TLS1(ECDHE_RSA_WITH_RC4_128_SHA), |
| 107 | {TLS1_CK_ECDHE_RSA_WITH_DES_192_CBC3_SHA, |
| 108 | "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA"}, |
| 109 | DEFINE_CIPHER_ENTRY_TLS1(ECDHE_RSA_WITH_AES_128_CBC_SHA), |
| 110 | DEFINE_CIPHER_ENTRY_TLS1(ECDHE_RSA_WITH_AES_256_CBC_SHA), |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 111 | |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 112 | // TLS v1.2 ciphersuites. |
| 113 | {TLS1_CK_RSA_WITH_AES_128_SHA256, "TLS_RSA_WITH_AES_128_CBC_SHA256"}, |
| 114 | {TLS1_CK_RSA_WITH_AES_256_SHA256, "TLS_RSA_WITH_AES_256_CBC_SHA256"}, |
| 115 | {TLS1_CK_DHE_RSA_WITH_AES_128_SHA256, |
| 116 | "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256"}, |
| 117 | {TLS1_CK_DHE_RSA_WITH_AES_256_SHA256, |
| 118 | "TLS_DHE_RSA_WITH_AES_256_CBC_SHA256"}, |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 119 | |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 120 | // TLS v1.2 GCM ciphersuites from RFC5288. |
| 121 | DEFINE_CIPHER_ENTRY_TLS1(RSA_WITH_AES_128_GCM_SHA256), |
| 122 | DEFINE_CIPHER_ENTRY_TLS1(RSA_WITH_AES_256_GCM_SHA384), |
| 123 | DEFINE_CIPHER_ENTRY_TLS1(DHE_RSA_WITH_AES_128_GCM_SHA256), |
| 124 | DEFINE_CIPHER_ENTRY_TLS1(DHE_RSA_WITH_AES_256_GCM_SHA384), |
| 125 | DEFINE_CIPHER_ENTRY_TLS1(DH_RSA_WITH_AES_128_GCM_SHA256), |
| 126 | DEFINE_CIPHER_ENTRY_TLS1(DH_RSA_WITH_AES_256_GCM_SHA384), |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 127 | |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 128 | // ECDH HMAC based ciphersuites from RFC5289. |
| 129 | {TLS1_CK_ECDHE_ECDSA_WITH_AES_128_SHA256, |
| 130 | "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256"}, |
| 131 | {TLS1_CK_ECDHE_ECDSA_WITH_AES_256_SHA384, |
| 132 | "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384"}, |
| 133 | {TLS1_CK_ECDHE_RSA_WITH_AES_128_SHA256, |
| 134 | "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256"}, |
| 135 | {TLS1_CK_ECDHE_RSA_WITH_AES_256_SHA384, |
| 136 | "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384"}, |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 137 | |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 138 | // ECDH GCM based ciphersuites from RFC5289. |
| 139 | DEFINE_CIPHER_ENTRY_TLS1(ECDHE_ECDSA_WITH_AES_128_GCM_SHA256), |
| 140 | DEFINE_CIPHER_ENTRY_TLS1(ECDHE_ECDSA_WITH_AES_256_GCM_SHA384), |
| 141 | DEFINE_CIPHER_ENTRY_TLS1(ECDHE_RSA_WITH_AES_128_GCM_SHA256), |
| 142 | DEFINE_CIPHER_ENTRY_TLS1(ECDHE_RSA_WITH_AES_256_GCM_SHA384), |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 143 | |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 144 | {0, nullptr}}; |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 145 | #endif // #ifndef OPENSSL_IS_BORINGSSL |
pthatcher@webrtc.org | 3ee4fe5 | 2015-02-11 22:34:36 +0000 | [diff] [blame] | 146 | |
Guo-wei Shieh | 456696a | 2015-09-30 21:48:54 -0700 | [diff] [blame] | 147 | #if defined(_MSC_VER) |
| 148 | #pragma warning(push) |
| 149 | #pragma warning(disable : 4309) |
| 150 | #pragma warning(disable : 4310) |
| 151 | #endif // defined(_MSC_VER) |
| 152 | |
Guo-wei Shieh | 456696a | 2015-09-30 21:48:54 -0700 | [diff] [blame] | 153 | #if defined(_MSC_VER) |
| 154 | #pragma warning(pop) |
| 155 | #endif // defined(_MSC_VER) |
| 156 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 157 | ////////////////////////////////////////////////////////////////////// |
| 158 | // StreamBIO |
| 159 | ////////////////////////////////////////////////////////////////////// |
| 160 | |
| 161 | static int stream_write(BIO* h, const char* buf, int num); |
| 162 | static int stream_read(BIO* h, char* buf, int size); |
| 163 | static int stream_puts(BIO* h, const char* str); |
| 164 | static long stream_ctrl(BIO* h, int cmd, long arg1, void* arg2); |
| 165 | static int stream_new(BIO* h); |
| 166 | static int stream_free(BIO* data); |
| 167 | |
David Benjamin | 85aa0b6 | 2017-09-28 16:42:58 -0400 | [diff] [blame] | 168 | static const BIO_METHOD methods_stream = { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 169 | BIO_TYPE_BIO, "stream", stream_write, stream_read, stream_puts, 0, |
| 170 | stream_ctrl, stream_new, stream_free, nullptr, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 171 | }; |
| 172 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 173 | static BIO* BIO_new_stream(StreamInterface* stream) { |
David Benjamin | 85aa0b6 | 2017-09-28 16:42:58 -0400 | [diff] [blame] | 174 | // TODO(davidben): Remove the const_cast when BoringSSL is assumed. |
| 175 | BIO* ret = BIO_new(const_cast<BIO_METHOD*>(&methods_stream)); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 176 | if (ret == nullptr) |
| 177 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 178 | ret->ptr = stream; |
| 179 | return ret; |
| 180 | } |
| 181 | |
| 182 | // bio methods return 1 (or at least non-zero) on success and 0 on failure. |
| 183 | |
| 184 | static int stream_new(BIO* b) { |
| 185 | b->shutdown = 0; |
| 186 | b->init = 1; |
| 187 | b->num = 0; // 1 means end-of-stream |
| 188 | b->ptr = 0; |
| 189 | return 1; |
| 190 | } |
| 191 | |
| 192 | static int stream_free(BIO* b) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 193 | if (b == nullptr) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 194 | return 0; |
| 195 | return 1; |
| 196 | } |
| 197 | |
| 198 | static int stream_read(BIO* b, char* out, int outl) { |
| 199 | if (!out) |
| 200 | return -1; |
| 201 | StreamInterface* stream = static_cast<StreamInterface*>(b->ptr); |
| 202 | BIO_clear_retry_flags(b); |
| 203 | size_t read; |
| 204 | int error; |
| 205 | StreamResult result = stream->Read(out, outl, &read, &error); |
| 206 | if (result == SR_SUCCESS) { |
henrike@webrtc.org | d89b69a | 2014-11-06 17:23:09 +0000 | [diff] [blame] | 207 | return checked_cast<int>(read); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 208 | } else if (result == SR_EOS) { |
| 209 | b->num = 1; |
| 210 | } else if (result == SR_BLOCK) { |
| 211 | BIO_set_retry_read(b); |
| 212 | } |
| 213 | return -1; |
| 214 | } |
| 215 | |
| 216 | static int stream_write(BIO* b, const char* in, int inl) { |
| 217 | if (!in) |
| 218 | return -1; |
| 219 | StreamInterface* stream = static_cast<StreamInterface*>(b->ptr); |
| 220 | BIO_clear_retry_flags(b); |
| 221 | size_t written; |
| 222 | int error; |
| 223 | StreamResult result = stream->Write(in, inl, &written, &error); |
| 224 | if (result == SR_SUCCESS) { |
henrike@webrtc.org | d89b69a | 2014-11-06 17:23:09 +0000 | [diff] [blame] | 225 | return checked_cast<int>(written); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 226 | } else if (result == SR_BLOCK) { |
| 227 | BIO_set_retry_write(b); |
| 228 | } |
| 229 | return -1; |
| 230 | } |
| 231 | |
| 232 | static int stream_puts(BIO* b, const char* str) { |
henrike@webrtc.org | d89b69a | 2014-11-06 17:23:09 +0000 | [diff] [blame] | 233 | return stream_write(b, str, checked_cast<int>(strlen(str))); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | 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] | 237 | switch (cmd) { |
| 238 | case BIO_CTRL_RESET: |
| 239 | return 0; |
| 240 | case BIO_CTRL_EOF: |
| 241 | return b->num; |
| 242 | case BIO_CTRL_WPENDING: |
| 243 | case BIO_CTRL_PENDING: |
| 244 | return 0; |
| 245 | case BIO_CTRL_FLUSH: |
| 246 | return 1; |
Henrik Lundin | f4baca5 | 2015-06-10 09:45:58 +0200 | [diff] [blame] | 247 | case BIO_CTRL_DGRAM_QUERY_MTU: |
| 248 | // openssl defaults to mtu=256 unless we return something here. |
| 249 | // The handshake doesn't actually need to send packets above 1k, |
| 250 | // so this seems like a sensible value that should work in most cases. |
| 251 | // Webrtc uses the same value for video packets. |
| 252 | return 1200; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 253 | default: |
| 254 | return 0; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | ///////////////////////////////////////////////////////////////////////////// |
| 259 | // OpenSSLStreamAdapter |
| 260 | ///////////////////////////////////////////////////////////////////////////// |
| 261 | |
| 262 | OpenSSLStreamAdapter::OpenSSLStreamAdapter(StreamInterface* stream) |
| 263 | : SSLStreamAdapter(stream), |
| 264 | state_(SSL_NONE), |
| 265 | role_(SSL_CLIENT), |
Guo-wei Shieh | a7446d2 | 2016-01-11 15:27:03 -0800 | [diff] [blame] | 266 | ssl_read_needs_write_(false), |
| 267 | ssl_write_needs_read_(false), |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 268 | ssl_(nullptr), |
| 269 | ssl_ctx_(nullptr), |
Joachim Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 270 | ssl_mode_(SSL_MODE_TLS), |
Guo-wei Shieh | a7446d2 | 2016-01-11 15:27:03 -0800 | [diff] [blame] | 271 | ssl_max_version_(SSL_PROTOCOL_TLS_12) {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 272 | |
| 273 | OpenSSLStreamAdapter::~OpenSSLStreamAdapter() { |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 274 | Cleanup(0); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | void OpenSSLStreamAdapter::SetIdentity(SSLIdentity* identity) { |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 278 | RTC_DCHECK(!identity_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 279 | identity_.reset(static_cast<OpenSSLIdentity*>(identity)); |
| 280 | } |
| 281 | |
| 282 | void OpenSSLStreamAdapter::SetServerRole(SSLRole role) { |
| 283 | role_ = role; |
| 284 | } |
| 285 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 286 | std::unique_ptr<SSLCertificate> OpenSSLStreamAdapter::GetPeerCertificate() |
kwiberg | b4d01c4 | 2016-04-06 05:15:06 -0700 | [diff] [blame] | 287 | const { |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 288 | return peer_certificate_ ? std::unique_ptr<SSLCertificate>( |
kwiberg | b4d01c4 | 2016-04-06 05:15:06 -0700 | [diff] [blame] | 289 | peer_certificate_->GetReference()) |
| 290 | : nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 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 | |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 322 | if (!peer_certificate_) { |
| 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 | |
henrike@webrtc.org | c10ecea | 2015-01-07 17:59:28 +0000 | [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 | |
skvlad | d030912 | 2017-02-02 17:18:37 -0800 | [diff] [blame] | 502 | void OpenSSLStreamAdapter::SetInitialRetransmissionTimeout( |
| 503 | int timeout_ms) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 504 | RTC_DCHECK(ssl_ctx_ == nullptr); |
skvlad | d030912 | 2017-02-02 17:18:37 -0800 | [diff] [blame] | 505 | dtls_handshake_timeout_ms_ = timeout_ms; |
| 506 | } |
| 507 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 508 | // |
| 509 | // StreamInterface Implementation |
| 510 | // |
| 511 | |
| 512 | StreamResult OpenSSLStreamAdapter::Write(const void* data, size_t data_len, |
| 513 | size_t* written, int* error) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 514 | RTC_LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::Write(" << data_len << ")"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 515 | |
| 516 | switch (state_) { |
| 517 | case SSL_NONE: |
| 518 | // pass-through in clear text |
| 519 | return StreamAdapterInterface::Write(data, data_len, written, error); |
| 520 | |
| 521 | case SSL_WAIT: |
| 522 | case SSL_CONNECTING: |
| 523 | return SR_BLOCK; |
| 524 | |
| 525 | case SSL_CONNECTED: |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 526 | if (waiting_to_verify_peer_certificate()) { |
| 527 | return SR_BLOCK; |
| 528 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 529 | break; |
| 530 | |
| 531 | case SSL_ERROR: |
| 532 | case SSL_CLOSED: |
| 533 | default: |
| 534 | if (error) |
| 535 | *error = ssl_error_code_; |
| 536 | return SR_ERROR; |
| 537 | } |
| 538 | |
| 539 | // OpenSSL will return an error if we try to write zero bytes |
| 540 | if (data_len == 0) { |
| 541 | if (written) |
| 542 | *written = 0; |
| 543 | return SR_SUCCESS; |
| 544 | } |
| 545 | |
| 546 | ssl_write_needs_read_ = false; |
| 547 | |
henrike@webrtc.org | d89b69a | 2014-11-06 17:23:09 +0000 | [diff] [blame] | 548 | int code = SSL_write(ssl_, data, checked_cast<int>(data_len)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 549 | int ssl_error = SSL_get_error(ssl_, code); |
| 550 | switch (ssl_error) { |
| 551 | case SSL_ERROR_NONE: |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 552 | RTC_LOG(LS_VERBOSE) << " -- success"; |
kwiberg | ee89e78 | 2017-08-09 17:22:01 -0700 | [diff] [blame] | 553 | RTC_DCHECK_GT(code, 0); |
| 554 | RTC_DCHECK_LE(code, data_len); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 555 | if (written) |
| 556 | *written = code; |
| 557 | return SR_SUCCESS; |
| 558 | case SSL_ERROR_WANT_READ: |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 559 | RTC_LOG(LS_VERBOSE) << " -- error want read"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 560 | ssl_write_needs_read_ = true; |
| 561 | return SR_BLOCK; |
| 562 | case SSL_ERROR_WANT_WRITE: |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 563 | RTC_LOG(LS_VERBOSE) << " -- error want write"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 564 | return SR_BLOCK; |
| 565 | |
| 566 | case SSL_ERROR_ZERO_RETURN: |
| 567 | default: |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 568 | Error("SSL_write", (ssl_error ? ssl_error : -1), 0, false); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 569 | if (error) |
| 570 | *error = ssl_error_code_; |
| 571 | return SR_ERROR; |
| 572 | } |
| 573 | // not reached |
| 574 | } |
| 575 | |
| 576 | StreamResult OpenSSLStreamAdapter::Read(void* data, size_t data_len, |
| 577 | size_t* read, int* error) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 578 | RTC_LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::Read(" << data_len << ")"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 579 | switch (state_) { |
| 580 | case SSL_NONE: |
| 581 | // pass-through in clear text |
| 582 | return StreamAdapterInterface::Read(data, data_len, read, error); |
| 583 | |
| 584 | case SSL_WAIT: |
| 585 | case SSL_CONNECTING: |
| 586 | return SR_BLOCK; |
| 587 | |
| 588 | case SSL_CONNECTED: |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 589 | if (waiting_to_verify_peer_certificate()) { |
| 590 | return SR_BLOCK; |
| 591 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 592 | break; |
| 593 | |
| 594 | case SSL_CLOSED: |
| 595 | return SR_EOS; |
| 596 | |
| 597 | case SSL_ERROR: |
| 598 | default: |
| 599 | if (error) |
| 600 | *error = ssl_error_code_; |
| 601 | return SR_ERROR; |
| 602 | } |
| 603 | |
| 604 | // Don't trust OpenSSL with zero byte reads |
| 605 | if (data_len == 0) { |
| 606 | if (read) |
| 607 | *read = 0; |
| 608 | return SR_SUCCESS; |
| 609 | } |
| 610 | |
| 611 | ssl_read_needs_write_ = false; |
| 612 | |
henrike@webrtc.org | d89b69a | 2014-11-06 17:23:09 +0000 | [diff] [blame] | 613 | int code = SSL_read(ssl_, data, checked_cast<int>(data_len)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 614 | int ssl_error = SSL_get_error(ssl_, code); |
| 615 | switch (ssl_error) { |
| 616 | case SSL_ERROR_NONE: |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 617 | RTC_LOG(LS_VERBOSE) << " -- success"; |
kwiberg | ee89e78 | 2017-08-09 17:22:01 -0700 | [diff] [blame] | 618 | RTC_DCHECK_GT(code, 0); |
| 619 | RTC_DCHECK_LE(code, data_len); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 620 | if (read) |
| 621 | *read = code; |
| 622 | |
| 623 | if (ssl_mode_ == SSL_MODE_DTLS) { |
| 624 | // Enforce atomic reads -- this is a short read |
| 625 | unsigned int pending = SSL_pending(ssl_); |
| 626 | |
| 627 | if (pending) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 628 | RTC_LOG(LS_INFO) << " -- short DTLS read. flushing"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 629 | FlushInput(pending); |
| 630 | if (error) |
| 631 | *error = SSE_MSG_TRUNC; |
| 632 | return SR_ERROR; |
| 633 | } |
| 634 | } |
| 635 | return SR_SUCCESS; |
| 636 | case SSL_ERROR_WANT_READ: |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 637 | RTC_LOG(LS_VERBOSE) << " -- error want read"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 638 | return SR_BLOCK; |
| 639 | case SSL_ERROR_WANT_WRITE: |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 640 | RTC_LOG(LS_VERBOSE) << " -- error want write"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 641 | ssl_read_needs_write_ = true; |
| 642 | return SR_BLOCK; |
| 643 | case SSL_ERROR_ZERO_RETURN: |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 644 | RTC_LOG(LS_VERBOSE) << " -- remote side closed"; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 645 | Close(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 646 | return SR_EOS; |
| 647 | break; |
| 648 | default: |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 649 | RTC_LOG(LS_VERBOSE) << " -- error " << code; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 650 | Error("SSL_read", (ssl_error ? ssl_error : -1), 0, false); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 651 | if (error) |
| 652 | *error = ssl_error_code_; |
| 653 | return SR_ERROR; |
| 654 | } |
| 655 | // not reached |
| 656 | } |
| 657 | |
| 658 | void OpenSSLStreamAdapter::FlushInput(unsigned int left) { |
| 659 | unsigned char buf[2048]; |
| 660 | |
| 661 | while (left) { |
| 662 | // This should always succeed |
| 663 | int toread = (sizeof(buf) < left) ? sizeof(buf) : left; |
| 664 | int code = SSL_read(ssl_, buf, toread); |
| 665 | |
| 666 | int ssl_error = SSL_get_error(ssl_, code); |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 667 | RTC_DCHECK(ssl_error == SSL_ERROR_NONE); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 668 | |
| 669 | if (ssl_error != SSL_ERROR_NONE) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 670 | RTC_LOG(LS_VERBOSE) << " -- error " << code; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 671 | Error("SSL_read", (ssl_error ? ssl_error : -1), 0, false); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 672 | return; |
| 673 | } |
| 674 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 675 | RTC_LOG(LS_VERBOSE) << " -- flushed " << code << " bytes"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 676 | left -= code; |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | void OpenSSLStreamAdapter::Close() { |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 681 | Cleanup(0); |
| 682 | RTC_DCHECK(state_ == SSL_CLOSED || state_ == SSL_ERROR); |
| 683 | // When we're closed at SSL layer, also close the stream level which |
| 684 | // performs necessary clean up. Otherwise, a new incoming packet after |
| 685 | // this could overflow the stream buffer. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 686 | StreamAdapterInterface::Close(); |
| 687 | } |
| 688 | |
| 689 | StreamState OpenSSLStreamAdapter::GetState() const { |
| 690 | switch (state_) { |
| 691 | case SSL_WAIT: |
| 692 | case SSL_CONNECTING: |
| 693 | return SS_OPENING; |
| 694 | case SSL_CONNECTED: |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 695 | if (waiting_to_verify_peer_certificate()) { |
| 696 | return SS_OPENING; |
| 697 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 698 | return SS_OPEN; |
| 699 | default: |
| 700 | return SS_CLOSED; |
| 701 | }; |
| 702 | // not reached |
| 703 | } |
| 704 | |
| 705 | void OpenSSLStreamAdapter::OnEvent(StreamInterface* stream, int events, |
| 706 | int err) { |
| 707 | int events_to_signal = 0; |
| 708 | int signal_error = 0; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 709 | RTC_DCHECK(stream == this->stream()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 710 | if ((events & SE_OPEN)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 711 | RTC_LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::OnEvent SE_OPEN"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 712 | if (state_ != SSL_WAIT) { |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 713 | RTC_DCHECK(state_ == SSL_NONE); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 714 | events_to_signal |= SE_OPEN; |
| 715 | } else { |
| 716 | state_ = SSL_CONNECTING; |
| 717 | if (int err = BeginSSL()) { |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 718 | Error("BeginSSL", err, 0, true); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 719 | return; |
| 720 | } |
| 721 | } |
| 722 | } |
| 723 | if ((events & (SE_READ|SE_WRITE))) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 724 | RTC_LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::OnEvent" |
| 725 | << ((events & SE_READ) ? " SE_READ" : "") |
| 726 | << ((events & SE_WRITE) ? " SE_WRITE" : ""); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 727 | if (state_ == SSL_NONE) { |
| 728 | events_to_signal |= events & (SE_READ|SE_WRITE); |
| 729 | } else if (state_ == SSL_CONNECTING) { |
| 730 | if (int err = ContinueSSL()) { |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 731 | Error("ContinueSSL", err, 0, true); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 732 | return; |
| 733 | } |
| 734 | } else if (state_ == SSL_CONNECTED) { |
| 735 | if (((events & SE_READ) && ssl_write_needs_read_) || |
| 736 | (events & SE_WRITE)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 737 | RTC_LOG(LS_VERBOSE) << " -- onStreamWriteable"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 738 | events_to_signal |= SE_WRITE; |
| 739 | } |
| 740 | if (((events & SE_WRITE) && ssl_read_needs_write_) || |
| 741 | (events & SE_READ)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 742 | RTC_LOG(LS_VERBOSE) << " -- onStreamReadable"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 743 | events_to_signal |= SE_READ; |
| 744 | } |
| 745 | } |
| 746 | } |
| 747 | if ((events & SE_CLOSE)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 748 | RTC_LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::OnEvent(SE_CLOSE, " << err |
| 749 | << ")"; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 750 | Cleanup(0); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 751 | events_to_signal |= SE_CLOSE; |
| 752 | // SE_CLOSE is the only event that uses the final parameter to OnEvent(). |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 753 | RTC_DCHECK(signal_error == 0); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 754 | signal_error = err; |
| 755 | } |
| 756 | if (events_to_signal) |
| 757 | StreamAdapterInterface::OnEvent(stream, events_to_signal, signal_error); |
| 758 | } |
| 759 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 760 | int OpenSSLStreamAdapter::BeginSSL() { |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 761 | RTC_DCHECK(state_ == SSL_CONNECTING); |
Taylor Brandstetter | c8762a8 | 2016-08-11 12:01:49 -0700 | [diff] [blame] | 762 | // The underlying stream has opened. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 763 | RTC_LOG(LS_INFO) << "BeginSSL with peer."; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 764 | |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 765 | BIO* bio = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 766 | |
Taylor Brandstetter | c8762a8 | 2016-08-11 12:01:49 -0700 | [diff] [blame] | 767 | // First set up the context. |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 768 | RTC_DCHECK(ssl_ctx_ == nullptr); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 769 | ssl_ctx_ = SetupSSLContext(); |
| 770 | if (!ssl_ctx_) |
| 771 | return -1; |
| 772 | |
| 773 | bio = BIO_new_stream(static_cast<StreamInterface*>(stream())); |
| 774 | if (!bio) |
| 775 | return -1; |
| 776 | |
| 777 | ssl_ = SSL_new(ssl_ctx_); |
| 778 | if (!ssl_) { |
| 779 | BIO_free(bio); |
| 780 | return -1; |
| 781 | } |
| 782 | |
| 783 | SSL_set_app_data(ssl_, this); |
| 784 | |
| 785 | 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] | 786 | if (ssl_mode_ == SSL_MODE_DTLS) { |
Taylor Brandstetter | 4f0dfbd | 2016-06-15 17:15:23 -0700 | [diff] [blame] | 787 | #ifdef OPENSSL_IS_BORINGSSL |
skvlad | d030912 | 2017-02-02 17:18:37 -0800 | [diff] [blame] | 788 | DTLSv1_set_initial_timeout_duration(ssl_, dtls_handshake_timeout_ms_); |
Taylor Brandstetter | 4f0dfbd | 2016-06-15 17:15:23 -0700 | [diff] [blame] | 789 | #else |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 790 | // Enable read-ahead for DTLS so whole packets are read from internal BIO |
| 791 | // before parsing. This is done internally by BoringSSL for DTLS. |
| 792 | SSL_set_read_ahead(ssl_, 1); |
philipel | 49c0869 | 2016-05-24 01:49:43 -0700 | [diff] [blame] | 793 | #endif |
Taylor Brandstetter | 4f0dfbd | 2016-06-15 17:15:23 -0700 | [diff] [blame] | 794 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 795 | |
| 796 | SSL_set_mode(ssl_, SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 797 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER); |
| 798 | |
David Benjamin | 60d5f3f | 2016-03-24 13:28:25 -0400 | [diff] [blame] | 799 | #if !defined(OPENSSL_IS_BORINGSSL) |
| 800 | // Specify an ECDH group for ECDHE ciphers, otherwise OpenSSL cannot |
| 801 | // negotiate them when acting as the server. Use NIST's P-256 which is |
| 802 | // commonly supported. BoringSSL doesn't need explicit configuration and has |
| 803 | // a reasonable default set. |
jiayl@webrtc.org | 11c6bde | 2014-08-28 16:14:38 +0000 | [diff] [blame] | 804 | EC_KEY* ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 805 | if (ecdh == nullptr) |
jiayl@webrtc.org | 11c6bde | 2014-08-28 16:14:38 +0000 | [diff] [blame] | 806 | return -1; |
| 807 | SSL_set_options(ssl_, SSL_OP_SINGLE_ECDH_USE); |
| 808 | SSL_set_tmp_ecdh(ssl_, ecdh); |
| 809 | EC_KEY_free(ecdh); |
David Benjamin | 60d5f3f | 2016-03-24 13:28:25 -0400 | [diff] [blame] | 810 | #endif |
jiayl@webrtc.org | 11c6bde | 2014-08-28 16:14:38 +0000 | [diff] [blame] | 811 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 812 | // Do the connect |
| 813 | return ContinueSSL(); |
| 814 | } |
| 815 | |
| 816 | int OpenSSLStreamAdapter::ContinueSSL() { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 817 | RTC_LOG(LS_VERBOSE) << "ContinueSSL"; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 818 | RTC_DCHECK(state_ == SSL_CONNECTING); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 819 | |
| 820 | // Clear the DTLS timer |
| 821 | Thread::Current()->Clear(this, MSG_TIMEOUT); |
| 822 | |
| 823 | int code = (role_ == SSL_CLIENT) ? SSL_connect(ssl_) : SSL_accept(ssl_); |
| 824 | int ssl_error; |
| 825 | switch (ssl_error = SSL_get_error(ssl_, code)) { |
| 826 | case SSL_ERROR_NONE: |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 827 | RTC_LOG(LS_VERBOSE) << " -- success"; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 828 | // By this point, OpenSSL should have given us a certificate, or errored |
| 829 | // out if one was missing. |
| 830 | RTC_DCHECK(peer_certificate_ || !client_auth_enabled()); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 831 | |
| 832 | state_ = SSL_CONNECTED; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 833 | if (!waiting_to_verify_peer_certificate()) { |
| 834 | // We have everything we need to start the connection, so signal |
| 835 | // SE_OPEN. If we need a client certificate fingerprint and don't have |
| 836 | // it yet, we'll instead signal SE_OPEN in SetPeerCertificateDigest. |
| 837 | // |
Taylor Brandstetter | fe69a74 | 2016-09-30 17:34:32 -0700 | [diff] [blame] | 838 | // TODO(deadbeef): Post this event asynchronously to unwind the stack. |
| 839 | // The caller of ContinueSSL may be the same object listening for these |
| 840 | // events and may not be prepared for reentrancy. |
| 841 | // PostEvent(SE_OPEN | SE_READ | SE_WRITE, 0); |
| 842 | StreamAdapterInterface::OnEvent(stream(), SE_OPEN | SE_READ | SE_WRITE, |
| 843 | 0); |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 844 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 845 | break; |
| 846 | |
| 847 | case SSL_ERROR_WANT_READ: { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 848 | RTC_LOG(LS_VERBOSE) << " -- error want read"; |
| 849 | struct timeval timeout; |
| 850 | if (DTLSv1_get_timeout(ssl_, &timeout)) { |
| 851 | int delay = timeout.tv_sec * 1000 + timeout.tv_usec / 1000; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 852 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 853 | Thread::Current()->PostDelayed(RTC_FROM_HERE, delay, this, MSG_TIMEOUT, |
| 854 | 0); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 855 | } |
| 856 | } |
| 857 | break; |
| 858 | |
| 859 | case SSL_ERROR_WANT_WRITE: |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 860 | RTC_LOG(LS_VERBOSE) << " -- error want write"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 861 | break; |
| 862 | |
| 863 | case SSL_ERROR_ZERO_RETURN: |
| 864 | default: |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 865 | RTC_LOG(LS_VERBOSE) << " -- error " << code; |
zhihuang | d82eee0 | 2016-08-26 11:25:05 -0700 | [diff] [blame] | 866 | SSLHandshakeError ssl_handshake_err = SSLHandshakeError::UNKNOWN; |
| 867 | int err_code = ERR_peek_last_error(); |
| 868 | if (err_code != 0 && ERR_GET_REASON(err_code) == SSL_R_NO_SHARED_CIPHER) { |
| 869 | ssl_handshake_err = SSLHandshakeError::INCOMPATIBLE_CIPHERSUITE; |
| 870 | } |
| 871 | SignalSSLHandshakeError(ssl_handshake_err); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 872 | return (ssl_error != 0) ? ssl_error : -1; |
| 873 | } |
| 874 | |
| 875 | return 0; |
| 876 | } |
| 877 | |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 878 | void OpenSSLStreamAdapter::Error(const char* context, |
| 879 | int err, |
| 880 | uint8_t alert, |
| 881 | bool signal) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 882 | RTC_LOG(LS_WARNING) << "OpenSSLStreamAdapter::Error(" << context << ", " |
| 883 | << err << ", " << static_cast<int>(alert) << ")"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 884 | state_ = SSL_ERROR; |
| 885 | ssl_error_code_ = err; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 886 | Cleanup(alert); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 887 | if (signal) |
| 888 | StreamAdapterInterface::OnEvent(stream(), SE_CLOSE, err); |
| 889 | } |
| 890 | |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 891 | void OpenSSLStreamAdapter::Cleanup(uint8_t alert) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 892 | RTC_LOG(LS_INFO) << "Cleanup"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 893 | |
| 894 | if (state_ != SSL_ERROR) { |
| 895 | state_ = SSL_CLOSED; |
| 896 | ssl_error_code_ = 0; |
| 897 | } |
| 898 | |
| 899 | if (ssl_) { |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 900 | int ret; |
| 901 | // SSL_send_fatal_alert is only available in BoringSSL. |
| 902 | #ifdef OPENSSL_IS_BORINGSSL |
| 903 | if (alert) { |
| 904 | ret = SSL_send_fatal_alert(ssl_, alert); |
| 905 | if (ret < 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 906 | RTC_LOG(LS_WARNING) << "SSL_send_fatal_alert failed, error = " |
| 907 | << SSL_get_error(ssl_, ret); |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 908 | } |
| 909 | } else { |
| 910 | #endif |
| 911 | ret = SSL_shutdown(ssl_); |
| 912 | if (ret < 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 913 | RTC_LOG(LS_WARNING) |
| 914 | << "SSL_shutdown failed, error = " << SSL_get_error(ssl_, ret); |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 915 | } |
| 916 | #ifdef OPENSSL_IS_BORINGSSL |
jiayl@webrtc.org | f1d751c | 2014-09-25 16:38:46 +0000 | [diff] [blame] | 917 | } |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 918 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 919 | SSL_free(ssl_); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 920 | ssl_ = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 921 | } |
| 922 | if (ssl_ctx_) { |
| 923 | SSL_CTX_free(ssl_ctx_); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 924 | ssl_ctx_ = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 925 | } |
| 926 | identity_.reset(); |
| 927 | peer_certificate_.reset(); |
| 928 | |
| 929 | // Clear the DTLS timer |
| 930 | Thread::Current()->Clear(this, MSG_TIMEOUT); |
| 931 | } |
| 932 | |
| 933 | |
| 934 | void OpenSSLStreamAdapter::OnMessage(Message* msg) { |
| 935 | // Process our own messages and then pass others to the superclass |
| 936 | if (MSG_TIMEOUT == msg->message_id) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 937 | RTC_LOG(LS_INFO) << "DTLS timeout expired"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 938 | DTLSv1_handle_timeout(ssl_); |
| 939 | ContinueSSL(); |
| 940 | } else { |
| 941 | StreamInterface::OnMessage(msg); |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | SSL_CTX* OpenSSLStreamAdapter::SetupSSLContext() { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 946 | SSL_CTX* ctx = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 947 | |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 948 | #ifdef OPENSSL_IS_BORINGSSL |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 949 | ctx = SSL_CTX_new(ssl_mode_ == SSL_MODE_DTLS ? |
Joachim Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 950 | 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) { |
| 976 | #if (OPENSSL_VERSION_NUMBER >= 0x10002000L) |
| 977 | // DTLS 1.2 only available starting from OpenSSL 1.0.2 |
| 978 | if (role_ == SSL_CLIENT) { |
| 979 | method = DTLS_client_method(); |
| 980 | } else { |
| 981 | method = DTLS_server_method(); |
| 982 | } |
| 983 | #else |
| 984 | if (role_ == SSL_CLIENT) { |
| 985 | method = DTLSv1_client_method(); |
| 986 | } else { |
| 987 | method = DTLSv1_server_method(); |
| 988 | } |
| 989 | #endif |
| 990 | } else { |
| 991 | #if (OPENSSL_VERSION_NUMBER >= 0x10100000L) |
| 992 | // New API only available starting from OpenSSL 1.1.0 |
| 993 | if (role_ == SSL_CLIENT) { |
| 994 | method = TLS_client_method(); |
| 995 | } else { |
| 996 | method = TLS_server_method(); |
| 997 | } |
| 998 | #else |
| 999 | if (role_ == SSL_CLIENT) { |
| 1000 | method = SSLv23_client_method(); |
| 1001 | } else { |
| 1002 | method = SSLv23_server_method(); |
| 1003 | } |
| 1004 | #endif |
| 1005 | } |
| 1006 | break; |
| 1007 | } |
| 1008 | ctx = SSL_CTX_new(method); |
| 1009 | #endif // OPENSSL_IS_BORINGSSL |
Joachim Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 1010 | |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 1011 | if (ctx == nullptr) |
| 1012 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1013 | |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 1014 | #ifdef OPENSSL_IS_BORINGSSL |
davidben | e36c46e | 2016-12-06 17:12:02 -0800 | [diff] [blame] | 1015 | SSL_CTX_set_min_proto_version(ctx, ssl_mode_ == SSL_MODE_DTLS ? |
Joachim Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 1016 | DTLS1_VERSION : TLS1_VERSION); |
| 1017 | switch (ssl_max_version_) { |
| 1018 | case SSL_PROTOCOL_TLS_10: |
davidben | e36c46e | 2016-12-06 17:12:02 -0800 | [diff] [blame] | 1019 | SSL_CTX_set_max_proto_version(ctx, ssl_mode_ == SSL_MODE_DTLS ? |
Joachim Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 1020 | DTLS1_VERSION : TLS1_VERSION); |
| 1021 | break; |
| 1022 | case SSL_PROTOCOL_TLS_11: |
davidben | e36c46e | 2016-12-06 17:12:02 -0800 | [diff] [blame] | 1023 | SSL_CTX_set_max_proto_version(ctx, ssl_mode_ == SSL_MODE_DTLS ? |
Joachim Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 1024 | DTLS1_VERSION : TLS1_1_VERSION); |
| 1025 | break; |
| 1026 | case SSL_PROTOCOL_TLS_12: |
| 1027 | default: |
davidben | e36c46e | 2016-12-06 17:12:02 -0800 | [diff] [blame] | 1028 | SSL_CTX_set_max_proto_version(ctx, ssl_mode_ == SSL_MODE_DTLS ? |
Joachim Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 1029 | DTLS1_2_VERSION : TLS1_2_VERSION); |
| 1030 | break; |
| 1031 | } |
deadbeef | 6cf94a0 | 2016-11-28 17:38:34 -0800 | [diff] [blame] | 1032 | if (g_use_time_callback_for_testing) { |
| 1033 | SSL_CTX_set_current_time_cb(ctx, &TimeCallbackForTesting); |
| 1034 | } |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 1035 | #endif |
Joachim Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 1036 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1037 | if (identity_ && !identity_->ConfigureIdentity(ctx)) { |
| 1038 | SSL_CTX_free(ctx); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 1039 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1040 | } |
| 1041 | |
tfarina | a41ab93 | 2015-10-30 16:08:48 -0700 | [diff] [blame] | 1042 | #if !defined(NDEBUG) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1043 | SSL_CTX_set_info_callback(ctx, OpenSSLAdapter::SSLInfoCallback); |
| 1044 | #endif |
| 1045 | |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 1046 | int mode = SSL_VERIFY_PEER; |
| 1047 | if (client_auth_enabled()) { |
| 1048 | // Require a certificate from the client. |
| 1049 | // Note: Normally this is always true in production, but it may be disabled |
| 1050 | // for testing purposes (e.g. SSLAdapter unit tests). |
| 1051 | mode |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 1052 | } |
| 1053 | |
David Benjamin | dc24656 | 2017-09-29 12:14:08 -0400 | [diff] [blame] | 1054 | // Configure a custom certificate verification callback to check the peer |
| 1055 | // certificate digest. Note the second argument to SSL_CTX_set_verify is to |
| 1056 | // override individual errors in the default verification logic, which is not |
| 1057 | // what we want here. |
| 1058 | SSL_CTX_set_verify(ctx, mode, nullptr); |
| 1059 | SSL_CTX_set_cert_verify_callback(ctx, SSLVerifyCallback, nullptr); |
| 1060 | |
Joachim Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 1061 | // Select list of available ciphers. Note that !SHA256 and !SHA384 only |
| 1062 | // remove HMAC-SHA256 and HMAC-SHA384 cipher suites, not GCM cipher suites |
| 1063 | // with SHA256 or SHA384 as the handshake hash. |
| 1064 | // This matches the list of SSLClientSocketOpenSSL in Chromium. |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 1065 | SSL_CTX_set_cipher_list( |
| 1066 | ctx, "DEFAULT:!NULL:!aNULL:!SHA256:!SHA384:!aECDH:!AESGCM+AES256:!aPSK"); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1067 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1068 | if (!srtp_ciphers_.empty()) { |
| 1069 | if (SSL_CTX_set_tlsext_use_srtp(ctx, srtp_ciphers_.c_str())) { |
| 1070 | SSL_CTX_free(ctx); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 1071 | return nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1072 | } |
| 1073 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1074 | |
| 1075 | return ctx; |
| 1076 | } |
| 1077 | |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 1078 | bool OpenSSLStreamAdapter::VerifyPeerCertificate() { |
| 1079 | if (!has_peer_certificate_digest() || !peer_certificate_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1080 | RTC_LOG(LS_WARNING) << "Missing digest or peer certificate."; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 1081 | return false; |
| 1082 | } |
deadbeef | 81f6f4f | 2016-09-19 17:20:52 -0700 | [diff] [blame] | 1083 | |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 1084 | unsigned char digest[EVP_MAX_MD_SIZE]; |
| 1085 | size_t digest_length; |
| 1086 | if (!OpenSSLCertificate::ComputeDigest( |
| 1087 | peer_certificate_->x509(), peer_certificate_digest_algorithm_, digest, |
| 1088 | sizeof(digest), &digest_length)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1089 | RTC_LOG(LS_WARNING) << "Failed to compute peer cert digest."; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 1090 | return false; |
| 1091 | } |
| 1092 | |
| 1093 | Buffer computed_digest(digest, digest_length); |
| 1094 | if (computed_digest != peer_certificate_digest_value_) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1095 | RTC_LOG(LS_WARNING) |
| 1096 | << "Rejected peer certificate due to mismatched digest."; |
jbauch | f8f457b | 2017-03-09 16:24:57 -0800 | [diff] [blame] | 1097 | return false; |
deadbeef | 81f6f4f | 2016-09-19 17:20:52 -0700 | [diff] [blame] | 1098 | } |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 1099 | // Ignore any verification error if the digest matches, since there is no |
| 1100 | // value in checking the validity of a self-signed cert issued by untrusted |
| 1101 | // sources. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1102 | RTC_LOG(LS_INFO) << "Accepted peer certificate."; |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 1103 | peer_certificate_verified_ = true; |
| 1104 | return true; |
| 1105 | } |
| 1106 | |
David Benjamin | dc24656 | 2017-09-29 12:14:08 -0400 | [diff] [blame] | 1107 | int OpenSSLStreamAdapter::SSLVerifyCallback(X509_STORE_CTX* store, void* arg) { |
| 1108 | // Get our SSL structure and OpenSSLStreamAdapter from the store. |
deadbeef | 89824f6 | 2016-09-30 11:55:43 -0700 | [diff] [blame] | 1109 | SSL* ssl = reinterpret_cast<SSL*>( |
| 1110 | 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] | 1111 | OpenSSLStreamAdapter* stream = |
| 1112 | reinterpret_cast<OpenSSLStreamAdapter*>(SSL_get_app_data(ssl)); |
henrike@webrtc.org | 4e5f65a | 2014-06-05 20:40:11 +0000 | [diff] [blame] | 1113 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1114 | // Record the peer's certificate. |
David Benjamin | dc24656 | 2017-09-29 12:14:08 -0400 | [diff] [blame] | 1115 | X509* cert = SSL_get_peer_certificate(ssl); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1116 | stream->peer_certificate_.reset(new OpenSSLCertificate(cert)); |
David Benjamin | dc24656 | 2017-09-29 12:14:08 -0400 | [diff] [blame] | 1117 | X509_free(cert); |
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[] = { |
| 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), |
| 1155 | #ifdef TLS1_CK_ECDHE_RSA_WITH_AES_256_GCM_SHA256 |
| 1156 | CDEF(ECDHE_RSA_WITH_AES_256_GCM_SHA256), |
| 1157 | #endif |
torbjorng | aad6780 | 2016-04-07 08:55:28 -0700 | [diff] [blame] | 1158 | #ifdef TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [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[] = { |
| 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), |
| 1167 | #ifdef TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA256 |
| 1168 | CDEF(ECDHE_ECDSA_WITH_AES_256_GCM_SHA256), |
| 1169 | #endif |
torbjorng | aad6780 | 2016-04-07 08:55:28 -0700 | [diff] [blame] | 1170 | #ifdef TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [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 |