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