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