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