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