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