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