blob: a126c4b08cbdfe3f7c2efe5a1ad678e23c8c6d27 [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"
Karl Wiberge40468b2017-11-22 10:42:26 +010029#include "rtc_base/numerics/safe_conversions.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "rtc_base/openssl.h"
31#include "rtc_base/openssladapter.h"
32#include "rtc_base/openssldigest.h"
33#include "rtc_base/opensslidentity.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020034#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
Jiawei Oua9c94d52018-01-30 23:05:07 -080045#if (OPENSSL_VERSION_NUMBER < 0x10100000L)
46#error "webrtc requires at least OpenSSL version 1.1.0, 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
David Benjamin3c1f05d2017-12-01 17:28:03 -050083// The "SSL_CIPHER_standard_name" function is only available in OpenSSL when
84// compiled with tracing, so we need to define the mapping manually here.
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +010085static const SslCipherMapEntry kSslCipherMap[] = {
deadbeef37f5ecf2017-02-27 14:06:41 -080086 // TLS v1.0 ciphersuites from RFC2246.
87 DEFINE_CIPHER_ENTRY_SSL3(RSA_RC4_128_SHA),
88 {SSL3_CK_RSA_DES_192_CBC3_SHA, "TLS_RSA_WITH_3DES_EDE_CBC_SHA"},
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +010089
deadbeef37f5ecf2017-02-27 14:06:41 -080090 // AES ciphersuites from RFC3268.
91 {TLS1_CK_RSA_WITH_AES_128_SHA, "TLS_RSA_WITH_AES_128_CBC_SHA"},
92 {TLS1_CK_DHE_RSA_WITH_AES_128_SHA, "TLS_DHE_RSA_WITH_AES_128_CBC_SHA"},
93 {TLS1_CK_RSA_WITH_AES_256_SHA, "TLS_RSA_WITH_AES_256_CBC_SHA"},
94 {TLS1_CK_DHE_RSA_WITH_AES_256_SHA, "TLS_DHE_RSA_WITH_AES_256_CBC_SHA"},
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +010095
deadbeef37f5ecf2017-02-27 14:06:41 -080096 // ECC ciphersuites from RFC4492.
97 DEFINE_CIPHER_ENTRY_TLS1(ECDHE_ECDSA_WITH_RC4_128_SHA),
98 {TLS1_CK_ECDHE_ECDSA_WITH_DES_192_CBC3_SHA,
99 "TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA"},
100 DEFINE_CIPHER_ENTRY_TLS1(ECDHE_ECDSA_WITH_AES_128_CBC_SHA),
101 DEFINE_CIPHER_ENTRY_TLS1(ECDHE_ECDSA_WITH_AES_256_CBC_SHA),
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100102
deadbeef37f5ecf2017-02-27 14:06:41 -0800103 DEFINE_CIPHER_ENTRY_TLS1(ECDHE_RSA_WITH_RC4_128_SHA),
104 {TLS1_CK_ECDHE_RSA_WITH_DES_192_CBC3_SHA,
105 "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA"},
106 DEFINE_CIPHER_ENTRY_TLS1(ECDHE_RSA_WITH_AES_128_CBC_SHA),
107 DEFINE_CIPHER_ENTRY_TLS1(ECDHE_RSA_WITH_AES_256_CBC_SHA),
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100108
deadbeef37f5ecf2017-02-27 14:06:41 -0800109 // TLS v1.2 ciphersuites.
110 {TLS1_CK_RSA_WITH_AES_128_SHA256, "TLS_RSA_WITH_AES_128_CBC_SHA256"},
111 {TLS1_CK_RSA_WITH_AES_256_SHA256, "TLS_RSA_WITH_AES_256_CBC_SHA256"},
112 {TLS1_CK_DHE_RSA_WITH_AES_128_SHA256,
113 "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256"},
114 {TLS1_CK_DHE_RSA_WITH_AES_256_SHA256,
115 "TLS_DHE_RSA_WITH_AES_256_CBC_SHA256"},
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100116
deadbeef37f5ecf2017-02-27 14:06:41 -0800117 // TLS v1.2 GCM ciphersuites from RFC5288.
118 DEFINE_CIPHER_ENTRY_TLS1(RSA_WITH_AES_128_GCM_SHA256),
119 DEFINE_CIPHER_ENTRY_TLS1(RSA_WITH_AES_256_GCM_SHA384),
120 DEFINE_CIPHER_ENTRY_TLS1(DHE_RSA_WITH_AES_128_GCM_SHA256),
121 DEFINE_CIPHER_ENTRY_TLS1(DHE_RSA_WITH_AES_256_GCM_SHA384),
122 DEFINE_CIPHER_ENTRY_TLS1(DH_RSA_WITH_AES_128_GCM_SHA256),
123 DEFINE_CIPHER_ENTRY_TLS1(DH_RSA_WITH_AES_256_GCM_SHA384),
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100124
deadbeef37f5ecf2017-02-27 14:06:41 -0800125 // ECDH HMAC based ciphersuites from RFC5289.
126 {TLS1_CK_ECDHE_ECDSA_WITH_AES_128_SHA256,
127 "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256"},
128 {TLS1_CK_ECDHE_ECDSA_WITH_AES_256_SHA384,
129 "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384"},
130 {TLS1_CK_ECDHE_RSA_WITH_AES_128_SHA256,
131 "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256"},
132 {TLS1_CK_ECDHE_RSA_WITH_AES_256_SHA384,
133 "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384"},
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100134
deadbeef37f5ecf2017-02-27 14:06:41 -0800135 // ECDH GCM based ciphersuites from RFC5289.
136 DEFINE_CIPHER_ENTRY_TLS1(ECDHE_ECDSA_WITH_AES_128_GCM_SHA256),
137 DEFINE_CIPHER_ENTRY_TLS1(ECDHE_ECDSA_WITH_AES_256_GCM_SHA384),
138 DEFINE_CIPHER_ENTRY_TLS1(ECDHE_RSA_WITH_AES_128_GCM_SHA256),
139 DEFINE_CIPHER_ENTRY_TLS1(ECDHE_RSA_WITH_AES_256_GCM_SHA384),
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100140
deadbeef37f5ecf2017-02-27 14:06:41 -0800141 {0, nullptr}};
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100142#endif // #ifndef OPENSSL_IS_BORINGSSL
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000143
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700144#if defined(_MSC_VER)
145#pragma warning(push)
146#pragma warning(disable : 4309)
147#pragma warning(disable : 4310)
148#endif // defined(_MSC_VER)
149
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700150#if defined(_MSC_VER)
151#pragma warning(pop)
152#endif // defined(_MSC_VER)
153
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000154//////////////////////////////////////////////////////////////////////
155// StreamBIO
156//////////////////////////////////////////////////////////////////////
157
158static int stream_write(BIO* h, const char* buf, int num);
159static int stream_read(BIO* h, char* buf, int size);
160static int stream_puts(BIO* h, const char* str);
161static long stream_ctrl(BIO* h, int cmd, long arg1, void* arg2);
162static int stream_new(BIO* h);
163static int stream_free(BIO* data);
164
David Benjamin85aa0b62017-09-28 16:42:58 -0400165static const BIO_METHOD methods_stream = {
deadbeef37f5ecf2017-02-27 14:06:41 -0800166 BIO_TYPE_BIO, "stream", stream_write, stream_read, stream_puts, 0,
167 stream_ctrl, stream_new, stream_free, nullptr,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000168};
169
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000170static BIO* BIO_new_stream(StreamInterface* stream) {
David Benjamin85aa0b62017-09-28 16:42:58 -0400171 // TODO(davidben): Remove the const_cast when BoringSSL is assumed.
172 BIO* ret = BIO_new(const_cast<BIO_METHOD*>(&methods_stream));
deadbeef37f5ecf2017-02-27 14:06:41 -0800173 if (ret == nullptr)
174 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000175 ret->ptr = stream;
176 return ret;
177}
178
179// bio methods return 1 (or at least non-zero) on success and 0 on failure.
180
181static int stream_new(BIO* b) {
182 b->shutdown = 0;
183 b->init = 1;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000184 b->ptr = 0;
185 return 1;
186}
187
188static int stream_free(BIO* b) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800189 if (b == nullptr)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000190 return 0;
191 return 1;
192}
193
194static int stream_read(BIO* b, char* out, int outl) {
195 if (!out)
196 return -1;
197 StreamInterface* stream = static_cast<StreamInterface*>(b->ptr);
198 BIO_clear_retry_flags(b);
199 size_t read;
200 int error;
201 StreamResult result = stream->Read(out, outl, &read, &error);
202 if (result == SR_SUCCESS) {
henrike@webrtc.orgd89b69a2014-11-06 17:23:09 +0000203 return checked_cast<int>(read);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000204 } else if (result == SR_BLOCK) {
205 BIO_set_retry_read(b);
206 }
207 return -1;
208}
209
210static int stream_write(BIO* b, const char* in, int inl) {
211 if (!in)
212 return -1;
213 StreamInterface* stream = static_cast<StreamInterface*>(b->ptr);
214 BIO_clear_retry_flags(b);
215 size_t written;
216 int error;
217 StreamResult result = stream->Write(in, inl, &written, &error);
218 if (result == SR_SUCCESS) {
henrike@webrtc.orgd89b69a2014-11-06 17:23:09 +0000219 return checked_cast<int>(written);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000220 } else if (result == SR_BLOCK) {
221 BIO_set_retry_write(b);
222 }
223 return -1;
224}
225
226static int stream_puts(BIO* b, const char* str) {
henrike@webrtc.orgd89b69a2014-11-06 17:23:09 +0000227 return stream_write(b, str, checked_cast<int>(strlen(str)));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000228}
229
230static long stream_ctrl(BIO* b, int cmd, long num, void* ptr) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000231 switch (cmd) {
232 case BIO_CTRL_RESET:
233 return 0;
Jiawei Ou018dd6e2018-01-30 12:13:48 -0800234 case BIO_CTRL_EOF: {
235 StreamInterface* stream = static_cast<StreamInterface*>(ptr);
236 // 1 means end-of-stream.
237 return (stream->GetState() == SS_CLOSED) ? 1 : 0;
238 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000239 case BIO_CTRL_WPENDING:
240 case BIO_CTRL_PENDING:
241 return 0;
242 case BIO_CTRL_FLUSH:
243 return 1;
Henrik Lundinf4baca52015-06-10 09:45:58 +0200244 case BIO_CTRL_DGRAM_QUERY_MTU:
245 // openssl defaults to mtu=256 unless we return something here.
246 // The handshake doesn't actually need to send packets above 1k,
247 // so this seems like a sensible value that should work in most cases.
248 // Webrtc uses the same value for video packets.
249 return 1200;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000250 default:
251 return 0;
252 }
253}
254
255/////////////////////////////////////////////////////////////////////////////
256// OpenSSLStreamAdapter
257/////////////////////////////////////////////////////////////////////////////
258
259OpenSSLStreamAdapter::OpenSSLStreamAdapter(StreamInterface* stream)
260 : SSLStreamAdapter(stream),
261 state_(SSL_NONE),
262 role_(SSL_CLIENT),
Guo-wei Shieha7446d22016-01-11 15:27:03 -0800263 ssl_read_needs_write_(false),
264 ssl_write_needs_read_(false),
deadbeef37f5ecf2017-02-27 14:06:41 -0800265 ssl_(nullptr),
266 ssl_ctx_(nullptr),
Joachim Bauch831c5582015-05-20 12:48:41 +0200267 ssl_mode_(SSL_MODE_TLS),
Guo-wei Shieha7446d22016-01-11 15:27:03 -0800268 ssl_max_version_(SSL_PROTOCOL_TLS_12) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000269
270OpenSSLStreamAdapter::~OpenSSLStreamAdapter() {
deadbeef89824f62016-09-30 11:55:43 -0700271 Cleanup(0);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000272}
273
274void OpenSSLStreamAdapter::SetIdentity(SSLIdentity* identity) {
deadbeef89824f62016-09-30 11:55:43 -0700275 RTC_DCHECK(!identity_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000276 identity_.reset(static_cast<OpenSSLIdentity*>(identity));
277}
278
279void OpenSSLStreamAdapter::SetServerRole(SSLRole role) {
280 role_ = role;
281}
282
jbauch555604a2016-04-26 03:13:22 -0700283std::unique_ptr<SSLCertificate> OpenSSLStreamAdapter::GetPeerCertificate()
kwibergb4d01c42016-04-06 05:15:06 -0700284 const {
jbauch555604a2016-04-26 03:13:22 -0700285 return peer_certificate_ ? std::unique_ptr<SSLCertificate>(
kwibergb4d01c42016-04-06 05:15:06 -0700286 peer_certificate_->GetReference())
287 : nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000288}
289
deadbeef89824f62016-09-30 11:55:43 -0700290bool OpenSSLStreamAdapter::SetPeerCertificateDigest(
291 const std::string& digest_alg,
292 const unsigned char* digest_val,
293 size_t digest_len,
294 SSLPeerCertificateDigestError* error) {
295 RTC_DCHECK(!peer_certificate_verified_);
296 RTC_DCHECK(!has_peer_certificate_digest());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000297 size_t expected_len;
deadbeef89824f62016-09-30 11:55:43 -0700298 if (error) {
299 *error = SSLPeerCertificateDigestError::NONE;
300 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000301
302 if (!OpenSSLDigest::GetDigestSize(digest_alg, &expected_len)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100303 RTC_LOG(LS_WARNING) << "Unknown digest algorithm: " << digest_alg;
deadbeef89824f62016-09-30 11:55:43 -0700304 if (error) {
305 *error = SSLPeerCertificateDigestError::UNKNOWN_ALGORITHM;
306 }
deadbeef81f6f4f2016-09-19 17:20:52 -0700307 return false;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000308 }
deadbeef89824f62016-09-30 11:55:43 -0700309 if (expected_len != digest_len) {
310 if (error) {
311 *error = SSLPeerCertificateDigestError::INVALID_LENGTH;
312 }
deadbeef81f6f4f2016-09-19 17:20:52 -0700313 return false;
deadbeef89824f62016-09-30 11:55:43 -0700314 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000315
316 peer_certificate_digest_value_.SetData(digest_val, digest_len);
317 peer_certificate_digest_algorithm_ = digest_alg;
318
deadbeef89824f62016-09-30 11:55:43 -0700319 if (!peer_certificate_) {
320 // Normal case, where the digest is set before we obtain the certificate
321 // from the handshake.
322 return true;
323 }
324
325 if (!VerifyPeerCertificate()) {
326 Error("SetPeerCertificateDigest", -1, SSL_AD_BAD_CERTIFICATE, false);
327 if (error) {
328 *error = SSLPeerCertificateDigestError::VERIFICATION_FAILED;
329 }
330 return false;
331 }
332
333 if (state_ == SSL_CONNECTED) {
334 // Post the event asynchronously to unwind the stack. The caller
335 // of ContinueSSL may be the same object listening for these
336 // events and may not be prepared for reentrancy.
337 PostEvent(SE_OPEN | SE_READ | SE_WRITE, 0);
338 }
339
deadbeef81f6f4f2016-09-19 17:20:52 -0700340 return true;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000341}
342
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800343std::string OpenSSLStreamAdapter::SslCipherSuiteToName(int cipher_suite) {
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100344#ifdef OPENSSL_IS_BORINGSSL
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800345 const SSL_CIPHER* ssl_cipher = SSL_get_cipher_by_value(cipher_suite);
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700346 if (!ssl_cipher) {
347 return std::string();
348 }
David Benjamina8f73762017-09-28 15:49:42 -0400349 return SSL_CIPHER_standard_name(ssl_cipher);
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100350#else
351 for (const SslCipherMapEntry* entry = kSslCipherMap; entry->rfc_name;
352 ++entry) {
353 if (cipher_suite == static_cast<int>(entry->openssl_id)) {
354 return entry->rfc_name;
355 }
356 }
357 return std::string();
358#endif
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700359}
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000360
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800361bool OpenSSLStreamAdapter::GetSslCipherSuite(int* cipher_suite) {
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000362 if (state_ != SSL_CONNECTED)
363 return false;
364
365 const SSL_CIPHER* current_cipher = SSL_get_current_cipher(ssl_);
deadbeef37f5ecf2017-02-27 14:06:41 -0800366 if (current_cipher == nullptr) {
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000367 return false;
368 }
369
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800370 *cipher_suite = static_cast<uint16_t>(SSL_CIPHER_get_id(current_cipher));
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000371 return true;
372}
373
torbjorng43166b82016-03-11 00:06:47 -0800374int OpenSSLStreamAdapter::GetSslVersion() const {
375 if (state_ != SSL_CONNECTED)
376 return -1;
377
378 int ssl_version = SSL_version(ssl_);
379 if (ssl_mode_ == SSL_MODE_DTLS) {
380 if (ssl_version == DTLS1_VERSION)
381 return SSL_PROTOCOL_DTLS_10;
382 else if (ssl_version == DTLS1_2_VERSION)
383 return SSL_PROTOCOL_DTLS_12;
384 } else {
385 if (ssl_version == TLS1_VERSION)
386 return SSL_PROTOCOL_TLS_10;
387 else if (ssl_version == TLS1_1_VERSION)
388 return SSL_PROTOCOL_TLS_11;
389 else if (ssl_version == TLS1_2_VERSION)
390 return SSL_PROTOCOL_TLS_12;
391 }
392
393 return -1;
394}
395
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000396// Key Extractor interface
397bool OpenSSLStreamAdapter::ExportKeyingMaterial(const std::string& label,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200398 const uint8_t* context,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000399 size_t context_len,
400 bool use_context,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200401 uint8_t* result,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000402 size_t result_len) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000403 int i;
404
Peter Boström0c4e06b2015-10-07 12:23:21 +0200405 i = SSL_export_keying_material(ssl_, result, result_len, label.c_str(),
406 label.length(), const_cast<uint8_t*>(context),
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000407 context_len, use_context);
408
409 if (i != 1)
410 return false;
411
412 return true;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000413}
414
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800415bool OpenSSLStreamAdapter::SetDtlsSrtpCryptoSuites(
416 const std::vector<int>& ciphers) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000417 std::string internal_ciphers;
418
419 if (state_ != SSL_NONE)
420 return false;
421
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800422 for (std::vector<int>::const_iterator cipher = ciphers.begin();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000423 cipher != ciphers.end(); ++cipher) {
424 bool found = false;
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800425 for (SrtpCipherMapEntry* entry = SrtpCipherMap; entry->internal_name;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000426 ++entry) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800427 if (*cipher == entry->id) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000428 found = true;
429 if (!internal_ciphers.empty())
430 internal_ciphers += ":";
431 internal_ciphers += entry->internal_name;
432 break;
433 }
434 }
435
436 if (!found) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100437 RTC_LOG(LS_ERROR) << "Could not find cipher: " << *cipher;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000438 return false;
439 }
440 }
441
442 if (internal_ciphers.empty())
443 return false;
444
445 srtp_ciphers_ = internal_ciphers;
446 return true;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000447}
448
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800449bool OpenSSLStreamAdapter::GetDtlsSrtpCryptoSuite(int* crypto_suite) {
deadbeef89824f62016-09-30 11:55:43 -0700450 RTC_DCHECK(state_ == SSL_CONNECTED);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000451 if (state_ != SSL_CONNECTED)
452 return false;
453
henrike@webrtc.orgc10ecea2015-01-07 17:59:28 +0000454 const SRTP_PROTECTION_PROFILE *srtp_profile =
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000455 SSL_get_selected_srtp_profile(ssl_);
456
457 if (!srtp_profile)
458 return false;
459
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800460 *crypto_suite = srtp_profile->id;
deadbeef89824f62016-09-30 11:55:43 -0700461 RTC_DCHECK(!SrtpCryptoSuiteToName(*crypto_suite).empty());
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800462 return true;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000463}
464
deadbeef89824f62016-09-30 11:55:43 -0700465bool OpenSSLStreamAdapter::IsTlsConnected() {
466 return state_ == SSL_CONNECTED;
467}
468
Taylor Brandstetterc8762a82016-08-11 12:01:49 -0700469int OpenSSLStreamAdapter::StartSSL() {
deadbeef89824f62016-09-30 11:55:43 -0700470 if (state_ != SSL_NONE) {
471 // Don't allow StartSSL to be called twice.
472 return -1;
473 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000474
Taylor Brandstetterc8762a82016-08-11 12:01:49 -0700475 if (StreamAdapterInterface::GetState() != SS_OPEN) {
476 state_ = SSL_WAIT;
477 return 0;
478 }
479
480 state_ = SSL_CONNECTING;
481 if (int err = BeginSSL()) {
deadbeef89824f62016-09-30 11:55:43 -0700482 Error("BeginSSL", err, 0, false);
Taylor Brandstetterc8762a82016-08-11 12:01:49 -0700483 return err;
484 }
485
486 return 0;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000487}
488
489void OpenSSLStreamAdapter::SetMode(SSLMode mode) {
deadbeef89824f62016-09-30 11:55:43 -0700490 RTC_DCHECK(state_ == SSL_NONE);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000491 ssl_mode_ = mode;
492}
493
Joachim Bauch831c5582015-05-20 12:48:41 +0200494void OpenSSLStreamAdapter::SetMaxProtocolVersion(SSLProtocolVersion version) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800495 RTC_DCHECK(ssl_ctx_ == nullptr);
Joachim Bauch831c5582015-05-20 12:48:41 +0200496 ssl_max_version_ = version;
497}
498
skvladd0309122017-02-02 17:18:37 -0800499void OpenSSLStreamAdapter::SetInitialRetransmissionTimeout(
500 int timeout_ms) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800501 RTC_DCHECK(ssl_ctx_ == nullptr);
skvladd0309122017-02-02 17:18:37 -0800502 dtls_handshake_timeout_ms_ = timeout_ms;
503}
504
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000505//
506// StreamInterface Implementation
507//
508
509StreamResult OpenSSLStreamAdapter::Write(const void* data, size_t data_len,
510 size_t* written, int* error) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100511 RTC_LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::Write(" << data_len << ")";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000512
513 switch (state_) {
514 case SSL_NONE:
515 // pass-through in clear text
516 return StreamAdapterInterface::Write(data, data_len, written, error);
517
518 case SSL_WAIT:
519 case SSL_CONNECTING:
520 return SR_BLOCK;
521
522 case SSL_CONNECTED:
deadbeef89824f62016-09-30 11:55:43 -0700523 if (waiting_to_verify_peer_certificate()) {
524 return SR_BLOCK;
525 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000526 break;
527
528 case SSL_ERROR:
529 case SSL_CLOSED:
530 default:
531 if (error)
532 *error = ssl_error_code_;
533 return SR_ERROR;
534 }
535
536 // OpenSSL will return an error if we try to write zero bytes
537 if (data_len == 0) {
538 if (written)
539 *written = 0;
540 return SR_SUCCESS;
541 }
542
543 ssl_write_needs_read_ = false;
544
henrike@webrtc.orgd89b69a2014-11-06 17:23:09 +0000545 int code = SSL_write(ssl_, data, checked_cast<int>(data_len));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000546 int ssl_error = SSL_get_error(ssl_, code);
547 switch (ssl_error) {
548 case SSL_ERROR_NONE:
Mirko Bonadei675513b2017-11-09 11:09:25 +0100549 RTC_LOG(LS_VERBOSE) << " -- success";
kwibergee89e782017-08-09 17:22:01 -0700550 RTC_DCHECK_GT(code, 0);
551 RTC_DCHECK_LE(code, data_len);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000552 if (written)
553 *written = code;
554 return SR_SUCCESS;
555 case SSL_ERROR_WANT_READ:
Mirko Bonadei675513b2017-11-09 11:09:25 +0100556 RTC_LOG(LS_VERBOSE) << " -- error want read";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000557 ssl_write_needs_read_ = true;
558 return SR_BLOCK;
559 case SSL_ERROR_WANT_WRITE:
Mirko Bonadei675513b2017-11-09 11:09:25 +0100560 RTC_LOG(LS_VERBOSE) << " -- error want write";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000561 return SR_BLOCK;
562
563 case SSL_ERROR_ZERO_RETURN:
564 default:
deadbeef89824f62016-09-30 11:55:43 -0700565 Error("SSL_write", (ssl_error ? ssl_error : -1), 0, false);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000566 if (error)
567 *error = ssl_error_code_;
568 return SR_ERROR;
569 }
570 // not reached
571}
572
573StreamResult OpenSSLStreamAdapter::Read(void* data, size_t data_len,
574 size_t* read, int* error) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100575 RTC_LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::Read(" << data_len << ")";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000576 switch (state_) {
577 case SSL_NONE:
578 // pass-through in clear text
579 return StreamAdapterInterface::Read(data, data_len, read, error);
580
581 case SSL_WAIT:
582 case SSL_CONNECTING:
583 return SR_BLOCK;
584
585 case SSL_CONNECTED:
deadbeef89824f62016-09-30 11:55:43 -0700586 if (waiting_to_verify_peer_certificate()) {
587 return SR_BLOCK;
588 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000589 break;
590
591 case SSL_CLOSED:
592 return SR_EOS;
593
594 case SSL_ERROR:
595 default:
596 if (error)
597 *error = ssl_error_code_;
598 return SR_ERROR;
599 }
600
601 // Don't trust OpenSSL with zero byte reads
602 if (data_len == 0) {
603 if (read)
604 *read = 0;
605 return SR_SUCCESS;
606 }
607
608 ssl_read_needs_write_ = false;
609
henrike@webrtc.orgd89b69a2014-11-06 17:23:09 +0000610 int code = SSL_read(ssl_, data, checked_cast<int>(data_len));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000611 int ssl_error = SSL_get_error(ssl_, code);
612 switch (ssl_error) {
613 case SSL_ERROR_NONE:
Mirko Bonadei675513b2017-11-09 11:09:25 +0100614 RTC_LOG(LS_VERBOSE) << " -- success";
kwibergee89e782017-08-09 17:22:01 -0700615 RTC_DCHECK_GT(code, 0);
616 RTC_DCHECK_LE(code, data_len);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000617 if (read)
618 *read = code;
619
620 if (ssl_mode_ == SSL_MODE_DTLS) {
621 // Enforce atomic reads -- this is a short read
622 unsigned int pending = SSL_pending(ssl_);
623
624 if (pending) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100625 RTC_LOG(LS_INFO) << " -- short DTLS read. flushing";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000626 FlushInput(pending);
627 if (error)
628 *error = SSE_MSG_TRUNC;
629 return SR_ERROR;
630 }
631 }
632 return SR_SUCCESS;
633 case SSL_ERROR_WANT_READ:
Mirko Bonadei675513b2017-11-09 11:09:25 +0100634 RTC_LOG(LS_VERBOSE) << " -- error want read";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000635 return SR_BLOCK;
636 case SSL_ERROR_WANT_WRITE:
Mirko Bonadei675513b2017-11-09 11:09:25 +0100637 RTC_LOG(LS_VERBOSE) << " -- error want write";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000638 ssl_read_needs_write_ = true;
639 return SR_BLOCK;
640 case SSL_ERROR_ZERO_RETURN:
Mirko Bonadei675513b2017-11-09 11:09:25 +0100641 RTC_LOG(LS_VERBOSE) << " -- remote side closed";
deadbeef89824f62016-09-30 11:55:43 -0700642 Close();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000643 return SR_EOS;
644 break;
645 default:
deadbeef89824f62016-09-30 11:55:43 -0700646 Error("SSL_read", (ssl_error ? ssl_error : -1), 0, false);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000647 if (error)
648 *error = ssl_error_code_;
649 return SR_ERROR;
650 }
651 // not reached
652}
653
654void OpenSSLStreamAdapter::FlushInput(unsigned int left) {
655 unsigned char buf[2048];
656
657 while (left) {
658 // This should always succeed
659 int toread = (sizeof(buf) < left) ? sizeof(buf) : left;
660 int code = SSL_read(ssl_, buf, toread);
661
662 int ssl_error = SSL_get_error(ssl_, code);
deadbeef89824f62016-09-30 11:55:43 -0700663 RTC_DCHECK(ssl_error == SSL_ERROR_NONE);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000664
665 if (ssl_error != SSL_ERROR_NONE) {
Jonas Olssonaddc3802018-02-01 09:53:06 +0100666 RTC_DLOG(LS_VERBOSE) << " -- error " << code;
deadbeef89824f62016-09-30 11:55:43 -0700667 Error("SSL_read", (ssl_error ? ssl_error : -1), 0, false);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000668 return;
669 }
670
Mirko Bonadei675513b2017-11-09 11:09:25 +0100671 RTC_LOG(LS_VERBOSE) << " -- flushed " << code << " bytes";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000672 left -= code;
673 }
674}
675
676void OpenSSLStreamAdapter::Close() {
deadbeef89824f62016-09-30 11:55:43 -0700677 Cleanup(0);
678 RTC_DCHECK(state_ == SSL_CLOSED || state_ == SSL_ERROR);
679 // When we're closed at SSL layer, also close the stream level which
680 // performs necessary clean up. Otherwise, a new incoming packet after
681 // this could overflow the stream buffer.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000682 StreamAdapterInterface::Close();
683}
684
685StreamState OpenSSLStreamAdapter::GetState() const {
686 switch (state_) {
687 case SSL_WAIT:
688 case SSL_CONNECTING:
689 return SS_OPENING;
690 case SSL_CONNECTED:
deadbeef89824f62016-09-30 11:55:43 -0700691 if (waiting_to_verify_peer_certificate()) {
692 return SS_OPENING;
693 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000694 return SS_OPEN;
695 default:
696 return SS_CLOSED;
697 };
698 // not reached
699}
700
701void OpenSSLStreamAdapter::OnEvent(StreamInterface* stream, int events,
702 int err) {
703 int events_to_signal = 0;
704 int signal_error = 0;
deadbeef89824f62016-09-30 11:55:43 -0700705 RTC_DCHECK(stream == this->stream());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000706 if ((events & SE_OPEN)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100707 RTC_LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::OnEvent SE_OPEN";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000708 if (state_ != SSL_WAIT) {
deadbeef89824f62016-09-30 11:55:43 -0700709 RTC_DCHECK(state_ == SSL_NONE);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000710 events_to_signal |= SE_OPEN;
711 } else {
712 state_ = SSL_CONNECTING;
713 if (int err = BeginSSL()) {
deadbeef89824f62016-09-30 11:55:43 -0700714 Error("BeginSSL", err, 0, true);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000715 return;
716 }
717 }
718 }
719 if ((events & (SE_READ|SE_WRITE))) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100720 RTC_LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::OnEvent"
721 << ((events & SE_READ) ? " SE_READ" : "")
722 << ((events & SE_WRITE) ? " SE_WRITE" : "");
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000723 if (state_ == SSL_NONE) {
724 events_to_signal |= events & (SE_READ|SE_WRITE);
725 } else if (state_ == SSL_CONNECTING) {
726 if (int err = ContinueSSL()) {
deadbeef89824f62016-09-30 11:55:43 -0700727 Error("ContinueSSL", err, 0, true);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000728 return;
729 }
730 } else if (state_ == SSL_CONNECTED) {
731 if (((events & SE_READ) && ssl_write_needs_read_) ||
732 (events & SE_WRITE)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100733 RTC_LOG(LS_VERBOSE) << " -- onStreamWriteable";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000734 events_to_signal |= SE_WRITE;
735 }
736 if (((events & SE_WRITE) && ssl_read_needs_write_) ||
737 (events & SE_READ)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100738 RTC_LOG(LS_VERBOSE) << " -- onStreamReadable";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000739 events_to_signal |= SE_READ;
740 }
741 }
742 }
743 if ((events & SE_CLOSE)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100744 RTC_LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::OnEvent(SE_CLOSE, " << err
745 << ")";
deadbeef89824f62016-09-30 11:55:43 -0700746 Cleanup(0);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000747 events_to_signal |= SE_CLOSE;
748 // SE_CLOSE is the only event that uses the final parameter to OnEvent().
deadbeef89824f62016-09-30 11:55:43 -0700749 RTC_DCHECK(signal_error == 0);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000750 signal_error = err;
751 }
752 if (events_to_signal)
753 StreamAdapterInterface::OnEvent(stream, events_to_signal, signal_error);
754}
755
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000756int OpenSSLStreamAdapter::BeginSSL() {
deadbeef89824f62016-09-30 11:55:43 -0700757 RTC_DCHECK(state_ == SSL_CONNECTING);
Taylor Brandstetterc8762a82016-08-11 12:01:49 -0700758 // The underlying stream has opened.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100759 RTC_LOG(LS_INFO) << "BeginSSL with peer.";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000760
deadbeef37f5ecf2017-02-27 14:06:41 -0800761 BIO* bio = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000762
Taylor Brandstetterc8762a82016-08-11 12:01:49 -0700763 // First set up the context.
deadbeef37f5ecf2017-02-27 14:06:41 -0800764 RTC_DCHECK(ssl_ctx_ == nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000765 ssl_ctx_ = SetupSSLContext();
766 if (!ssl_ctx_)
767 return -1;
768
769 bio = BIO_new_stream(static_cast<StreamInterface*>(stream()));
770 if (!bio)
771 return -1;
772
773 ssl_ = SSL_new(ssl_ctx_);
774 if (!ssl_) {
775 BIO_free(bio);
776 return -1;
777 }
778
779 SSL_set_app_data(ssl_, this);
780
781 SSL_set_bio(ssl_, bio, bio); // the SSL object owns the bio now.
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100782 if (ssl_mode_ == SSL_MODE_DTLS) {
Taylor Brandstetter4f0dfbd2016-06-15 17:15:23 -0700783#ifdef OPENSSL_IS_BORINGSSL
skvladd0309122017-02-02 17:18:37 -0800784 DTLSv1_set_initial_timeout_duration(ssl_, dtls_handshake_timeout_ms_);
Taylor Brandstetter4f0dfbd2016-06-15 17:15:23 -0700785#else
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100786 // Enable read-ahead for DTLS so whole packets are read from internal BIO
787 // before parsing. This is done internally by BoringSSL for DTLS.
788 SSL_set_read_ahead(ssl_, 1);
philipel49c08692016-05-24 01:49:43 -0700789#endif
Taylor Brandstetter4f0dfbd2016-06-15 17:15:23 -0700790 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000791
792 SSL_set_mode(ssl_, SSL_MODE_ENABLE_PARTIAL_WRITE |
793 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
794
David Benjamin60d5f3f2016-03-24 13:28:25 -0400795#if !defined(OPENSSL_IS_BORINGSSL)
796 // Specify an ECDH group for ECDHE ciphers, otherwise OpenSSL cannot
797 // negotiate them when acting as the server. Use NIST's P-256 which is
798 // commonly supported. BoringSSL doesn't need explicit configuration and has
799 // a reasonable default set.
jiayl@webrtc.org11c6bde2014-08-28 16:14:38 +0000800 EC_KEY* ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
deadbeef37f5ecf2017-02-27 14:06:41 -0800801 if (ecdh == nullptr)
jiayl@webrtc.org11c6bde2014-08-28 16:14:38 +0000802 return -1;
803 SSL_set_options(ssl_, SSL_OP_SINGLE_ECDH_USE);
804 SSL_set_tmp_ecdh(ssl_, ecdh);
805 EC_KEY_free(ecdh);
David Benjamin60d5f3f2016-03-24 13:28:25 -0400806#endif
jiayl@webrtc.org11c6bde2014-08-28 16:14:38 +0000807
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000808 // Do the connect
809 return ContinueSSL();
810}
811
812int OpenSSLStreamAdapter::ContinueSSL() {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100813 RTC_LOG(LS_VERBOSE) << "ContinueSSL";
deadbeef89824f62016-09-30 11:55:43 -0700814 RTC_DCHECK(state_ == SSL_CONNECTING);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000815
816 // Clear the DTLS timer
817 Thread::Current()->Clear(this, MSG_TIMEOUT);
818
819 int code = (role_ == SSL_CLIENT) ? SSL_connect(ssl_) : SSL_accept(ssl_);
820 int ssl_error;
821 switch (ssl_error = SSL_get_error(ssl_, code)) {
822 case SSL_ERROR_NONE:
Mirko Bonadei675513b2017-11-09 11:09:25 +0100823 RTC_LOG(LS_VERBOSE) << " -- success";
deadbeef89824f62016-09-30 11:55:43 -0700824 // By this point, OpenSSL should have given us a certificate, or errored
825 // out if one was missing.
826 RTC_DCHECK(peer_certificate_ || !client_auth_enabled());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000827
828 state_ = SSL_CONNECTED;
deadbeef89824f62016-09-30 11:55:43 -0700829 if (!waiting_to_verify_peer_certificate()) {
830 // We have everything we need to start the connection, so signal
831 // SE_OPEN. If we need a client certificate fingerprint and don't have
832 // it yet, we'll instead signal SE_OPEN in SetPeerCertificateDigest.
833 //
Taylor Brandstetterfe69a742016-09-30 17:34:32 -0700834 // TODO(deadbeef): Post this event asynchronously to unwind the stack.
835 // The caller of ContinueSSL may be the same object listening for these
836 // events and may not be prepared for reentrancy.
837 // PostEvent(SE_OPEN | SE_READ | SE_WRITE, 0);
838 StreamAdapterInterface::OnEvent(stream(), SE_OPEN | SE_READ | SE_WRITE,
839 0);
deadbeef89824f62016-09-30 11:55:43 -0700840 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000841 break;
842
843 case SSL_ERROR_WANT_READ: {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100844 RTC_LOG(LS_VERBOSE) << " -- error want read";
845 struct timeval timeout;
846 if (DTLSv1_get_timeout(ssl_, &timeout)) {
847 int delay = timeout.tv_sec * 1000 + timeout.tv_usec / 1000;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000848
Mirko Bonadei675513b2017-11-09 11:09:25 +0100849 Thread::Current()->PostDelayed(RTC_FROM_HERE, delay, this, MSG_TIMEOUT,
850 0);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000851 }
852 }
853 break;
854
855 case SSL_ERROR_WANT_WRITE:
Mirko Bonadei675513b2017-11-09 11:09:25 +0100856 RTC_LOG(LS_VERBOSE) << " -- error want write";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000857 break;
858
859 case SSL_ERROR_ZERO_RETURN:
860 default:
Mirko Bonadei675513b2017-11-09 11:09:25 +0100861 RTC_LOG(LS_VERBOSE) << " -- error " << code;
zhihuangd82eee02016-08-26 11:25:05 -0700862 SSLHandshakeError ssl_handshake_err = SSLHandshakeError::UNKNOWN;
863 int err_code = ERR_peek_last_error();
864 if (err_code != 0 && ERR_GET_REASON(err_code) == SSL_R_NO_SHARED_CIPHER) {
865 ssl_handshake_err = SSLHandshakeError::INCOMPATIBLE_CIPHERSUITE;
866 }
867 SignalSSLHandshakeError(ssl_handshake_err);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000868 return (ssl_error != 0) ? ssl_error : -1;
869 }
870
871 return 0;
872}
873
deadbeef89824f62016-09-30 11:55:43 -0700874void OpenSSLStreamAdapter::Error(const char* context,
875 int err,
876 uint8_t alert,
877 bool signal) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100878 RTC_LOG(LS_WARNING) << "OpenSSLStreamAdapter::Error(" << context << ", "
879 << err << ", " << static_cast<int>(alert) << ")";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000880 state_ = SSL_ERROR;
881 ssl_error_code_ = err;
deadbeef89824f62016-09-30 11:55:43 -0700882 Cleanup(alert);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000883 if (signal)
884 StreamAdapterInterface::OnEvent(stream(), SE_CLOSE, err);
885}
886
deadbeef89824f62016-09-30 11:55:43 -0700887void OpenSSLStreamAdapter::Cleanup(uint8_t alert) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100888 RTC_LOG(LS_INFO) << "Cleanup";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000889
890 if (state_ != SSL_ERROR) {
891 state_ = SSL_CLOSED;
892 ssl_error_code_ = 0;
893 }
894
895 if (ssl_) {
deadbeef89824f62016-09-30 11:55:43 -0700896 int ret;
897// SSL_send_fatal_alert is only available in BoringSSL.
898#ifdef OPENSSL_IS_BORINGSSL
899 if (alert) {
900 ret = SSL_send_fatal_alert(ssl_, alert);
901 if (ret < 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100902 RTC_LOG(LS_WARNING) << "SSL_send_fatal_alert failed, error = "
903 << SSL_get_error(ssl_, ret);
deadbeef89824f62016-09-30 11:55:43 -0700904 }
905 } else {
906#endif
907 ret = SSL_shutdown(ssl_);
908 if (ret < 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100909 RTC_LOG(LS_WARNING)
910 << "SSL_shutdown failed, error = " << SSL_get_error(ssl_, ret);
deadbeef89824f62016-09-30 11:55:43 -0700911 }
912#ifdef OPENSSL_IS_BORINGSSL
jiayl@webrtc.orgf1d751c2014-09-25 16:38:46 +0000913 }
deadbeef89824f62016-09-30 11:55:43 -0700914#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000915 SSL_free(ssl_);
deadbeef37f5ecf2017-02-27 14:06:41 -0800916 ssl_ = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000917 }
918 if (ssl_ctx_) {
919 SSL_CTX_free(ssl_ctx_);
deadbeef37f5ecf2017-02-27 14:06:41 -0800920 ssl_ctx_ = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000921 }
922 identity_.reset();
923 peer_certificate_.reset();
924
925 // Clear the DTLS timer
926 Thread::Current()->Clear(this, MSG_TIMEOUT);
927}
928
929
930void OpenSSLStreamAdapter::OnMessage(Message* msg) {
931 // Process our own messages and then pass others to the superclass
932 if (MSG_TIMEOUT == msg->message_id) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100933 RTC_LOG(LS_INFO) << "DTLS timeout expired";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000934 DTLSv1_handle_timeout(ssl_);
935 ContinueSSL();
936 } else {
937 StreamInterface::OnMessage(msg);
938 }
939}
940
941SSL_CTX* OpenSSLStreamAdapter::SetupSSLContext() {
deadbeef37f5ecf2017-02-27 14:06:41 -0800942 SSL_CTX* ctx = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000943
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100944#ifdef OPENSSL_IS_BORINGSSL
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000945 ctx = SSL_CTX_new(ssl_mode_ == SSL_MODE_DTLS ?
Joachim Bauch831c5582015-05-20 12:48:41 +0200946 DTLS_method() : TLS_method());
947 // Version limiting for BoringSSL will be done below.
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100948#else
949 const SSL_METHOD* method;
950 switch (ssl_max_version_) {
951 case SSL_PROTOCOL_TLS_10:
952 case SSL_PROTOCOL_TLS_11:
953 // OpenSSL doesn't support setting min/max versions, so we always use
954 // (D)TLS 1.0 if a max. version below the max. available is requested.
955 if (ssl_mode_ == SSL_MODE_DTLS) {
956 if (role_ == SSL_CLIENT) {
957 method = DTLSv1_client_method();
958 } else {
959 method = DTLSv1_server_method();
960 }
961 } else {
962 if (role_ == SSL_CLIENT) {
963 method = TLSv1_client_method();
964 } else {
965 method = TLSv1_server_method();
966 }
967 }
968 break;
969 case SSL_PROTOCOL_TLS_12:
970 default:
971 if (ssl_mode_ == SSL_MODE_DTLS) {
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100972 if (role_ == SSL_CLIENT) {
973 method = DTLS_client_method();
974 } else {
975 method = DTLS_server_method();
976 }
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100977 } else {
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100978 if (role_ == SSL_CLIENT) {
979 method = TLS_client_method();
980 } else {
981 method = TLS_server_method();
982 }
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100983 }
984 break;
985 }
986 ctx = SSL_CTX_new(method);
987#endif // OPENSSL_IS_BORINGSSL
Joachim Bauch831c5582015-05-20 12:48:41 +0200988
deadbeef37f5ecf2017-02-27 14:06:41 -0800989 if (ctx == nullptr)
990 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000991
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100992#ifdef OPENSSL_IS_BORINGSSL
davidbene36c46e2016-12-06 17:12:02 -0800993 SSL_CTX_set_min_proto_version(ctx, ssl_mode_ == SSL_MODE_DTLS ?
Joachim Bauch831c5582015-05-20 12:48:41 +0200994 DTLS1_VERSION : TLS1_VERSION);
995 switch (ssl_max_version_) {
996 case SSL_PROTOCOL_TLS_10:
davidbene36c46e2016-12-06 17:12:02 -0800997 SSL_CTX_set_max_proto_version(ctx, ssl_mode_ == SSL_MODE_DTLS ?
Joachim Bauch831c5582015-05-20 12:48:41 +0200998 DTLS1_VERSION : TLS1_VERSION);
999 break;
1000 case SSL_PROTOCOL_TLS_11:
davidbene36c46e2016-12-06 17:12:02 -08001001 SSL_CTX_set_max_proto_version(ctx, ssl_mode_ == SSL_MODE_DTLS ?
Joachim Bauch831c5582015-05-20 12:48:41 +02001002 DTLS1_VERSION : TLS1_1_VERSION);
1003 break;
1004 case SSL_PROTOCOL_TLS_12:
1005 default:
davidbene36c46e2016-12-06 17:12:02 -08001006 SSL_CTX_set_max_proto_version(ctx, ssl_mode_ == SSL_MODE_DTLS ?
Joachim Bauch831c5582015-05-20 12:48:41 +02001007 DTLS1_2_VERSION : TLS1_2_VERSION);
1008 break;
1009 }
deadbeef6cf94a02016-11-28 17:38:34 -08001010 if (g_use_time_callback_for_testing) {
1011 SSL_CTX_set_current_time_cb(ctx, &TimeCallbackForTesting);
1012 }
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +01001013#endif
Joachim Bauch831c5582015-05-20 12:48:41 +02001014
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001015 if (identity_ && !identity_->ConfigureIdentity(ctx)) {
1016 SSL_CTX_free(ctx);
deadbeef37f5ecf2017-02-27 14:06:41 -08001017 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001018 }
1019
tfarinaa41ab932015-10-30 16:08:48 -07001020#if !defined(NDEBUG)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001021 SSL_CTX_set_info_callback(ctx, OpenSSLAdapter::SSLInfoCallback);
1022#endif
1023
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +00001024 int mode = SSL_VERIFY_PEER;
1025 if (client_auth_enabled()) {
1026 // Require a certificate from the client.
1027 // Note: Normally this is always true in production, but it may be disabled
1028 // for testing purposes (e.g. SSLAdapter unit tests).
1029 mode |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
1030 }
1031
David Benjamindc246562017-09-29 12:14:08 -04001032 // Configure a custom certificate verification callback to check the peer
1033 // certificate digest. Note the second argument to SSL_CTX_set_verify is to
1034 // override individual errors in the default verification logic, which is not
1035 // what we want here.
1036 SSL_CTX_set_verify(ctx, mode, nullptr);
1037 SSL_CTX_set_cert_verify_callback(ctx, SSLVerifyCallback, nullptr);
1038
Joachim Bauch831c5582015-05-20 12:48:41 +02001039 // Select list of available ciphers. Note that !SHA256 and !SHA384 only
1040 // remove HMAC-SHA256 and HMAC-SHA384 cipher suites, not GCM cipher suites
1041 // with SHA256 or SHA384 as the handshake hash.
1042 // This matches the list of SSLClientSocketOpenSSL in Chromium.
deadbeef37f5ecf2017-02-27 14:06:41 -08001043 SSL_CTX_set_cipher_list(
1044 ctx, "DEFAULT:!NULL:!aNULL:!SHA256:!SHA384:!aECDH:!AESGCM+AES256:!aPSK");
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001045
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001046 if (!srtp_ciphers_.empty()) {
1047 if (SSL_CTX_set_tlsext_use_srtp(ctx, srtp_ciphers_.c_str())) {
1048 SSL_CTX_free(ctx);
deadbeef37f5ecf2017-02-27 14:06:41 -08001049 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001050 }
1051 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001052
1053 return ctx;
1054}
1055
deadbeef89824f62016-09-30 11:55:43 -07001056bool OpenSSLStreamAdapter::VerifyPeerCertificate() {
1057 if (!has_peer_certificate_digest() || !peer_certificate_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001058 RTC_LOG(LS_WARNING) << "Missing digest or peer certificate.";
deadbeef89824f62016-09-30 11:55:43 -07001059 return false;
1060 }
deadbeef81f6f4f2016-09-19 17:20:52 -07001061
deadbeef89824f62016-09-30 11:55:43 -07001062 unsigned char digest[EVP_MAX_MD_SIZE];
1063 size_t digest_length;
1064 if (!OpenSSLCertificate::ComputeDigest(
1065 peer_certificate_->x509(), peer_certificate_digest_algorithm_, digest,
1066 sizeof(digest), &digest_length)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001067 RTC_LOG(LS_WARNING) << "Failed to compute peer cert digest.";
deadbeef89824f62016-09-30 11:55:43 -07001068 return false;
1069 }
1070
1071 Buffer computed_digest(digest, digest_length);
1072 if (computed_digest != peer_certificate_digest_value_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001073 RTC_LOG(LS_WARNING)
1074 << "Rejected peer certificate due to mismatched digest.";
jbauchf8f457b2017-03-09 16:24:57 -08001075 return false;
deadbeef81f6f4f2016-09-19 17:20:52 -07001076 }
deadbeef89824f62016-09-30 11:55:43 -07001077 // Ignore any verification error if the digest matches, since there is no
1078 // value in checking the validity of a self-signed cert issued by untrusted
1079 // sources.
Mirko Bonadei675513b2017-11-09 11:09:25 +01001080 RTC_LOG(LS_INFO) << "Accepted peer certificate.";
deadbeef89824f62016-09-30 11:55:43 -07001081 peer_certificate_verified_ = true;
1082 return true;
1083}
1084
Jian Cui0a8798b2017-11-16 16:58:02 -08001085std::unique_ptr<SSLCertChain> OpenSSLStreamAdapter::GetPeerSSLCertChain()
1086 const {
1087 return std::unique_ptr<SSLCertChain>(peer_cert_chain_->Copy());
1088}
1089
David Benjamindc246562017-09-29 12:14:08 -04001090int OpenSSLStreamAdapter::SSLVerifyCallback(X509_STORE_CTX* store, void* arg) {
1091 // Get our SSL structure and OpenSSLStreamAdapter from the store.
deadbeef89824f62016-09-30 11:55:43 -07001092 SSL* ssl = reinterpret_cast<SSL*>(
1093 X509_STORE_CTX_get_ex_data(store, SSL_get_ex_data_X509_STORE_CTX_idx()));
deadbeef89824f62016-09-30 11:55:43 -07001094 OpenSSLStreamAdapter* stream =
1095 reinterpret_cast<OpenSSLStreamAdapter*>(SSL_get_app_data(ssl));
henrike@webrtc.org4e5f65a2014-06-05 20:40:11 +00001096
Jian Cui0a8798b2017-11-16 16:58:02 -08001097#if defined(OPENSSL_IS_BORINGSSL)
1098 STACK_OF(X509)* chain = SSL_get_peer_full_cert_chain(ssl);
1099 // Creates certificate.
1100 stream->peer_certificate_.reset(
1101 new OpenSSLCertificate(sk_X509_value(chain, 0)));
1102 // Creates certificate chain.
1103 std::vector<std::unique_ptr<SSLCertificate>> cert_chain;
1104 for (X509* cert : chain) {
1105 cert_chain.emplace_back(new OpenSSLCertificate(cert));
1106 }
1107 stream->peer_cert_chain_.reset(new SSLCertChain(std::move(cert_chain)));
1108#else
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001109 // Record the peer's certificate.
David Benjamindc246562017-09-29 12:14:08 -04001110 X509* cert = SSL_get_peer_certificate(ssl);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001111 stream->peer_certificate_.reset(new OpenSSLCertificate(cert));
David Benjamindc246562017-09-29 12:14:08 -04001112 X509_free(cert);
Jian Cui0a8798b2017-11-16 16:58:02 -08001113#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001114
deadbeef89824f62016-09-30 11:55:43 -07001115 // If the peer certificate digest isn't known yet, we'll wait to verify
1116 // until it's known, and for now just return a success status.
1117 if (stream->peer_certificate_digest_algorithm_.empty()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001118 RTC_LOG(LS_INFO) << "Waiting to verify certificate until digest is known.";
deadbeef89824f62016-09-30 11:55:43 -07001119 return 1;
1120 }
1121
David Benjamindc246562017-09-29 12:14:08 -04001122 if (!stream->VerifyPeerCertificate()) {
1123 X509_STORE_CTX_set_error(store, X509_V_ERR_CERT_REJECTED);
1124 return 0;
1125 }
1126
1127 return 1;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001128}
1129
Taylor Brandstetter4f0dfbd2016-06-15 17:15:23 -07001130bool OpenSSLStreamAdapter::IsBoringSsl() {
1131#ifdef OPENSSL_IS_BORINGSSL
1132 return true;
1133#else
1134 return false;
1135#endif
1136}
1137
torbjorng43166b82016-03-11 00:06:47 -08001138#define CDEF(X) \
1139 { static_cast<uint16_t>(TLS1_CK_##X & 0xffff), "TLS_" #X }
1140
1141struct cipher_list {
1142 uint16_t cipher;
1143 const char* cipher_str;
1144};
1145
1146// TODO(torbjorng): Perhaps add more cipher suites to these lists.
1147static const cipher_list OK_RSA_ciphers[] = {
1148 CDEF(ECDHE_RSA_WITH_AES_128_CBC_SHA),
1149 CDEF(ECDHE_RSA_WITH_AES_256_CBC_SHA),
1150 CDEF(ECDHE_RSA_WITH_AES_128_GCM_SHA256),
1151#ifdef TLS1_CK_ECDHE_RSA_WITH_AES_256_GCM_SHA256
1152 CDEF(ECDHE_RSA_WITH_AES_256_GCM_SHA256),
1153#endif
torbjorngaad67802016-04-07 08:55:28 -07001154#ifdef TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256
torbjorng43166b82016-03-11 00:06:47 -08001155 CDEF(ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256),
torbjorngaad67802016-04-07 08:55:28 -07001156#endif
torbjorng43166b82016-03-11 00:06:47 -08001157};
1158
1159static const cipher_list OK_ECDSA_ciphers[] = {
1160 CDEF(ECDHE_ECDSA_WITH_AES_128_CBC_SHA),
1161 CDEF(ECDHE_ECDSA_WITH_AES_256_CBC_SHA),
1162 CDEF(ECDHE_ECDSA_WITH_AES_128_GCM_SHA256),
1163#ifdef TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA256
1164 CDEF(ECDHE_ECDSA_WITH_AES_256_GCM_SHA256),
1165#endif
torbjorngaad67802016-04-07 08:55:28 -07001166#ifdef TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256
torbjorng43166b82016-03-11 00:06:47 -08001167 CDEF(ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256),
torbjorngaad67802016-04-07 08:55:28 -07001168#endif
torbjorng43166b82016-03-11 00:06:47 -08001169};
1170#undef CDEF
1171
1172bool OpenSSLStreamAdapter::IsAcceptableCipher(int cipher, KeyType key_type) {
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +02001173 if (key_type == KT_RSA) {
torbjorng43166b82016-03-11 00:06:47 -08001174 for (const cipher_list& c : OK_RSA_ciphers) {
1175 if (cipher == c.cipher)
1176 return true;
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +02001177 }
Joachim Bauch831c5582015-05-20 12:48:41 +02001178 }
torbjorng43166b82016-03-11 00:06:47 -08001179
1180 if (key_type == KT_ECDSA) {
1181 for (const cipher_list& c : OK_ECDSA_ciphers) {
1182 if (cipher == c.cipher)
1183 return true;
1184 }
1185 }
1186
1187 return false;
1188}
1189
1190bool OpenSSLStreamAdapter::IsAcceptableCipher(const std::string& cipher,
1191 KeyType key_type) {
1192 if (key_type == KT_RSA) {
1193 for (const cipher_list& c : OK_RSA_ciphers) {
1194 if (cipher == c.cipher_str)
1195 return true;
1196 }
1197 }
1198
1199 if (key_type == KT_ECDSA) {
1200 for (const cipher_list& c : OK_ECDSA_ciphers) {
1201 if (cipher == c.cipher_str)
1202 return true;
1203 }
1204 }
1205
1206 return false;
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +00001207}
1208
deadbeef6cf94a02016-11-28 17:38:34 -08001209void OpenSSLStreamAdapter::enable_time_callback_for_testing() {
1210 g_use_time_callback_for_testing = true;
1211}
1212
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001213} // namespace rtc