blob: adbc0c04d026c7a5f7c4986b0994101fe39ca7de [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
Jiawei Oueb0df082018-02-02 14:51:18 -0800165static BIO_METHOD* BIO_stream_method() {
166 static BIO_METHOD* method = [] {
167 BIO_METHOD* method = BIO_meth_new(BIO_TYPE_BIO, "stream");
168 BIO_meth_set_write(method, stream_write);
169 BIO_meth_set_read(method, stream_read);
170 BIO_meth_set_puts(method, stream_puts);
171 BIO_meth_set_ctrl(method, stream_ctrl);
172 BIO_meth_set_create(method, stream_new);
173 BIO_meth_set_destroy(method, stream_free);
174 return method;
175 }();
176 return method;
177}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000178
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000179static BIO* BIO_new_stream(StreamInterface* stream) {
Jiawei Oueb0df082018-02-02 14:51:18 -0800180 BIO* ret = BIO_new(BIO_stream_method());
deadbeef37f5ecf2017-02-27 14:06:41 -0800181 if (ret == nullptr)
182 return nullptr;
Jiawei Oueb0df082018-02-02 14:51:18 -0800183 BIO_set_data(ret, stream);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000184 return ret;
185}
186
187// bio methods return 1 (or at least non-zero) on success and 0 on failure.
188
189static int stream_new(BIO* b) {
Jiawei Oueb0df082018-02-02 14:51:18 -0800190 BIO_set_shutdown(b, 0);
191 BIO_set_init(b, 1);
192 BIO_set_data(b, 0);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000193 return 1;
194}
195
196static int stream_free(BIO* b) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800197 if (b == nullptr)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000198 return 0;
199 return 1;
200}
201
202static int stream_read(BIO* b, char* out, int outl) {
203 if (!out)
204 return -1;
Jiawei Oueb0df082018-02-02 14:51:18 -0800205 StreamInterface* stream = static_cast<StreamInterface*>(BIO_get_data(b));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000206 BIO_clear_retry_flags(b);
207 size_t read;
208 int error;
209 StreamResult result = stream->Read(out, outl, &read, &error);
210 if (result == SR_SUCCESS) {
henrike@webrtc.orgd89b69a2014-11-06 17:23:09 +0000211 return checked_cast<int>(read);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000212 } else if (result == SR_BLOCK) {
213 BIO_set_retry_read(b);
214 }
215 return -1;
216}
217
218static int stream_write(BIO* b, const char* in, int inl) {
219 if (!in)
220 return -1;
Jiawei Oueb0df082018-02-02 14:51:18 -0800221 StreamInterface* stream = static_cast<StreamInterface*>(BIO_get_data(b));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000222 BIO_clear_retry_flags(b);
223 size_t written;
224 int error;
225 StreamResult result = stream->Write(in, inl, &written, &error);
226 if (result == SR_SUCCESS) {
henrike@webrtc.orgd89b69a2014-11-06 17:23:09 +0000227 return checked_cast<int>(written);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000228 } else if (result == SR_BLOCK) {
229 BIO_set_retry_write(b);
230 }
231 return -1;
232}
233
234static int stream_puts(BIO* b, const char* str) {
henrike@webrtc.orgd89b69a2014-11-06 17:23:09 +0000235 return stream_write(b, str, checked_cast<int>(strlen(str)));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000236}
237
238static long stream_ctrl(BIO* b, int cmd, long num, void* ptr) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000239 switch (cmd) {
240 case BIO_CTRL_RESET:
241 return 0;
Jiawei Ou018dd6e2018-01-30 12:13:48 -0800242 case BIO_CTRL_EOF: {
243 StreamInterface* stream = static_cast<StreamInterface*>(ptr);
244 // 1 means end-of-stream.
245 return (stream->GetState() == SS_CLOSED) ? 1 : 0;
246 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000247 case BIO_CTRL_WPENDING:
248 case BIO_CTRL_PENDING:
249 return 0;
250 case BIO_CTRL_FLUSH:
251 return 1;
Henrik Lundinf4baca52015-06-10 09:45:58 +0200252 case BIO_CTRL_DGRAM_QUERY_MTU:
253 // openssl defaults to mtu=256 unless we return something here.
254 // The handshake doesn't actually need to send packets above 1k,
255 // so this seems like a sensible value that should work in most cases.
256 // Webrtc uses the same value for video packets.
257 return 1200;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000258 default:
259 return 0;
260 }
261}
262
263/////////////////////////////////////////////////////////////////////////////
264// OpenSSLStreamAdapter
265/////////////////////////////////////////////////////////////////////////////
266
267OpenSSLStreamAdapter::OpenSSLStreamAdapter(StreamInterface* stream)
268 : SSLStreamAdapter(stream),
269 state_(SSL_NONE),
270 role_(SSL_CLIENT),
Guo-wei Shieha7446d22016-01-11 15:27:03 -0800271 ssl_read_needs_write_(false),
272 ssl_write_needs_read_(false),
deadbeef37f5ecf2017-02-27 14:06:41 -0800273 ssl_(nullptr),
274 ssl_ctx_(nullptr),
Joachim Bauch831c5582015-05-20 12:48:41 +0200275 ssl_mode_(SSL_MODE_TLS),
Guo-wei Shieha7446d22016-01-11 15:27:03 -0800276 ssl_max_version_(SSL_PROTOCOL_TLS_12) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000277
278OpenSSLStreamAdapter::~OpenSSLStreamAdapter() {
deadbeef89824f62016-09-30 11:55:43 -0700279 Cleanup(0);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000280}
281
282void OpenSSLStreamAdapter::SetIdentity(SSLIdentity* identity) {
deadbeef89824f62016-09-30 11:55:43 -0700283 RTC_DCHECK(!identity_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000284 identity_.reset(static_cast<OpenSSLIdentity*>(identity));
285}
286
287void OpenSSLStreamAdapter::SetServerRole(SSLRole role) {
288 role_ = role;
289}
290
deadbeef89824f62016-09-30 11:55:43 -0700291bool OpenSSLStreamAdapter::SetPeerCertificateDigest(
292 const std::string& digest_alg,
293 const unsigned char* digest_val,
294 size_t digest_len,
295 SSLPeerCertificateDigestError* error) {
296 RTC_DCHECK(!peer_certificate_verified_);
297 RTC_DCHECK(!has_peer_certificate_digest());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000298 size_t expected_len;
deadbeef89824f62016-09-30 11:55:43 -0700299 if (error) {
300 *error = SSLPeerCertificateDigestError::NONE;
301 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000302
303 if (!OpenSSLDigest::GetDigestSize(digest_alg, &expected_len)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100304 RTC_LOG(LS_WARNING) << "Unknown digest algorithm: " << digest_alg;
deadbeef89824f62016-09-30 11:55:43 -0700305 if (error) {
306 *error = SSLPeerCertificateDigestError::UNKNOWN_ALGORITHM;
307 }
deadbeef81f6f4f2016-09-19 17:20:52 -0700308 return false;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000309 }
deadbeef89824f62016-09-30 11:55:43 -0700310 if (expected_len != digest_len) {
311 if (error) {
312 *error = SSLPeerCertificateDigestError::INVALID_LENGTH;
313 }
deadbeef81f6f4f2016-09-19 17:20:52 -0700314 return false;
deadbeef89824f62016-09-30 11:55:43 -0700315 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000316
317 peer_certificate_digest_value_.SetData(digest_val, digest_len);
318 peer_certificate_digest_algorithm_ = digest_alg;
319
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800320 if (!peer_cert_chain_) {
deadbeef89824f62016-09-30 11:55:43 -0700321 // Normal case, where the digest is set before we obtain the certificate
322 // from the handshake.
323 return true;
324 }
325
326 if (!VerifyPeerCertificate()) {
327 Error("SetPeerCertificateDigest", -1, SSL_AD_BAD_CERTIFICATE, false);
328 if (error) {
329 *error = SSLPeerCertificateDigestError::VERIFICATION_FAILED;
330 }
331 return false;
332 }
333
334 if (state_ == SSL_CONNECTED) {
335 // Post the event asynchronously to unwind the stack. The caller
336 // of ContinueSSL may be the same object listening for these
337 // events and may not be prepared for reentrancy.
338 PostEvent(SE_OPEN | SE_READ | SE_WRITE, 0);
339 }
340
deadbeef81f6f4f2016-09-19 17:20:52 -0700341 return true;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000342}
343
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800344std::string OpenSSLStreamAdapter::SslCipherSuiteToName(int cipher_suite) {
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100345#ifdef OPENSSL_IS_BORINGSSL
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800346 const SSL_CIPHER* ssl_cipher = SSL_get_cipher_by_value(cipher_suite);
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700347 if (!ssl_cipher) {
348 return std::string();
349 }
David Benjamina8f73762017-09-28 15:49:42 -0400350 return SSL_CIPHER_standard_name(ssl_cipher);
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100351#else
352 for (const SslCipherMapEntry* entry = kSslCipherMap; entry->rfc_name;
353 ++entry) {
354 if (cipher_suite == static_cast<int>(entry->openssl_id)) {
355 return entry->rfc_name;
356 }
357 }
358 return std::string();
359#endif
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700360}
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000361
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800362bool OpenSSLStreamAdapter::GetSslCipherSuite(int* cipher_suite) {
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000363 if (state_ != SSL_CONNECTED)
364 return false;
365
366 const SSL_CIPHER* current_cipher = SSL_get_current_cipher(ssl_);
deadbeef37f5ecf2017-02-27 14:06:41 -0800367 if (current_cipher == nullptr) {
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000368 return false;
369 }
370
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800371 *cipher_suite = static_cast<uint16_t>(SSL_CIPHER_get_id(current_cipher));
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000372 return true;
373}
374
torbjorng43166b82016-03-11 00:06:47 -0800375int OpenSSLStreamAdapter::GetSslVersion() const {
376 if (state_ != SSL_CONNECTED)
377 return -1;
378
379 int ssl_version = SSL_version(ssl_);
380 if (ssl_mode_ == SSL_MODE_DTLS) {
381 if (ssl_version == DTLS1_VERSION)
382 return SSL_PROTOCOL_DTLS_10;
383 else if (ssl_version == DTLS1_2_VERSION)
384 return SSL_PROTOCOL_DTLS_12;
385 } else {
386 if (ssl_version == TLS1_VERSION)
387 return SSL_PROTOCOL_TLS_10;
388 else if (ssl_version == TLS1_1_VERSION)
389 return SSL_PROTOCOL_TLS_11;
390 else if (ssl_version == TLS1_2_VERSION)
391 return SSL_PROTOCOL_TLS_12;
392 }
393
394 return -1;
395}
396
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000397// Key Extractor interface
398bool OpenSSLStreamAdapter::ExportKeyingMaterial(const std::string& label,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200399 const uint8_t* context,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000400 size_t context_len,
401 bool use_context,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200402 uint8_t* result,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000403 size_t result_len) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000404 int i;
405
Peter Boström0c4e06b2015-10-07 12:23:21 +0200406 i = SSL_export_keying_material(ssl_, result, result_len, label.c_str(),
407 label.length(), const_cast<uint8_t*>(context),
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000408 context_len, use_context);
409
410 if (i != 1)
411 return false;
412
413 return true;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000414}
415
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800416bool OpenSSLStreamAdapter::SetDtlsSrtpCryptoSuites(
417 const std::vector<int>& ciphers) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000418 std::string internal_ciphers;
419
420 if (state_ != SSL_NONE)
421 return false;
422
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800423 for (std::vector<int>::const_iterator cipher = ciphers.begin();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000424 cipher != ciphers.end(); ++cipher) {
425 bool found = false;
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800426 for (SrtpCipherMapEntry* entry = SrtpCipherMap; entry->internal_name;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000427 ++entry) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800428 if (*cipher == entry->id) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000429 found = true;
430 if (!internal_ciphers.empty())
431 internal_ciphers += ":";
432 internal_ciphers += entry->internal_name;
433 break;
434 }
435 }
436
437 if (!found) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100438 RTC_LOG(LS_ERROR) << "Could not find cipher: " << *cipher;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000439 return false;
440 }
441 }
442
443 if (internal_ciphers.empty())
444 return false;
445
446 srtp_ciphers_ = internal_ciphers;
447 return true;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000448}
449
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800450bool OpenSSLStreamAdapter::GetDtlsSrtpCryptoSuite(int* crypto_suite) {
deadbeef89824f62016-09-30 11:55:43 -0700451 RTC_DCHECK(state_ == SSL_CONNECTED);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000452 if (state_ != SSL_CONNECTED)
453 return false;
454
henrike@webrtc.orgc10ecea2015-01-07 17:59:28 +0000455 const SRTP_PROTECTION_PROFILE *srtp_profile =
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000456 SSL_get_selected_srtp_profile(ssl_);
457
458 if (!srtp_profile)
459 return false;
460
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800461 *crypto_suite = srtp_profile->id;
deadbeef89824f62016-09-30 11:55:43 -0700462 RTC_DCHECK(!SrtpCryptoSuiteToName(*crypto_suite).empty());
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800463 return true;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000464}
465
deadbeef89824f62016-09-30 11:55:43 -0700466bool OpenSSLStreamAdapter::IsTlsConnected() {
467 return state_ == SSL_CONNECTED;
468}
469
Taylor Brandstetterc8762a82016-08-11 12:01:49 -0700470int OpenSSLStreamAdapter::StartSSL() {
deadbeef89824f62016-09-30 11:55:43 -0700471 if (state_ != SSL_NONE) {
472 // Don't allow StartSSL to be called twice.
473 return -1;
474 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000475
Taylor Brandstetterc8762a82016-08-11 12:01:49 -0700476 if (StreamAdapterInterface::GetState() != SS_OPEN) {
477 state_ = SSL_WAIT;
478 return 0;
479 }
480
481 state_ = SSL_CONNECTING;
482 if (int err = BeginSSL()) {
deadbeef89824f62016-09-30 11:55:43 -0700483 Error("BeginSSL", err, 0, false);
Taylor Brandstetterc8762a82016-08-11 12:01:49 -0700484 return err;
485 }
486
487 return 0;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000488}
489
490void OpenSSLStreamAdapter::SetMode(SSLMode mode) {
deadbeef89824f62016-09-30 11:55:43 -0700491 RTC_DCHECK(state_ == SSL_NONE);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000492 ssl_mode_ = mode;
493}
494
Joachim Bauch831c5582015-05-20 12:48:41 +0200495void OpenSSLStreamAdapter::SetMaxProtocolVersion(SSLProtocolVersion version) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800496 RTC_DCHECK(ssl_ctx_ == nullptr);
Joachim Bauch831c5582015-05-20 12:48:41 +0200497 ssl_max_version_ = version;
498}
499
skvladd0309122017-02-02 17:18:37 -0800500void OpenSSLStreamAdapter::SetInitialRetransmissionTimeout(
501 int timeout_ms) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800502 RTC_DCHECK(ssl_ctx_ == nullptr);
skvladd0309122017-02-02 17:18:37 -0800503 dtls_handshake_timeout_ms_ = timeout_ms;
504}
505
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000506//
507// StreamInterface Implementation
508//
509
510StreamResult OpenSSLStreamAdapter::Write(const void* data, size_t data_len,
511 size_t* written, int* error) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100512 RTC_LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::Write(" << data_len << ")";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000513
514 switch (state_) {
515 case SSL_NONE:
516 // pass-through in clear text
517 return StreamAdapterInterface::Write(data, data_len, written, error);
518
519 case SSL_WAIT:
520 case SSL_CONNECTING:
521 return SR_BLOCK;
522
523 case SSL_CONNECTED:
deadbeef89824f62016-09-30 11:55:43 -0700524 if (waiting_to_verify_peer_certificate()) {
525 return SR_BLOCK;
526 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000527 break;
528
529 case SSL_ERROR:
530 case SSL_CLOSED:
531 default:
532 if (error)
533 *error = ssl_error_code_;
534 return SR_ERROR;
535 }
536
537 // OpenSSL will return an error if we try to write zero bytes
538 if (data_len == 0) {
539 if (written)
540 *written = 0;
541 return SR_SUCCESS;
542 }
543
544 ssl_write_needs_read_ = false;
545
henrike@webrtc.orgd89b69a2014-11-06 17:23:09 +0000546 int code = SSL_write(ssl_, data, checked_cast<int>(data_len));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000547 int ssl_error = SSL_get_error(ssl_, code);
548 switch (ssl_error) {
549 case SSL_ERROR_NONE:
Mirko Bonadei675513b2017-11-09 11:09:25 +0100550 RTC_LOG(LS_VERBOSE) << " -- success";
kwibergee89e782017-08-09 17:22:01 -0700551 RTC_DCHECK_GT(code, 0);
552 RTC_DCHECK_LE(code, data_len);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000553 if (written)
554 *written = code;
555 return SR_SUCCESS;
556 case SSL_ERROR_WANT_READ:
Mirko Bonadei675513b2017-11-09 11:09:25 +0100557 RTC_LOG(LS_VERBOSE) << " -- error want read";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000558 ssl_write_needs_read_ = true;
559 return SR_BLOCK;
560 case SSL_ERROR_WANT_WRITE:
Mirko Bonadei675513b2017-11-09 11:09:25 +0100561 RTC_LOG(LS_VERBOSE) << " -- error want write";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000562 return SR_BLOCK;
563
564 case SSL_ERROR_ZERO_RETURN:
565 default:
deadbeef89824f62016-09-30 11:55:43 -0700566 Error("SSL_write", (ssl_error ? ssl_error : -1), 0, false);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000567 if (error)
568 *error = ssl_error_code_;
569 return SR_ERROR;
570 }
571 // not reached
572}
573
574StreamResult OpenSSLStreamAdapter::Read(void* data, size_t data_len,
575 size_t* read, int* error) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100576 RTC_LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::Read(" << data_len << ")";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000577 switch (state_) {
578 case SSL_NONE:
579 // pass-through in clear text
580 return StreamAdapterInterface::Read(data, data_len, read, error);
581
582 case SSL_WAIT:
583 case SSL_CONNECTING:
584 return SR_BLOCK;
585
586 case SSL_CONNECTED:
deadbeef89824f62016-09-30 11:55:43 -0700587 if (waiting_to_verify_peer_certificate()) {
588 return SR_BLOCK;
589 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000590 break;
591
592 case SSL_CLOSED:
593 return SR_EOS;
594
595 case SSL_ERROR:
596 default:
597 if (error)
598 *error = ssl_error_code_;
599 return SR_ERROR;
600 }
601
602 // Don't trust OpenSSL with zero byte reads
603 if (data_len == 0) {
604 if (read)
605 *read = 0;
606 return SR_SUCCESS;
607 }
608
609 ssl_read_needs_write_ = false;
610
henrike@webrtc.orgd89b69a2014-11-06 17:23:09 +0000611 int code = SSL_read(ssl_, data, checked_cast<int>(data_len));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000612 int ssl_error = SSL_get_error(ssl_, code);
613 switch (ssl_error) {
614 case SSL_ERROR_NONE:
Mirko Bonadei675513b2017-11-09 11:09:25 +0100615 RTC_LOG(LS_VERBOSE) << " -- success";
kwibergee89e782017-08-09 17:22:01 -0700616 RTC_DCHECK_GT(code, 0);
617 RTC_DCHECK_LE(code, data_len);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000618 if (read)
619 *read = code;
620
621 if (ssl_mode_ == SSL_MODE_DTLS) {
622 // Enforce atomic reads -- this is a short read
623 unsigned int pending = SSL_pending(ssl_);
624
625 if (pending) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100626 RTC_LOG(LS_INFO) << " -- short DTLS read. flushing";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000627 FlushInput(pending);
628 if (error)
629 *error = SSE_MSG_TRUNC;
630 return SR_ERROR;
631 }
632 }
633 return SR_SUCCESS;
634 case SSL_ERROR_WANT_READ:
Mirko Bonadei675513b2017-11-09 11:09:25 +0100635 RTC_LOG(LS_VERBOSE) << " -- error want read";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000636 return SR_BLOCK;
637 case SSL_ERROR_WANT_WRITE:
Mirko Bonadei675513b2017-11-09 11:09:25 +0100638 RTC_LOG(LS_VERBOSE) << " -- error want write";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000639 ssl_read_needs_write_ = true;
640 return SR_BLOCK;
641 case SSL_ERROR_ZERO_RETURN:
Mirko Bonadei675513b2017-11-09 11:09:25 +0100642 RTC_LOG(LS_VERBOSE) << " -- remote side closed";
deadbeef89824f62016-09-30 11:55:43 -0700643 Close();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000644 return SR_EOS;
645 break;
646 default:
deadbeef89824f62016-09-30 11:55:43 -0700647 Error("SSL_read", (ssl_error ? ssl_error : -1), 0, false);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000648 if (error)
649 *error = ssl_error_code_;
650 return SR_ERROR;
651 }
652 // not reached
653}
654
655void OpenSSLStreamAdapter::FlushInput(unsigned int left) {
656 unsigned char buf[2048];
657
658 while (left) {
659 // This should always succeed
660 int toread = (sizeof(buf) < left) ? sizeof(buf) : left;
661 int code = SSL_read(ssl_, buf, toread);
662
663 int ssl_error = SSL_get_error(ssl_, code);
deadbeef89824f62016-09-30 11:55:43 -0700664 RTC_DCHECK(ssl_error == SSL_ERROR_NONE);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000665
666 if (ssl_error != SSL_ERROR_NONE) {
Jonas Olssonaddc3802018-02-01 09:53:06 +0100667 RTC_DLOG(LS_VERBOSE) << " -- error " << code;
deadbeef89824f62016-09-30 11:55:43 -0700668 Error("SSL_read", (ssl_error ? ssl_error : -1), 0, false);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000669 return;
670 }
671
Mirko Bonadei675513b2017-11-09 11:09:25 +0100672 RTC_LOG(LS_VERBOSE) << " -- flushed " << code << " bytes";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000673 left -= code;
674 }
675}
676
677void OpenSSLStreamAdapter::Close() {
deadbeef89824f62016-09-30 11:55:43 -0700678 Cleanup(0);
679 RTC_DCHECK(state_ == SSL_CLOSED || state_ == SSL_ERROR);
680 // When we're closed at SSL layer, also close the stream level which
681 // performs necessary clean up. Otherwise, a new incoming packet after
682 // this could overflow the stream buffer.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000683 StreamAdapterInterface::Close();
684}
685
686StreamState OpenSSLStreamAdapter::GetState() const {
687 switch (state_) {
688 case SSL_WAIT:
689 case SSL_CONNECTING:
690 return SS_OPENING;
691 case SSL_CONNECTED:
deadbeef89824f62016-09-30 11:55:43 -0700692 if (waiting_to_verify_peer_certificate()) {
693 return SS_OPENING;
694 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000695 return SS_OPEN;
696 default:
697 return SS_CLOSED;
698 };
699 // not reached
700}
701
702void OpenSSLStreamAdapter::OnEvent(StreamInterface* stream, int events,
703 int err) {
704 int events_to_signal = 0;
705 int signal_error = 0;
deadbeef89824f62016-09-30 11:55:43 -0700706 RTC_DCHECK(stream == this->stream());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000707 if ((events & SE_OPEN)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100708 RTC_LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::OnEvent SE_OPEN";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000709 if (state_ != SSL_WAIT) {
deadbeef89824f62016-09-30 11:55:43 -0700710 RTC_DCHECK(state_ == SSL_NONE);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000711 events_to_signal |= SE_OPEN;
712 } else {
713 state_ = SSL_CONNECTING;
714 if (int err = BeginSSL()) {
deadbeef89824f62016-09-30 11:55:43 -0700715 Error("BeginSSL", err, 0, true);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000716 return;
717 }
718 }
719 }
720 if ((events & (SE_READ|SE_WRITE))) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100721 RTC_LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::OnEvent"
722 << ((events & SE_READ) ? " SE_READ" : "")
723 << ((events & SE_WRITE) ? " SE_WRITE" : "");
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000724 if (state_ == SSL_NONE) {
725 events_to_signal |= events & (SE_READ|SE_WRITE);
726 } else if (state_ == SSL_CONNECTING) {
727 if (int err = ContinueSSL()) {
deadbeef89824f62016-09-30 11:55:43 -0700728 Error("ContinueSSL", err, 0, true);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000729 return;
730 }
731 } else if (state_ == SSL_CONNECTED) {
732 if (((events & SE_READ) && ssl_write_needs_read_) ||
733 (events & SE_WRITE)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100734 RTC_LOG(LS_VERBOSE) << " -- onStreamWriteable";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000735 events_to_signal |= SE_WRITE;
736 }
737 if (((events & SE_WRITE) && ssl_read_needs_write_) ||
738 (events & SE_READ)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100739 RTC_LOG(LS_VERBOSE) << " -- onStreamReadable";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000740 events_to_signal |= SE_READ;
741 }
742 }
743 }
744 if ((events & SE_CLOSE)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100745 RTC_LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::OnEvent(SE_CLOSE, " << err
746 << ")";
deadbeef89824f62016-09-30 11:55:43 -0700747 Cleanup(0);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000748 events_to_signal |= SE_CLOSE;
749 // SE_CLOSE is the only event that uses the final parameter to OnEvent().
deadbeef89824f62016-09-30 11:55:43 -0700750 RTC_DCHECK(signal_error == 0);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000751 signal_error = err;
752 }
753 if (events_to_signal)
754 StreamAdapterInterface::OnEvent(stream, events_to_signal, signal_error);
755}
756
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000757int OpenSSLStreamAdapter::BeginSSL() {
deadbeef89824f62016-09-30 11:55:43 -0700758 RTC_DCHECK(state_ == SSL_CONNECTING);
Taylor Brandstetterc8762a82016-08-11 12:01:49 -0700759 // The underlying stream has opened.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100760 RTC_LOG(LS_INFO) << "BeginSSL with peer.";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000761
deadbeef37f5ecf2017-02-27 14:06:41 -0800762 BIO* bio = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000763
Taylor Brandstetterc8762a82016-08-11 12:01:49 -0700764 // First set up the context.
deadbeef37f5ecf2017-02-27 14:06:41 -0800765 RTC_DCHECK(ssl_ctx_ == nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000766 ssl_ctx_ = SetupSSLContext();
767 if (!ssl_ctx_)
768 return -1;
769
770 bio = BIO_new_stream(static_cast<StreamInterface*>(stream()));
771 if (!bio)
772 return -1;
773
774 ssl_ = SSL_new(ssl_ctx_);
775 if (!ssl_) {
776 BIO_free(bio);
777 return -1;
778 }
779
780 SSL_set_app_data(ssl_, this);
781
782 SSL_set_bio(ssl_, bio, bio); // the SSL object owns the bio now.
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100783 if (ssl_mode_ == SSL_MODE_DTLS) {
Taylor Brandstetter4f0dfbd2016-06-15 17:15:23 -0700784#ifdef OPENSSL_IS_BORINGSSL
skvladd0309122017-02-02 17:18:37 -0800785 DTLSv1_set_initial_timeout_duration(ssl_, dtls_handshake_timeout_ms_);
Taylor Brandstetter4f0dfbd2016-06-15 17:15:23 -0700786#else
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100787 // Enable read-ahead for DTLS so whole packets are read from internal BIO
788 // before parsing. This is done internally by BoringSSL for DTLS.
789 SSL_set_read_ahead(ssl_, 1);
philipel49c08692016-05-24 01:49:43 -0700790#endif
Taylor Brandstetter4f0dfbd2016-06-15 17:15:23 -0700791 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000792
793 SSL_set_mode(ssl_, SSL_MODE_ENABLE_PARTIAL_WRITE |
794 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
795
David Benjamin60d5f3f2016-03-24 13:28:25 -0400796#if !defined(OPENSSL_IS_BORINGSSL)
797 // Specify an ECDH group for ECDHE ciphers, otherwise OpenSSL cannot
798 // negotiate them when acting as the server. Use NIST's P-256 which is
799 // commonly supported. BoringSSL doesn't need explicit configuration and has
800 // a reasonable default set.
jiayl@webrtc.org11c6bde2014-08-28 16:14:38 +0000801 EC_KEY* ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
deadbeef37f5ecf2017-02-27 14:06:41 -0800802 if (ecdh == nullptr)
jiayl@webrtc.org11c6bde2014-08-28 16:14:38 +0000803 return -1;
804 SSL_set_options(ssl_, SSL_OP_SINGLE_ECDH_USE);
805 SSL_set_tmp_ecdh(ssl_, ecdh);
806 EC_KEY_free(ecdh);
David Benjamin60d5f3f2016-03-24 13:28:25 -0400807#endif
jiayl@webrtc.org11c6bde2014-08-28 16:14:38 +0000808
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000809 // Do the connect
810 return ContinueSSL();
811}
812
813int OpenSSLStreamAdapter::ContinueSSL() {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100814 RTC_LOG(LS_VERBOSE) << "ContinueSSL";
deadbeef89824f62016-09-30 11:55:43 -0700815 RTC_DCHECK(state_ == SSL_CONNECTING);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000816
817 // Clear the DTLS timer
818 Thread::Current()->Clear(this, MSG_TIMEOUT);
819
820 int code = (role_ == SSL_CLIENT) ? SSL_connect(ssl_) : SSL_accept(ssl_);
821 int ssl_error;
822 switch (ssl_error = SSL_get_error(ssl_, code)) {
823 case SSL_ERROR_NONE:
Mirko Bonadei675513b2017-11-09 11:09:25 +0100824 RTC_LOG(LS_VERBOSE) << " -- success";
deadbeef89824f62016-09-30 11:55:43 -0700825 // By this point, OpenSSL should have given us a certificate, or errored
826 // out if one was missing.
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800827 RTC_DCHECK(peer_cert_chain_ || !client_auth_enabled());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000828
829 state_ = SSL_CONNECTED;
deadbeef89824f62016-09-30 11:55:43 -0700830 if (!waiting_to_verify_peer_certificate()) {
831 // We have everything we need to start the connection, so signal
832 // SE_OPEN. If we need a client certificate fingerprint and don't have
833 // it yet, we'll instead signal SE_OPEN in SetPeerCertificateDigest.
834 //
Taylor Brandstetterfe69a742016-09-30 17:34:32 -0700835 // TODO(deadbeef): Post this event asynchronously to unwind the stack.
836 // The caller of ContinueSSL may be the same object listening for these
837 // events and may not be prepared for reentrancy.
838 // PostEvent(SE_OPEN | SE_READ | SE_WRITE, 0);
839 StreamAdapterInterface::OnEvent(stream(), SE_OPEN | SE_READ | SE_WRITE,
840 0);
deadbeef89824f62016-09-30 11:55:43 -0700841 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000842 break;
843
844 case SSL_ERROR_WANT_READ: {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100845 RTC_LOG(LS_VERBOSE) << " -- error want read";
846 struct timeval timeout;
847 if (DTLSv1_get_timeout(ssl_, &timeout)) {
848 int delay = timeout.tv_sec * 1000 + timeout.tv_usec / 1000;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000849
Mirko Bonadei675513b2017-11-09 11:09:25 +0100850 Thread::Current()->PostDelayed(RTC_FROM_HERE, delay, this, MSG_TIMEOUT,
851 0);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000852 }
853 }
854 break;
855
856 case SSL_ERROR_WANT_WRITE:
Mirko Bonadei675513b2017-11-09 11:09:25 +0100857 RTC_LOG(LS_VERBOSE) << " -- error want write";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000858 break;
859
860 case SSL_ERROR_ZERO_RETURN:
861 default:
Mirko Bonadei675513b2017-11-09 11:09:25 +0100862 RTC_LOG(LS_VERBOSE) << " -- error " << code;
zhihuangd82eee02016-08-26 11:25:05 -0700863 SSLHandshakeError ssl_handshake_err = SSLHandshakeError::UNKNOWN;
864 int err_code = ERR_peek_last_error();
865 if (err_code != 0 && ERR_GET_REASON(err_code) == SSL_R_NO_SHARED_CIPHER) {
866 ssl_handshake_err = SSLHandshakeError::INCOMPATIBLE_CIPHERSUITE;
867 }
868 SignalSSLHandshakeError(ssl_handshake_err);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000869 return (ssl_error != 0) ? ssl_error : -1;
870 }
871
872 return 0;
873}
874
deadbeef89824f62016-09-30 11:55:43 -0700875void OpenSSLStreamAdapter::Error(const char* context,
876 int err,
877 uint8_t alert,
878 bool signal) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100879 RTC_LOG(LS_WARNING) << "OpenSSLStreamAdapter::Error(" << context << ", "
880 << err << ", " << static_cast<int>(alert) << ")";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000881 state_ = SSL_ERROR;
882 ssl_error_code_ = err;
deadbeef89824f62016-09-30 11:55:43 -0700883 Cleanup(alert);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000884 if (signal)
885 StreamAdapterInterface::OnEvent(stream(), SE_CLOSE, err);
886}
887
deadbeef89824f62016-09-30 11:55:43 -0700888void OpenSSLStreamAdapter::Cleanup(uint8_t alert) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100889 RTC_LOG(LS_INFO) << "Cleanup";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000890
891 if (state_ != SSL_ERROR) {
892 state_ = SSL_CLOSED;
893 ssl_error_code_ = 0;
894 }
895
896 if (ssl_) {
deadbeef89824f62016-09-30 11:55:43 -0700897 int ret;
898// SSL_send_fatal_alert is only available in BoringSSL.
899#ifdef OPENSSL_IS_BORINGSSL
900 if (alert) {
901 ret = SSL_send_fatal_alert(ssl_, alert);
902 if (ret < 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100903 RTC_LOG(LS_WARNING) << "SSL_send_fatal_alert failed, error = "
904 << SSL_get_error(ssl_, ret);
deadbeef89824f62016-09-30 11:55:43 -0700905 }
906 } else {
907#endif
908 ret = SSL_shutdown(ssl_);
909 if (ret < 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100910 RTC_LOG(LS_WARNING)
911 << "SSL_shutdown failed, error = " << SSL_get_error(ssl_, ret);
deadbeef89824f62016-09-30 11:55:43 -0700912 }
913#ifdef OPENSSL_IS_BORINGSSL
jiayl@webrtc.orgf1d751c2014-09-25 16:38:46 +0000914 }
deadbeef89824f62016-09-30 11:55:43 -0700915#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000916 SSL_free(ssl_);
deadbeef37f5ecf2017-02-27 14:06:41 -0800917 ssl_ = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000918 }
919 if (ssl_ctx_) {
920 SSL_CTX_free(ssl_ctx_);
deadbeef37f5ecf2017-02-27 14:06:41 -0800921 ssl_ctx_ = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000922 }
923 identity_.reset();
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800924 peer_cert_chain_.reset();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000925
926 // Clear the DTLS timer
927 Thread::Current()->Clear(this, MSG_TIMEOUT);
928}
929
930
931void OpenSSLStreamAdapter::OnMessage(Message* msg) {
932 // Process our own messages and then pass others to the superclass
933 if (MSG_TIMEOUT == msg->message_id) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100934 RTC_LOG(LS_INFO) << "DTLS timeout expired";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000935 DTLSv1_handle_timeout(ssl_);
936 ContinueSSL();
937 } else {
938 StreamInterface::OnMessage(msg);
939 }
940}
941
942SSL_CTX* OpenSSLStreamAdapter::SetupSSLContext() {
deadbeef37f5ecf2017-02-27 14:06:41 -0800943 SSL_CTX* ctx = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000944
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100945#ifdef OPENSSL_IS_BORINGSSL
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000946 ctx = SSL_CTX_new(ssl_mode_ == SSL_MODE_DTLS ?
Joachim Bauch831c5582015-05-20 12:48:41 +0200947 DTLS_method() : TLS_method());
948 // Version limiting for BoringSSL will be done below.
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100949#else
950 const SSL_METHOD* method;
951 switch (ssl_max_version_) {
952 case SSL_PROTOCOL_TLS_10:
953 case SSL_PROTOCOL_TLS_11:
954 // OpenSSL doesn't support setting min/max versions, so we always use
955 // (D)TLS 1.0 if a max. version below the max. available is requested.
956 if (ssl_mode_ == SSL_MODE_DTLS) {
957 if (role_ == SSL_CLIENT) {
958 method = DTLSv1_client_method();
959 } else {
960 method = DTLSv1_server_method();
961 }
962 } else {
963 if (role_ == SSL_CLIENT) {
964 method = TLSv1_client_method();
965 } else {
966 method = TLSv1_server_method();
967 }
968 }
969 break;
970 case SSL_PROTOCOL_TLS_12:
971 default:
972 if (ssl_mode_ == SSL_MODE_DTLS) {
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100973 if (role_ == SSL_CLIENT) {
974 method = DTLS_client_method();
975 } else {
976 method = DTLS_server_method();
977 }
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100978 } else {
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100979 if (role_ == SSL_CLIENT) {
980 method = TLS_client_method();
981 } else {
982 method = TLS_server_method();
983 }
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100984 }
985 break;
986 }
987 ctx = SSL_CTX_new(method);
988#endif // OPENSSL_IS_BORINGSSL
Joachim Bauch831c5582015-05-20 12:48:41 +0200989
deadbeef37f5ecf2017-02-27 14:06:41 -0800990 if (ctx == nullptr)
991 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000992
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100993#ifdef OPENSSL_IS_BORINGSSL
davidbene36c46e2016-12-06 17:12:02 -0800994 SSL_CTX_set_min_proto_version(ctx, ssl_mode_ == SSL_MODE_DTLS ?
Joachim Bauch831c5582015-05-20 12:48:41 +0200995 DTLS1_VERSION : TLS1_VERSION);
996 switch (ssl_max_version_) {
997 case SSL_PROTOCOL_TLS_10:
davidbene36c46e2016-12-06 17:12:02 -0800998 SSL_CTX_set_max_proto_version(ctx, ssl_mode_ == SSL_MODE_DTLS ?
Joachim Bauch831c5582015-05-20 12:48:41 +0200999 DTLS1_VERSION : TLS1_VERSION);
1000 break;
1001 case SSL_PROTOCOL_TLS_11:
davidbene36c46e2016-12-06 17:12:02 -08001002 SSL_CTX_set_max_proto_version(ctx, ssl_mode_ == SSL_MODE_DTLS ?
Joachim Bauch831c5582015-05-20 12:48:41 +02001003 DTLS1_VERSION : TLS1_1_VERSION);
1004 break;
1005 case SSL_PROTOCOL_TLS_12:
1006 default:
davidbene36c46e2016-12-06 17:12:02 -08001007 SSL_CTX_set_max_proto_version(ctx, ssl_mode_ == SSL_MODE_DTLS ?
Joachim Bauch831c5582015-05-20 12:48:41 +02001008 DTLS1_2_VERSION : TLS1_2_VERSION);
1009 break;
1010 }
deadbeef6cf94a02016-11-28 17:38:34 -08001011 if (g_use_time_callback_for_testing) {
1012 SSL_CTX_set_current_time_cb(ctx, &TimeCallbackForTesting);
1013 }
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +01001014#endif
Joachim Bauch831c5582015-05-20 12:48:41 +02001015
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001016 if (identity_ && !identity_->ConfigureIdentity(ctx)) {
1017 SSL_CTX_free(ctx);
deadbeef37f5ecf2017-02-27 14:06:41 -08001018 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001019 }
1020
tfarinaa41ab932015-10-30 16:08:48 -07001021#if !defined(NDEBUG)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001022 SSL_CTX_set_info_callback(ctx, OpenSSLAdapter::SSLInfoCallback);
1023#endif
1024
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +00001025 int mode = SSL_VERIFY_PEER;
1026 if (client_auth_enabled()) {
1027 // Require a certificate from the client.
1028 // Note: Normally this is always true in production, but it may be disabled
1029 // for testing purposes (e.g. SSLAdapter unit tests).
1030 mode |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
1031 }
1032
David Benjamindc246562017-09-29 12:14:08 -04001033 // Configure a custom certificate verification callback to check the peer
1034 // certificate digest. Note the second argument to SSL_CTX_set_verify is to
1035 // override individual errors in the default verification logic, which is not
1036 // what we want here.
1037 SSL_CTX_set_verify(ctx, mode, nullptr);
1038 SSL_CTX_set_cert_verify_callback(ctx, SSLVerifyCallback, nullptr);
1039
Joachim Bauch831c5582015-05-20 12:48:41 +02001040 // Select list of available ciphers. Note that !SHA256 and !SHA384 only
1041 // remove HMAC-SHA256 and HMAC-SHA384 cipher suites, not GCM cipher suites
1042 // with SHA256 or SHA384 as the handshake hash.
1043 // This matches the list of SSLClientSocketOpenSSL in Chromium.
deadbeef37f5ecf2017-02-27 14:06:41 -08001044 SSL_CTX_set_cipher_list(
1045 ctx, "DEFAULT:!NULL:!aNULL:!SHA256:!SHA384:!aECDH:!AESGCM+AES256:!aPSK");
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001046
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001047 if (!srtp_ciphers_.empty()) {
1048 if (SSL_CTX_set_tlsext_use_srtp(ctx, srtp_ciphers_.c_str())) {
1049 SSL_CTX_free(ctx);
deadbeef37f5ecf2017-02-27 14:06:41 -08001050 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001051 }
1052 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001053
1054 return ctx;
1055}
1056
deadbeef89824f62016-09-30 11:55:43 -07001057bool OpenSSLStreamAdapter::VerifyPeerCertificate() {
Taylor Brandstetterc3928662018-02-23 13:04:51 -08001058 if (!has_peer_certificate_digest() || !peer_cert_chain_ ||
1059 !peer_cert_chain_->GetSize()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001060 RTC_LOG(LS_WARNING) << "Missing digest or peer certificate.";
deadbeef89824f62016-09-30 11:55:43 -07001061 return false;
1062 }
Taylor Brandstetterc3928662018-02-23 13:04:51 -08001063 const OpenSSLCertificate* leaf_cert =
1064 static_cast<const OpenSSLCertificate*>(&peer_cert_chain_->Get(0));
deadbeef81f6f4f2016-09-19 17:20:52 -07001065
deadbeef89824f62016-09-30 11:55:43 -07001066 unsigned char digest[EVP_MAX_MD_SIZE];
1067 size_t digest_length;
1068 if (!OpenSSLCertificate::ComputeDigest(
Taylor Brandstetterc3928662018-02-23 13:04:51 -08001069 leaf_cert->x509(), peer_certificate_digest_algorithm_, digest,
deadbeef89824f62016-09-30 11:55:43 -07001070 sizeof(digest), &digest_length)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001071 RTC_LOG(LS_WARNING) << "Failed to compute peer cert digest.";
deadbeef89824f62016-09-30 11:55:43 -07001072 return false;
1073 }
1074
1075 Buffer computed_digest(digest, digest_length);
1076 if (computed_digest != peer_certificate_digest_value_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001077 RTC_LOG(LS_WARNING)
1078 << "Rejected peer certificate due to mismatched digest.";
jbauchf8f457b2017-03-09 16:24:57 -08001079 return false;
deadbeef81f6f4f2016-09-19 17:20:52 -07001080 }
deadbeef89824f62016-09-30 11:55:43 -07001081 // Ignore any verification error if the digest matches, since there is no
1082 // value in checking the validity of a self-signed cert issued by untrusted
1083 // sources.
Mirko Bonadei675513b2017-11-09 11:09:25 +01001084 RTC_LOG(LS_INFO) << "Accepted peer certificate.";
deadbeef89824f62016-09-30 11:55:43 -07001085 peer_certificate_verified_ = true;
1086 return true;
1087}
1088
Jian Cui0a8798b2017-11-16 16:58:02 -08001089std::unique_ptr<SSLCertChain> OpenSSLStreamAdapter::GetPeerSSLCertChain()
1090 const {
Taylor Brandstetterc3928662018-02-23 13:04:51 -08001091 return peer_cert_chain_ ? peer_cert_chain_->UniqueCopy() : nullptr;
Jian Cui0a8798b2017-11-16 16:58:02 -08001092}
1093
David Benjamindc246562017-09-29 12:14:08 -04001094int OpenSSLStreamAdapter::SSLVerifyCallback(X509_STORE_CTX* store, void* arg) {
1095 // Get our SSL structure and OpenSSLStreamAdapter from the store.
deadbeef89824f62016-09-30 11:55:43 -07001096 SSL* ssl = reinterpret_cast<SSL*>(
1097 X509_STORE_CTX_get_ex_data(store, SSL_get_ex_data_X509_STORE_CTX_idx()));
deadbeef89824f62016-09-30 11:55:43 -07001098 OpenSSLStreamAdapter* stream =
1099 reinterpret_cast<OpenSSLStreamAdapter*>(SSL_get_app_data(ssl));
henrike@webrtc.org4e5f65a2014-06-05 20:40:11 +00001100
Jian Cui0a8798b2017-11-16 16:58:02 -08001101#if defined(OPENSSL_IS_BORINGSSL)
1102 STACK_OF(X509)* chain = SSL_get_peer_full_cert_chain(ssl);
Jian Cui0a8798b2017-11-16 16:58:02 -08001103 // Creates certificate chain.
1104 std::vector<std::unique_ptr<SSLCertificate>> cert_chain;
1105 for (X509* cert : chain) {
1106 cert_chain.emplace_back(new OpenSSLCertificate(cert));
1107 }
1108 stream->peer_cert_chain_.reset(new SSLCertChain(std::move(cert_chain)));
1109#else
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001110 // Record the peer's certificate.
Jiawei Ou9d4e8402018-05-23 15:44:20 -07001111 X509* cert = X509_STORE_CTX_get0_cert(store);
Taylor Brandstetterc3928662018-02-23 13:04:51 -08001112 stream->peer_cert_chain_.reset(
1113 new SSLCertChain(new OpenSSLCertificate(cert)));
Jian Cui0a8798b2017-11-16 16:58:02 -08001114#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001115
deadbeef89824f62016-09-30 11:55:43 -07001116 // If the peer certificate digest isn't known yet, we'll wait to verify
1117 // until it's known, and for now just return a success status.
1118 if (stream->peer_certificate_digest_algorithm_.empty()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001119 RTC_LOG(LS_INFO) << "Waiting to verify certificate until digest is known.";
deadbeef89824f62016-09-30 11:55:43 -07001120 return 1;
1121 }
1122
David Benjamindc246562017-09-29 12:14:08 -04001123 if (!stream->VerifyPeerCertificate()) {
1124 X509_STORE_CTX_set_error(store, X509_V_ERR_CERT_REJECTED);
1125 return 0;
1126 }
1127
1128 return 1;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001129}
1130
Taylor Brandstetter4f0dfbd2016-06-15 17:15:23 -07001131bool OpenSSLStreamAdapter::IsBoringSsl() {
1132#ifdef OPENSSL_IS_BORINGSSL
1133 return true;
1134#else
1135 return false;
1136#endif
1137}
1138
torbjorng43166b82016-03-11 00:06:47 -08001139#define CDEF(X) \
1140 { static_cast<uint16_t>(TLS1_CK_##X & 0xffff), "TLS_" #X }
1141
1142struct cipher_list {
1143 uint16_t cipher;
1144 const char* cipher_str;
1145};
1146
1147// TODO(torbjorng): Perhaps add more cipher suites to these lists.
1148static const cipher_list OK_RSA_ciphers[] = {
1149 CDEF(ECDHE_RSA_WITH_AES_128_CBC_SHA),
1150 CDEF(ECDHE_RSA_WITH_AES_256_CBC_SHA),
1151 CDEF(ECDHE_RSA_WITH_AES_128_GCM_SHA256),
1152#ifdef TLS1_CK_ECDHE_RSA_WITH_AES_256_GCM_SHA256
1153 CDEF(ECDHE_RSA_WITH_AES_256_GCM_SHA256),
1154#endif
torbjorngaad67802016-04-07 08:55:28 -07001155#ifdef TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256
torbjorng43166b82016-03-11 00:06:47 -08001156 CDEF(ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256),
torbjorngaad67802016-04-07 08:55:28 -07001157#endif
torbjorng43166b82016-03-11 00:06:47 -08001158};
1159
1160static const cipher_list OK_ECDSA_ciphers[] = {
1161 CDEF(ECDHE_ECDSA_WITH_AES_128_CBC_SHA),
1162 CDEF(ECDHE_ECDSA_WITH_AES_256_CBC_SHA),
1163 CDEF(ECDHE_ECDSA_WITH_AES_128_GCM_SHA256),
1164#ifdef TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA256
1165 CDEF(ECDHE_ECDSA_WITH_AES_256_GCM_SHA256),
1166#endif
torbjorngaad67802016-04-07 08:55:28 -07001167#ifdef TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256
torbjorng43166b82016-03-11 00:06:47 -08001168 CDEF(ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256),
torbjorngaad67802016-04-07 08:55:28 -07001169#endif
torbjorng43166b82016-03-11 00:06:47 -08001170};
1171#undef CDEF
1172
1173bool OpenSSLStreamAdapter::IsAcceptableCipher(int cipher, KeyType key_type) {
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +02001174 if (key_type == KT_RSA) {
torbjorng43166b82016-03-11 00:06:47 -08001175 for (const cipher_list& c : OK_RSA_ciphers) {
1176 if (cipher == c.cipher)
1177 return true;
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +02001178 }
Joachim Bauch831c5582015-05-20 12:48:41 +02001179 }
torbjorng43166b82016-03-11 00:06:47 -08001180
1181 if (key_type == KT_ECDSA) {
1182 for (const cipher_list& c : OK_ECDSA_ciphers) {
1183 if (cipher == c.cipher)
1184 return true;
1185 }
1186 }
1187
1188 return false;
1189}
1190
1191bool OpenSSLStreamAdapter::IsAcceptableCipher(const std::string& cipher,
1192 KeyType key_type) {
1193 if (key_type == KT_RSA) {
1194 for (const cipher_list& c : OK_RSA_ciphers) {
1195 if (cipher == c.cipher_str)
1196 return true;
1197 }
1198 }
1199
1200 if (key_type == KT_ECDSA) {
1201 for (const cipher_list& c : OK_ECDSA_ciphers) {
1202 if (cipher == c.cipher_str)
1203 return true;
1204 }
1205 }
1206
1207 return false;
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +00001208}
1209
deadbeef6cf94a02016-11-28 17:38:34 -08001210void OpenSSLStreamAdapter::enable_time_callback_for_testing() {
1211 g_use_time_callback_for_testing = true;
1212}
1213
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001214} // namespace rtc