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