blob: d715e27eccc35d4cbae5ff9862d14ae45bc4145e [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
jbauch555604a2016-04-26 03:13:22 -0700291std::unique_ptr<SSLCertificate> OpenSSLStreamAdapter::GetPeerCertificate()
kwibergb4d01c42016-04-06 05:15:06 -0700292 const {
jbauch555604a2016-04-26 03:13:22 -0700293 return peer_certificate_ ? std::unique_ptr<SSLCertificate>(
kwibergb4d01c42016-04-06 05:15:06 -0700294 peer_certificate_->GetReference())
295 : nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000296}
297
deadbeef89824f62016-09-30 11:55:43 -0700298bool OpenSSLStreamAdapter::SetPeerCertificateDigest(
299 const std::string& digest_alg,
300 const unsigned char* digest_val,
301 size_t digest_len,
302 SSLPeerCertificateDigestError* error) {
303 RTC_DCHECK(!peer_certificate_verified_);
304 RTC_DCHECK(!has_peer_certificate_digest());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000305 size_t expected_len;
deadbeef89824f62016-09-30 11:55:43 -0700306 if (error) {
307 *error = SSLPeerCertificateDigestError::NONE;
308 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000309
310 if (!OpenSSLDigest::GetDigestSize(digest_alg, &expected_len)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100311 RTC_LOG(LS_WARNING) << "Unknown digest algorithm: " << digest_alg;
deadbeef89824f62016-09-30 11:55:43 -0700312 if (error) {
313 *error = SSLPeerCertificateDigestError::UNKNOWN_ALGORITHM;
314 }
deadbeef81f6f4f2016-09-19 17:20:52 -0700315 return false;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000316 }
deadbeef89824f62016-09-30 11:55:43 -0700317 if (expected_len != digest_len) {
318 if (error) {
319 *error = SSLPeerCertificateDigestError::INVALID_LENGTH;
320 }
deadbeef81f6f4f2016-09-19 17:20:52 -0700321 return false;
deadbeef89824f62016-09-30 11:55:43 -0700322 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000323
324 peer_certificate_digest_value_.SetData(digest_val, digest_len);
325 peer_certificate_digest_algorithm_ = digest_alg;
326
deadbeef89824f62016-09-30 11:55:43 -0700327 if (!peer_certificate_) {
328 // Normal case, where the digest is set before we obtain the certificate
329 // from the handshake.
330 return true;
331 }
332
333 if (!VerifyPeerCertificate()) {
334 Error("SetPeerCertificateDigest", -1, SSL_AD_BAD_CERTIFICATE, false);
335 if (error) {
336 *error = SSLPeerCertificateDigestError::VERIFICATION_FAILED;
337 }
338 return false;
339 }
340
341 if (state_ == SSL_CONNECTED) {
342 // Post the event asynchronously to unwind the stack. The caller
343 // of ContinueSSL may be the same object listening for these
344 // events and may not be prepared for reentrancy.
345 PostEvent(SE_OPEN | SE_READ | SE_WRITE, 0);
346 }
347
deadbeef81f6f4f2016-09-19 17:20:52 -0700348 return true;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000349}
350
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800351std::string OpenSSLStreamAdapter::SslCipherSuiteToName(int cipher_suite) {
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100352#ifdef OPENSSL_IS_BORINGSSL
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800353 const SSL_CIPHER* ssl_cipher = SSL_get_cipher_by_value(cipher_suite);
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700354 if (!ssl_cipher) {
355 return std::string();
356 }
David Benjamina8f73762017-09-28 15:49:42 -0400357 return SSL_CIPHER_standard_name(ssl_cipher);
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100358#else
359 for (const SslCipherMapEntry* entry = kSslCipherMap; entry->rfc_name;
360 ++entry) {
361 if (cipher_suite == static_cast<int>(entry->openssl_id)) {
362 return entry->rfc_name;
363 }
364 }
365 return std::string();
366#endif
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700367}
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000368
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800369bool OpenSSLStreamAdapter::GetSslCipherSuite(int* cipher_suite) {
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000370 if (state_ != SSL_CONNECTED)
371 return false;
372
373 const SSL_CIPHER* current_cipher = SSL_get_current_cipher(ssl_);
deadbeef37f5ecf2017-02-27 14:06:41 -0800374 if (current_cipher == nullptr) {
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000375 return false;
376 }
377
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800378 *cipher_suite = static_cast<uint16_t>(SSL_CIPHER_get_id(current_cipher));
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000379 return true;
380}
381
torbjorng43166b82016-03-11 00:06:47 -0800382int OpenSSLStreamAdapter::GetSslVersion() const {
383 if (state_ != SSL_CONNECTED)
384 return -1;
385
386 int ssl_version = SSL_version(ssl_);
387 if (ssl_mode_ == SSL_MODE_DTLS) {
388 if (ssl_version == DTLS1_VERSION)
389 return SSL_PROTOCOL_DTLS_10;
390 else if (ssl_version == DTLS1_2_VERSION)
391 return SSL_PROTOCOL_DTLS_12;
392 } else {
393 if (ssl_version == TLS1_VERSION)
394 return SSL_PROTOCOL_TLS_10;
395 else if (ssl_version == TLS1_1_VERSION)
396 return SSL_PROTOCOL_TLS_11;
397 else if (ssl_version == TLS1_2_VERSION)
398 return SSL_PROTOCOL_TLS_12;
399 }
400
401 return -1;
402}
403
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000404// Key Extractor interface
405bool OpenSSLStreamAdapter::ExportKeyingMaterial(const std::string& label,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200406 const uint8_t* context,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000407 size_t context_len,
408 bool use_context,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200409 uint8_t* result,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000410 size_t result_len) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000411 int i;
412
Peter Boström0c4e06b2015-10-07 12:23:21 +0200413 i = SSL_export_keying_material(ssl_, result, result_len, label.c_str(),
414 label.length(), const_cast<uint8_t*>(context),
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000415 context_len, use_context);
416
417 if (i != 1)
418 return false;
419
420 return true;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000421}
422
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800423bool OpenSSLStreamAdapter::SetDtlsSrtpCryptoSuites(
424 const std::vector<int>& ciphers) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000425 std::string internal_ciphers;
426
427 if (state_ != SSL_NONE)
428 return false;
429
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800430 for (std::vector<int>::const_iterator cipher = ciphers.begin();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000431 cipher != ciphers.end(); ++cipher) {
432 bool found = false;
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800433 for (SrtpCipherMapEntry* entry = SrtpCipherMap; entry->internal_name;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000434 ++entry) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800435 if (*cipher == entry->id) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000436 found = true;
437 if (!internal_ciphers.empty())
438 internal_ciphers += ":";
439 internal_ciphers += entry->internal_name;
440 break;
441 }
442 }
443
444 if (!found) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100445 RTC_LOG(LS_ERROR) << "Could not find cipher: " << *cipher;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000446 return false;
447 }
448 }
449
450 if (internal_ciphers.empty())
451 return false;
452
453 srtp_ciphers_ = internal_ciphers;
454 return true;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000455}
456
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800457bool OpenSSLStreamAdapter::GetDtlsSrtpCryptoSuite(int* crypto_suite) {
deadbeef89824f62016-09-30 11:55:43 -0700458 RTC_DCHECK(state_ == SSL_CONNECTED);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000459 if (state_ != SSL_CONNECTED)
460 return false;
461
henrike@webrtc.orgc10ecea2015-01-07 17:59:28 +0000462 const SRTP_PROTECTION_PROFILE *srtp_profile =
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000463 SSL_get_selected_srtp_profile(ssl_);
464
465 if (!srtp_profile)
466 return false;
467
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800468 *crypto_suite = srtp_profile->id;
deadbeef89824f62016-09-30 11:55:43 -0700469 RTC_DCHECK(!SrtpCryptoSuiteToName(*crypto_suite).empty());
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800470 return true;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000471}
472
deadbeef89824f62016-09-30 11:55:43 -0700473bool OpenSSLStreamAdapter::IsTlsConnected() {
474 return state_ == SSL_CONNECTED;
475}
476
Taylor Brandstetterc8762a82016-08-11 12:01:49 -0700477int OpenSSLStreamAdapter::StartSSL() {
deadbeef89824f62016-09-30 11:55:43 -0700478 if (state_ != SSL_NONE) {
479 // Don't allow StartSSL to be called twice.
480 return -1;
481 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000482
Taylor Brandstetterc8762a82016-08-11 12:01:49 -0700483 if (StreamAdapterInterface::GetState() != SS_OPEN) {
484 state_ = SSL_WAIT;
485 return 0;
486 }
487
488 state_ = SSL_CONNECTING;
489 if (int err = BeginSSL()) {
deadbeef89824f62016-09-30 11:55:43 -0700490 Error("BeginSSL", err, 0, false);
Taylor Brandstetterc8762a82016-08-11 12:01:49 -0700491 return err;
492 }
493
494 return 0;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000495}
496
497void OpenSSLStreamAdapter::SetMode(SSLMode mode) {
deadbeef89824f62016-09-30 11:55:43 -0700498 RTC_DCHECK(state_ == SSL_NONE);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000499 ssl_mode_ = mode;
500}
501
Joachim Bauch831c5582015-05-20 12:48:41 +0200502void OpenSSLStreamAdapter::SetMaxProtocolVersion(SSLProtocolVersion version) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800503 RTC_DCHECK(ssl_ctx_ == nullptr);
Joachim Bauch831c5582015-05-20 12:48:41 +0200504 ssl_max_version_ = version;
505}
506
skvladd0309122017-02-02 17:18:37 -0800507void OpenSSLStreamAdapter::SetInitialRetransmissionTimeout(
508 int timeout_ms) {
deadbeef37f5ecf2017-02-27 14:06:41 -0800509 RTC_DCHECK(ssl_ctx_ == nullptr);
skvladd0309122017-02-02 17:18:37 -0800510 dtls_handshake_timeout_ms_ = timeout_ms;
511}
512
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000513//
514// StreamInterface Implementation
515//
516
517StreamResult OpenSSLStreamAdapter::Write(const void* data, size_t data_len,
518 size_t* written, int* error) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100519 RTC_LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::Write(" << data_len << ")";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000520
521 switch (state_) {
522 case SSL_NONE:
523 // pass-through in clear text
524 return StreamAdapterInterface::Write(data, data_len, written, error);
525
526 case SSL_WAIT:
527 case SSL_CONNECTING:
528 return SR_BLOCK;
529
530 case SSL_CONNECTED:
deadbeef89824f62016-09-30 11:55:43 -0700531 if (waiting_to_verify_peer_certificate()) {
532 return SR_BLOCK;
533 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000534 break;
535
536 case SSL_ERROR:
537 case SSL_CLOSED:
538 default:
539 if (error)
540 *error = ssl_error_code_;
541 return SR_ERROR;
542 }
543
544 // OpenSSL will return an error if we try to write zero bytes
545 if (data_len == 0) {
546 if (written)
547 *written = 0;
548 return SR_SUCCESS;
549 }
550
551 ssl_write_needs_read_ = false;
552
henrike@webrtc.orgd89b69a2014-11-06 17:23:09 +0000553 int code = SSL_write(ssl_, data, checked_cast<int>(data_len));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000554 int ssl_error = SSL_get_error(ssl_, code);
555 switch (ssl_error) {
556 case SSL_ERROR_NONE:
Mirko Bonadei675513b2017-11-09 11:09:25 +0100557 RTC_LOG(LS_VERBOSE) << " -- success";
kwibergee89e782017-08-09 17:22:01 -0700558 RTC_DCHECK_GT(code, 0);
559 RTC_DCHECK_LE(code, data_len);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000560 if (written)
561 *written = code;
562 return SR_SUCCESS;
563 case SSL_ERROR_WANT_READ:
Mirko Bonadei675513b2017-11-09 11:09:25 +0100564 RTC_LOG(LS_VERBOSE) << " -- error want read";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000565 ssl_write_needs_read_ = true;
566 return SR_BLOCK;
567 case SSL_ERROR_WANT_WRITE:
Mirko Bonadei675513b2017-11-09 11:09:25 +0100568 RTC_LOG(LS_VERBOSE) << " -- error want write";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000569 return SR_BLOCK;
570
571 case SSL_ERROR_ZERO_RETURN:
572 default:
deadbeef89824f62016-09-30 11:55:43 -0700573 Error("SSL_write", (ssl_error ? ssl_error : -1), 0, false);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000574 if (error)
575 *error = ssl_error_code_;
576 return SR_ERROR;
577 }
578 // not reached
579}
580
581StreamResult OpenSSLStreamAdapter::Read(void* data, size_t data_len,
582 size_t* read, int* error) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100583 RTC_LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::Read(" << data_len << ")";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000584 switch (state_) {
585 case SSL_NONE:
586 // pass-through in clear text
587 return StreamAdapterInterface::Read(data, data_len, read, error);
588
589 case SSL_WAIT:
590 case SSL_CONNECTING:
591 return SR_BLOCK;
592
593 case SSL_CONNECTED:
deadbeef89824f62016-09-30 11:55:43 -0700594 if (waiting_to_verify_peer_certificate()) {
595 return SR_BLOCK;
596 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000597 break;
598
599 case SSL_CLOSED:
600 return SR_EOS;
601
602 case SSL_ERROR:
603 default:
604 if (error)
605 *error = ssl_error_code_;
606 return SR_ERROR;
607 }
608
609 // Don't trust OpenSSL with zero byte reads
610 if (data_len == 0) {
611 if (read)
612 *read = 0;
613 return SR_SUCCESS;
614 }
615
616 ssl_read_needs_write_ = false;
617
henrike@webrtc.orgd89b69a2014-11-06 17:23:09 +0000618 int code = SSL_read(ssl_, data, checked_cast<int>(data_len));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000619 int ssl_error = SSL_get_error(ssl_, code);
620 switch (ssl_error) {
621 case SSL_ERROR_NONE:
Mirko Bonadei675513b2017-11-09 11:09:25 +0100622 RTC_LOG(LS_VERBOSE) << " -- success";
kwibergee89e782017-08-09 17:22:01 -0700623 RTC_DCHECK_GT(code, 0);
624 RTC_DCHECK_LE(code, data_len);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000625 if (read)
626 *read = code;
627
628 if (ssl_mode_ == SSL_MODE_DTLS) {
629 // Enforce atomic reads -- this is a short read
630 unsigned int pending = SSL_pending(ssl_);
631
632 if (pending) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100633 RTC_LOG(LS_INFO) << " -- short DTLS read. flushing";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000634 FlushInput(pending);
635 if (error)
636 *error = SSE_MSG_TRUNC;
637 return SR_ERROR;
638 }
639 }
640 return SR_SUCCESS;
641 case SSL_ERROR_WANT_READ:
Mirko Bonadei675513b2017-11-09 11:09:25 +0100642 RTC_LOG(LS_VERBOSE) << " -- error want read";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000643 return SR_BLOCK;
644 case SSL_ERROR_WANT_WRITE:
Mirko Bonadei675513b2017-11-09 11:09:25 +0100645 RTC_LOG(LS_VERBOSE) << " -- error want write";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000646 ssl_read_needs_write_ = true;
647 return SR_BLOCK;
648 case SSL_ERROR_ZERO_RETURN:
Mirko Bonadei675513b2017-11-09 11:09:25 +0100649 RTC_LOG(LS_VERBOSE) << " -- remote side closed";
deadbeef89824f62016-09-30 11:55:43 -0700650 Close();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000651 return SR_EOS;
652 break;
653 default:
deadbeef89824f62016-09-30 11:55:43 -0700654 Error("SSL_read", (ssl_error ? ssl_error : -1), 0, false);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000655 if (error)
656 *error = ssl_error_code_;
657 return SR_ERROR;
658 }
659 // not reached
660}
661
662void OpenSSLStreamAdapter::FlushInput(unsigned int left) {
663 unsigned char buf[2048];
664
665 while (left) {
666 // This should always succeed
667 int toread = (sizeof(buf) < left) ? sizeof(buf) : left;
668 int code = SSL_read(ssl_, buf, toread);
669
670 int ssl_error = SSL_get_error(ssl_, code);
deadbeef89824f62016-09-30 11:55:43 -0700671 RTC_DCHECK(ssl_error == SSL_ERROR_NONE);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000672
673 if (ssl_error != SSL_ERROR_NONE) {
Jonas Olssonaddc3802018-02-01 09:53:06 +0100674 RTC_DLOG(LS_VERBOSE) << " -- error " << code;
deadbeef89824f62016-09-30 11:55:43 -0700675 Error("SSL_read", (ssl_error ? ssl_error : -1), 0, false);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000676 return;
677 }
678
Mirko Bonadei675513b2017-11-09 11:09:25 +0100679 RTC_LOG(LS_VERBOSE) << " -- flushed " << code << " bytes";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000680 left -= code;
681 }
682}
683
684void OpenSSLStreamAdapter::Close() {
deadbeef89824f62016-09-30 11:55:43 -0700685 Cleanup(0);
686 RTC_DCHECK(state_ == SSL_CLOSED || state_ == SSL_ERROR);
687 // When we're closed at SSL layer, also close the stream level which
688 // performs necessary clean up. Otherwise, a new incoming packet after
689 // this could overflow the stream buffer.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000690 StreamAdapterInterface::Close();
691}
692
693StreamState OpenSSLStreamAdapter::GetState() const {
694 switch (state_) {
695 case SSL_WAIT:
696 case SSL_CONNECTING:
697 return SS_OPENING;
698 case SSL_CONNECTED:
deadbeef89824f62016-09-30 11:55:43 -0700699 if (waiting_to_verify_peer_certificate()) {
700 return SS_OPENING;
701 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000702 return SS_OPEN;
703 default:
704 return SS_CLOSED;
705 };
706 // not reached
707}
708
709void OpenSSLStreamAdapter::OnEvent(StreamInterface* stream, int events,
710 int err) {
711 int events_to_signal = 0;
712 int signal_error = 0;
deadbeef89824f62016-09-30 11:55:43 -0700713 RTC_DCHECK(stream == this->stream());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000714 if ((events & SE_OPEN)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100715 RTC_LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::OnEvent SE_OPEN";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000716 if (state_ != SSL_WAIT) {
deadbeef89824f62016-09-30 11:55:43 -0700717 RTC_DCHECK(state_ == SSL_NONE);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000718 events_to_signal |= SE_OPEN;
719 } else {
720 state_ = SSL_CONNECTING;
721 if (int err = BeginSSL()) {
deadbeef89824f62016-09-30 11:55:43 -0700722 Error("BeginSSL", err, 0, true);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000723 return;
724 }
725 }
726 }
727 if ((events & (SE_READ|SE_WRITE))) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100728 RTC_LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::OnEvent"
729 << ((events & SE_READ) ? " SE_READ" : "")
730 << ((events & SE_WRITE) ? " SE_WRITE" : "");
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000731 if (state_ == SSL_NONE) {
732 events_to_signal |= events & (SE_READ|SE_WRITE);
733 } else if (state_ == SSL_CONNECTING) {
734 if (int err = ContinueSSL()) {
deadbeef89824f62016-09-30 11:55:43 -0700735 Error("ContinueSSL", err, 0, true);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000736 return;
737 }
738 } else if (state_ == SSL_CONNECTED) {
739 if (((events & SE_READ) && ssl_write_needs_read_) ||
740 (events & SE_WRITE)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100741 RTC_LOG(LS_VERBOSE) << " -- onStreamWriteable";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000742 events_to_signal |= SE_WRITE;
743 }
744 if (((events & SE_WRITE) && ssl_read_needs_write_) ||
745 (events & SE_READ)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100746 RTC_LOG(LS_VERBOSE) << " -- onStreamReadable";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000747 events_to_signal |= SE_READ;
748 }
749 }
750 }
751 if ((events & SE_CLOSE)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100752 RTC_LOG(LS_VERBOSE) << "OpenSSLStreamAdapter::OnEvent(SE_CLOSE, " << err
753 << ")";
deadbeef89824f62016-09-30 11:55:43 -0700754 Cleanup(0);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000755 events_to_signal |= SE_CLOSE;
756 // SE_CLOSE is the only event that uses the final parameter to OnEvent().
deadbeef89824f62016-09-30 11:55:43 -0700757 RTC_DCHECK(signal_error == 0);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000758 signal_error = err;
759 }
760 if (events_to_signal)
761 StreamAdapterInterface::OnEvent(stream, events_to_signal, signal_error);
762}
763
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000764int OpenSSLStreamAdapter::BeginSSL() {
deadbeef89824f62016-09-30 11:55:43 -0700765 RTC_DCHECK(state_ == SSL_CONNECTING);
Taylor Brandstetterc8762a82016-08-11 12:01:49 -0700766 // The underlying stream has opened.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100767 RTC_LOG(LS_INFO) << "BeginSSL with peer.";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000768
deadbeef37f5ecf2017-02-27 14:06:41 -0800769 BIO* bio = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000770
Taylor Brandstetterc8762a82016-08-11 12:01:49 -0700771 // First set up the context.
deadbeef37f5ecf2017-02-27 14:06:41 -0800772 RTC_DCHECK(ssl_ctx_ == nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000773 ssl_ctx_ = SetupSSLContext();
774 if (!ssl_ctx_)
775 return -1;
776
777 bio = BIO_new_stream(static_cast<StreamInterface*>(stream()));
778 if (!bio)
779 return -1;
780
781 ssl_ = SSL_new(ssl_ctx_);
782 if (!ssl_) {
783 BIO_free(bio);
784 return -1;
785 }
786
787 SSL_set_app_data(ssl_, this);
788
789 SSL_set_bio(ssl_, bio, bio); // the SSL object owns the bio now.
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100790 if (ssl_mode_ == SSL_MODE_DTLS) {
Taylor Brandstetter4f0dfbd2016-06-15 17:15:23 -0700791#ifdef OPENSSL_IS_BORINGSSL
skvladd0309122017-02-02 17:18:37 -0800792 DTLSv1_set_initial_timeout_duration(ssl_, dtls_handshake_timeout_ms_);
Taylor Brandstetter4f0dfbd2016-06-15 17:15:23 -0700793#else
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100794 // Enable read-ahead for DTLS so whole packets are read from internal BIO
795 // before parsing. This is done internally by BoringSSL for DTLS.
796 SSL_set_read_ahead(ssl_, 1);
philipel49c08692016-05-24 01:49:43 -0700797#endif
Taylor Brandstetter4f0dfbd2016-06-15 17:15:23 -0700798 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000799
800 SSL_set_mode(ssl_, SSL_MODE_ENABLE_PARTIAL_WRITE |
801 SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
802
David Benjamin60d5f3f2016-03-24 13:28:25 -0400803#if !defined(OPENSSL_IS_BORINGSSL)
804 // Specify an ECDH group for ECDHE ciphers, otherwise OpenSSL cannot
805 // negotiate them when acting as the server. Use NIST's P-256 which is
806 // commonly supported. BoringSSL doesn't need explicit configuration and has
807 // a reasonable default set.
jiayl@webrtc.org11c6bde2014-08-28 16:14:38 +0000808 EC_KEY* ecdh = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
deadbeef37f5ecf2017-02-27 14:06:41 -0800809 if (ecdh == nullptr)
jiayl@webrtc.org11c6bde2014-08-28 16:14:38 +0000810 return -1;
811 SSL_set_options(ssl_, SSL_OP_SINGLE_ECDH_USE);
812 SSL_set_tmp_ecdh(ssl_, ecdh);
813 EC_KEY_free(ecdh);
David Benjamin60d5f3f2016-03-24 13:28:25 -0400814#endif
jiayl@webrtc.org11c6bde2014-08-28 16:14:38 +0000815
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000816 // Do the connect
817 return ContinueSSL();
818}
819
820int OpenSSLStreamAdapter::ContinueSSL() {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100821 RTC_LOG(LS_VERBOSE) << "ContinueSSL";
deadbeef89824f62016-09-30 11:55:43 -0700822 RTC_DCHECK(state_ == SSL_CONNECTING);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000823
824 // Clear the DTLS timer
825 Thread::Current()->Clear(this, MSG_TIMEOUT);
826
827 int code = (role_ == SSL_CLIENT) ? SSL_connect(ssl_) : SSL_accept(ssl_);
828 int ssl_error;
829 switch (ssl_error = SSL_get_error(ssl_, code)) {
830 case SSL_ERROR_NONE:
Mirko Bonadei675513b2017-11-09 11:09:25 +0100831 RTC_LOG(LS_VERBOSE) << " -- success";
deadbeef89824f62016-09-30 11:55:43 -0700832 // By this point, OpenSSL should have given us a certificate, or errored
833 // out if one was missing.
834 RTC_DCHECK(peer_certificate_ || !client_auth_enabled());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000835
836 state_ = SSL_CONNECTED;
deadbeef89824f62016-09-30 11:55:43 -0700837 if (!waiting_to_verify_peer_certificate()) {
838 // We have everything we need to start the connection, so signal
839 // SE_OPEN. If we need a client certificate fingerprint and don't have
840 // it yet, we'll instead signal SE_OPEN in SetPeerCertificateDigest.
841 //
Taylor Brandstetterfe69a742016-09-30 17:34:32 -0700842 // TODO(deadbeef): Post this event asynchronously to unwind the stack.
843 // The caller of ContinueSSL may be the same object listening for these
844 // events and may not be prepared for reentrancy.
845 // PostEvent(SE_OPEN | SE_READ | SE_WRITE, 0);
846 StreamAdapterInterface::OnEvent(stream(), SE_OPEN | SE_READ | SE_WRITE,
847 0);
deadbeef89824f62016-09-30 11:55:43 -0700848 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000849 break;
850
851 case SSL_ERROR_WANT_READ: {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100852 RTC_LOG(LS_VERBOSE) << " -- error want read";
853 struct timeval timeout;
854 if (DTLSv1_get_timeout(ssl_, &timeout)) {
855 int delay = timeout.tv_sec * 1000 + timeout.tv_usec / 1000;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000856
Mirko Bonadei675513b2017-11-09 11:09:25 +0100857 Thread::Current()->PostDelayed(RTC_FROM_HERE, delay, this, MSG_TIMEOUT,
858 0);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000859 }
860 }
861 break;
862
863 case SSL_ERROR_WANT_WRITE:
Mirko Bonadei675513b2017-11-09 11:09:25 +0100864 RTC_LOG(LS_VERBOSE) << " -- error want write";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000865 break;
866
867 case SSL_ERROR_ZERO_RETURN:
868 default:
Mirko Bonadei675513b2017-11-09 11:09:25 +0100869 RTC_LOG(LS_VERBOSE) << " -- error " << code;
zhihuangd82eee02016-08-26 11:25:05 -0700870 SSLHandshakeError ssl_handshake_err = SSLHandshakeError::UNKNOWN;
871 int err_code = ERR_peek_last_error();
872 if (err_code != 0 && ERR_GET_REASON(err_code) == SSL_R_NO_SHARED_CIPHER) {
873 ssl_handshake_err = SSLHandshakeError::INCOMPATIBLE_CIPHERSUITE;
874 }
875 SignalSSLHandshakeError(ssl_handshake_err);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000876 return (ssl_error != 0) ? ssl_error : -1;
877 }
878
879 return 0;
880}
881
deadbeef89824f62016-09-30 11:55:43 -0700882void OpenSSLStreamAdapter::Error(const char* context,
883 int err,
884 uint8_t alert,
885 bool signal) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100886 RTC_LOG(LS_WARNING) << "OpenSSLStreamAdapter::Error(" << context << ", "
887 << err << ", " << static_cast<int>(alert) << ")";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000888 state_ = SSL_ERROR;
889 ssl_error_code_ = err;
deadbeef89824f62016-09-30 11:55:43 -0700890 Cleanup(alert);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000891 if (signal)
892 StreamAdapterInterface::OnEvent(stream(), SE_CLOSE, err);
893}
894
deadbeef89824f62016-09-30 11:55:43 -0700895void OpenSSLStreamAdapter::Cleanup(uint8_t alert) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100896 RTC_LOG(LS_INFO) << "Cleanup";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000897
898 if (state_ != SSL_ERROR) {
899 state_ = SSL_CLOSED;
900 ssl_error_code_ = 0;
901 }
902
903 if (ssl_) {
deadbeef89824f62016-09-30 11:55:43 -0700904 int ret;
905// SSL_send_fatal_alert is only available in BoringSSL.
906#ifdef OPENSSL_IS_BORINGSSL
907 if (alert) {
908 ret = SSL_send_fatal_alert(ssl_, alert);
909 if (ret < 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100910 RTC_LOG(LS_WARNING) << "SSL_send_fatal_alert failed, error = "
911 << SSL_get_error(ssl_, ret);
deadbeef89824f62016-09-30 11:55:43 -0700912 }
913 } else {
914#endif
915 ret = SSL_shutdown(ssl_);
916 if (ret < 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100917 RTC_LOG(LS_WARNING)
918 << "SSL_shutdown failed, error = " << SSL_get_error(ssl_, ret);
deadbeef89824f62016-09-30 11:55:43 -0700919 }
920#ifdef OPENSSL_IS_BORINGSSL
jiayl@webrtc.orgf1d751c2014-09-25 16:38:46 +0000921 }
deadbeef89824f62016-09-30 11:55:43 -0700922#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000923 SSL_free(ssl_);
deadbeef37f5ecf2017-02-27 14:06:41 -0800924 ssl_ = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000925 }
926 if (ssl_ctx_) {
927 SSL_CTX_free(ssl_ctx_);
deadbeef37f5ecf2017-02-27 14:06:41 -0800928 ssl_ctx_ = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000929 }
930 identity_.reset();
931 peer_certificate_.reset();
932
933 // Clear the DTLS timer
934 Thread::Current()->Clear(this, MSG_TIMEOUT);
935}
936
937
938void OpenSSLStreamAdapter::OnMessage(Message* msg) {
939 // Process our own messages and then pass others to the superclass
940 if (MSG_TIMEOUT == msg->message_id) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100941 RTC_LOG(LS_INFO) << "DTLS timeout expired";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000942 DTLSv1_handle_timeout(ssl_);
943 ContinueSSL();
944 } else {
945 StreamInterface::OnMessage(msg);
946 }
947}
948
949SSL_CTX* OpenSSLStreamAdapter::SetupSSLContext() {
deadbeef37f5ecf2017-02-27 14:06:41 -0800950 SSL_CTX* ctx = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000951
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100952#ifdef OPENSSL_IS_BORINGSSL
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000953 ctx = SSL_CTX_new(ssl_mode_ == SSL_MODE_DTLS ?
Joachim Bauch831c5582015-05-20 12:48:41 +0200954 DTLS_method() : TLS_method());
955 // Version limiting for BoringSSL will be done below.
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100956#else
957 const SSL_METHOD* method;
958 switch (ssl_max_version_) {
959 case SSL_PROTOCOL_TLS_10:
960 case SSL_PROTOCOL_TLS_11:
961 // OpenSSL doesn't support setting min/max versions, so we always use
962 // (D)TLS 1.0 if a max. version below the max. available is requested.
963 if (ssl_mode_ == SSL_MODE_DTLS) {
964 if (role_ == SSL_CLIENT) {
965 method = DTLSv1_client_method();
966 } else {
967 method = DTLSv1_server_method();
968 }
969 } else {
970 if (role_ == SSL_CLIENT) {
971 method = TLSv1_client_method();
972 } else {
973 method = TLSv1_server_method();
974 }
975 }
976 break;
977 case SSL_PROTOCOL_TLS_12:
978 default:
979 if (ssl_mode_ == SSL_MODE_DTLS) {
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100980 if (role_ == SSL_CLIENT) {
981 method = DTLS_client_method();
982 } else {
983 method = DTLS_server_method();
984 }
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100985 } else {
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100986 if (role_ == SSL_CLIENT) {
987 method = TLS_client_method();
988 } else {
989 method = TLS_server_method();
990 }
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +0100991 }
992 break;
993 }
994 ctx = SSL_CTX_new(method);
995#endif // OPENSSL_IS_BORINGSSL
Joachim Bauch831c5582015-05-20 12:48:41 +0200996
deadbeef37f5ecf2017-02-27 14:06:41 -0800997 if (ctx == nullptr)
998 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000999
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +01001000#ifdef OPENSSL_IS_BORINGSSL
davidbene36c46e2016-12-06 17:12:02 -08001001 SSL_CTX_set_min_proto_version(ctx, ssl_mode_ == SSL_MODE_DTLS ?
Joachim Bauch831c5582015-05-20 12:48:41 +02001002 DTLS1_VERSION : TLS1_VERSION);
1003 switch (ssl_max_version_) {
1004 case SSL_PROTOCOL_TLS_10:
davidbene36c46e2016-12-06 17:12:02 -08001005 SSL_CTX_set_max_proto_version(ctx, ssl_mode_ == SSL_MODE_DTLS ?
Joachim Bauch831c5582015-05-20 12:48:41 +02001006 DTLS1_VERSION : TLS1_VERSION);
1007 break;
1008 case SSL_PROTOCOL_TLS_11:
davidbene36c46e2016-12-06 17:12:02 -08001009 SSL_CTX_set_max_proto_version(ctx, ssl_mode_ == SSL_MODE_DTLS ?
Joachim Bauch831c5582015-05-20 12:48:41 +02001010 DTLS1_VERSION : TLS1_1_VERSION);
1011 break;
1012 case SSL_PROTOCOL_TLS_12:
1013 default:
davidbene36c46e2016-12-06 17:12:02 -08001014 SSL_CTX_set_max_proto_version(ctx, ssl_mode_ == SSL_MODE_DTLS ?
Joachim Bauch831c5582015-05-20 12:48:41 +02001015 DTLS1_2_VERSION : TLS1_2_VERSION);
1016 break;
1017 }
deadbeef6cf94a02016-11-28 17:38:34 -08001018 if (g_use_time_callback_for_testing) {
1019 SSL_CTX_set_current_time_cb(ctx, &TimeCallbackForTesting);
1020 }
Torbjorn Granlund9adc91d2016-03-24 14:05:06 +01001021#endif
Joachim Bauch831c5582015-05-20 12:48:41 +02001022
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001023 if (identity_ && !identity_->ConfigureIdentity(ctx)) {
1024 SSL_CTX_free(ctx);
deadbeef37f5ecf2017-02-27 14:06:41 -08001025 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001026 }
1027
tfarinaa41ab932015-10-30 16:08:48 -07001028#if !defined(NDEBUG)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001029 SSL_CTX_set_info_callback(ctx, OpenSSLAdapter::SSLInfoCallback);
1030#endif
1031
tkchin@webrtc.orgc569a492014-09-23 05:56:44 +00001032 int mode = SSL_VERIFY_PEER;
1033 if (client_auth_enabled()) {
1034 // Require a certificate from the client.
1035 // Note: Normally this is always true in production, but it may be disabled
1036 // for testing purposes (e.g. SSLAdapter unit tests).
1037 mode |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
1038 }
1039
David Benjamindc246562017-09-29 12:14:08 -04001040 // Configure a custom certificate verification callback to check the peer
1041 // certificate digest. Note the second argument to SSL_CTX_set_verify is to
1042 // override individual errors in the default verification logic, which is not
1043 // what we want here.
1044 SSL_CTX_set_verify(ctx, mode, nullptr);
1045 SSL_CTX_set_cert_verify_callback(ctx, SSLVerifyCallback, nullptr);
1046
Joachim Bauch831c5582015-05-20 12:48:41 +02001047 // Select list of available ciphers. Note that !SHA256 and !SHA384 only
1048 // remove HMAC-SHA256 and HMAC-SHA384 cipher suites, not GCM cipher suites
1049 // with SHA256 or SHA384 as the handshake hash.
1050 // This matches the list of SSLClientSocketOpenSSL in Chromium.
deadbeef37f5ecf2017-02-27 14:06:41 -08001051 SSL_CTX_set_cipher_list(
1052 ctx, "DEFAULT:!NULL:!aNULL:!SHA256:!SHA384:!aECDH:!AESGCM+AES256:!aPSK");
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001053
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001054 if (!srtp_ciphers_.empty()) {
1055 if (SSL_CTX_set_tlsext_use_srtp(ctx, srtp_ciphers_.c_str())) {
1056 SSL_CTX_free(ctx);
deadbeef37f5ecf2017-02-27 14:06:41 -08001057 return nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001058 }
1059 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001060
1061 return ctx;
1062}
1063
deadbeef89824f62016-09-30 11:55:43 -07001064bool OpenSSLStreamAdapter::VerifyPeerCertificate() {
1065 if (!has_peer_certificate_digest() || !peer_certificate_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001066 RTC_LOG(LS_WARNING) << "Missing digest or peer certificate.";
deadbeef89824f62016-09-30 11:55:43 -07001067 return false;
1068 }
deadbeef81f6f4f2016-09-19 17:20:52 -07001069
deadbeef89824f62016-09-30 11:55:43 -07001070 unsigned char digest[EVP_MAX_MD_SIZE];
1071 size_t digest_length;
1072 if (!OpenSSLCertificate::ComputeDigest(
1073 peer_certificate_->x509(), peer_certificate_digest_algorithm_, digest,
1074 sizeof(digest), &digest_length)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001075 RTC_LOG(LS_WARNING) << "Failed to compute peer cert digest.";
deadbeef89824f62016-09-30 11:55:43 -07001076 return false;
1077 }
1078
1079 Buffer computed_digest(digest, digest_length);
1080 if (computed_digest != peer_certificate_digest_value_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001081 RTC_LOG(LS_WARNING)
1082 << "Rejected peer certificate due to mismatched digest.";
jbauchf8f457b2017-03-09 16:24:57 -08001083 return false;
deadbeef81f6f4f2016-09-19 17:20:52 -07001084 }
deadbeef89824f62016-09-30 11:55:43 -07001085 // Ignore any verification error if the digest matches, since there is no
1086 // value in checking the validity of a self-signed cert issued by untrusted
1087 // sources.
Mirko Bonadei675513b2017-11-09 11:09:25 +01001088 RTC_LOG(LS_INFO) << "Accepted peer certificate.";
deadbeef89824f62016-09-30 11:55:43 -07001089 peer_certificate_verified_ = true;
1090 return true;
1091}
1092
Jian Cui0a8798b2017-11-16 16:58:02 -08001093std::unique_ptr<SSLCertChain> OpenSSLStreamAdapter::GetPeerSSLCertChain()
1094 const {
1095 return std::unique_ptr<SSLCertChain>(peer_cert_chain_->Copy());
1096}
1097
David Benjamindc246562017-09-29 12:14:08 -04001098int OpenSSLStreamAdapter::SSLVerifyCallback(X509_STORE_CTX* store, void* arg) {
1099 // Get our SSL structure and OpenSSLStreamAdapter from the store.
deadbeef89824f62016-09-30 11:55:43 -07001100 SSL* ssl = reinterpret_cast<SSL*>(
1101 X509_STORE_CTX_get_ex_data(store, SSL_get_ex_data_X509_STORE_CTX_idx()));
deadbeef89824f62016-09-30 11:55:43 -07001102 OpenSSLStreamAdapter* stream =
1103 reinterpret_cast<OpenSSLStreamAdapter*>(SSL_get_app_data(ssl));
henrike@webrtc.org4e5f65a2014-06-05 20:40:11 +00001104
Jian Cui0a8798b2017-11-16 16:58:02 -08001105#if defined(OPENSSL_IS_BORINGSSL)
1106 STACK_OF(X509)* chain = SSL_get_peer_full_cert_chain(ssl);
1107 // Creates certificate.
1108 stream->peer_certificate_.reset(
1109 new OpenSSLCertificate(sk_X509_value(chain, 0)));
1110 // Creates certificate chain.
1111 std::vector<std::unique_ptr<SSLCertificate>> cert_chain;
1112 for (X509* cert : chain) {
1113 cert_chain.emplace_back(new OpenSSLCertificate(cert));
1114 }
1115 stream->peer_cert_chain_.reset(new SSLCertChain(std::move(cert_chain)));
1116#else
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001117 // Record the peer's certificate.
David Benjamindc246562017-09-29 12:14:08 -04001118 X509* cert = SSL_get_peer_certificate(ssl);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001119 stream->peer_certificate_.reset(new OpenSSLCertificate(cert));
David Benjamindc246562017-09-29 12:14:08 -04001120 X509_free(cert);
Jian Cui0a8798b2017-11-16 16:58:02 -08001121#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001122
deadbeef89824f62016-09-30 11:55:43 -07001123 // If the peer certificate digest isn't known yet, we'll wait to verify
1124 // until it's known, and for now just return a success status.
1125 if (stream->peer_certificate_digest_algorithm_.empty()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001126 RTC_LOG(LS_INFO) << "Waiting to verify certificate until digest is known.";
deadbeef89824f62016-09-30 11:55:43 -07001127 return 1;
1128 }
1129
David Benjamindc246562017-09-29 12:14:08 -04001130 if (!stream->VerifyPeerCertificate()) {
1131 X509_STORE_CTX_set_error(store, X509_V_ERR_CERT_REJECTED);
1132 return 0;
1133 }
1134
1135 return 1;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001136}
1137
Taylor Brandstetter4f0dfbd2016-06-15 17:15:23 -07001138bool OpenSSLStreamAdapter::IsBoringSsl() {
1139#ifdef OPENSSL_IS_BORINGSSL
1140 return true;
1141#else
1142 return false;
1143#endif
1144}
1145
torbjorng43166b82016-03-11 00:06:47 -08001146#define CDEF(X) \
1147 { static_cast<uint16_t>(TLS1_CK_##X & 0xffff), "TLS_" #X }
1148
1149struct cipher_list {
1150 uint16_t cipher;
1151 const char* cipher_str;
1152};
1153
1154// TODO(torbjorng): Perhaps add more cipher suites to these lists.
1155static const cipher_list OK_RSA_ciphers[] = {
1156 CDEF(ECDHE_RSA_WITH_AES_128_CBC_SHA),
1157 CDEF(ECDHE_RSA_WITH_AES_256_CBC_SHA),
1158 CDEF(ECDHE_RSA_WITH_AES_128_GCM_SHA256),
1159#ifdef TLS1_CK_ECDHE_RSA_WITH_AES_256_GCM_SHA256
1160 CDEF(ECDHE_RSA_WITH_AES_256_GCM_SHA256),
1161#endif
torbjorngaad67802016-04-07 08:55:28 -07001162#ifdef TLS1_CK_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256
torbjorng43166b82016-03-11 00:06:47 -08001163 CDEF(ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256),
torbjorngaad67802016-04-07 08:55:28 -07001164#endif
torbjorng43166b82016-03-11 00:06:47 -08001165};
1166
1167static const cipher_list OK_ECDSA_ciphers[] = {
1168 CDEF(ECDHE_ECDSA_WITH_AES_128_CBC_SHA),
1169 CDEF(ECDHE_ECDSA_WITH_AES_256_CBC_SHA),
1170 CDEF(ECDHE_ECDSA_WITH_AES_128_GCM_SHA256),
1171#ifdef TLS1_CK_ECDHE_ECDSA_WITH_AES_256_GCM_SHA256
1172 CDEF(ECDHE_ECDSA_WITH_AES_256_GCM_SHA256),
1173#endif
torbjorngaad67802016-04-07 08:55:28 -07001174#ifdef TLS1_CK_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256
torbjorng43166b82016-03-11 00:06:47 -08001175 CDEF(ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256),
torbjorngaad67802016-04-07 08:55:28 -07001176#endif
torbjorng43166b82016-03-11 00:06:47 -08001177};
1178#undef CDEF
1179
1180bool OpenSSLStreamAdapter::IsAcceptableCipher(int cipher, KeyType key_type) {
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +02001181 if (key_type == KT_RSA) {
torbjorng43166b82016-03-11 00:06:47 -08001182 for (const cipher_list& c : OK_RSA_ciphers) {
1183 if (cipher == c.cipher)
1184 return true;
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +02001185 }
Joachim Bauch831c5582015-05-20 12:48:41 +02001186 }
torbjorng43166b82016-03-11 00:06:47 -08001187
1188 if (key_type == KT_ECDSA) {
1189 for (const cipher_list& c : OK_ECDSA_ciphers) {
1190 if (cipher == c.cipher)
1191 return true;
1192 }
1193 }
1194
1195 return false;
1196}
1197
1198bool OpenSSLStreamAdapter::IsAcceptableCipher(const std::string& cipher,
1199 KeyType key_type) {
1200 if (key_type == KT_RSA) {
1201 for (const cipher_list& c : OK_RSA_ciphers) {
1202 if (cipher == c.cipher_str)
1203 return true;
1204 }
1205 }
1206
1207 if (key_type == KT_ECDSA) {
1208 for (const cipher_list& c : OK_ECDSA_ciphers) {
1209 if (cipher == c.cipher_str)
1210 return true;
1211 }
1212 }
1213
1214 return false;
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +00001215}
1216
deadbeef6cf94a02016-11-28 17:38:34 -08001217void OpenSSLStreamAdapter::enable_time_callback_for_testing() {
1218 g_use_time_callback_for_testing = true;
1219}
1220
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001221} // namespace rtc