blob: c1ce2c6def71b68ebff9fd0eaac0df069ec8c6a5 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "rtc_base/opensslstreamadapter.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000012
13#include <openssl/bio.h>
14#include <openssl/crypto.h>
15#include <openssl/err.h>
16#include <openssl/rand.h>
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +000017#include <openssl/tls1.h>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000018#include <openssl/x509v3.h>
torbjorngaad67802016-04-07 08:55:28 -070019#ifndef OPENSSL_IS_BORINGSSL
20#include <openssl/dtls1.h>
ssarohabbfed522016-12-11 18:42:07 -080021#include <openssl/ssl.h>
torbjorngaad67802016-04-07 08:55:28 -070022#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000023
jbauch555604a2016-04-26 03:13:22 -070024#include <memory>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000025#include <vector>
26
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "rtc_base/checks.h"
28#include "rtc_base/logging.h"
29#include "rtc_base/openssl.h"
30#include "rtc_base/openssladapter.h"
31#include "rtc_base/openssldigest.h"
32#include "rtc_base/opensslidentity.h"
33#include "rtc_base/safe_conversions.h"
34#include "rtc_base/stream.h"
35#include "rtc_base/stringutils.h"
36#include "rtc_base/thread.h"
37#include "rtc_base/timeutils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000038
deadbeef6cf94a02016-11-28 17:38:34 -080039namespace {
40 bool g_use_time_callback_for_testing = false;
41}
42
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000043namespace rtc {
44
deadbeef1b54a5f2017-01-23 19:39:57 -080045#if (OPENSSL_VERSION_NUMBER < 0x10001000L)
46#error "webrtc requires at least OpenSSL version 1.0.1, to support DTLS-SRTP"
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +010047#endif
48
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -080049// SRTP cipher suite table. |internal_name| is used to construct a
50// colon-separated profile strings which is needed by
51// SSL_CTX_set_tlsext_use_srtp().
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000052struct SrtpCipherMapEntry {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000053 const char* internal_name;
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -080054 const int id;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000055};
56
57// This isn't elegant, but it's better than an external reference
58static SrtpCipherMapEntry SrtpCipherMap[] = {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -080059 {"SRTP_AES128_CM_SHA1_80", SRTP_AES128_CM_SHA1_80},
60 {"SRTP_AES128_CM_SHA1_32", SRTP_AES128_CM_SHA1_32},
jbauchcb560652016-08-04 05:20:32 -070061 {"SRTP_AEAD_AES_128_GCM", SRTP_AEAD_AES_128_GCM},
62 {"SRTP_AEAD_AES_256_GCM", SRTP_AEAD_AES_256_GCM},
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -080063 {nullptr, 0}};
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +010064
Taylor Brandstetter4f0dfbd2016-06-15 17:15:23 -070065#ifdef OPENSSL_IS_BORINGSSL
deadbeef6cf94a02016-11-28 17:38:34 -080066// Not used in production code. Actual time should be relative to Jan 1, 1970.
67static void TimeCallbackForTesting(const SSL* ssl, struct timeval* out_clock) {
nissedeb95f32016-11-28 01:54:54 -080068 int64_t time = TimeNanos();
Taylor Brandstetter4f0dfbd2016-06-15 17:15:23 -070069 out_clock->tv_sec = time / kNumNanosecsPerSec;
deadbeef7a07f132016-11-21 14:33:57 -080070 out_clock->tv_usec = (time % kNumNanosecsPerSec) / kNumNanosecsPerMicrosec;
Taylor Brandstetter4f0dfbd2016-06-15 17:15:23 -070071}
72#else // #ifdef OPENSSL_IS_BORINGSSL
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +010073
74// Cipher name table. Maps internal OpenSSL cipher ids to the RFC name.
75struct SslCipherMapEntry {
76 uint32_t openssl_id;
77 const char* rfc_name;
78};
79
80#define DEFINE_CIPHER_ENTRY_SSL3(name) {SSL3_CK_##name, "TLS_"#name}
81#define DEFINE_CIPHER_ENTRY_TLS1(name) {TLS1_CK_##name, "TLS_"#name}
82
83// There currently is no method available to get a RFC-compliant name for a
84// cipher suite from BoringSSL, so we need to define the mapping manually here.
85// This should go away once BoringSSL supports "SSL_CIPHER_standard_name"
86// (as available in OpenSSL if compiled with tracing enabled) or a similar
87// method.
88static const SslCipherMapEntry kSslCipherMap[] = {
deadbeef37f5ecf2017-02-27 14:06:41 -080089 // TLS v1.0 ciphersuites from RFC2246.
90 DEFINE_CIPHER_ENTRY_SSL3(RSA_RC4_128_SHA),
91 {SSL3_CK_RSA_DES_192_CBC3_SHA, "TLS_RSA_WITH_3DES_EDE_CBC_SHA"},
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +010092
deadbeef37f5ecf2017-02-27 14:06:41 -080093 // AES ciphersuites from RFC3268.
94 {TLS1_CK_RSA_WITH_AES_128_SHA, "TLS_RSA_WITH_AES_128_CBC_SHA"},
95 {TLS1_CK_DHE_RSA_WITH_AES_128_SHA, "TLS_DHE_RSA_WITH_AES_128_CBC_SHA"},
96 {TLS1_CK_RSA_WITH_AES_256_SHA, "TLS_RSA_WITH_AES_256_CBC_SHA"},
97 {TLS1_CK_DHE_RSA_WITH_AES_256_SHA, "TLS_DHE_RSA_WITH_AES_256_CBC_SHA"},
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +010098
deadbeef37f5ecf2017-02-27 14:06:41 -080099 // ECC ciphersuites from RFC4492.
100 DEFINE_CIPHER_ENTRY_TLS1(ECDHE_ECDSA_WITH_RC4_128_SHA),
101 {TLS1_CK_ECDHE_ECDSA_WITH_DES_192_CBC3_SHA,
102 "TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA"},
103 DEFINE_CIPHER_ENTRY_TLS1(ECDHE_ECDSA_WITH_AES_128_CBC_SHA),
104 DEFINE_CIPHER_ENTRY_TLS1(ECDHE_ECDSA_WITH_AES_256_CBC_SHA),
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100105
deadbeef37f5ecf2017-02-27 14:06:41 -0800106 DEFINE_CIPHER_ENTRY_TLS1(ECDHE_RSA_WITH_RC4_128_SHA),
107 {TLS1_CK_ECDHE_RSA_WITH_DES_192_CBC3_SHA,
108 "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA"},
109 DEFINE_CIPHER_ENTRY_TLS1(ECDHE_RSA_WITH_AES_128_CBC_SHA),
110 DEFINE_CIPHER_ENTRY_TLS1(ECDHE_RSA_WITH_AES_256_CBC_SHA),
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100111
deadbeef37f5ecf2017-02-27 14:06:41 -0800112 // TLS v1.2 ciphersuites.
113 {TLS1_CK_RSA_WITH_AES_128_SHA256, "TLS_RSA_WITH_AES_128_CBC_SHA256"},
114 {TLS1_CK_RSA_WITH_AES_256_SHA256, "TLS_RSA_WITH_AES_256_CBC_SHA256"},
115 {TLS1_CK_DHE_RSA_WITH_AES_128_SHA256,
116 "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256"},
117 {TLS1_CK_DHE_RSA_WITH_AES_256_SHA256,
118 "TLS_DHE_RSA_WITH_AES_256_CBC_SHA256"},
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100119
deadbeef37f5ecf2017-02-27 14:06:41 -0800120 // TLS v1.2 GCM ciphersuites from RFC5288.
121 DEFINE_CIPHER_ENTRY_TLS1(RSA_WITH_AES_128_GCM_SHA256),
122 DEFINE_CIPHER_ENTRY_TLS1(RSA_WITH_AES_256_GCM_SHA384),
123 DEFINE_CIPHER_ENTRY_TLS1(DHE_RSA_WITH_AES_128_GCM_SHA256),
124 DEFINE_CIPHER_ENTRY_TLS1(DHE_RSA_WITH_AES_256_GCM_SHA384),
125 DEFINE_CIPHER_ENTRY_TLS1(DH_RSA_WITH_AES_128_GCM_SHA256),
126 DEFINE_CIPHER_ENTRY_TLS1(DH_RSA_WITH_AES_256_GCM_SHA384),
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100127
deadbeef37f5ecf2017-02-27 14:06:41 -0800128 // ECDH HMAC based ciphersuites from RFC5289.
129 {TLS1_CK_ECDHE_ECDSA_WITH_AES_128_SHA256,
130 "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256"},
131 {TLS1_CK_ECDHE_ECDSA_WITH_AES_256_SHA384,
132 "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384"},
133 {TLS1_CK_ECDHE_RSA_WITH_AES_128_SHA256,
134 "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256"},
135 {TLS1_CK_ECDHE_RSA_WITH_AES_256_SHA384,
136 "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384"},
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100137
deadbeef37f5ecf2017-02-27 14:06:41 -0800138 // ECDH GCM based ciphersuites from RFC5289.
139 DEFINE_CIPHER_ENTRY_TLS1(ECDHE_ECDSA_WITH_AES_128_GCM_SHA256),
140 DEFINE_CIPHER_ENTRY_TLS1(ECDHE_ECDSA_WITH_AES_256_GCM_SHA384),
141 DEFINE_CIPHER_ENTRY_TLS1(ECDHE_RSA_WITH_AES_128_GCM_SHA256),
142 DEFINE_CIPHER_ENTRY_TLS1(ECDHE_RSA_WITH_AES_256_GCM_SHA384),
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100143
deadbeef37f5ecf2017-02-27 14:06:41 -0800144 {0, nullptr}};
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100145#endif // #ifndef OPENSSL_IS_BORINGSSL
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000146
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700147#if defined(_MSC_VER)
148#pragma warning(push)
149#pragma warning(disable : 4309)
150#pragma warning(disable : 4310)
151#endif // defined(_MSC_VER)
152
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700153#if defined(_MSC_VER)
154#pragma warning(pop)
155#endif // defined(_MSC_VER)
156
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000157//////////////////////////////////////////////////////////////////////
158// StreamBIO
159//////////////////////////////////////////////////////////////////////
160
161static int stream_write(BIO* h, const char* buf, int num);
162static int stream_read(BIO* h, char* buf, int size);
163static int stream_puts(BIO* h, const char* str);
164static long stream_ctrl(BIO* h, int cmd, long arg1, void* arg2);
165static int stream_new(BIO* h);
166static int stream_free(BIO* data);
167
David Benjamin85aa0b62017-09-28 16:42:58 -0400168static const BIO_METHOD methods_stream = {
deadbeef37f5ecf2017-02-27 14:06:41 -0800169 BIO_TYPE_BIO, "stream", stream_write, stream_read, stream_puts, 0,
170 stream_ctrl, stream_new, stream_free, nullptr,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000171};
172
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000173static BIO* BIO_new_stream(StreamInterface* stream) {
David Benjamin85aa0b62017-09-28 16:42:58 -0400174 // TODO(davidben): Remove the const_cast when BoringSSL is assumed.
175 BIO* ret = BIO_new(const_cast<BIO_METHOD*>(&methods_stream));
deadbeef37f5ecf2017-02-27 14:06:41 -0800176 if (ret == nullptr)
177 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000178 ret->ptr = stream;
179 return ret;
180}
181
182// bio methods return 1 (or at least non-zero) on success and 0 on failure.
183
184static int stream_new(BIO* b) {
185 b->shutdown = 0;
186 b->init = 1;
187 b->num = 0; // 1 means end-of-stream
188 b->ptr = 0;
189 return 1;
190}
191
192static int stream_free(BIO* b) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800193 if (b == nullptr)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000194 return 0;
195 return 1;
196}
197
198static int stream_read(BIO* b, char* out, int outl) {
199 if (!out)
200 return -1;
201 StreamInterface* stream = static_cast<StreamInterface*>(b->ptr);
202 BIO_clear_retry_flags(b);
203 size_t read;
204 int error;
205 StreamResult result = stream->Read(out, outl, &read, &error);
206 if (result == SR_SUCCESS) {
henrike@webrtc.orgd89b69a2014-11-06 17:23:09 +0000207 return checked_cast<int>(read);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000208 } else if (result == SR_EOS) {
209 b->num = 1;
210 } else if (result == SR_BLOCK) {
211 BIO_set_retry_read(b);
212 }
213 return -1;
214}
215
216static int stream_write(BIO* b, const char* in, int inl) {
217 if (!in)
218 return -1;
219 StreamInterface* stream = static_cast<StreamInterface*>(b->ptr);
220 BIO_clear_retry_flags(b);
221 size_t written;
222 int error;
223 StreamResult result = stream->Write(in, inl, &written, &error);
224 if (result == SR_SUCCESS) {
henrike@webrtc.orgd89b69a2014-11-06 17:23:09 +0000225 return checked_cast<int>(written);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000226 } else if (result == SR_BLOCK) {
227 BIO_set_retry_write(b);
228 }
229 return -1;
230}
231
232static int stream_puts(BIO* b, const char* str) {
henrike@webrtc.orgd89b69a2014-11-06 17:23:09 +0000233 return stream_write(b, str, checked_cast<int>(strlen(str)));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000234}
235
236static long stream_ctrl(BIO* b, int cmd, long num, void* ptr) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000237 switch (cmd) {
238 case BIO_CTRL_RESET:
239 return 0;
240 case BIO_CTRL_EOF:
241 return b->num;
242 case BIO_CTRL_WPENDING:
243 case BIO_CTRL_PENDING:
244 return 0;
245 case BIO_CTRL_FLUSH:
246 return 1;
Henrik Lundinf4baca52015-06-10 09:45:58 +0200247 case BIO_CTRL_DGRAM_QUERY_MTU:
248 // openssl defaults to mtu=256 unless we return something here.
249 // The handshake doesn't actually need to send packets above 1k,
250 // so this seems like a sensible value that should work in most cases.
251 // Webrtc uses the same value for video packets.
252 return 1200;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000253 default:
254 return 0;
255 }
256}
257
258/////////////////////////////////////////////////////////////////////////////
259// OpenSSLStreamAdapter
260/////////////////////////////////////////////////////////////////////////////
261
262OpenSSLStreamAdapter::OpenSSLStreamAdapter(StreamInterface* stream)
263 : SSLStreamAdapter(stream),
264 state_(SSL_NONE),
265 role_(SSL_CLIENT),
Guo-wei Shieha7446d22016-01-11 15:27:03 -0800266 ssl_read_needs_write_(false),
267 ssl_write_needs_read_(false),
deadbeef37f5ecf2017-02-27 14:06:41 -0800268 ssl_(nullptr),
269 ssl_ctx_(nullptr),
Joachim Bauch831c5582015-05-20 12:48:41 +0200270 ssl_mode_(SSL_MODE_TLS),
Guo-wei Shieha7446d22016-01-11 15:27:03 -0800271 ssl_max_version_(SSL_PROTOCOL_TLS_12) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000272
273OpenSSLStreamAdapter::~OpenSSLStreamAdapter() {
deadbeef89824f62016-09-30 11:55:43 -0700274 Cleanup(0);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000275}
276
277void OpenSSLStreamAdapter::SetIdentity(SSLIdentity* identity) {
deadbeef89824f62016-09-30 11:55:43 -0700278 RTC_DCHECK(!identity_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000279 identity_.reset(static_cast<OpenSSLIdentity*>(identity));
280}
281
282void OpenSSLStreamAdapter::SetServerRole(SSLRole role) {
283 role_ = role;
284}
285
jbauch555604a2016-04-26 03:13:22 -0700286std::unique_ptr<SSLCertificate> OpenSSLStreamAdapter::GetPeerCertificate()
kwibergb4d01c42016-04-06 05:15:06 -0700287 const {
jbauch555604a2016-04-26 03:13:22 -0700288 return peer_certificate_ ? std::unique_ptr<SSLCertificate>(
kwibergb4d01c42016-04-06 05:15:06 -0700289 peer_certificate_->GetReference())
290 : nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000291}
292
deadbeef89824f62016-09-30 11:55:43 -0700293bool OpenSSLStreamAdapter::SetPeerCertificateDigest(
294 const std::string& digest_alg,
295 const unsigned char* digest_val,
296 size_t digest_len,
297 SSLPeerCertificateDigestError* error) {
298 RTC_DCHECK(!peer_certificate_verified_);
299 RTC_DCHECK(!has_peer_certificate_digest());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000300 size_t expected_len;
deadbeef89824f62016-09-30 11:55:43 -0700301 if (error) {
302 *error = SSLPeerCertificateDigestError::NONE;
303 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000304
305 if (!OpenSSLDigest::GetDigestSize(digest_alg, &expected_len)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100306 RTC_LOG(LS_WARNING) << "Unknown digest algorithm: " << digest_alg;
deadbeef89824f62016-09-30 11:55:43 -0700307 if (error) {
308 *error = SSLPeerCertificateDigestError::UNKNOWN_ALGORITHM;
309 }
deadbeef81f6f4f2016-09-19 17:20:52 -0700310 return false;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000311 }
deadbeef89824f62016-09-30 11:55:43 -0700312 if (expected_len != digest_len) {
313 if (error) {
314 *error = SSLPeerCertificateDigestError::INVALID_LENGTH;
315 }
deadbeef81f6f4f2016-09-19 17:20:52 -0700316 return false;
deadbeef89824f62016-09-30 11:55:43 -0700317 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000318
319 peer_certificate_digest_value_.SetData(digest_val, digest_len);
320 peer_certificate_digest_algorithm_ = digest_alg;
321
deadbeef89824f62016-09-30 11:55:43 -0700322 if (!peer_certificate_) {
323 // Normal case, where the digest is set before we obtain the certificate
324 // from the handshake.
325 return true;
326 }
327
328 if (!VerifyPeerCertificate()) {
329 Error("SetPeerCertificateDigest", -1, SSL_AD_BAD_CERTIFICATE, false);
330 if (error) {
331 *error = SSLPeerCertificateDigestError::VERIFICATION_FAILED;
332 }
333 return false;
334 }
335
336 if (state_ == SSL_CONNECTED) {
337 // Post the event asynchronously to unwind the stack. The caller
338 // of ContinueSSL may be the same object listening for these
339 // events and may not be prepared for reentrancy.
340 PostEvent(SE_OPEN | SE_READ | SE_WRITE, 0);
341 }
342
deadbeef81f6f4f2016-09-19 17:20:52 -0700343 return true;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000344}
345
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800346std::string OpenSSLStreamAdapter::SslCipherSuiteToName(int cipher_suite) {
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100347#ifdef OPENSSL_IS_BORINGSSL
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800348 const SSL_CIPHER* ssl_cipher = SSL_get_cipher_by_value(cipher_suite);
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700349 if (!ssl_cipher) {
350 return std::string();
351 }
David Benjamina8f73762017-09-28 15:49:42 -0400352 return SSL_CIPHER_standard_name(ssl_cipher);
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100353#else
354 for (const SslCipherMapEntry* entry = kSslCipherMap; entry->rfc_name;
355 ++entry) {
356 if (cipher_suite == static_cast<int>(entry->openssl_id)) {
357 return entry->rfc_name;
358 }
359 }
360 return std::string();
361#endif
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700362}
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000363
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800364bool OpenSSLStreamAdapter::GetSslCipherSuite(int* cipher_suite) {
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000365 if (state_ != SSL_CONNECTED)
366 return false;
367
368 const SSL_CIPHER* current_cipher = SSL_get_current_cipher(ssl_);
deadbeef37f5ecf2017-02-27 14:06:41 -0800369 if (current_cipher == nullptr) {
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000370 return false;
371 }
372
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800373 *cipher_suite = static_cast<uint16_t>(SSL_CIPHER_get_id(current_cipher));
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000374 return true;
375}
376
torbjorng43166b82016-03-11 00:06:47 -0800377int OpenSSLStreamAdapter::GetSslVersion() const {
378 if (state_ != SSL_CONNECTED)
379 return -1;
380
381 int ssl_version = SSL_version(ssl_);
382 if (ssl_mode_ == SSL_MODE_DTLS) {
383 if (ssl_version == DTLS1_VERSION)
384 return SSL_PROTOCOL_DTLS_10;
385 else if (ssl_version == DTLS1_2_VERSION)
386 return SSL_PROTOCOL_DTLS_12;
387 } else {
388 if (ssl_version == TLS1_VERSION)
389 return SSL_PROTOCOL_TLS_10;
390 else if (ssl_version == TLS1_1_VERSION)
391 return SSL_PROTOCOL_TLS_11;
392 else if (ssl_version == TLS1_2_VERSION)
393 return SSL_PROTOCOL_TLS_12;
394 }
395
396 return -1;
397}
398
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000399// Key Extractor interface
400bool OpenSSLStreamAdapter::ExportKeyingMaterial(const std::string& label,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200401 const uint8_t* context,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000402 size_t context_len,
403 bool use_context,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200404 uint8_t* result,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000405 size_t result_len) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000406 int i;
407
Peter Boström0c4e06b2015-10-07 12:23:21 +0200408 i = SSL_export_keying_material(ssl_, result, result_len, label.c_str(),
409 label.length(), const_cast<uint8_t*>(context),
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000410 context_len, use_context);
411
412 if (i != 1)
413 return false;
414
415 return true;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000416}
417
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800418bool OpenSSLStreamAdapter::SetDtlsSrtpCryptoSuites(
419 const std::vector<int>& ciphers) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000420 std::string internal_ciphers;
421
422 if (state_ != SSL_NONE)
423 return false;
424
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800425 for (std::vector<int>::const_iterator cipher = ciphers.begin();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000426 cipher != ciphers.end(); ++cipher) {
427 bool found = false;
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800428 for (SrtpCipherMapEntry* entry = SrtpCipherMap; entry->internal_name;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000429 ++entry) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800430 if (*cipher == entry->id) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000431 found = true;
432 if (!internal_ciphers.empty())
433 internal_ciphers += ":";
434 internal_ciphers += entry->internal_name;
435 break;
436 }
437 }
438
439 if (!found) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100440 RTC_LOG(LS_ERROR) << "Could not find cipher: " << *cipher;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000441 return false;
442 }
443 }
444
445 if (internal_ciphers.empty())
446 return false;
447
448 srtp_ciphers_ = internal_ciphers;
449 return true;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000450}
451
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800452bool OpenSSLStreamAdapter::GetDtlsSrtpCryptoSuite(int* crypto_suite) {
deadbeef89824f62016-09-30 11:55:43 -0700453 RTC_DCHECK(state_ == SSL_CONNECTED);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000454 if (state_ != SSL_CONNECTED)
455 return false;
456
henrike@webrtc.orgc10ecea2015-01-07 17:59:28 +0000457 const SRTP_PROTECTION_PROFILE *srtp_profile =
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000458 SSL_get_selected_srtp_profile(ssl_);
459
460 if (!srtp_profile)
461 return false;
462
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800463 *crypto_suite = srtp_profile->id;
deadbeef89824f62016-09-30 11:55:43 -0700464 RTC_DCHECK(!SrtpCryptoSuiteToName(*crypto_suite).empty());
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800465 return true;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000466}
467
deadbeef89824f62016-09-30 11:55:43 -0700468bool OpenSSLStreamAdapter::IsTlsConnected() {
469 return state_ == SSL_CONNECTED;
470}
471
Taylor Brandstetterc8762a82016-08-11 12:01:49 -0700472int OpenSSLStreamAdapter::StartSSL() {
deadbeef89824f62016-09-30 11:55:43 -0700473 if (state_ != SSL_NONE) {
474 // Don't allow StartSSL to be called twice.
475 return -1;
476 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000477
Taylor Brandstetterc8762a82016-08-11 12:01:49 -0700478 if (StreamAdapterInterface::GetState() != SS_OPEN) {
479 state_ = SSL_WAIT;
480 return 0;
481 }
482
483 state_ = SSL_CONNECTING;
484 if (int err = BeginSSL()) {
deadbeef89824f62016-09-30 11:55:43 -0700485 Error("BeginSSL", err, 0, false);
Taylor Brandstetterc8762a82016-08-11 12:01:49 -0700486 return err;
487 }
488
489 return 0;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000490}
491
492void OpenSSLStreamAdapter::SetMode(SSLMode mode) {
deadbeef89824f62016-09-30 11:55:43 -0700493 RTC_DCHECK(state_ == SSL_NONE);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000494 ssl_mode_ = mode;
495}
496
Joachim Bauch831c5582015-05-20 12:48:41 +0200497void OpenSSLStreamAdapter::SetMaxProtocolVersion(SSLProtocolVersion version) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800498 RTC_DCHECK(ssl_ctx_ == nullptr);
Joachim Bauch831c5582015-05-20 12:48:41 +0200499 ssl_max_version_ = version;
500}
501
skvladd0309122017-02-02 17:18:37 -0800502void OpenSSLStreamAdapter::SetInitialRetransmissionTimeout(
503 int timeout_ms) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800504 RTC_DCHECK(ssl_ctx_ == nullptr);
skvladd0309122017-02-02 17:18:37 -0800505 dtls_handshake_timeout_ms_ = timeout_ms;
506}
507
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000508//
509// StreamInterface Implementation
510//
511
512StreamResult OpenSSLStreamAdapter::Write(const void* data, size_t data_len,
513 size_t* written, int* error) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100514 RTC_LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::Write(" << data_len << ")";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000515
516 switch (state_) {
517 case SSL_NONE:
518 // pass-through in clear text
519 return StreamAdapterInterface::Write(data, data_len, written, error);
520
521 case SSL_WAIT:
522 case SSL_CONNECTING:
523 return SR_BLOCK;
524
525 case SSL_CONNECTED:
deadbeef89824f62016-09-30 11:55:43 -0700526 if (waiting_to_verify_peer_certificate()) {
527 return SR_BLOCK;
528 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000529 break;
530
531 case SSL_ERROR:
532 case SSL_CLOSED:
533 default:
534 if (error)
535 *error = ssl_error_code_;
536 return SR_ERROR;
537 }
538
539 // OpenSSL will return an error if we try to write zero bytes
540 if (data_len == 0) {
541 if (written)
542 *written = 0;
543 return SR_SUCCESS;
544 }
545
546 ssl_write_needs_read_ = false;
547
henrike@webrtc.orgd89b69a2014-11-06 17:23:09 +0000548 int code = SSL_write(ssl_, data, checked_cast<int>(data_len));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000549 int ssl_error = SSL_get_error(ssl_, code);
550 switch (ssl_error) {
551 case SSL_ERROR_NONE:
Mirko Bonadei675513b2017-11-09 11:09:25 +0100552 RTC_LOG(LS_VERBOSE) << " -- success";
kwibergee89e782017-08-09 17:22:01 -0700553 RTC_DCHECK_GT(code, 0);
554 RTC_DCHECK_LE(code, data_len);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000555 if (written)
556 *written = code;
557 return SR_SUCCESS;
558 case SSL_ERROR_WANT_READ:
Mirko Bonadei675513b2017-11-09 11:09:25 +0100559 RTC_LOG(LS_VERBOSE) << " -- error want read";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000560 ssl_write_needs_read_ = true;
561 return SR_BLOCK;
562 case SSL_ERROR_WANT_WRITE:
Mirko Bonadei675513b2017-11-09 11:09:25 +0100563 RTC_LOG(LS_VERBOSE) << " -- error want write";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000564 return SR_BLOCK;
565
566 case SSL_ERROR_ZERO_RETURN:
567 default:
deadbeef89824f62016-09-30 11:55:43 -0700568 Error("SSL_write", (ssl_error ? ssl_error : -1), 0, false);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000569 if (error)
570 *error = ssl_error_code_;
571 return SR_ERROR;
572 }
573 // not reached
574}
575
576StreamResult OpenSSLStreamAdapter::Read(void* data, size_t data_len,
577 size_t* read, int* error) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100578 RTC_LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::Read(" << data_len << ")";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000579 switch (state_) {
580 case SSL_NONE:
581 // pass-through in clear text
582 return StreamAdapterInterface::Read(data, data_len, read, error);
583
584 case SSL_WAIT:
585 case SSL_CONNECTING:
586 return SR_BLOCK;
587
588 case SSL_CONNECTED:
deadbeef89824f62016-09-30 11:55:43 -0700589 if (waiting_to_verify_peer_certificate()) {
590 return SR_BLOCK;
591 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000592 break;
593
594 case SSL_CLOSED:
595 return SR_EOS;
596
597 case SSL_ERROR:
598 default:
599 if (error)
600 *error = ssl_error_code_;
601 return SR_ERROR;
602 }
603
604 // Don't trust OpenSSL with zero byte reads
605 if (data_len == 0) {
606 if (read)
607 *read = 0;
608 return SR_SUCCESS;
609 }
610
611 ssl_read_needs_write_ = false;
612
henrike@webrtc.orgd89b69a2014-11-06 17:23:09 +0000613 int code = SSL_read(ssl_, data, checked_cast<int>(data_len));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000614 int ssl_error = SSL_get_error(ssl_, code);
615 switch (ssl_error) {
616 case SSL_ERROR_NONE:
Mirko Bonadei675513b2017-11-09 11:09:25 +0100617 RTC_LOG(LS_VERBOSE) << " -- success";
kwibergee89e782017-08-09 17:22:01 -0700618 RTC_DCHECK_GT(code, 0);
619 RTC_DCHECK_LE(code, data_len);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000620 if (read)
621 *read = code;
622
623 if (ssl_mode_ == SSL_MODE_DTLS) {
624 // Enforce atomic reads -- this is a short read
625 unsigned int pending = SSL_pending(ssl_);
626
627 if (pending) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100628 RTC_LOG(LS_INFO) << " -- short DTLS read. flushing";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000629 FlushInput(pending);
630 if (error)
631 *error = SSE_MSG_TRUNC;
632 return SR_ERROR;
633 }
634 }
635 return SR_SUCCESS;
636 case SSL_ERROR_WANT_READ:
Mirko Bonadei675513b2017-11-09 11:09:25 +0100637 RTC_LOG(LS_VERBOSE) << " -- error want read";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000638 return SR_BLOCK;
639 case SSL_ERROR_WANT_WRITE:
Mirko Bonadei675513b2017-11-09 11:09:25 +0100640 RTC_LOG(LS_VERBOSE) << " -- error want write";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000641 ssl_read_needs_write_ = true;
642 return SR_BLOCK;
643 case SSL_ERROR_ZERO_RETURN:
Mirko Bonadei675513b2017-11-09 11:09:25 +0100644 RTC_LOG(LS_VERBOSE) << " -- remote side closed";
deadbeef89824f62016-09-30 11:55:43 -0700645 Close();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000646 return SR_EOS;
647 break;
648 default:
Mirko Bonadei675513b2017-11-09 11:09:25 +0100649 RTC_LOG(LS_VERBOSE) << " -- error " << code;
deadbeef89824f62016-09-30 11:55:43 -0700650 Error("SSL_read", (ssl_error ? ssl_error : -1), 0, false);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000651 if (error)
652 *error = ssl_error_code_;
653 return SR_ERROR;
654 }
655 // not reached
656}
657
658void OpenSSLStreamAdapter::FlushInput(unsigned int left) {
659 unsigned char buf[2048];
660
661 while (left) {
662 // This should always succeed
663 int toread = (sizeof(buf) < left) ? sizeof(buf) : left;
664 int code = SSL_read(ssl_, buf, toread);
665
666 int ssl_error = SSL_get_error(ssl_, code);
deadbeef89824f62016-09-30 11:55:43 -0700667 RTC_DCHECK(ssl_error == SSL_ERROR_NONE);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000668
669 if (ssl_error != SSL_ERROR_NONE) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100670 RTC_LOG(LS_VERBOSE) << " -- error " << code;
deadbeef89824f62016-09-30 11:55:43 -0700671 Error("SSL_read", (ssl_error ? ssl_error : -1), 0, false);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000672 return;
673 }
674
Mirko Bonadei675513b2017-11-09 11:09:25 +0100675 RTC_LOG(LS_VERBOSE) << " -- flushed " << code << " bytes";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000676 left -= code;
677 }
678}
679
680void OpenSSLStreamAdapter::Close() {
deadbeef89824f62016-09-30 11:55:43 -0700681 Cleanup(0);
682 RTC_DCHECK(state_ == SSL_CLOSED || state_ == SSL_ERROR);
683 // When we're closed at SSL layer, also close the stream level which
684 // performs necessary clean up. Otherwise, a new incoming packet after
685 // this could overflow the stream buffer.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000686 StreamAdapterInterface::Close();
687}
688
689StreamState OpenSSLStreamAdapter::GetState() const {
690 switch (state_) {
691 case SSL_WAIT:
692 case SSL_CONNECTING:
693 return SS_OPENING;
694 case SSL_CONNECTED:
deadbeef89824f62016-09-30 11:55:43 -0700695 if (waiting_to_verify_peer_certificate()) {
696 return SS_OPENING;
697 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000698 return SS_OPEN;
699 default:
700 return SS_CLOSED;
701 };
702 // not reached
703}
704
705void OpenSSLStreamAdapter::OnEvent(StreamInterface* stream, int events,
706 int err) {
707 int events_to_signal = 0;
708 int signal_error = 0;
deadbeef89824f62016-09-30 11:55:43 -0700709 RTC_DCHECK(stream == this->stream());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000710 if ((events & SE_OPEN)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100711 RTC_LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::OnEvent SE_OPEN";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000712 if (state_ != SSL_WAIT) {
deadbeef89824f62016-09-30 11:55:43 -0700713 RTC_DCHECK(state_ == SSL_NONE);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000714 events_to_signal |= SE_OPEN;
715 } else {
716 state_ = SSL_CONNECTING;
717 if (int err = BeginSSL()) {
deadbeef89824f62016-09-30 11:55:43 -0700718 Error("BeginSSL", err, 0, true);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000719 return;
720 }
721 }
722 }
723 if ((events & (SE_READ|SE_WRITE))) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100724 RTC_LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::OnEvent"
725 << ((events & SE_READ) ? " SE_READ" : "")
726 << ((events & SE_WRITE) ? " SE_WRITE" : "");
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000727 if (state_ == SSL_NONE) {
728 events_to_signal |= events & (SE_READ|SE_WRITE);
729 } else if (state_ == SSL_CONNECTING) {
730 if (int err = ContinueSSL()) {
deadbeef89824f62016-09-30 11:55:43 -0700731 Error("ContinueSSL", err, 0, true);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000732 return;
733 }
734 } else if (state_ == SSL_CONNECTED) {
735 if (((events & SE_READ) && ssl_write_needs_read_) ||
736 (events & SE_WRITE)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100737 RTC_LOG(LS_VERBOSE) << " -- onStreamWriteable";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000738 events_to_signal |= SE_WRITE;
739 }
740 if (((events & SE_WRITE) && ssl_read_needs_write_) ||
741 (events & SE_READ)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100742 RTC_LOG(LS_VERBOSE) << " -- onStreamReadable";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000743 events_to_signal |= SE_READ;
744 }
745 }
746 }
747 if ((events & SE_CLOSE)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100748 RTC_LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::OnEvent(SE_CLOSE, " << err
749 << ")";
deadbeef89824f62016-09-30 11:55:43 -0700750 Cleanup(0);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000751 events_to_signal |= SE_CLOSE;
752 // SE_CLOSE is the only event that uses the final parameter to OnEvent().
deadbeef89824f62016-09-30 11:55:43 -0700753 RTC_DCHECK(signal_error == 0);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000754 signal_error = err;
755 }
756 if (events_to_signal)
757 StreamAdapterInterface::OnEvent(stream, events_to_signal, signal_error);
758}
759
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000760int OpenSSLStreamAdapter::BeginSSL() {
deadbeef89824f62016-09-30 11:55:43 -0700761 RTC_DCHECK(state_ == SSL_CONNECTING);
Taylor Brandstetterc8762a82016-08-11 12:01:49 -0700762 // The underlying stream has opened.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100763 RTC_LOG(LS_INFO) << "BeginSSL with peer.";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000764
deadbeef37f5ecf2017-02-27 14:06:41 -0800765 BIO* bio = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000766
Taylor Brandstetterc8762a82016-08-11 12:01:49 -0700767 // First set up the context.
deadbeef37f5ecf2017-02-27 14:06:41 -0800768 RTC_DCHECK(ssl_ctx_ == nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000769 ssl_ctx_ = SetupSSLContext();
770 if (!ssl_ctx_)
771 return -1;
772
773 bio = BIO_new_stream(static_cast<StreamInterface*>(stream()));
774 if (!bio)
775 return -1;
776
777 ssl_ = SSL_new(ssl_ctx_);
778 if (!ssl_) {
779 BIO_free(bio);
780 return -1;
781 }
782
783 SSL_set_app_data(ssl_, this);
784
785 SSL_set_bio(ssl_, bio, bio); // the SSL object owns the bio now.
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100786 if (ssl_mode_ == SSL_MODE_DTLS) {
Taylor Brandstetter4f0dfbd2016-06-15 17:15:23 -0700787#ifdef OPENSSL_IS_BORINGSSL
skvladd0309122017-02-02 17:18:37 -0800788 DTLSv1_set_initial_timeout_duration(ssl_, dtls_handshake_timeout_ms_);
Taylor Brandstetter4f0dfbd2016-06-15 17:15:23 -0700789#else
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100790 // Enable read-ahead for DTLS so whole packets are read from internal BIO
791 // before parsing. This is done internally by BoringSSL for DTLS.
792 SSL_set_read_ahead(ssl_, 1);
philipel49c08692016-05-24 01:49:43 -0700793#endif
Taylor Brandstetter4f0dfbd2016-06-15 17:15:23 -0700794 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000795
796 SSL_set_mode(ssl_, SSL_MODE_ENABLE_PARTIAL_WRITE |
797 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
798
David Benjamin60d5f3f2016-03-24 13:28:25 -0400799#if !defined(OPENSSL_IS_BORINGSSL)
800 // Specify an ECDH group for ECDHE ciphers, otherwise OpenSSL cannot
801 // negotiate them when acting as the server. Use NIST's P-256 which is
802 // commonly supported. BoringSSL doesn't need explicit configuration and has
803 // a reasonable default set.
jiayl@webrtc.org11c6bde2014-08-28 16:14:38 +0000804 EC_KEY* ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
deadbeef37f5ecf2017-02-27 14:06:41 -0800805 if (ecdh == nullptr)
jiayl@webrtc.org11c6bde2014-08-28 16:14:38 +0000806 return -1;
807 SSL_set_options(ssl_, SSL_OP_SINGLE_ECDH_USE);
808 SSL_set_tmp_ecdh(ssl_, ecdh);
809 EC_KEY_free(ecdh);
David Benjamin60d5f3f2016-03-24 13:28:25 -0400810#endif
jiayl@webrtc.org11c6bde2014-08-28 16:14:38 +0000811
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000812 // Do the connect
813 return ContinueSSL();
814}
815
816int OpenSSLStreamAdapter::ContinueSSL() {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100817 RTC_LOG(LS_VERBOSE) << "ContinueSSL";
deadbeef89824f62016-09-30 11:55:43 -0700818 RTC_DCHECK(state_ == SSL_CONNECTING);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000819
820 // Clear the DTLS timer
821 Thread::Current()->Clear(this, MSG_TIMEOUT);
822
823 int code = (role_ == SSL_CLIENT) ? SSL_connect(ssl_) : SSL_accept(ssl_);
824 int ssl_error;
825 switch (ssl_error = SSL_get_error(ssl_, code)) {
826 case SSL_ERROR_NONE:
Mirko Bonadei675513b2017-11-09 11:09:25 +0100827 RTC_LOG(LS_VERBOSE) << " -- success";
deadbeef89824f62016-09-30 11:55:43 -0700828 // By this point, OpenSSL should have given us a certificate, or errored
829 // out if one was missing.
830 RTC_DCHECK(peer_certificate_ || !client_auth_enabled());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000831
832 state_ = SSL_CONNECTED;
deadbeef89824f62016-09-30 11:55:43 -0700833 if (!waiting_to_verify_peer_certificate()) {
834 // We have everything we need to start the connection, so signal
835 // SE_OPEN. If we need a client certificate fingerprint and don't have
836 // it yet, we'll instead signal SE_OPEN in SetPeerCertificateDigest.
837 //
Taylor Brandstetterfe69a742016-09-30 17:34:32 -0700838 // TODO(deadbeef): Post this event asynchronously to unwind the stack.
839 // The caller of ContinueSSL may be the same object listening for these
840 // events and may not be prepared for reentrancy.
841 // PostEvent(SE_OPEN | SE_READ | SE_WRITE, 0);
842 StreamAdapterInterface::OnEvent(stream(), SE_OPEN | SE_READ | SE_WRITE,
843 0);
deadbeef89824f62016-09-30 11:55:43 -0700844 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000845 break;
846
847 case SSL_ERROR_WANT_READ: {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100848 RTC_LOG(LS_VERBOSE) << " -- error want read";
849 struct timeval timeout;
850 if (DTLSv1_get_timeout(ssl_, &timeout)) {
851 int delay = timeout.tv_sec * 1000 + timeout.tv_usec / 1000;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000852
Mirko Bonadei675513b2017-11-09 11:09:25 +0100853 Thread::Current()->PostDelayed(RTC_FROM_HERE, delay, this, MSG_TIMEOUT,
854 0);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000855 }
856 }
857 break;
858
859 case SSL_ERROR_WANT_WRITE:
Mirko Bonadei675513b2017-11-09 11:09:25 +0100860 RTC_LOG(LS_VERBOSE) << " -- error want write";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000861 break;
862
863 case SSL_ERROR_ZERO_RETURN:
864 default:
Mirko Bonadei675513b2017-11-09 11:09:25 +0100865 RTC_LOG(LS_VERBOSE) << " -- error " << code;
zhihuangd82eee02016-08-26 11:25:05 -0700866 SSLHandshakeError ssl_handshake_err = SSLHandshakeError::UNKNOWN;
867 int err_code = ERR_peek_last_error();
868 if (err_code != 0 && ERR_GET_REASON(err_code) == SSL_R_NO_SHARED_CIPHER) {
869 ssl_handshake_err = SSLHandshakeError::INCOMPATIBLE_CIPHERSUITE;
870 }
871 SignalSSLHandshakeError(ssl_handshake_err);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000872 return (ssl_error != 0) ? ssl_error : -1;
873 }
874
875 return 0;
876}
877
deadbeef89824f62016-09-30 11:55:43 -0700878void OpenSSLStreamAdapter::Error(const char* context,
879 int err,
880 uint8_t alert,
881 bool signal) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100882 RTC_LOG(LS_WARNING) << "OpenSSLStreamAdapter::Error(" << context << ", "
883 << err << ", " << static_cast<int>(alert) << ")";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000884 state_ = SSL_ERROR;
885 ssl_error_code_ = err;
deadbeef89824f62016-09-30 11:55:43 -0700886 Cleanup(alert);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000887 if (signal)
888 StreamAdapterInterface::OnEvent(stream(), SE_CLOSE, err);
889}
890
deadbeef89824f62016-09-30 11:55:43 -0700891void OpenSSLStreamAdapter::Cleanup(uint8_t alert) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100892 RTC_LOG(LS_INFO) << "Cleanup";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000893
894 if (state_ != SSL_ERROR) {
895 state_ = SSL_CLOSED;
896 ssl_error_code_ = 0;
897 }
898
899 if (ssl_) {
deadbeef89824f62016-09-30 11:55:43 -0700900 int ret;
901// SSL_send_fatal_alert is only available in BoringSSL.
902#ifdef OPENSSL_IS_BORINGSSL
903 if (alert) {
904 ret = SSL_send_fatal_alert(ssl_, alert);
905 if (ret < 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100906 RTC_LOG(LS_WARNING) << "SSL_send_fatal_alert failed, error = "
907 << SSL_get_error(ssl_, ret);
deadbeef89824f62016-09-30 11:55:43 -0700908 }
909 } else {
910#endif
911 ret = SSL_shutdown(ssl_);
912 if (ret < 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100913 RTC_LOG(LS_WARNING)
914 << "SSL_shutdown failed, error = " << SSL_get_error(ssl_, ret);
deadbeef89824f62016-09-30 11:55:43 -0700915 }
916#ifdef OPENSSL_IS_BORINGSSL
jiayl@webrtc.orgf1d751c2014-09-25 16:38:46 +0000917 }
deadbeef89824f62016-09-30 11:55:43 -0700918#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000919 SSL_free(ssl_);
deadbeef37f5ecf2017-02-27 14:06:41 -0800920 ssl_ = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000921 }
922 if (ssl_ctx_) {
923 SSL_CTX_free(ssl_ctx_);
deadbeef37f5ecf2017-02-27 14:06:41 -0800924 ssl_ctx_ = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000925 }
926 identity_.reset();
927 peer_certificate_.reset();
928
929 // Clear the DTLS timer
930 Thread::Current()->Clear(this, MSG_TIMEOUT);
931}
932
933
934void OpenSSLStreamAdapter::OnMessage(Message* msg) {
935 // Process our own messages and then pass others to the superclass
936 if (MSG_TIMEOUT == msg->message_id) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100937 RTC_LOG(LS_INFO) << "DTLS timeout expired";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000938 DTLSv1_handle_timeout(ssl_);
939 ContinueSSL();
940 } else {
941 StreamInterface::OnMessage(msg);
942 }
943}
944
945SSL_CTX* OpenSSLStreamAdapter::SetupSSLContext() {
deadbeef37f5ecf2017-02-27 14:06:41 -0800946 SSL_CTX* ctx = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000947
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100948#ifdef OPENSSL_IS_BORINGSSL
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000949 ctx = SSL_CTX_new(ssl_mode_ == SSL_MODE_DTLS ?
Joachim Bauch831c5582015-05-20 12:48:41 +0200950 DTLS_method() : TLS_method());
951 // Version limiting for BoringSSL will be done below.
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100952#else
953 const SSL_METHOD* method;
954 switch (ssl_max_version_) {
955 case SSL_PROTOCOL_TLS_10:
956 case SSL_PROTOCOL_TLS_11:
957 // OpenSSL doesn't support setting min/max versions, so we always use
958 // (D)TLS 1.0 if a max. version below the max. available is requested.
959 if (ssl_mode_ == SSL_MODE_DTLS) {
960 if (role_ == SSL_CLIENT) {
961 method = DTLSv1_client_method();
962 } else {
963 method = DTLSv1_server_method();
964 }
965 } else {
966 if (role_ == SSL_CLIENT) {
967 method = TLSv1_client_method();
968 } else {
969 method = TLSv1_server_method();
970 }
971 }
972 break;
973 case SSL_PROTOCOL_TLS_12:
974 default:
975 if (ssl_mode_ == SSL_MODE_DTLS) {
976#if (OPENSSL_VERSION_NUMBER >= 0x10002000L)
977 // DTLS 1.2 only available starting from OpenSSL 1.0.2
978 if (role_ == SSL_CLIENT) {
979 method = DTLS_client_method();
980 } else {
981 method = DTLS_server_method();
982 }
983#else
984 if (role_ == SSL_CLIENT) {
985 method = DTLSv1_client_method();
986 } else {
987 method = DTLSv1_server_method();
988 }
989#endif
990 } else {
991#if (OPENSSL_VERSION_NUMBER >= 0x10100000L)
992 // New API only available starting from OpenSSL 1.1.0
993 if (role_ == SSL_CLIENT) {
994 method = TLS_client_method();
995 } else {
996 method = TLS_server_method();
997 }
998#else
999 if (role_ == SSL_CLIENT) {
1000 method = SSLv23_client_method();
1001 } else {
1002 method = SSLv23_server_method();
1003 }
1004#endif
1005 }
1006 break;
1007 }
1008 ctx = SSL_CTX_new(method);
1009#endif // OPENSSL_IS_BORINGSSL
Joachim Bauch831c5582015-05-20 12:48:41 +02001010
deadbeef37f5ecf2017-02-27 14:06:41 -08001011 if (ctx == nullptr)
1012 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001013
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +01001014#ifdef OPENSSL_IS_BORINGSSL
davidbene36c46e2016-12-06 17:12:02 -08001015 SSL_CTX_set_min_proto_version(ctx, ssl_mode_ == SSL_MODE_DTLS ?
Joachim Bauch831c5582015-05-20 12:48:41 +02001016 DTLS1_VERSION : TLS1_VERSION);
1017 switch (ssl_max_version_) {
1018 case SSL_PROTOCOL_TLS_10:
davidbene36c46e2016-12-06 17:12:02 -08001019 SSL_CTX_set_max_proto_version(ctx, ssl_mode_ == SSL_MODE_DTLS ?
Joachim Bauch831c5582015-05-20 12:48:41 +02001020 DTLS1_VERSION : TLS1_VERSION);
1021 break;
1022 case SSL_PROTOCOL_TLS_11:
davidbene36c46e2016-12-06 17:12:02 -08001023 SSL_CTX_set_max_proto_version(ctx, ssl_mode_ == SSL_MODE_DTLS ?
Joachim Bauch831c5582015-05-20 12:48:41 +02001024 DTLS1_VERSION : TLS1_1_VERSION);
1025 break;
1026 case SSL_PROTOCOL_TLS_12:
1027 default:
davidbene36c46e2016-12-06 17:12:02 -08001028 SSL_CTX_set_max_proto_version(ctx, ssl_mode_ == SSL_MODE_DTLS ?
Joachim Bauch831c5582015-05-20 12:48:41 +02001029 DTLS1_2_VERSION : TLS1_2_VERSION);
1030 break;
1031 }
deadbeef6cf94a02016-11-28 17:38:34 -08001032 if (g_use_time_callback_for_testing) {
1033 SSL_CTX_set_current_time_cb(ctx, &TimeCallbackForTesting);
1034 }
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +01001035#endif
Joachim Bauch831c5582015-05-20 12:48:41 +02001036
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001037 if (identity_ && !identity_->ConfigureIdentity(ctx)) {
1038 SSL_CTX_free(ctx);
deadbeef37f5ecf2017-02-27 14:06:41 -08001039 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001040 }
1041
tfarinaa41ab932015-10-30 16:08:48 -07001042#if !defined(NDEBUG)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001043 SSL_CTX_set_info_callback(ctx, OpenSSLAdapter::SSLInfoCallback);
1044#endif
1045
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +00001046 int mode = SSL_VERIFY_PEER;
1047 if (client_auth_enabled()) {
1048 // Require a certificate from the client.
1049 // Note: Normally this is always true in production, but it may be disabled
1050 // for testing purposes (e.g. SSLAdapter unit tests).
1051 mode |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
1052 }
1053
David Benjamindc246562017-09-29 12:14:08 -04001054 // Configure a custom certificate verification callback to check the peer
1055 // certificate digest. Note the second argument to SSL_CTX_set_verify is to
1056 // override individual errors in the default verification logic, which is not
1057 // what we want here.
1058 SSL_CTX_set_verify(ctx, mode, nullptr);
1059 SSL_CTX_set_cert_verify_callback(ctx, SSLVerifyCallback, nullptr);
1060
Joachim Bauch831c5582015-05-20 12:48:41 +02001061 // Select list of available ciphers. Note that !SHA256 and !SHA384 only
1062 // remove HMAC-SHA256 and HMAC-SHA384 cipher suites, not GCM cipher suites
1063 // with SHA256 or SHA384 as the handshake hash.
1064 // This matches the list of SSLClientSocketOpenSSL in Chromium.
deadbeef37f5ecf2017-02-27 14:06:41 -08001065 SSL_CTX_set_cipher_list(
1066 ctx, "DEFAULT:!NULL:!aNULL:!SHA256:!SHA384:!aECDH:!AESGCM+AES256:!aPSK");
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001067
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001068 if (!srtp_ciphers_.empty()) {
1069 if (SSL_CTX_set_tlsext_use_srtp(ctx, srtp_ciphers_.c_str())) {
1070 SSL_CTX_free(ctx);
deadbeef37f5ecf2017-02-27 14:06:41 -08001071 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001072 }
1073 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001074
1075 return ctx;
1076}
1077
deadbeef89824f62016-09-30 11:55:43 -07001078bool OpenSSLStreamAdapter::VerifyPeerCertificate() {
1079 if (!has_peer_certificate_digest() || !peer_certificate_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001080 RTC_LOG(LS_WARNING) << "Missing digest or peer certificate.";
deadbeef89824f62016-09-30 11:55:43 -07001081 return false;
1082 }
deadbeef81f6f4f2016-09-19 17:20:52 -07001083
deadbeef89824f62016-09-30 11:55:43 -07001084 unsigned char digest[EVP_MAX_MD_SIZE];
1085 size_t digest_length;
1086 if (!OpenSSLCertificate::ComputeDigest(
1087 peer_certificate_->x509(), peer_certificate_digest_algorithm_, digest,
1088 sizeof(digest), &digest_length)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001089 RTC_LOG(LS_WARNING) << "Failed to compute peer cert digest.";
deadbeef89824f62016-09-30 11:55:43 -07001090 return false;
1091 }
1092
1093 Buffer computed_digest(digest, digest_length);
1094 if (computed_digest != peer_certificate_digest_value_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001095 RTC_LOG(LS_WARNING)
1096 << "Rejected peer certificate due to mismatched digest.";
jbauchf8f457b2017-03-09 16:24:57 -08001097 return false;
deadbeef81f6f4f2016-09-19 17:20:52 -07001098 }
deadbeef89824f62016-09-30 11:55:43 -07001099 // Ignore any verification error if the digest matches, since there is no
1100 // value in checking the validity of a self-signed cert issued by untrusted
1101 // sources.
Mirko Bonadei675513b2017-11-09 11:09:25 +01001102 RTC_LOG(LS_INFO) << "Accepted peer certificate.";
deadbeef89824f62016-09-30 11:55:43 -07001103 peer_certificate_verified_ = true;
1104 return true;
1105}
1106
David Benjamindc246562017-09-29 12:14:08 -04001107int OpenSSLStreamAdapter::SSLVerifyCallback(X509_STORE_CTX* store, void* arg) {
1108 // Get our SSL structure and OpenSSLStreamAdapter from the store.
deadbeef89824f62016-09-30 11:55:43 -07001109 SSL* ssl = reinterpret_cast<SSL*>(
1110 X509_STORE_CTX_get_ex_data(store, SSL_get_ex_data_X509_STORE_CTX_idx()));
deadbeef89824f62016-09-30 11:55:43 -07001111 OpenSSLStreamAdapter* stream =
1112 reinterpret_cast<OpenSSLStreamAdapter*>(SSL_get_app_data(ssl));
henrike@webrtc.org4e5f65a2014-06-05 20:40:11 +00001113
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001114 // Record the peer's certificate.
David Benjamindc246562017-09-29 12:14:08 -04001115 X509* cert = SSL_get_peer_certificate(ssl);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001116 stream->peer_certificate_.reset(new OpenSSLCertificate(cert));
David Benjamindc246562017-09-29 12:14:08 -04001117 X509_free(cert);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001118
deadbeef89824f62016-09-30 11:55:43 -07001119 // If the peer certificate digest isn't known yet, we'll wait to verify
1120 // until it's known, and for now just return a success status.
1121 if (stream->peer_certificate_digest_algorithm_.empty()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001122 RTC_LOG(LS_INFO) << "Waiting to verify certificate until digest is known.";
deadbeef89824f62016-09-30 11:55:43 -07001123 return 1;
1124 }
1125
David Benjamindc246562017-09-29 12:14:08 -04001126 if (!stream->VerifyPeerCertificate()) {
1127 X509_STORE_CTX_set_error(store, X509_V_ERR_CERT_REJECTED);
1128 return 0;
1129 }
1130
1131 return 1;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001132}
1133
Taylor Brandstetter4f0dfbd2016-06-15 17:15:23 -07001134bool OpenSSLStreamAdapter::IsBoringSsl() {
1135#ifdef OPENSSL_IS_BORINGSSL
1136 return true;
1137#else
1138 return false;
1139#endif
1140}
1141
torbjorng43166b82016-03-11 00:06:47 -08001142#define CDEF(X) \
1143 { static_cast<uint16_t>(TLS1_CK_##X & 0xffff), "TLS_" #X }
1144
1145struct cipher_list {
1146 uint16_t cipher;
1147 const char* cipher_str;
1148};
1149
1150// TODO(torbjorng): Perhaps add more cipher suites to these lists.
1151static const cipher_list OK_RSA_ciphers[] = {
1152 CDEF(ECDHE_RSA_WITH_AES_128_CBC_SHA),
1153 CDEF(ECDHE_RSA_WITH_AES_256_CBC_SHA),
1154 CDEF(ECDHE_RSA_WITH_AES_128_GCM_SHA256),
1155#ifdef TLS1_CK_ECDHE_RSA_WITH_AES_256_GCM_SHA256
1156 CDEF(ECDHE_RSA_WITH_AES_256_GCM_SHA256),
1157#endif
torbjorngaad67802016-04-07 08:55:28 -07001158#ifdef TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256
torbjorng43166b82016-03-11 00:06:47 -08001159 CDEF(ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256),
torbjorngaad67802016-04-07 08:55:28 -07001160#endif
torbjorng43166b82016-03-11 00:06:47 -08001161};
1162
1163static const cipher_list OK_ECDSA_ciphers[] = {
1164 CDEF(ECDHE_ECDSA_WITH_AES_128_CBC_SHA),
1165 CDEF(ECDHE_ECDSA_WITH_AES_256_CBC_SHA),
1166 CDEF(ECDHE_ECDSA_WITH_AES_128_GCM_SHA256),
1167#ifdef TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA256
1168 CDEF(ECDHE_ECDSA_WITH_AES_256_GCM_SHA256),
1169#endif
torbjorngaad67802016-04-07 08:55:28 -07001170#ifdef TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256
torbjorng43166b82016-03-11 00:06:47 -08001171 CDEF(ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256),
torbjorngaad67802016-04-07 08:55:28 -07001172#endif
torbjorng43166b82016-03-11 00:06:47 -08001173};
1174#undef CDEF
1175
1176bool OpenSSLStreamAdapter::IsAcceptableCipher(int cipher, KeyType key_type) {
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +02001177 if (key_type == KT_RSA) {
torbjorng43166b82016-03-11 00:06:47 -08001178 for (const cipher_list& c : OK_RSA_ciphers) {
1179 if (cipher == c.cipher)
1180 return true;
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +02001181 }
Joachim Bauch831c5582015-05-20 12:48:41 +02001182 }
torbjorng43166b82016-03-11 00:06:47 -08001183
1184 if (key_type == KT_ECDSA) {
1185 for (const cipher_list& c : OK_ECDSA_ciphers) {
1186 if (cipher == c.cipher)
1187 return true;
1188 }
1189 }
1190
1191 return false;
1192}
1193
1194bool OpenSSLStreamAdapter::IsAcceptableCipher(const std::string& cipher,
1195 KeyType key_type) {
1196 if (key_type == KT_RSA) {
1197 for (const cipher_list& c : OK_RSA_ciphers) {
1198 if (cipher == c.cipher_str)
1199 return true;
1200 }
1201 }
1202
1203 if (key_type == KT_ECDSA) {
1204 for (const cipher_list& c : OK_ECDSA_ciphers) {
1205 if (cipher == c.cipher_str)
1206 return true;
1207 }
1208 }
1209
1210 return false;
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +00001211}
1212
deadbeef6cf94a02016-11-28 17:38:34 -08001213void OpenSSLStreamAdapter::enable_time_callback_for_testing() {
1214 g_use_time_callback_for_testing = true;
1215}
1216
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001217} // namespace rtc