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