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