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