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), |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 289 | custom_verification_succeeded_(false), |
Joachim Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 290 | ssl_mode_(SSL_MODE_TLS), |
Guo-wei Shieh | a7446d2 | 2016-01-11 15:27:03 -0800 | [diff] [blame] | 291 | ssl_max_version_(SSL_PROTOCOL_TLS_12) {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 292 | |
| 293 | OpenSSLStreamAdapter::~OpenSSLStreamAdapter() { |
| 294 | Cleanup(); |
| 295 | } |
| 296 | |
| 297 | void OpenSSLStreamAdapter::SetIdentity(SSLIdentity* identity) { |
| 298 | ASSERT(!identity_); |
| 299 | identity_.reset(static_cast<OpenSSLIdentity*>(identity)); |
| 300 | } |
| 301 | |
| 302 | void OpenSSLStreamAdapter::SetServerRole(SSLRole role) { |
| 303 | role_ = role; |
| 304 | } |
| 305 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 306 | std::unique_ptr<SSLCertificate> OpenSSLStreamAdapter::GetPeerCertificate() |
kwiberg | b4d01c4 | 2016-04-06 05:15:06 -0700 | [diff] [blame] | 307 | const { |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 308 | return peer_certificate_ ? std::unique_ptr<SSLCertificate>( |
kwiberg | b4d01c4 | 2016-04-06 05:15:06 -0700 | [diff] [blame] | 309 | peer_certificate_->GetReference()) |
| 310 | : nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | bool OpenSSLStreamAdapter::SetPeerCertificateDigest(const std::string |
| 314 | &digest_alg, |
| 315 | const unsigned char* |
| 316 | digest_val, |
| 317 | size_t digest_len) { |
| 318 | ASSERT(!peer_certificate_); |
| 319 | ASSERT(peer_certificate_digest_algorithm_.size() == 0); |
| 320 | ASSERT(ssl_server_name_.empty()); |
| 321 | size_t expected_len; |
| 322 | |
| 323 | if (!OpenSSLDigest::GetDigestSize(digest_alg, &expected_len)) { |
| 324 | LOG(LS_WARNING) << "Unknown digest algorithm: " << digest_alg; |
| 325 | return false; |
| 326 | } |
| 327 | if (expected_len != digest_len) |
| 328 | return false; |
| 329 | |
| 330 | peer_certificate_digest_value_.SetData(digest_val, digest_len); |
| 331 | peer_certificate_digest_algorithm_ = digest_alg; |
| 332 | |
| 333 | return true; |
| 334 | } |
| 335 | |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 336 | std::string OpenSSLStreamAdapter::SslCipherSuiteToName(int cipher_suite) { |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 337 | #ifdef OPENSSL_IS_BORINGSSL |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 338 | 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] | 339 | if (!ssl_cipher) { |
| 340 | return std::string(); |
| 341 | } |
| 342 | char* cipher_name = SSL_CIPHER_get_rfc_name(ssl_cipher); |
| 343 | std::string rfc_name = std::string(cipher_name); |
| 344 | OPENSSL_free(cipher_name); |
| 345 | return rfc_name; |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 346 | #else |
| 347 | for (const SslCipherMapEntry* entry = kSslCipherMap; entry->rfc_name; |
| 348 | ++entry) { |
| 349 | if (cipher_suite == static_cast<int>(entry->openssl_id)) { |
| 350 | return entry->rfc_name; |
| 351 | } |
| 352 | } |
| 353 | return std::string(); |
| 354 | #endif |
Guo-wei Shieh | 456696a | 2015-09-30 21:48:54 -0700 | [diff] [blame] | 355 | } |
pthatcher@webrtc.org | 3ee4fe5 | 2015-02-11 22:34:36 +0000 | [diff] [blame] | 356 | |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 357 | bool OpenSSLStreamAdapter::GetSslCipherSuite(int* cipher_suite) { |
pthatcher@webrtc.org | 3ee4fe5 | 2015-02-11 22:34:36 +0000 | [diff] [blame] | 358 | if (state_ != SSL_CONNECTED) |
| 359 | return false; |
| 360 | |
| 361 | const SSL_CIPHER* current_cipher = SSL_get_current_cipher(ssl_); |
| 362 | if (current_cipher == NULL) { |
| 363 | return false; |
| 364 | } |
| 365 | |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 366 | *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] | 367 | return true; |
| 368 | } |
| 369 | |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 370 | int OpenSSLStreamAdapter::GetSslVersion() const { |
| 371 | if (state_ != SSL_CONNECTED) |
| 372 | return -1; |
| 373 | |
| 374 | int ssl_version = SSL_version(ssl_); |
| 375 | if (ssl_mode_ == SSL_MODE_DTLS) { |
| 376 | if (ssl_version == DTLS1_VERSION) |
| 377 | return SSL_PROTOCOL_DTLS_10; |
| 378 | else if (ssl_version == DTLS1_2_VERSION) |
| 379 | return SSL_PROTOCOL_DTLS_12; |
| 380 | } else { |
| 381 | if (ssl_version == TLS1_VERSION) |
| 382 | return SSL_PROTOCOL_TLS_10; |
| 383 | else if (ssl_version == TLS1_1_VERSION) |
| 384 | return SSL_PROTOCOL_TLS_11; |
| 385 | else if (ssl_version == TLS1_2_VERSION) |
| 386 | return SSL_PROTOCOL_TLS_12; |
| 387 | } |
| 388 | |
| 389 | return -1; |
| 390 | } |
| 391 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 392 | // Key Extractor interface |
| 393 | bool OpenSSLStreamAdapter::ExportKeyingMaterial(const std::string& label, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 394 | const uint8_t* context, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 395 | size_t context_len, |
| 396 | bool use_context, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 397 | uint8_t* result, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 398 | size_t result_len) { |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 399 | #ifdef HAVE_DTLS_SRTP |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 400 | int i; |
| 401 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 402 | i = SSL_export_keying_material(ssl_, result, result_len, label.c_str(), |
| 403 | label.length(), const_cast<uint8_t*>(context), |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 404 | context_len, use_context); |
| 405 | |
| 406 | if (i != 1) |
| 407 | return false; |
| 408 | |
| 409 | return true; |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 410 | #else |
| 411 | return false; |
| 412 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 415 | bool OpenSSLStreamAdapter::SetDtlsSrtpCryptoSuites( |
| 416 | const std::vector<int>& ciphers) { |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 417 | #ifdef HAVE_DTLS_SRTP |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 418 | std::string internal_ciphers; |
| 419 | |
| 420 | if (state_ != SSL_NONE) |
| 421 | return false; |
| 422 | |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 423 | for (std::vector<int>::const_iterator cipher = ciphers.begin(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 424 | cipher != ciphers.end(); ++cipher) { |
| 425 | bool found = false; |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 426 | for (SrtpCipherMapEntry* entry = SrtpCipherMap; entry->internal_name; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 427 | ++entry) { |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 428 | if (*cipher == entry->id) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 429 | found = true; |
| 430 | if (!internal_ciphers.empty()) |
| 431 | internal_ciphers += ":"; |
| 432 | internal_ciphers += entry->internal_name; |
| 433 | break; |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | if (!found) { |
| 438 | LOG(LS_ERROR) << "Could not find cipher: " << *cipher; |
| 439 | return false; |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | if (internal_ciphers.empty()) |
| 444 | return false; |
| 445 | |
| 446 | srtp_ciphers_ = internal_ciphers; |
| 447 | return true; |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 448 | #else |
| 449 | return false; |
| 450 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 453 | bool OpenSSLStreamAdapter::GetDtlsSrtpCryptoSuite(int* crypto_suite) { |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 454 | #ifdef HAVE_DTLS_SRTP |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 455 | ASSERT(state_ == SSL_CONNECTED); |
| 456 | if (state_ != SSL_CONNECTED) |
| 457 | return false; |
| 458 | |
henrike@webrtc.org | c10ecea | 2015-01-07 17:59:28 +0000 | [diff] [blame] | 459 | const SRTP_PROTECTION_PROFILE *srtp_profile = |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 460 | SSL_get_selected_srtp_profile(ssl_); |
| 461 | |
| 462 | if (!srtp_profile) |
| 463 | return false; |
| 464 | |
Guo-wei Shieh | 521ed7b | 2015-11-18 19:41:53 -0800 | [diff] [blame] | 465 | *crypto_suite = srtp_profile->id; |
| 466 | ASSERT(!SrtpCryptoSuiteToName(*crypto_suite).empty()); |
| 467 | return true; |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 468 | #else |
| 469 | return false; |
| 470 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | int OpenSSLStreamAdapter::StartSSLWithServer(const char* server_name) { |
| 474 | ASSERT(server_name != NULL && server_name[0] != '\0'); |
| 475 | ssl_server_name_ = server_name; |
| 476 | return StartSSL(); |
| 477 | } |
| 478 | |
| 479 | int OpenSSLStreamAdapter::StartSSLWithPeer() { |
| 480 | ASSERT(ssl_server_name_.empty()); |
| 481 | // It is permitted to specify peer_certificate_ only later. |
| 482 | return StartSSL(); |
| 483 | } |
| 484 | |
| 485 | void OpenSSLStreamAdapter::SetMode(SSLMode mode) { |
| 486 | ASSERT(state_ == SSL_NONE); |
| 487 | ssl_mode_ = mode; |
| 488 | } |
| 489 | |
Joachim Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 490 | void OpenSSLStreamAdapter::SetMaxProtocolVersion(SSLProtocolVersion version) { |
| 491 | ASSERT(ssl_ctx_ == NULL); |
| 492 | ssl_max_version_ = version; |
| 493 | } |
| 494 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 495 | // |
| 496 | // StreamInterface Implementation |
| 497 | // |
| 498 | |
| 499 | StreamResult OpenSSLStreamAdapter::Write(const void* data, size_t data_len, |
| 500 | size_t* written, int* error) { |
| 501 | LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::Write(" << data_len << ")"; |
| 502 | |
| 503 | switch (state_) { |
| 504 | case SSL_NONE: |
| 505 | // pass-through in clear text |
| 506 | return StreamAdapterInterface::Write(data, data_len, written, error); |
| 507 | |
| 508 | case SSL_WAIT: |
| 509 | case SSL_CONNECTING: |
| 510 | return SR_BLOCK; |
| 511 | |
| 512 | case SSL_CONNECTED: |
| 513 | break; |
| 514 | |
| 515 | case SSL_ERROR: |
| 516 | case SSL_CLOSED: |
| 517 | default: |
| 518 | if (error) |
| 519 | *error = ssl_error_code_; |
| 520 | return SR_ERROR; |
| 521 | } |
| 522 | |
| 523 | // OpenSSL will return an error if we try to write zero bytes |
| 524 | if (data_len == 0) { |
| 525 | if (written) |
| 526 | *written = 0; |
| 527 | return SR_SUCCESS; |
| 528 | } |
| 529 | |
| 530 | ssl_write_needs_read_ = false; |
| 531 | |
henrike@webrtc.org | d89b69a | 2014-11-06 17:23:09 +0000 | [diff] [blame] | 532 | int code = SSL_write(ssl_, data, checked_cast<int>(data_len)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 533 | int ssl_error = SSL_get_error(ssl_, code); |
| 534 | switch (ssl_error) { |
| 535 | case SSL_ERROR_NONE: |
| 536 | LOG(LS_VERBOSE) << " -- success"; |
| 537 | ASSERT(0 < code && static_cast<unsigned>(code) <= data_len); |
| 538 | if (written) |
| 539 | *written = code; |
| 540 | return SR_SUCCESS; |
| 541 | case SSL_ERROR_WANT_READ: |
| 542 | LOG(LS_VERBOSE) << " -- error want read"; |
| 543 | ssl_write_needs_read_ = true; |
| 544 | return SR_BLOCK; |
| 545 | case SSL_ERROR_WANT_WRITE: |
| 546 | LOG(LS_VERBOSE) << " -- error want write"; |
| 547 | return SR_BLOCK; |
| 548 | |
| 549 | case SSL_ERROR_ZERO_RETURN: |
| 550 | default: |
| 551 | Error("SSL_write", (ssl_error ? ssl_error : -1), false); |
| 552 | if (error) |
| 553 | *error = ssl_error_code_; |
| 554 | return SR_ERROR; |
| 555 | } |
| 556 | // not reached |
| 557 | } |
| 558 | |
| 559 | StreamResult OpenSSLStreamAdapter::Read(void* data, size_t data_len, |
| 560 | size_t* read, int* error) { |
| 561 | LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::Read(" << data_len << ")"; |
| 562 | switch (state_) { |
| 563 | case SSL_NONE: |
| 564 | // pass-through in clear text |
| 565 | return StreamAdapterInterface::Read(data, data_len, read, error); |
| 566 | |
| 567 | case SSL_WAIT: |
| 568 | case SSL_CONNECTING: |
| 569 | return SR_BLOCK; |
| 570 | |
| 571 | case SSL_CONNECTED: |
| 572 | break; |
| 573 | |
| 574 | case SSL_CLOSED: |
| 575 | return SR_EOS; |
| 576 | |
| 577 | case SSL_ERROR: |
| 578 | default: |
| 579 | if (error) |
| 580 | *error = ssl_error_code_; |
| 581 | return SR_ERROR; |
| 582 | } |
| 583 | |
| 584 | // Don't trust OpenSSL with zero byte reads |
| 585 | if (data_len == 0) { |
| 586 | if (read) |
| 587 | *read = 0; |
| 588 | return SR_SUCCESS; |
| 589 | } |
| 590 | |
| 591 | ssl_read_needs_write_ = false; |
| 592 | |
henrike@webrtc.org | d89b69a | 2014-11-06 17:23:09 +0000 | [diff] [blame] | 593 | int code = SSL_read(ssl_, data, checked_cast<int>(data_len)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 594 | int ssl_error = SSL_get_error(ssl_, code); |
| 595 | switch (ssl_error) { |
| 596 | case SSL_ERROR_NONE: |
| 597 | LOG(LS_VERBOSE) << " -- success"; |
| 598 | ASSERT(0 < code && static_cast<unsigned>(code) <= data_len); |
| 599 | if (read) |
| 600 | *read = code; |
| 601 | |
| 602 | if (ssl_mode_ == SSL_MODE_DTLS) { |
| 603 | // Enforce atomic reads -- this is a short read |
| 604 | unsigned int pending = SSL_pending(ssl_); |
| 605 | |
| 606 | if (pending) { |
| 607 | LOG(LS_INFO) << " -- short DTLS read. flushing"; |
| 608 | FlushInput(pending); |
| 609 | if (error) |
| 610 | *error = SSE_MSG_TRUNC; |
| 611 | return SR_ERROR; |
| 612 | } |
| 613 | } |
| 614 | return SR_SUCCESS; |
| 615 | case SSL_ERROR_WANT_READ: |
| 616 | LOG(LS_VERBOSE) << " -- error want read"; |
| 617 | return SR_BLOCK; |
| 618 | case SSL_ERROR_WANT_WRITE: |
| 619 | LOG(LS_VERBOSE) << " -- error want write"; |
| 620 | ssl_read_needs_write_ = true; |
| 621 | return SR_BLOCK; |
| 622 | case SSL_ERROR_ZERO_RETURN: |
| 623 | LOG(LS_VERBOSE) << " -- remote side closed"; |
guoweis | 4cc9f98 | 2016-02-24 11:10:06 -0800 | [diff] [blame] | 624 | // When we're closed at SSL layer, also close the stream level which |
| 625 | // performs necessary clean up. Otherwise, a new incoming packet after |
| 626 | // this could overflow the stream buffer. |
| 627 | this->stream()->Close(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 628 | return SR_EOS; |
| 629 | break; |
| 630 | default: |
| 631 | LOG(LS_VERBOSE) << " -- error " << code; |
| 632 | Error("SSL_read", (ssl_error ? ssl_error : -1), false); |
| 633 | if (error) |
| 634 | *error = ssl_error_code_; |
| 635 | return SR_ERROR; |
| 636 | } |
| 637 | // not reached |
| 638 | } |
| 639 | |
| 640 | void OpenSSLStreamAdapter::FlushInput(unsigned int left) { |
| 641 | unsigned char buf[2048]; |
| 642 | |
| 643 | while (left) { |
| 644 | // This should always succeed |
| 645 | int toread = (sizeof(buf) < left) ? sizeof(buf) : left; |
| 646 | int code = SSL_read(ssl_, buf, toread); |
| 647 | |
| 648 | int ssl_error = SSL_get_error(ssl_, code); |
| 649 | ASSERT(ssl_error == SSL_ERROR_NONE); |
| 650 | |
| 651 | if (ssl_error != SSL_ERROR_NONE) { |
| 652 | LOG(LS_VERBOSE) << " -- error " << code; |
| 653 | Error("SSL_read", (ssl_error ? ssl_error : -1), false); |
| 654 | return; |
| 655 | } |
| 656 | |
| 657 | LOG(LS_VERBOSE) << " -- flushed " << code << " bytes"; |
| 658 | left -= code; |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | void OpenSSLStreamAdapter::Close() { |
| 663 | Cleanup(); |
| 664 | ASSERT(state_ == SSL_CLOSED || state_ == SSL_ERROR); |
| 665 | StreamAdapterInterface::Close(); |
| 666 | } |
| 667 | |
| 668 | StreamState OpenSSLStreamAdapter::GetState() const { |
| 669 | switch (state_) { |
| 670 | case SSL_WAIT: |
| 671 | case SSL_CONNECTING: |
| 672 | return SS_OPENING; |
| 673 | case SSL_CONNECTED: |
| 674 | return SS_OPEN; |
| 675 | default: |
| 676 | return SS_CLOSED; |
| 677 | }; |
| 678 | // not reached |
| 679 | } |
| 680 | |
| 681 | void OpenSSLStreamAdapter::OnEvent(StreamInterface* stream, int events, |
| 682 | int err) { |
| 683 | int events_to_signal = 0; |
| 684 | int signal_error = 0; |
| 685 | ASSERT(stream == this->stream()); |
| 686 | if ((events & SE_OPEN)) { |
| 687 | LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::OnEvent SE_OPEN"; |
| 688 | if (state_ != SSL_WAIT) { |
| 689 | ASSERT(state_ == SSL_NONE); |
| 690 | events_to_signal |= SE_OPEN; |
| 691 | } else { |
| 692 | state_ = SSL_CONNECTING; |
| 693 | if (int err = BeginSSL()) { |
| 694 | Error("BeginSSL", err, true); |
| 695 | return; |
| 696 | } |
| 697 | } |
| 698 | } |
| 699 | if ((events & (SE_READ|SE_WRITE))) { |
| 700 | LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::OnEvent" |
| 701 | << ((events & SE_READ) ? " SE_READ" : "") |
| 702 | << ((events & SE_WRITE) ? " SE_WRITE" : ""); |
| 703 | if (state_ == SSL_NONE) { |
| 704 | events_to_signal |= events & (SE_READ|SE_WRITE); |
| 705 | } else if (state_ == SSL_CONNECTING) { |
| 706 | if (int err = ContinueSSL()) { |
| 707 | Error("ContinueSSL", err, true); |
| 708 | return; |
| 709 | } |
| 710 | } else if (state_ == SSL_CONNECTED) { |
| 711 | if (((events & SE_READ) && ssl_write_needs_read_) || |
| 712 | (events & SE_WRITE)) { |
| 713 | LOG(LS_VERBOSE) << " -- onStreamWriteable"; |
| 714 | events_to_signal |= SE_WRITE; |
| 715 | } |
| 716 | if (((events & SE_WRITE) && ssl_read_needs_write_) || |
| 717 | (events & SE_READ)) { |
| 718 | LOG(LS_VERBOSE) << " -- onStreamReadable"; |
| 719 | events_to_signal |= SE_READ; |
| 720 | } |
| 721 | } |
| 722 | } |
| 723 | if ((events & SE_CLOSE)) { |
| 724 | LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::OnEvent(SE_CLOSE, " << err << ")"; |
| 725 | Cleanup(); |
| 726 | events_to_signal |= SE_CLOSE; |
| 727 | // SE_CLOSE is the only event that uses the final parameter to OnEvent(). |
| 728 | ASSERT(signal_error == 0); |
| 729 | signal_error = err; |
| 730 | } |
| 731 | if (events_to_signal) |
| 732 | StreamAdapterInterface::OnEvent(stream, events_to_signal, signal_error); |
| 733 | } |
| 734 | |
| 735 | int OpenSSLStreamAdapter::StartSSL() { |
| 736 | ASSERT(state_ == SSL_NONE); |
| 737 | |
| 738 | if (StreamAdapterInterface::GetState() != SS_OPEN) { |
| 739 | state_ = SSL_WAIT; |
| 740 | return 0; |
| 741 | } |
| 742 | |
| 743 | state_ = SSL_CONNECTING; |
| 744 | if (int err = BeginSSL()) { |
| 745 | Error("BeginSSL", err, false); |
| 746 | return err; |
| 747 | } |
| 748 | |
| 749 | return 0; |
| 750 | } |
| 751 | |
| 752 | int OpenSSLStreamAdapter::BeginSSL() { |
| 753 | ASSERT(state_ == SSL_CONNECTING); |
| 754 | // The underlying stream has open. If we are in peer-to-peer mode |
| 755 | // then a peer certificate must have been specified by now. |
| 756 | ASSERT(!ssl_server_name_.empty() || |
| 757 | !peer_certificate_digest_algorithm_.empty()); |
| 758 | LOG(LS_INFO) << "BeginSSL: " |
| 759 | << (!ssl_server_name_.empty() ? ssl_server_name_ : |
| 760 | "with peer"); |
| 761 | |
| 762 | BIO* bio = NULL; |
| 763 | |
| 764 | // First set up the context |
| 765 | ASSERT(ssl_ctx_ == NULL); |
| 766 | ssl_ctx_ = SetupSSLContext(); |
| 767 | if (!ssl_ctx_) |
| 768 | return -1; |
| 769 | |
| 770 | bio = BIO_new_stream(static_cast<StreamInterface*>(stream())); |
| 771 | if (!bio) |
| 772 | return -1; |
| 773 | |
| 774 | ssl_ = SSL_new(ssl_ctx_); |
| 775 | if (!ssl_) { |
| 776 | BIO_free(bio); |
| 777 | return -1; |
| 778 | } |
| 779 | |
| 780 | SSL_set_app_data(ssl_, this); |
| 781 | |
| 782 | 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] | 783 | if (ssl_mode_ == SSL_MODE_DTLS) { |
Taylor Brandstetter | 4f0dfbd | 2016-06-15 17:15:23 -0700 | [diff] [blame] | 784 | #ifdef OPENSSL_IS_BORINGSSL |
| 785 | // Change the initial retransmission timer from 1 second to 50ms. |
| 786 | // This will likely result in some spurious retransmissions, but |
| 787 | // it's useful for ensuring a timely handshake when there's packet |
| 788 | // loss. |
| 789 | DTLSv1_set_initial_timeout_duration(ssl_, 50); |
| 790 | #else |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 791 | // Enable read-ahead for DTLS so whole packets are read from internal BIO |
| 792 | // before parsing. This is done internally by BoringSSL for DTLS. |
| 793 | SSL_set_read_ahead(ssl_, 1); |
philipel | 49c0869 | 2016-05-24 01:49:43 -0700 | [diff] [blame] | 794 | #endif |
Taylor Brandstetter | 4f0dfbd | 2016-06-15 17:15:23 -0700 | [diff] [blame] | 795 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 796 | |
| 797 | SSL_set_mode(ssl_, SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 798 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER); |
| 799 | |
David Benjamin | 60d5f3f | 2016-03-24 13:28:25 -0400 | [diff] [blame] | 800 | #if !defined(OPENSSL_IS_BORINGSSL) |
| 801 | // Specify an ECDH group for ECDHE ciphers, otherwise OpenSSL cannot |
| 802 | // negotiate them when acting as the server. Use NIST's P-256 which is |
| 803 | // commonly supported. BoringSSL doesn't need explicit configuration and has |
| 804 | // a reasonable default set. |
jiayl@webrtc.org | 11c6bde | 2014-08-28 16:14:38 +0000 | [diff] [blame] | 805 | EC_KEY* ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1); |
| 806 | if (ecdh == NULL) |
| 807 | return -1; |
| 808 | SSL_set_options(ssl_, SSL_OP_SINGLE_ECDH_USE); |
| 809 | SSL_set_tmp_ecdh(ssl_, ecdh); |
| 810 | EC_KEY_free(ecdh); |
David Benjamin | 60d5f3f | 2016-03-24 13:28:25 -0400 | [diff] [blame] | 811 | #endif |
jiayl@webrtc.org | 11c6bde | 2014-08-28 16:14:38 +0000 | [diff] [blame] | 812 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 813 | // Do the connect |
| 814 | return ContinueSSL(); |
| 815 | } |
| 816 | |
| 817 | int OpenSSLStreamAdapter::ContinueSSL() { |
| 818 | LOG(LS_VERBOSE) << "ContinueSSL"; |
| 819 | ASSERT(state_ == SSL_CONNECTING); |
| 820 | |
| 821 | // Clear the DTLS timer |
| 822 | Thread::Current()->Clear(this, MSG_TIMEOUT); |
| 823 | |
| 824 | int code = (role_ == SSL_CLIENT) ? SSL_connect(ssl_) : SSL_accept(ssl_); |
| 825 | int ssl_error; |
| 826 | switch (ssl_error = SSL_get_error(ssl_, code)) { |
| 827 | case SSL_ERROR_NONE: |
| 828 | LOG(LS_VERBOSE) << " -- success"; |
| 829 | |
| 830 | if (!SSLPostConnectionCheck(ssl_, ssl_server_name_.c_str(), NULL, |
| 831 | peer_certificate_digest_algorithm_)) { |
| 832 | LOG(LS_ERROR) << "TLS post connection check failed"; |
| 833 | return -1; |
| 834 | } |
| 835 | |
| 836 | state_ = SSL_CONNECTED; |
| 837 | StreamAdapterInterface::OnEvent(stream(), SE_OPEN|SE_READ|SE_WRITE, 0); |
| 838 | break; |
| 839 | |
| 840 | case SSL_ERROR_WANT_READ: { |
| 841 | LOG(LS_VERBOSE) << " -- error want read"; |
| 842 | struct timeval timeout; |
| 843 | if (DTLSv1_get_timeout(ssl_, &timeout)) { |
| 844 | int delay = timeout.tv_sec * 1000 + timeout.tv_usec/1000; |
| 845 | |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 846 | Thread::Current()->PostDelayed(RTC_FROM_HERE, delay, this, |
| 847 | MSG_TIMEOUT, 0); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 848 | } |
| 849 | } |
| 850 | break; |
| 851 | |
| 852 | case SSL_ERROR_WANT_WRITE: |
| 853 | LOG(LS_VERBOSE) << " -- error want write"; |
| 854 | break; |
| 855 | |
| 856 | case SSL_ERROR_ZERO_RETURN: |
| 857 | default: |
| 858 | LOG(LS_VERBOSE) << " -- error " << code; |
| 859 | return (ssl_error != 0) ? ssl_error : -1; |
| 860 | } |
| 861 | |
| 862 | return 0; |
| 863 | } |
| 864 | |
| 865 | void OpenSSLStreamAdapter::Error(const char* context, int err, bool signal) { |
| 866 | LOG(LS_WARNING) << "OpenSSLStreamAdapter::Error(" |
| 867 | << context << ", " << err << ")"; |
| 868 | state_ = SSL_ERROR; |
| 869 | ssl_error_code_ = err; |
| 870 | Cleanup(); |
| 871 | if (signal) |
| 872 | StreamAdapterInterface::OnEvent(stream(), SE_CLOSE, err); |
| 873 | } |
| 874 | |
| 875 | void OpenSSLStreamAdapter::Cleanup() { |
| 876 | LOG(LS_INFO) << "Cleanup"; |
| 877 | |
| 878 | if (state_ != SSL_ERROR) { |
| 879 | state_ = SSL_CLOSED; |
| 880 | ssl_error_code_ = 0; |
| 881 | } |
| 882 | |
| 883 | if (ssl_) { |
jiayl@webrtc.org | f1d751c | 2014-09-25 16:38:46 +0000 | [diff] [blame] | 884 | int ret = SSL_shutdown(ssl_); |
| 885 | if (ret < 0) { |
| 886 | LOG(LS_WARNING) << "SSL_shutdown failed, error = " |
| 887 | << SSL_get_error(ssl_, ret); |
| 888 | } |
| 889 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 890 | SSL_free(ssl_); |
| 891 | ssl_ = NULL; |
| 892 | } |
| 893 | if (ssl_ctx_) { |
| 894 | SSL_CTX_free(ssl_ctx_); |
| 895 | ssl_ctx_ = NULL; |
| 896 | } |
| 897 | identity_.reset(); |
| 898 | peer_certificate_.reset(); |
| 899 | |
| 900 | // Clear the DTLS timer |
| 901 | Thread::Current()->Clear(this, MSG_TIMEOUT); |
| 902 | } |
| 903 | |
| 904 | |
| 905 | void OpenSSLStreamAdapter::OnMessage(Message* msg) { |
| 906 | // Process our own messages and then pass others to the superclass |
| 907 | if (MSG_TIMEOUT == msg->message_id) { |
| 908 | LOG(LS_INFO) << "DTLS timeout expired"; |
| 909 | DTLSv1_handle_timeout(ssl_); |
| 910 | ContinueSSL(); |
| 911 | } else { |
| 912 | StreamInterface::OnMessage(msg); |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | SSL_CTX* OpenSSLStreamAdapter::SetupSSLContext() { |
| 917 | SSL_CTX *ctx = NULL; |
| 918 | |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 919 | #ifdef OPENSSL_IS_BORINGSSL |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 920 | ctx = SSL_CTX_new(ssl_mode_ == SSL_MODE_DTLS ? |
Joachim Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 921 | DTLS_method() : TLS_method()); |
| 922 | // Version limiting for BoringSSL will be done below. |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 923 | #else |
| 924 | const SSL_METHOD* method; |
| 925 | switch (ssl_max_version_) { |
| 926 | case SSL_PROTOCOL_TLS_10: |
| 927 | case SSL_PROTOCOL_TLS_11: |
| 928 | // OpenSSL doesn't support setting min/max versions, so we always use |
| 929 | // (D)TLS 1.0 if a max. version below the max. available is requested. |
| 930 | if (ssl_mode_ == SSL_MODE_DTLS) { |
| 931 | if (role_ == SSL_CLIENT) { |
| 932 | method = DTLSv1_client_method(); |
| 933 | } else { |
| 934 | method = DTLSv1_server_method(); |
| 935 | } |
| 936 | } else { |
| 937 | if (role_ == SSL_CLIENT) { |
| 938 | method = TLSv1_client_method(); |
| 939 | } else { |
| 940 | method = TLSv1_server_method(); |
| 941 | } |
| 942 | } |
| 943 | break; |
| 944 | case SSL_PROTOCOL_TLS_12: |
| 945 | default: |
| 946 | if (ssl_mode_ == SSL_MODE_DTLS) { |
| 947 | #if (OPENSSL_VERSION_NUMBER >= 0x10002000L) |
| 948 | // DTLS 1.2 only available starting from OpenSSL 1.0.2 |
| 949 | if (role_ == SSL_CLIENT) { |
| 950 | method = DTLS_client_method(); |
| 951 | } else { |
| 952 | method = DTLS_server_method(); |
| 953 | } |
| 954 | #else |
| 955 | if (role_ == SSL_CLIENT) { |
| 956 | method = DTLSv1_client_method(); |
| 957 | } else { |
| 958 | method = DTLSv1_server_method(); |
| 959 | } |
| 960 | #endif |
| 961 | } else { |
| 962 | #if (OPENSSL_VERSION_NUMBER >= 0x10100000L) |
| 963 | // New API only available starting from OpenSSL 1.1.0 |
| 964 | if (role_ == SSL_CLIENT) { |
| 965 | method = TLS_client_method(); |
| 966 | } else { |
| 967 | method = TLS_server_method(); |
| 968 | } |
| 969 | #else |
| 970 | if (role_ == SSL_CLIENT) { |
| 971 | method = SSLv23_client_method(); |
| 972 | } else { |
| 973 | method = SSLv23_server_method(); |
| 974 | } |
| 975 | #endif |
| 976 | } |
| 977 | break; |
| 978 | } |
| 979 | ctx = SSL_CTX_new(method); |
| 980 | #endif // OPENSSL_IS_BORINGSSL |
Joachim Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 981 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 982 | if (ctx == NULL) |
| 983 | return NULL; |
| 984 | |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 985 | #ifdef OPENSSL_IS_BORINGSSL |
Joachim Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 986 | SSL_CTX_set_min_version(ctx, ssl_mode_ == SSL_MODE_DTLS ? |
| 987 | DTLS1_VERSION : TLS1_VERSION); |
| 988 | switch (ssl_max_version_) { |
| 989 | case SSL_PROTOCOL_TLS_10: |
| 990 | SSL_CTX_set_max_version(ctx, ssl_mode_ == SSL_MODE_DTLS ? |
| 991 | DTLS1_VERSION : TLS1_VERSION); |
| 992 | break; |
| 993 | case SSL_PROTOCOL_TLS_11: |
| 994 | SSL_CTX_set_max_version(ctx, ssl_mode_ == SSL_MODE_DTLS ? |
| 995 | DTLS1_VERSION : TLS1_1_VERSION); |
| 996 | break; |
| 997 | case SSL_PROTOCOL_TLS_12: |
| 998 | default: |
| 999 | SSL_CTX_set_max_version(ctx, ssl_mode_ == SSL_MODE_DTLS ? |
| 1000 | DTLS1_2_VERSION : TLS1_2_VERSION); |
| 1001 | break; |
| 1002 | } |
Taylor Brandstetter | 4f0dfbd | 2016-06-15 17:15:23 -0700 | [diff] [blame] | 1003 | // Set a time callback for BoringSSL because: |
| 1004 | // 1. Our time function is more accurate (doesn't just use gettimeofday). |
| 1005 | // 2. This allows us to inject a fake clock for testing. |
deadbeef | d685fef | 2016-06-20 12:00:44 -0700 | [diff] [blame] | 1006 | SSL_CTX_set_current_time_cb(ctx, &TimeCallback); |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 1007 | #endif |
Joachim Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 1008 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1009 | if (identity_ && !identity_->ConfigureIdentity(ctx)) { |
| 1010 | SSL_CTX_free(ctx); |
| 1011 | return NULL; |
| 1012 | } |
| 1013 | |
tfarina | a41ab93 | 2015-10-30 16:08:48 -0700 | [diff] [blame] | 1014 | #if !defined(NDEBUG) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1015 | SSL_CTX_set_info_callback(ctx, OpenSSLAdapter::SSLInfoCallback); |
| 1016 | #endif |
| 1017 | |
tkchin@webrtc.org | c569a49 | 2014-09-23 05:56:44 +0000 | [diff] [blame] | 1018 | int mode = SSL_VERIFY_PEER; |
| 1019 | if (client_auth_enabled()) { |
| 1020 | // Require a certificate from the client. |
| 1021 | // Note: Normally this is always true in production, but it may be disabled |
| 1022 | // for testing purposes (e.g. SSLAdapter unit tests). |
| 1023 | mode |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 1024 | } |
| 1025 | |
| 1026 | SSL_CTX_set_verify(ctx, mode, SSLVerifyCallback); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1027 | SSL_CTX_set_verify_depth(ctx, 4); |
Joachim Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 1028 | // Select list of available ciphers. Note that !SHA256 and !SHA384 only |
| 1029 | // remove HMAC-SHA256 and HMAC-SHA384 cipher suites, not GCM cipher suites |
| 1030 | // with SHA256 or SHA384 as the handshake hash. |
| 1031 | // This matches the list of SSLClientSocketOpenSSL in Chromium. |
| 1032 | SSL_CTX_set_cipher_list(ctx, |
| 1033 | "DEFAULT:!NULL:!aNULL:!SHA256:!SHA384:!aECDH:!AESGCM+AES256:!aPSK"); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1034 | |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 1035 | #ifdef HAVE_DTLS_SRTP |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1036 | if (!srtp_ciphers_.empty()) { |
| 1037 | if (SSL_CTX_set_tlsext_use_srtp(ctx, srtp_ciphers_.c_str())) { |
| 1038 | SSL_CTX_free(ctx); |
| 1039 | return NULL; |
| 1040 | } |
| 1041 | } |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 1042 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1043 | |
| 1044 | return ctx; |
| 1045 | } |
| 1046 | |
| 1047 | int OpenSSLStreamAdapter::SSLVerifyCallback(int ok, X509_STORE_CTX* store) { |
| 1048 | // Get our SSL structure from the store |
| 1049 | SSL* ssl = reinterpret_cast<SSL*>(X509_STORE_CTX_get_ex_data( |
| 1050 | store, |
| 1051 | SSL_get_ex_data_X509_STORE_CTX_idx())); |
| 1052 | OpenSSLStreamAdapter* stream = |
| 1053 | reinterpret_cast<OpenSSLStreamAdapter*>(SSL_get_app_data(ssl)); |
| 1054 | |
| 1055 | if (stream->peer_certificate_digest_algorithm_.empty()) { |
| 1056 | return 0; |
| 1057 | } |
| 1058 | X509* cert = X509_STORE_CTX_get_current_cert(store); |
henrike@webrtc.org | 4e5f65a | 2014-06-05 20:40:11 +0000 | [diff] [blame] | 1059 | int depth = X509_STORE_CTX_get_error_depth(store); |
| 1060 | |
| 1061 | // For now We ignore the parent certificates and verify the leaf against |
| 1062 | // the digest. |
| 1063 | // |
| 1064 | // 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] | 1065 | // |stream->peer_certificate_|. |
henrike@webrtc.org | 4e5f65a | 2014-06-05 20:40:11 +0000 | [diff] [blame] | 1066 | if (depth > 0) { |
| 1067 | LOG(LS_INFO) << "Ignored chained certificate at depth " << depth; |
| 1068 | return 1; |
| 1069 | } |
| 1070 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1071 | unsigned char digest[EVP_MAX_MD_SIZE]; |
| 1072 | size_t digest_length; |
| 1073 | if (!OpenSSLCertificate::ComputeDigest( |
| 1074 | cert, |
| 1075 | stream->peer_certificate_digest_algorithm_, |
| 1076 | digest, sizeof(digest), |
| 1077 | &digest_length)) { |
| 1078 | LOG(LS_WARNING) << "Failed to compute peer cert digest."; |
| 1079 | return 0; |
| 1080 | } |
henrike@webrtc.org | 4e5f65a | 2014-06-05 20:40:11 +0000 | [diff] [blame] | 1081 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1082 | Buffer computed_digest(digest, digest_length); |
| 1083 | if (computed_digest != stream->peer_certificate_digest_value_) { |
| 1084 | LOG(LS_WARNING) << "Rejected peer certificate due to mismatched digest."; |
| 1085 | return 0; |
| 1086 | } |
| 1087 | // Ignore any verification error if the digest matches, since there is no |
| 1088 | // value in checking the validity of a self-signed cert issued by untrusted |
| 1089 | // sources. |
| 1090 | LOG(LS_INFO) << "Accepted peer certificate."; |
henrike@webrtc.org | 4e5f65a | 2014-06-05 20:40:11 +0000 | [diff] [blame] | 1091 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1092 | // Record the peer's certificate. |
| 1093 | stream->peer_certificate_.reset(new OpenSSLCertificate(cert)); |
| 1094 | return 1; |
| 1095 | } |
| 1096 | |
| 1097 | // This code is taken from the "Network Security with OpenSSL" |
| 1098 | // sample in chapter 5 |
| 1099 | bool OpenSSLStreamAdapter::SSLPostConnectionCheck(SSL* ssl, |
| 1100 | const char* server_name, |
| 1101 | const X509* peer_cert, |
| 1102 | const std::string |
| 1103 | &peer_digest) { |
| 1104 | ASSERT(server_name != NULL); |
| 1105 | bool ok; |
| 1106 | if (server_name[0] != '\0') { // traditional mode |
| 1107 | ok = OpenSSLAdapter::VerifyServerName(ssl, server_name, ignore_bad_cert()); |
| 1108 | |
| 1109 | if (ok) { |
| 1110 | ok = (SSL_get_verify_result(ssl) == X509_V_OK || |
| 1111 | custom_verification_succeeded_); |
| 1112 | } |
| 1113 | } else { // peer-to-peer mode |
| 1114 | ASSERT((peer_cert != NULL) || (!peer_digest.empty())); |
| 1115 | // no server name validation |
| 1116 | ok = true; |
| 1117 | } |
| 1118 | |
| 1119 | if (!ok && ignore_bad_cert()) { |
| 1120 | LOG(LS_ERROR) << "SSL_get_verify_result(ssl) = " |
| 1121 | << SSL_get_verify_result(ssl); |
| 1122 | LOG(LS_INFO) << "Other TLS post connection checks failed."; |
| 1123 | ok = true; |
| 1124 | } |
| 1125 | |
| 1126 | return ok; |
| 1127 | } |
| 1128 | |
| 1129 | bool OpenSSLStreamAdapter::HaveDtls() { |
| 1130 | return true; |
| 1131 | } |
| 1132 | |
| 1133 | bool OpenSSLStreamAdapter::HaveDtlsSrtp() { |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 1134 | #ifdef HAVE_DTLS_SRTP |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1135 | return true; |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 1136 | #else |
| 1137 | return false; |
| 1138 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
| 1141 | bool OpenSSLStreamAdapter::HaveExporter() { |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 1142 | #ifdef HAVE_DTLS_SRTP |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1143 | return true; |
Torbjorn Granlund | 9adc91d | 2016-03-24 14:05:06 +0100 | [diff] [blame] | 1144 | #else |
| 1145 | return false; |
| 1146 | #endif |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
Taylor Brandstetter | 4f0dfbd | 2016-06-15 17:15:23 -0700 | [diff] [blame] | 1149 | bool OpenSSLStreamAdapter::IsBoringSsl() { |
| 1150 | #ifdef OPENSSL_IS_BORINGSSL |
| 1151 | return true; |
| 1152 | #else |
| 1153 | return false; |
| 1154 | #endif |
| 1155 | } |
| 1156 | |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1157 | #define CDEF(X) \ |
| 1158 | { static_cast<uint16_t>(TLS1_CK_##X & 0xffff), "TLS_" #X } |
| 1159 | |
| 1160 | struct cipher_list { |
| 1161 | uint16_t cipher; |
| 1162 | const char* cipher_str; |
| 1163 | }; |
| 1164 | |
| 1165 | // TODO(torbjorng): Perhaps add more cipher suites to these lists. |
| 1166 | static const cipher_list OK_RSA_ciphers[] = { |
| 1167 | CDEF(ECDHE_RSA_WITH_AES_128_CBC_SHA), |
| 1168 | CDEF(ECDHE_RSA_WITH_AES_256_CBC_SHA), |
| 1169 | CDEF(ECDHE_RSA_WITH_AES_128_GCM_SHA256), |
| 1170 | #ifdef TLS1_CK_ECDHE_RSA_WITH_AES_256_GCM_SHA256 |
| 1171 | CDEF(ECDHE_RSA_WITH_AES_256_GCM_SHA256), |
| 1172 | #endif |
torbjorng | aad6780 | 2016-04-07 08:55:28 -0700 | [diff] [blame] | 1173 | #ifdef TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1174 | CDEF(ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256), |
torbjorng | aad6780 | 2016-04-07 08:55:28 -0700 | [diff] [blame] | 1175 | #endif |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1176 | }; |
| 1177 | |
| 1178 | static const cipher_list OK_ECDSA_ciphers[] = { |
| 1179 | CDEF(ECDHE_ECDSA_WITH_AES_128_CBC_SHA), |
| 1180 | CDEF(ECDHE_ECDSA_WITH_AES_256_CBC_SHA), |
| 1181 | CDEF(ECDHE_ECDSA_WITH_AES_128_GCM_SHA256), |
| 1182 | #ifdef TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA256 |
| 1183 | CDEF(ECDHE_ECDSA_WITH_AES_256_GCM_SHA256), |
| 1184 | #endif |
torbjorng | aad6780 | 2016-04-07 08:55:28 -0700 | [diff] [blame] | 1185 | #ifdef TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1186 | CDEF(ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256), |
torbjorng | aad6780 | 2016-04-07 08:55:28 -0700 | [diff] [blame] | 1187 | #endif |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1188 | }; |
| 1189 | #undef CDEF |
| 1190 | |
| 1191 | bool OpenSSLStreamAdapter::IsAcceptableCipher(int cipher, KeyType key_type) { |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 1192 | if (key_type == KT_RSA) { |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1193 | for (const cipher_list& c : OK_RSA_ciphers) { |
| 1194 | if (cipher == c.cipher) |
| 1195 | return true; |
Torbjorn Granlund | b6d4ec4 | 2015-08-17 14:08:59 +0200 | [diff] [blame] | 1196 | } |
Joachim Bauch | 831c558 | 2015-05-20 12:48:41 +0200 | [diff] [blame] | 1197 | } |
torbjorng | 43166b8 | 2016-03-11 00:06:47 -0800 | [diff] [blame] | 1198 | |
| 1199 | if (key_type == KT_ECDSA) { |
| 1200 | for (const cipher_list& c : OK_ECDSA_ciphers) { |
| 1201 | if (cipher == c.cipher) |
| 1202 | return true; |
| 1203 | } |
| 1204 | } |
| 1205 | |
| 1206 | return false; |
| 1207 | } |
| 1208 | |
| 1209 | bool OpenSSLStreamAdapter::IsAcceptableCipher(const std::string& cipher, |
| 1210 | KeyType key_type) { |
| 1211 | if (key_type == KT_RSA) { |
| 1212 | for (const cipher_list& c : OK_RSA_ciphers) { |
| 1213 | if (cipher == c.cipher_str) |
| 1214 | return true; |
| 1215 | } |
| 1216 | } |
| 1217 | |
| 1218 | if (key_type == KT_ECDSA) { |
| 1219 | for (const cipher_list& c : OK_ECDSA_ciphers) { |
| 1220 | if (cipher == c.cipher_str) |
| 1221 | return true; |
| 1222 | } |
| 1223 | } |
| 1224 | |
| 1225 | return false; |
pthatcher@webrtc.org | 3ee4fe5 | 2015-02-11 22:34:36 +0000 | [diff] [blame] | 1226 | } |
| 1227 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1228 | } // namespace rtc |
| 1229 | |
| 1230 | #endif // HAVE_OPENSSL_SSL_H |