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