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