blob: 3403bdbe727ee33f0862860c9f71aad1db6d9fe7 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
2 * Copyright 2011 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
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000011#include <algorithm>
jbauch555604a2016-04-26 03:13:22 -070012#include <memory>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013#include <set>
14#include <string>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/bufferqueue.h"
17#include "rtc_base/checks.h"
18#include "rtc_base/gunit.h"
19#include "rtc_base/helpers.h"
20#include "rtc_base/ssladapter.h"
21#include "rtc_base/sslidentity.h"
22#include "rtc_base/sslstreamadapter.h"
23#include "rtc_base/stream.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000024
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +020025using ::testing::WithParamInterface;
26using ::testing::Values;
27using ::testing::Combine;
28using ::testing::tuple;
29
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000030static const int kBlockSize = 4096;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000031static const char kExporterLabel[] = "label";
32static const unsigned char kExporterContext[] = "context";
33static int kExporterContextLen = sizeof(kExporterContext);
34
35static const char kRSA_PRIVATE_KEY_PEM[] =
36 "-----BEGIN RSA PRIVATE KEY-----\n"
37 "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMYRkbhmI7kVA/rM\n"
38 "czsZ+6JDhDvnkF+vn6yCAGuRPV03zuRqZtDy4N4to7PZu9PjqrRl7nDMXrG3YG9y\n"
39 "rlIAZ72KjcKKFAJxQyAKLCIdawKRyp8RdK3LEySWEZb0AV58IadqPZDTNHHRX8d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6tNHKj5s3TGWL46oqGyZ0KdGNhrtC\n"
126 "rIGenMhth1vPYjyy0XuGBndXT85yi+IM2l8g8oU845+plxIhgpSI8bbC0oLwnhQ5\n"
127 "ARfsiYLkXDE7imSS0CSUmye76372mlzAIB1is4bBB/SzpPQtBuB9LDKtONgpSGHn\n"
128 "dGaXBy+qbVXVyGXaeEbIRjtJ6m92\n"
129 "-----END CERTIFICATE-----\n";
130
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000131class SSLStreamAdapterTestBase;
132
jbauche488a0d2015-11-19 05:17:58 -0800133class SSLDummyStreamBase : public rtc::StreamInterface,
134 public sigslot::has_slots<> {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000135 public:
jbauche488a0d2015-11-19 05:17:58 -0800136 SSLDummyStreamBase(SSLStreamAdapterTestBase* test,
Yves Gerey665174f2018-06-19 15:03:05 +0200137 const std::string& side,
jbauche488a0d2015-11-19 05:17:58 -0800138 rtc::StreamInterface* in,
Yves Gerey665174f2018-06-19 15:03:05 +0200139 rtc::StreamInterface* out)
140 : test_base_(test), side_(side), in_(in), out_(out), first_packet_(true) {
jbauche488a0d2015-11-19 05:17:58 -0800141 in_->SignalEvent.connect(this, &SSLDummyStreamBase::OnEventIn);
142 out_->SignalEvent.connect(this, &SSLDummyStreamBase::OnEventOut);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000143 }
144
jbauche488a0d2015-11-19 05:17:58 -0800145 rtc::StreamState GetState() const override { return rtc::SS_OPEN; }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000146
Yves Gerey665174f2018-06-19 15:03:05 +0200147 rtc::StreamResult Read(void* buffer,
148 size_t buffer_len,
149 size_t* read,
150 int* error) override {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000151 rtc::StreamResult r;
152
153 r = in_->Read(buffer, buffer_len, read, error);
154 if (r == rtc::SR_BLOCK)
155 return rtc::SR_BLOCK;
156 if (r == rtc::SR_EOS)
157 return rtc::SR_EOS;
158
159 if (r != rtc::SR_SUCCESS) {
160 ADD_FAILURE();
161 return rtc::SR_ERROR;
162 }
163
164 return rtc::SR_SUCCESS;
165 }
166
167 // Catch readability events on in and pass them up.
jbauche488a0d2015-11-19 05:17:58 -0800168 void OnEventIn(rtc::StreamInterface* stream, int sig, int err) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000169 int mask = (rtc::SE_READ | rtc::SE_CLOSE);
170
171 if (sig & mask) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100172 RTC_LOG(LS_VERBOSE) << "SSLDummyStreamBase::OnEvent side=" << side_
173 << " sig=" << sig << " forwarding upward";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000174 PostEvent(sig & mask, 0);
175 }
176 }
177
178 // Catch writeability events on out and pass them up.
jbauche488a0d2015-11-19 05:17:58 -0800179 void OnEventOut(rtc::StreamInterface* stream, int sig, int err) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000180 if (sig & rtc::SE_WRITE) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100181 RTC_LOG(LS_VERBOSE) << "SSLDummyStreamBase::OnEvent side=" << side_
182 << " sig=" << sig << " forwarding upward";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000183
184 PostEvent(sig & rtc::SE_WRITE, 0);
185 }
186 }
187
188 // Write to the outgoing FifoBuffer
Yves Gerey665174f2018-06-19 15:03:05 +0200189 rtc::StreamResult WriteData(const void* data,
190 size_t data_len,
191 size_t* written,
192 int* error) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000193 return out_->Write(data, data_len, written, error);
194 }
195
Yves Gerey665174f2018-06-19 15:03:05 +0200196 rtc::StreamResult Write(const void* data,
197 size_t data_len,
198 size_t* written,
199 int* error) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000200
jbauche488a0d2015-11-19 05:17:58 -0800201 void Close() override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100202 RTC_LOG(LS_INFO) << "Closing outbound stream";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000203 out_->Close();
204 }
205
jbauche488a0d2015-11-19 05:17:58 -0800206 protected:
207 SSLStreamAdapterTestBase* test_base_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000208 const std::string side_;
jbauche488a0d2015-11-19 05:17:58 -0800209 rtc::StreamInterface* in_;
210 rtc::StreamInterface* out_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000211 bool first_packet_;
212};
213
jbauche488a0d2015-11-19 05:17:58 -0800214class SSLDummyStreamTLS : public SSLDummyStreamBase {
215 public:
216 SSLDummyStreamTLS(SSLStreamAdapterTestBase* test,
217 const std::string& side,
218 rtc::FifoBuffer* in,
Yves Gerey665174f2018-06-19 15:03:05 +0200219 rtc::FifoBuffer* out)
220 : SSLDummyStreamBase(test, side, in, out) {}
jbauche488a0d2015-11-19 05:17:58 -0800221};
222
Yves Gerey665174f2018-06-19 15:03:05 +0200223class BufferQueueStream : public rtc::BufferQueue, public rtc::StreamInterface {
jbauche488a0d2015-11-19 05:17:58 -0800224 public:
225 BufferQueueStream(size_t capacity, size_t default_size)
Yves Gerey665174f2018-06-19 15:03:05 +0200226 : rtc::BufferQueue(capacity, default_size) {}
jbauche488a0d2015-11-19 05:17:58 -0800227
228 // Implementation of abstract StreamInterface methods.
229
230 // A buffer queue stream is always "open".
231 rtc::StreamState GetState() const override { return rtc::SS_OPEN; }
232
233 // Reading a buffer queue stream will either succeed or block.
Yves Gerey665174f2018-06-19 15:03:05 +0200234 rtc::StreamResult Read(void* buffer,
235 size_t buffer_len,
236 size_t* read,
237 int* error) override {
jbauche488a0d2015-11-19 05:17:58 -0800238 if (!ReadFront(buffer, buffer_len, read)) {
239 return rtc::SR_BLOCK;
240 }
241 return rtc::SR_SUCCESS;
242 }
243
244 // Writing to a buffer queue stream will either succeed or block.
Yves Gerey665174f2018-06-19 15:03:05 +0200245 rtc::StreamResult Write(const void* data,
246 size_t data_len,
247 size_t* written,
248 int* error) override {
jbauche488a0d2015-11-19 05:17:58 -0800249 if (!WriteBack(data, data_len, written)) {
250 return rtc::SR_BLOCK;
251 }
252 return rtc::SR_SUCCESS;
253 }
254
255 // A buffer queue stream can not be closed.
256 void Close() override {}
257
258 protected:
Yves Gerey665174f2018-06-19 15:03:05 +0200259 void NotifyReadableForTest() override { PostEvent(rtc::SE_READ, 0); }
jbauche488a0d2015-11-19 05:17:58 -0800260
Yves Gerey665174f2018-06-19 15:03:05 +0200261 void NotifyWritableForTest() override { PostEvent(rtc::SE_WRITE, 0); }
jbauche488a0d2015-11-19 05:17:58 -0800262};
263
264class SSLDummyStreamDTLS : public SSLDummyStreamBase {
265 public:
266 SSLDummyStreamDTLS(SSLStreamAdapterTestBase* test,
267 const std::string& side,
268 BufferQueueStream* in,
Yves Gerey665174f2018-06-19 15:03:05 +0200269 BufferQueueStream* out)
270 : SSLDummyStreamBase(test, side, in, out) {}
jbauche488a0d2015-11-19 05:17:58 -0800271};
272
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000273static const int kFifoBufferSize = 4096;
jbauche488a0d2015-11-19 05:17:58 -0800274static const int kBufferCapacity = 1;
275static const size_t kDefaultBufferSize = 2048;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000276
277class SSLStreamAdapterTestBase : public testing::Test,
278 public sigslot::has_slots<> {
279 public:
torbjorng4e572472015-10-08 09:42:49 -0700280 SSLStreamAdapterTestBase(
281 const std::string& client_cert_pem,
282 const std::string& client_private_key_pem,
283 bool dtls,
284 rtc::KeyParams client_key_type = rtc::KeyParams(rtc::KT_DEFAULT),
285 rtc::KeyParams server_key_type = rtc::KeyParams(rtc::KT_DEFAULT))
jbauche488a0d2015-11-19 05:17:58 -0800286 : client_cert_pem_(client_cert_pem),
287 client_private_key_pem_(client_private_key_pem),
288 client_key_type_(client_key_type),
289 server_key_type_(server_key_type),
deadbeef37f5ecf2017-02-27 14:06:41 -0800290 client_stream_(nullptr),
291 server_stream_(nullptr),
292 client_identity_(nullptr),
293 server_identity_(nullptr),
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200294 delay_(0),
295 mtu_(1460),
296 loss_(0),
297 lose_first_packet_(false),
298 damage_(false),
299 dtls_(dtls),
300 handshake_wait_(5000),
301 identities_set_(false) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000302 // Set use of the test RNG to get predictable loss patterns.
303 rtc::SetRandomTestMode(true);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000304 }
305
Steve Anton9de3aac2017-10-24 10:08:26 -0700306 ~SSLStreamAdapterTestBase() override {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000307 // Put it back for the next test.
308 rtc::SetRandomTestMode(false);
309 }
310
torbjorng7593aad2015-11-19 12:20:51 -0800311 void SetUp() override {
jbauche488a0d2015-11-19 05:17:58 -0800312 CreateStreams();
313
314 client_ssl_.reset(rtc::SSLStreamAdapter::Create(client_stream_));
315 server_ssl_.reset(rtc::SSLStreamAdapter::Create(server_stream_));
316
317 // Set up the slots
318 client_ssl_->SignalEvent.connect(this, &SSLStreamAdapterTestBase::OnEvent);
319 server_ssl_->SignalEvent.connect(this, &SSLStreamAdapterTestBase::OnEvent);
320
321 if (!client_cert_pem_.empty() && !client_private_key_pem_.empty()) {
322 client_identity_ = rtc::SSLIdentity::FromPEMStrings(
323 client_private_key_pem_, client_cert_pem_);
324 } else {
325 client_identity_ = rtc::SSLIdentity::Generate("client", client_key_type_);
326 }
327 server_identity_ = rtc::SSLIdentity::Generate("server", server_key_type_);
328
329 client_ssl_->SetIdentity(client_identity_);
330 server_ssl_->SetIdentity(server_identity_);
331 }
332
torbjorng7593aad2015-11-19 12:20:51 -0800333 void TearDown() override {
jbauche488a0d2015-11-19 05:17:58 -0800334 client_ssl_.reset(nullptr);
335 server_ssl_.reset(nullptr);
336 }
337
338 virtual void CreateStreams() = 0;
339
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000340 // Recreate the client/server identities with the specified validity period.
341 // |not_before| and |not_after| are offsets from the current time in number
342 // of seconds.
343 void ResetIdentitiesWithValidity(int not_before, int not_after) {
jbauche488a0d2015-11-19 05:17:58 -0800344 CreateStreams();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000345
346 client_ssl_.reset(rtc::SSLStreamAdapter::Create(client_stream_));
347 server_ssl_.reset(rtc::SSLStreamAdapter::Create(server_stream_));
348
349 client_ssl_->SignalEvent.connect(this, &SSLStreamAdapterTestBase::OnEvent);
350 server_ssl_->SignalEvent.connect(this, &SSLStreamAdapterTestBase::OnEvent);
351
Torbjorn Granlund46c9cc02015-12-01 13:06:34 +0100352 time_t now = time(nullptr);
353
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000354 rtc::SSLIdentityParams client_params;
torbjorng4e572472015-10-08 09:42:49 -0700355 client_params.key_params = rtc::KeyParams(rtc::KT_DEFAULT);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000356 client_params.common_name = "client";
Torbjorn Granlund46c9cc02015-12-01 13:06:34 +0100357 client_params.not_before = now + not_before;
358 client_params.not_after = now + not_after;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000359 client_identity_ = rtc::SSLIdentity::GenerateForTest(client_params);
360
361 rtc::SSLIdentityParams server_params;
torbjorng4e572472015-10-08 09:42:49 -0700362 server_params.key_params = rtc::KeyParams(rtc::KT_DEFAULT);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000363 server_params.common_name = "server";
Torbjorn Granlund46c9cc02015-12-01 13:06:34 +0100364 server_params.not_before = now + not_before;
365 server_params.not_after = now + not_after;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000366 server_identity_ = rtc::SSLIdentity::GenerateForTest(server_params);
367
368 client_ssl_->SetIdentity(client_identity_);
369 server_ssl_->SetIdentity(server_identity_);
370 }
371
Yves Gerey665174f2018-06-19 15:03:05 +0200372 virtual void OnEvent(rtc::StreamInterface* stream, int sig, int err) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100373 RTC_LOG(LS_VERBOSE) << "SSLStreamAdapterTestBase::OnEvent sig=" << sig;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000374
375 if (sig & rtc::SE_READ) {
376 ReadData(stream);
377 }
378
379 if ((stream == client_ssl_.get()) && (sig & rtc::SE_WRITE)) {
380 WriteData();
381 }
382 }
383
deadbeef89824f62016-09-30 11:55:43 -0700384 void SetPeerIdentitiesByDigest(bool correct, bool expect_success) {
385 unsigned char server_digest[20];
386 size_t server_digest_len;
387 unsigned char client_digest[20];
388 size_t client_digest_len;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000389 bool rv;
deadbeef89824f62016-09-30 11:55:43 -0700390 rtc::SSLPeerCertificateDigestError err;
391 rtc::SSLPeerCertificateDigestError expected_err =
392 expect_success
393 ? rtc::SSLPeerCertificateDigestError::NONE
394 : rtc::SSLPeerCertificateDigestError::VERIFICATION_FAILED;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000395
Mirko Bonadei675513b2017-11-09 11:09:25 +0100396 RTC_LOG(LS_INFO) << "Setting peer identities by digest";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000397
deadbeef89824f62016-09-30 11:55:43 -0700398 rv = server_identity_->certificate().ComputeDigest(
399 rtc::DIGEST_SHA_1, server_digest, 20, &server_digest_len);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000400 ASSERT_TRUE(rv);
deadbeef89824f62016-09-30 11:55:43 -0700401 rv = client_identity_->certificate().ComputeDigest(
402 rtc::DIGEST_SHA_1, client_digest, 20, &client_digest_len);
403 ASSERT_TRUE(rv);
404
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000405 if (!correct) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100406 RTC_LOG(LS_INFO) << "Setting bogus digest for server cert";
deadbeef89824f62016-09-30 11:55:43 -0700407 server_digest[0]++;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000408 }
deadbeef89824f62016-09-30 11:55:43 -0700409 rv = client_ssl_->SetPeerCertificateDigest(rtc::DIGEST_SHA_1, server_digest,
410 server_digest_len, &err);
411 EXPECT_EQ(expected_err, err);
412 EXPECT_EQ(expect_success, rv);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000413
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000414 if (!correct) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100415 RTC_LOG(LS_INFO) << "Setting bogus digest for client cert";
deadbeef89824f62016-09-30 11:55:43 -0700416 client_digest[0]++;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000417 }
deadbeef89824f62016-09-30 11:55:43 -0700418 rv = server_ssl_->SetPeerCertificateDigest(rtc::DIGEST_SHA_1, client_digest,
419 client_digest_len, &err);
420 EXPECT_EQ(expected_err, err);
421 EXPECT_EQ(expect_success, rv);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000422
423 identities_set_ = true;
424 }
425
Joachim Bauch831c5582015-05-20 12:48:41 +0200426 void SetupProtocolVersions(rtc::SSLProtocolVersion server_version,
427 rtc::SSLProtocolVersion client_version) {
428 server_ssl_->SetMaxProtocolVersion(server_version);
429 client_ssl_->SetMaxProtocolVersion(client_version);
430 }
431
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000432 void TestHandshake(bool expect_success = true) {
Yves Gerey665174f2018-06-19 15:03:05 +0200433 server_ssl_->SetMode(dtls_ ? rtc::SSL_MODE_DTLS : rtc::SSL_MODE_TLS);
434 client_ssl_->SetMode(dtls_ ? rtc::SSL_MODE_DTLS : rtc::SSL_MODE_TLS);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000435
436 if (!dtls_) {
437 // Make sure we simulate a reliable network for TLS.
438 // This is just a check to make sure that people don't write wrong
439 // tests.
nissec16fa5e2017-02-07 07:18:43 -0800440 RTC_CHECK_EQ(1460, mtu_);
441 RTC_CHECK(!loss_);
442 RTC_CHECK(!lose_first_packet_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000443 }
444
445 if (!identities_set_)
deadbeef89824f62016-09-30 11:55:43 -0700446 SetPeerIdentitiesByDigest(true, true);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000447
448 // Start the handshake
449 int rv;
450
451 server_ssl_->SetServerRole();
Taylor Brandstetterc8762a82016-08-11 12:01:49 -0700452 rv = server_ssl_->StartSSL();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000453 ASSERT_EQ(0, rv);
454
Taylor Brandstetterc8762a82016-08-11 12:01:49 -0700455 rv = client_ssl_->StartSSL();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000456 ASSERT_EQ(0, rv);
457
458 // Now run the handshake
459 if (expect_success) {
Yves Gerey665174f2018-06-19 15:03:05 +0200460 EXPECT_TRUE_WAIT((client_ssl_->GetState() == rtc::SS_OPEN) &&
461 (server_ssl_->GetState() == rtc::SS_OPEN),
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000462 handshake_wait_);
463 } else {
464 EXPECT_TRUE_WAIT(client_ssl_->GetState() == rtc::SS_CLOSED,
465 handshake_wait_);
466 }
467 }
468
deadbeef89824f62016-09-30 11:55:43 -0700469 // This tests that the handshake can complete before the identity is
470 // verified, and the identity will be verified after the fact.
471 void TestHandshakeWithDelayedIdentity(bool valid_identity) {
472 server_ssl_->SetMode(dtls_ ? rtc::SSL_MODE_DTLS : rtc::SSL_MODE_TLS);
473 client_ssl_->SetMode(dtls_ ? rtc::SSL_MODE_DTLS : rtc::SSL_MODE_TLS);
474
475 if (!dtls_) {
476 // Make sure we simulate a reliable network for TLS.
477 // This is just a check to make sure that people don't write wrong
478 // tests.
nissec16fa5e2017-02-07 07:18:43 -0800479 RTC_CHECK_EQ(1460, mtu_);
480 RTC_CHECK(!loss_);
481 RTC_CHECK(!lose_first_packet_);
deadbeef89824f62016-09-30 11:55:43 -0700482 }
483
484 // Start the handshake
485 int rv;
486
487 server_ssl_->SetServerRole();
488 rv = server_ssl_->StartSSL();
489 ASSERT_EQ(0, rv);
490
491 rv = client_ssl_->StartSSL();
492 ASSERT_EQ(0, rv);
493
494 // Now run the handshake.
495 EXPECT_TRUE_WAIT(
496 client_ssl_->IsTlsConnected() && server_ssl_->IsTlsConnected(),
497 handshake_wait_);
498
499 // Until the identity has been verified, the state should still be
500 // SS_OPENING and writes should return SR_BLOCK.
501 EXPECT_EQ(rtc::SS_OPENING, client_ssl_->GetState());
502 EXPECT_EQ(rtc::SS_OPENING, server_ssl_->GetState());
503 unsigned char packet[1];
504 size_t sent;
505 EXPECT_EQ(rtc::SR_BLOCK, client_ssl_->Write(&packet, 1, &sent, 0));
506 EXPECT_EQ(rtc::SR_BLOCK, server_ssl_->Write(&packet, 1, &sent, 0));
507
508 // If we set an invalid identity at this point, SetPeerCertificateDigest
509 // should return false.
510 SetPeerIdentitiesByDigest(valid_identity, valid_identity);
511 // State should then transition to SS_OPEN or SS_CLOSED based on validation
512 // of the identity.
513 if (valid_identity) {
514 EXPECT_EQ(rtc::SS_OPEN, client_ssl_->GetState());
515 EXPECT_EQ(rtc::SS_OPEN, server_ssl_->GetState());
516 } else {
517 EXPECT_EQ(rtc::SS_CLOSED, client_ssl_->GetState());
518 EXPECT_EQ(rtc::SS_CLOSED, server_ssl_->GetState());
519 }
520 }
521
Yves Gerey665174f2018-06-19 15:03:05 +0200522 rtc::StreamResult DataWritten(SSLDummyStreamBase* from,
523 const void* data,
524 size_t data_len,
525 size_t* written,
526 int* error) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000527 // Randomly drop loss_ percent of packets
Peter Boström0c4e06b2015-10-07 12:23:21 +0200528 if (rtc::CreateRandomId() % 100 < static_cast<uint32_t>(loss_)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100529 RTC_LOG(LS_VERBOSE) << "Randomly dropping packet, size=" << data_len;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000530 *written = data_len;
531 return rtc::SR_SUCCESS;
532 }
533 if (dtls_ && (data_len > mtu_)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100534 RTC_LOG(LS_VERBOSE) << "Dropping packet > mtu, size=" << data_len;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000535 *written = data_len;
536 return rtc::SR_SUCCESS;
537 }
538
539 // Optionally damage application data (type 23). Note that we don't damage
540 // handshake packets and we damage the last byte to keep the header
541 // intact but break the MAC.
Yves Gerey665174f2018-06-19 15:03:05 +0200542 if (damage_ && (*static_cast<const unsigned char*>(data) == 23)) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000543 std::vector<char> buf(data_len);
544
Mirko Bonadei675513b2017-11-09 11:09:25 +0100545 RTC_LOG(LS_VERBOSE) << "Damaging packet";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000546
547 memcpy(&buf[0], data, data_len);
548 buf[data_len - 1]++;
549
550 return from->WriteData(&buf[0], data_len, written, error);
551 }
552
553 return from->WriteData(data, data_len, written, error);
554 }
555
Yves Gerey665174f2018-06-19 15:03:05 +0200556 void SetDelay(int delay) { delay_ = delay; }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000557 int GetDelay() { return delay_; }
558
Yves Gerey665174f2018-06-19 15:03:05 +0200559 void SetLoseFirstPacket(bool lose) { lose_first_packet_ = lose; }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000560 bool GetLoseFirstPacket() { return lose_first_packet_; }
561
Yves Gerey665174f2018-06-19 15:03:05 +0200562 void SetLoss(int percent) { loss_ = percent; }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000563
Yves Gerey665174f2018-06-19 15:03:05 +0200564 void SetDamage() { damage_ = true; }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000565
Yves Gerey665174f2018-06-19 15:03:05 +0200566 void SetMtu(size_t mtu) { mtu_ = mtu; }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000567
Yves Gerey665174f2018-06-19 15:03:05 +0200568 void SetHandshakeWait(int wait) { handshake_wait_ = wait; }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000569
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800570 void SetDtlsSrtpCryptoSuites(const std::vector<int>& ciphers, bool client) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000571 if (client)
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800572 client_ssl_->SetDtlsSrtpCryptoSuites(ciphers);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000573 else
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800574 server_ssl_->SetDtlsSrtpCryptoSuites(ciphers);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000575 }
576
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800577 bool GetDtlsSrtpCryptoSuite(bool client, int* retval) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000578 if (client)
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800579 return client_ssl_->GetDtlsSrtpCryptoSuite(retval);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000580 else
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800581 return server_ssl_->GetDtlsSrtpCryptoSuite(retval);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000582 }
583
jbauch555604a2016-04-26 03:13:22 -0700584 std::unique_ptr<rtc::SSLCertificate> GetPeerCertificate(bool client) {
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800585 std::unique_ptr<rtc::SSLCertChain> chain;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000586 if (client)
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800587 chain = client_ssl_->GetPeerSSLCertChain();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000588 else
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800589 chain = server_ssl_->GetPeerSSLCertChain();
590 return (chain && chain->GetSize()) ? chain->Get(0).GetUniqueReference()
591 : nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000592 }
593
Guo-wei Shieh6caafbe2015-10-05 12:43:27 -0700594 bool GetSslCipherSuite(bool client, int* retval) {
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000595 if (client)
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700596 return client_ssl_->GetSslCipherSuite(retval);
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000597 else
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700598 return server_ssl_->GetSslCipherSuite(retval);
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000599 }
600
torbjorng43166b82016-03-11 00:06:47 -0800601 int GetSslVersion(bool client) {
602 if (client)
603 return client_ssl_->GetSslVersion();
604 else
605 return server_ssl_->GetSslVersion();
606 }
607
Yves Gerey665174f2018-06-19 15:03:05 +0200608 bool ExportKeyingMaterial(const char* label,
609 const unsigned char* context,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000610 size_t context_len,
611 bool use_context,
612 bool client,
Yves Gerey665174f2018-06-19 15:03:05 +0200613 unsigned char* result,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000614 size_t result_len) {
615 if (client)
Yves Gerey665174f2018-06-19 15:03:05 +0200616 return client_ssl_->ExportKeyingMaterial(label, context, context_len,
617 use_context, result, result_len);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000618 else
Yves Gerey665174f2018-06-19 15:03:05 +0200619 return server_ssl_->ExportKeyingMaterial(label, context, context_len,
620 use_context, result, result_len);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000621 }
622
623 // To be implemented by subclasses.
624 virtual void WriteData() = 0;
Yves Gerey665174f2018-06-19 15:03:05 +0200625 virtual void ReadData(rtc::StreamInterface* stream) = 0;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000626 virtual void TestTransfer(int size) = 0;
627
628 protected:
jbauche488a0d2015-11-19 05:17:58 -0800629 std::string client_cert_pem_;
630 std::string client_private_key_pem_;
631 rtc::KeyParams client_key_type_;
632 rtc::KeyParams server_key_type_;
Yves Gerey665174f2018-06-19 15:03:05 +0200633 SSLDummyStreamBase* client_stream_; // freed by client_ssl_ destructor
634 SSLDummyStreamBase* server_stream_; // freed by server_ssl_ destructor
jbauch555604a2016-04-26 03:13:22 -0700635 std::unique_ptr<rtc::SSLStreamAdapter> client_ssl_;
636 std::unique_ptr<rtc::SSLStreamAdapter> server_ssl_;
Yves Gerey665174f2018-06-19 15:03:05 +0200637 rtc::SSLIdentity* client_identity_; // freed by client_ssl_ destructor
638 rtc::SSLIdentity* server_identity_; // freed by server_ssl_ destructor
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000639 int delay_;
640 size_t mtu_;
641 int loss_;
642 bool lose_first_packet_;
643 bool damage_;
644 bool dtls_;
645 int handshake_wait_;
646 bool identities_set_;
647};
648
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200649class SSLStreamAdapterTestTLS
650 : public SSLStreamAdapterTestBase,
torbjorng4e572472015-10-08 09:42:49 -0700651 public WithParamInterface<tuple<rtc::KeyParams, rtc::KeyParams>> {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000652 public:
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200653 SSLStreamAdapterTestTLS()
654 : SSLStreamAdapterTestBase("",
655 "",
656 false,
657 ::testing::get<0>(GetParam()),
jbauche488a0d2015-11-19 05:17:58 -0800658 ::testing::get<1>(GetParam())),
659 client_buffer_(kFifoBufferSize),
Yves Gerey665174f2018-06-19 15:03:05 +0200660 server_buffer_(kFifoBufferSize) {}
jbauche488a0d2015-11-19 05:17:58 -0800661
torbjorng7593aad2015-11-19 12:20:51 -0800662 void CreateStreams() override {
jbauche488a0d2015-11-19 05:17:58 -0800663 client_stream_ =
664 new SSLDummyStreamTLS(this, "c2s", &client_buffer_, &server_buffer_);
665 server_stream_ =
666 new SSLDummyStreamTLS(this, "s2c", &server_buffer_, &client_buffer_);
667 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000668
669 // Test data transfer for TLS
nisseef8b61e2016-04-29 06:09:15 -0700670 void TestTransfer(int size) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100671 RTC_LOG(LS_INFO) << "Starting transfer test with " << size << " bytes";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000672 // Create some dummy data to send.
673 size_t received;
674
675 send_stream_.ReserveSize(size);
676 for (int i = 0; i < size; ++i) {
677 char ch = static_cast<char>(i);
deadbeef37f5ecf2017-02-27 14:06:41 -0800678 send_stream_.Write(&ch, 1, nullptr, nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000679 }
680 send_stream_.Rewind();
681
682 // Prepare the receive stream.
683 recv_stream_.ReserveSize(size);
684
685 // Start sending
686 WriteData();
687
688 // Wait for the client to close
689 EXPECT_TRUE_WAIT(server_ssl_->GetState() == rtc::SS_CLOSED, 10000);
690
691 // Now check the data
692 recv_stream_.GetSize(&received);
693
694 EXPECT_EQ(static_cast<size_t>(size), received);
Yves Gerey665174f2018-06-19 15:03:05 +0200695 EXPECT_EQ(0,
696 memcmp(send_stream_.GetBuffer(), recv_stream_.GetBuffer(), size));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000697 }
698
nisseef8b61e2016-04-29 06:09:15 -0700699 void WriteData() override {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000700 size_t position, tosend, size;
701 rtc::StreamResult rv;
702 size_t sent;
703 char block[kBlockSize];
704
705 send_stream_.GetSize(&size);
706 if (!size)
707 return;
708
709 for (;;) {
710 send_stream_.GetPosition(&position);
deadbeef37f5ecf2017-02-27 14:06:41 -0800711 if (send_stream_.Read(block, sizeof(block), &tosend, nullptr) !=
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000712 rtc::SR_EOS) {
713 rv = client_ssl_->Write(block, tosend, &sent, 0);
714
715 if (rv == rtc::SR_SUCCESS) {
716 send_stream_.SetPosition(position + sent);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100717 RTC_LOG(LS_VERBOSE) << "Sent: " << position + sent;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000718 } else if (rv == rtc::SR_BLOCK) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100719 RTC_LOG(LS_VERBOSE) << "Blocked...";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000720 send_stream_.SetPosition(position);
721 break;
722 } else {
723 ADD_FAILURE();
724 break;
725 }
726 } else {
727 // Now close
Mirko Bonadei675513b2017-11-09 11:09:25 +0100728 RTC_LOG(LS_INFO) << "Wrote " << position << " bytes. Closing";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000729 client_ssl_->Close();
730 break;
731 }
732 }
733 };
734
Yves Gerey665174f2018-06-19 15:03:05 +0200735 void ReadData(rtc::StreamInterface* stream) override {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000736 char buffer[1600];
737 size_t bread;
738 int err2;
739 rtc::StreamResult r;
740
741 for (;;) {
742 r = stream->Read(buffer, sizeof(buffer), &bread, &err2);
743
744 if (r == rtc::SR_ERROR || r == rtc::SR_EOS) {
745 // Unfortunately, errors are the way that the stream adapter
torbjorng7593aad2015-11-19 12:20:51 -0800746 // signals close in OpenSSL.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000747 stream->Close();
748 return;
749 }
750
751 if (r == rtc::SR_BLOCK)
752 break;
753
754 ASSERT_EQ(rtc::SR_SUCCESS, r);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100755 RTC_LOG(LS_VERBOSE) << "Read " << bread;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000756
deadbeef37f5ecf2017-02-27 14:06:41 -0800757 recv_stream_.Write(buffer, bread, nullptr, nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000758 }
759 }
760
761 private:
jbauche488a0d2015-11-19 05:17:58 -0800762 rtc::FifoBuffer client_buffer_;
763 rtc::FifoBuffer server_buffer_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000764 rtc::MemoryStream send_stream_;
765 rtc::MemoryStream recv_stream_;
766};
767
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200768class SSLStreamAdapterTestDTLS
769 : public SSLStreamAdapterTestBase,
torbjorng4e572472015-10-08 09:42:49 -0700770 public WithParamInterface<tuple<rtc::KeyParams, rtc::KeyParams>> {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000771 public:
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200772 SSLStreamAdapterTestDTLS()
773 : SSLStreamAdapterTestBase("",
774 "",
775 true,
776 ::testing::get<0>(GetParam()),
777 ::testing::get<1>(GetParam())),
jbauche488a0d2015-11-19 05:17:58 -0800778 client_buffer_(kBufferCapacity, kDefaultBufferSize),
779 server_buffer_(kBufferCapacity, kDefaultBufferSize),
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200780 packet_size_(1000),
781 count_(0),
782 sent_(0) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000783
784 SSLStreamAdapterTestDTLS(const std::string& cert_pem,
Yves Gerey665174f2018-06-19 15:03:05 +0200785 const std::string& private_key_pem)
786 : SSLStreamAdapterTestBase(cert_pem, private_key_pem, true),
787 client_buffer_(kBufferCapacity, kDefaultBufferSize),
788 server_buffer_(kBufferCapacity, kDefaultBufferSize),
789 packet_size_(1000),
790 count_(0),
791 sent_(0) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000792
torbjorng7593aad2015-11-19 12:20:51 -0800793 void CreateStreams() override {
jbauche488a0d2015-11-19 05:17:58 -0800794 client_stream_ =
795 new SSLDummyStreamDTLS(this, "c2s", &client_buffer_, &server_buffer_);
796 server_stream_ =
797 new SSLDummyStreamDTLS(this, "s2c", &server_buffer_, &client_buffer_);
798 }
799
nisseef8b61e2016-04-29 06:09:15 -0700800 void WriteData() override {
Yves Gerey665174f2018-06-19 15:03:05 +0200801 unsigned char* packet = new unsigned char[1600];
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000802
jbauche488a0d2015-11-19 05:17:58 -0800803 while (sent_ < count_) {
torbjorng7593aad2015-11-19 12:20:51 -0800804 unsigned int rand_state = sent_;
805 packet[0] = sent_;
806 for (size_t i = 1; i < packet_size_; i++) {
807 // This is a simple LC PRNG. Keep in synch with identical code below.
808 rand_state = (rand_state * 251 + 19937) >> 7;
809 packet[i] = rand_state & 0xff;
810 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000811
812 size_t sent;
torbjorng7593aad2015-11-19 12:20:51 -0800813 rtc::StreamResult rv = client_ssl_->Write(packet, packet_size_, &sent, 0);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000814 if (rv == rtc::SR_SUCCESS) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100815 RTC_LOG(LS_VERBOSE) << "Sent: " << sent_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000816 sent_++;
817 } else if (rv == rtc::SR_BLOCK) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100818 RTC_LOG(LS_VERBOSE) << "Blocked...";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000819 break;
820 } else {
821 ADD_FAILURE();
822 break;
823 }
jbauche488a0d2015-11-19 05:17:58 -0800824 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000825
Yves Gerey665174f2018-06-19 15:03:05 +0200826 delete[] packet;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000827 }
828
Yves Gerey665174f2018-06-19 15:03:05 +0200829 void ReadData(rtc::StreamInterface* stream) override {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000830 unsigned char buffer[2000];
831 size_t bread;
832 int err2;
833 rtc::StreamResult r;
834
835 for (;;) {
836 r = stream->Read(buffer, 2000, &bread, &err2);
837
838 if (r == rtc::SR_ERROR) {
839 // Unfortunately, errors are the way that the stream adapter
840 // signals close right now
841 stream->Close();
842 return;
843 }
844
845 if (r == rtc::SR_BLOCK)
846 break;
847
848 ASSERT_EQ(rtc::SR_SUCCESS, r);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100849 RTC_LOG(LS_VERBOSE) << "Read " << bread;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000850
851 // Now parse the datagram
852 ASSERT_EQ(packet_size_, bread);
torbjorng7593aad2015-11-19 12:20:51 -0800853 unsigned char packet_num = buffer[0];
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000854
torbjorng7593aad2015-11-19 12:20:51 -0800855 unsigned int rand_state = packet_num;
856 for (size_t i = 1; i < packet_size_; i++) {
857 // This is a simple LC PRNG. Keep in synch with identical code above.
858 rand_state = (rand_state * 251 + 19937) >> 7;
859 ASSERT_EQ(rand_state & 0xff, buffer[i]);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000860 }
861 received_.insert(packet_num);
862 }
863 }
864
nisseef8b61e2016-04-29 06:09:15 -0700865 void TestTransfer(int count) override {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000866 count_ = count;
867
868 WriteData();
869
870 EXPECT_TRUE_WAIT(sent_ == count_, 10000);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100871 RTC_LOG(LS_INFO) << "sent_ == " << sent_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000872
873 if (damage_) {
874 WAIT(false, 2000);
875 EXPECT_EQ(0U, received_.size());
876 } else if (loss_ == 0) {
Yves Gerey665174f2018-06-19 15:03:05 +0200877 EXPECT_EQ_WAIT(static_cast<size_t>(sent_), received_.size(), 1000);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000878 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100879 RTC_LOG(LS_INFO) << "Sent " << sent_ << " packets; received "
880 << received_.size();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000881 }
882 };
883
884 private:
jbauche488a0d2015-11-19 05:17:58 -0800885 BufferQueueStream client_buffer_;
886 BufferQueueStream server_buffer_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000887 size_t packet_size_;
888 int count_;
889 int sent_;
890 std::set<int> received_;
891};
892
Yves Gerey665174f2018-06-19 15:03:05 +0200893rtc::StreamResult SSLDummyStreamBase::Write(const void* data,
894 size_t data_len,
895 size_t* written,
896 int* error) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100897 RTC_LOG(LS_VERBOSE) << "Writing to loopback " << data_len;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000898
899 if (first_packet_) {
900 first_packet_ = false;
jbauche488a0d2015-11-19 05:17:58 -0800901 if (test_base_->GetLoseFirstPacket()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100902 RTC_LOG(LS_INFO) << "Losing initial packet of length " << data_len;
torbjorng7593aad2015-11-19 12:20:51 -0800903 *written = data_len; // Fake successful writing also to writer.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000904 return rtc::SR_SUCCESS;
905 }
906 }
907
jbauche488a0d2015-11-19 05:17:58 -0800908 return test_base_->DataWritten(this, data, data_len, written, error);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000909};
910
911class SSLStreamAdapterTestDTLSFromPEMStrings : public SSLStreamAdapterTestDTLS {
912 public:
Yves Gerey665174f2018-06-19 15:03:05 +0200913 SSLStreamAdapterTestDTLSFromPEMStrings()
914 : SSLStreamAdapterTestDTLS(kCERT_PEM, kRSA_PRIVATE_KEY_PEM) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000915};
916
Jian Cui0a8798b2017-11-16 16:58:02 -0800917// Test fixture for certificate chaining. Server will push more than one
918// certificate.
919class SSLStreamAdapterTestDTLSCertChain : public SSLStreamAdapterTestDTLS {
920 public:
921 SSLStreamAdapterTestDTLSCertChain() : SSLStreamAdapterTestDTLS("", ""){};
922 void SetUp() override {
923 CreateStreams();
924
925 client_ssl_.reset(rtc::SSLStreamAdapter::Create(client_stream_));
926 server_ssl_.reset(rtc::SSLStreamAdapter::Create(server_stream_));
927
928 // Set up the slots
929 client_ssl_->SignalEvent.connect(
930 reinterpret_cast<SSLStreamAdapterTestBase*>(this),
931 &SSLStreamAdapterTestBase::OnEvent);
932 server_ssl_->SignalEvent.connect(
933 reinterpret_cast<SSLStreamAdapterTestBase*>(this),
934 &SSLStreamAdapterTestBase::OnEvent);
935
936 if (!client_cert_pem_.empty() && !client_private_key_pem_.empty()) {
937 client_identity_ = rtc::SSLIdentity::FromPEMStrings(
938 client_private_key_pem_, client_cert_pem_);
939 } else {
940 client_identity_ = rtc::SSLIdentity::Generate("client", client_key_type_);
941 }
942
943 client_ssl_->SetIdentity(client_identity_);
944 }
945};
946
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000947// Basic tests: TLS
948
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000949// Test that we can make a handshake work
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200950TEST_P(SSLStreamAdapterTestTLS, TestTLSConnect) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000951 TestHandshake();
952};
953
Jian Cui0a8798b2017-11-16 16:58:02 -0800954TEST_P(SSLStreamAdapterTestTLS, GetPeerCertChainWithOneCertificate) {
955 TestHandshake();
956 std::unique_ptr<rtc::SSLCertChain> cert_chain =
957 client_ssl_->GetPeerSSLCertChain();
Jian Cui0a8798b2017-11-16 16:58:02 -0800958 ASSERT_NE(nullptr, cert_chain);
959 EXPECT_EQ(1u, cert_chain->GetSize());
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800960 EXPECT_EQ(cert_chain->Get(0).ToPEMString(),
961 server_identity_->certificate().ToPEMString());
Jian Cui0a8798b2017-11-16 16:58:02 -0800962}
963
964TEST_F(SSLStreamAdapterTestDTLSCertChain, TwoCertHandshake) {
965 server_identity_ = rtc::SSLIdentity::FromPEMChainStrings(
966 kRSA_PRIVATE_KEY_PEM, std::string(kCERT_PEM) + kCACert);
967 server_ssl_->SetIdentity(server_identity_);
968 TestHandshake();
969 std::unique_ptr<rtc::SSLCertChain> peer_cert_chain =
970 client_ssl_->GetPeerSSLCertChain();
971 ASSERT_NE(nullptr, peer_cert_chain);
972 ASSERT_EQ(2u, peer_cert_chain->GetSize());
973 EXPECT_EQ(kCERT_PEM, peer_cert_chain->Get(0).ToPEMString());
974 EXPECT_EQ(kCACert, peer_cert_chain->Get(1).ToPEMString());
975}
976
David Benjaminea84b6b2017-12-01 17:25:45 -0500977TEST_F(SSLStreamAdapterTestDTLSCertChain, TwoCertHandshakeWithCopy) {
978 std::unique_ptr<rtc::SSLIdentity> identity(
979 rtc::SSLIdentity::FromPEMChainStrings(kRSA_PRIVATE_KEY_PEM,
980 std::string(kCERT_PEM) + kCACert));
981 server_identity_ = identity->GetReference();
982 server_ssl_->SetIdentity(server_identity_);
983 TestHandshake();
984 std::unique_ptr<rtc::SSLCertChain> peer_cert_chain =
985 client_ssl_->GetPeerSSLCertChain();
986 ASSERT_NE(nullptr, peer_cert_chain);
987 ASSERT_EQ(2u, peer_cert_chain->GetSize());
988 EXPECT_EQ(kCERT_PEM, peer_cert_chain->Get(0).ToPEMString());
989 EXPECT_EQ(kCACert, peer_cert_chain->Get(1).ToPEMString());
990}
991
Jian Cui0a8798b2017-11-16 16:58:02 -0800992TEST_F(SSLStreamAdapterTestDTLSCertChain, ThreeCertHandshake) {
993 server_identity_ = rtc::SSLIdentity::FromPEMChainStrings(
994 kRSA_PRIVATE_KEY_PEM, std::string(kCERT_PEM) + kIntCert1 + kCACert);
995 server_ssl_->SetIdentity(server_identity_);
996 TestHandshake();
997 std::unique_ptr<rtc::SSLCertChain> peer_cert_chain =
998 client_ssl_->GetPeerSSLCertChain();
999 ASSERT_NE(nullptr, peer_cert_chain);
1000 ASSERT_EQ(3u, peer_cert_chain->GetSize());
1001 EXPECT_EQ(kCERT_PEM, peer_cert_chain->Get(0).ToPEMString());
1002 EXPECT_EQ(kIntCert1, peer_cert_chain->Get(1).ToPEMString());
1003 EXPECT_EQ(kCACert, peer_cert_chain->Get(2).ToPEMString());
1004}
1005
jiayl@webrtc.orgf1d751c2014-09-25 16:38:46 +00001006// Test that closing the connection on one side updates the other side.
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +02001007TEST_P(SSLStreamAdapterTestTLS, TestTLSClose) {
jiayl@webrtc.orgf1d751c2014-09-25 16:38:46 +00001008 TestHandshake();
1009 client_ssl_->Close();
1010 EXPECT_EQ_WAIT(rtc::SS_CLOSED, server_ssl_->GetState(), handshake_wait_);
1011};
1012
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001013// Test transfer -- trivial
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +02001014TEST_P(SSLStreamAdapterTestTLS, TestTLSTransfer) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001015 TestHandshake();
1016 TestTransfer(100000);
1017};
1018
1019// Test read-write after close.
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +02001020TEST_P(SSLStreamAdapterTestTLS, ReadWriteAfterClose) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001021 TestHandshake();
1022 TestTransfer(100000);
1023 client_ssl_->Close();
1024
1025 rtc::StreamResult rv;
1026 char block[kBlockSize];
1027 size_t dummy;
1028
1029 // It's an error to write after closed.
deadbeef37f5ecf2017-02-27 14:06:41 -08001030 rv = client_ssl_->Write(block, sizeof(block), &dummy, nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001031 ASSERT_EQ(rtc::SR_ERROR, rv);
1032
1033 // But after closed read gives you EOS.
deadbeef37f5ecf2017-02-27 14:06:41 -08001034 rv = client_ssl_->Read(block, sizeof(block), &dummy, nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001035 ASSERT_EQ(rtc::SR_EOS, rv);
1036};
1037
1038// Test a handshake with a bogus peer digest
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +02001039TEST_P(SSLStreamAdapterTestTLS, TestTLSBogusDigest) {
deadbeef89824f62016-09-30 11:55:43 -07001040 SetPeerIdentitiesByDigest(false, true);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001041 TestHandshake(false);
1042};
1043
deadbeef89824f62016-09-30 11:55:43 -07001044TEST_P(SSLStreamAdapterTestTLS, TestTLSDelayedIdentity) {
1045 TestHandshakeWithDelayedIdentity(true);
1046};
1047
1048TEST_P(SSLStreamAdapterTestTLS, TestTLSDelayedIdentityWithBogusDigest) {
1049 TestHandshakeWithDelayedIdentity(false);
1050};
1051
1052// Test that the correct error is returned when SetPeerCertificateDigest is
1053// called with an unknown algorithm.
1054TEST_P(SSLStreamAdapterTestTLS,
1055 TestSetPeerCertificateDigestWithUnknownAlgorithm) {
1056 unsigned char server_digest[20];
1057 size_t server_digest_len;
1058 bool rv;
1059 rtc::SSLPeerCertificateDigestError err;
1060
1061 rv = server_identity_->certificate().ComputeDigest(
1062 rtc::DIGEST_SHA_1, server_digest, 20, &server_digest_len);
1063 ASSERT_TRUE(rv);
1064
1065 rv = client_ssl_->SetPeerCertificateDigest("unknown algorithm", server_digest,
1066 server_digest_len, &err);
1067 EXPECT_EQ(rtc::SSLPeerCertificateDigestError::UNKNOWN_ALGORITHM, err);
1068 EXPECT_FALSE(rv);
1069}
1070
1071// Test that the correct error is returned when SetPeerCertificateDigest is
1072// called with an invalid digest length.
1073TEST_P(SSLStreamAdapterTestTLS, TestSetPeerCertificateDigestWithInvalidLength) {
1074 unsigned char server_digest[20];
1075 size_t server_digest_len;
1076 bool rv;
1077 rtc::SSLPeerCertificateDigestError err;
1078
1079 rv = server_identity_->certificate().ComputeDigest(
1080 rtc::DIGEST_SHA_1, server_digest, 20, &server_digest_len);
1081 ASSERT_TRUE(rv);
1082
1083 rv = client_ssl_->SetPeerCertificateDigest(rtc::DIGEST_SHA_1, server_digest,
1084 server_digest_len - 1, &err);
1085 EXPECT_EQ(rtc::SSLPeerCertificateDigestError::INVALID_LENGTH, err);
1086 EXPECT_FALSE(rv);
1087}
1088
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001089// Test moving a bunch of data
1090
1091// Basic tests: DTLS
1092// Test that we can make a handshake work
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +02001093TEST_P(SSLStreamAdapterTestDTLS, TestDTLSConnect) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001094 TestHandshake();
1095};
1096
1097// Test that we can make a handshake work if the first packet in
1098// each direction is lost. This gives us predictable loss
1099// rather than having to tune random
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +02001100TEST_P(SSLStreamAdapterTestDTLS, TestDTLSConnectWithLostFirstPacket) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001101 SetLoseFirstPacket(true);
1102 TestHandshake();
1103};
1104
1105// Test a handshake with loss and delay
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +02001106TEST_P(SSLStreamAdapterTestDTLS, TestDTLSConnectWithLostFirstPacketDelay2s) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001107 SetLoseFirstPacket(true);
1108 SetDelay(2000);
1109 SetHandshakeWait(20000);
1110 TestHandshake();
1111};
1112
1113// Test a handshake with small MTU
pbos@webrtc.org127ca3f2014-10-09 07:52:03 +00001114// Disabled due to https://code.google.com/p/webrtc/issues/detail?id=3910
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +02001115TEST_P(SSLStreamAdapterTestDTLS, DISABLED_TestDTLSConnectWithSmallMtu) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001116 SetMtu(700);
1117 SetHandshakeWait(20000);
1118 TestHandshake();
1119};
1120
1121// Test transfer -- trivial
jbauche488a0d2015-11-19 05:17:58 -08001122TEST_P(SSLStreamAdapterTestDTLS, TestDTLSTransfer) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001123 TestHandshake();
1124 TestTransfer(100);
1125};
1126
jbauche488a0d2015-11-19 05:17:58 -08001127TEST_P(SSLStreamAdapterTestDTLS, TestDTLSTransferWithLoss) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001128 TestHandshake();
1129 SetLoss(10);
1130 TestTransfer(100);
1131};
1132
jbauche488a0d2015-11-19 05:17:58 -08001133TEST_P(SSLStreamAdapterTestDTLS, TestDTLSTransferWithDamage) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001134 SetDamage(); // Must be called first because first packet
1135 // write happens at end of handshake.
1136 TestHandshake();
1137 TestTransfer(100);
1138};
1139
deadbeef89824f62016-09-30 11:55:43 -07001140TEST_P(SSLStreamAdapterTestDTLS, TestDTLSDelayedIdentity) {
1141 TestHandshakeWithDelayedIdentity(true);
1142};
1143
1144TEST_P(SSLStreamAdapterTestDTLS, TestDTLSDelayedIdentityWithBogusDigest) {
1145 TestHandshakeWithDelayedIdentity(false);
1146};
1147
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001148// Test DTLS-SRTP with all high ciphers
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +02001149TEST_P(SSLStreamAdapterTestDTLS, TestDTLSSrtpHigh) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001150 std::vector<int> high;
1151 high.push_back(rtc::SRTP_AES128_CM_SHA1_80);
1152 SetDtlsSrtpCryptoSuites(high, true);
1153 SetDtlsSrtpCryptoSuites(high, false);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001154 TestHandshake();
1155
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001156 int client_cipher;
1157 ASSERT_TRUE(GetDtlsSrtpCryptoSuite(true, &client_cipher));
1158 int server_cipher;
1159 ASSERT_TRUE(GetDtlsSrtpCryptoSuite(false, &server_cipher));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001160
1161 ASSERT_EQ(client_cipher, server_cipher);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001162 ASSERT_EQ(client_cipher, rtc::SRTP_AES128_CM_SHA1_80);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001163};
1164
1165// Test DTLS-SRTP with all low ciphers
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +02001166TEST_P(SSLStreamAdapterTestDTLS, TestDTLSSrtpLow) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001167 std::vector<int> low;
1168 low.push_back(rtc::SRTP_AES128_CM_SHA1_32);
1169 SetDtlsSrtpCryptoSuites(low, true);
1170 SetDtlsSrtpCryptoSuites(low, false);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001171 TestHandshake();
1172
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001173 int client_cipher;
1174 ASSERT_TRUE(GetDtlsSrtpCryptoSuite(true, &client_cipher));
1175 int server_cipher;
1176 ASSERT_TRUE(GetDtlsSrtpCryptoSuite(false, &server_cipher));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001177
1178 ASSERT_EQ(client_cipher, server_cipher);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001179 ASSERT_EQ(client_cipher, rtc::SRTP_AES128_CM_SHA1_32);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001180};
1181
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001182// Test DTLS-SRTP with a mismatch -- should not converge
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +02001183TEST_P(SSLStreamAdapterTestDTLS, TestDTLSSrtpHighLow) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001184 std::vector<int> high;
1185 high.push_back(rtc::SRTP_AES128_CM_SHA1_80);
1186 std::vector<int> low;
1187 low.push_back(rtc::SRTP_AES128_CM_SHA1_32);
1188 SetDtlsSrtpCryptoSuites(high, true);
1189 SetDtlsSrtpCryptoSuites(low, false);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001190 TestHandshake();
1191
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001192 int client_cipher;
1193 ASSERT_FALSE(GetDtlsSrtpCryptoSuite(true, &client_cipher));
1194 int server_cipher;
1195 ASSERT_FALSE(GetDtlsSrtpCryptoSuite(false, &server_cipher));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001196};
1197
1198// Test DTLS-SRTP with each side being mixed -- should select high
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +02001199TEST_P(SSLStreamAdapterTestDTLS, TestDTLSSrtpMixed) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001200 std::vector<int> mixed;
1201 mixed.push_back(rtc::SRTP_AES128_CM_SHA1_80);
1202 mixed.push_back(rtc::SRTP_AES128_CM_SHA1_32);
1203 SetDtlsSrtpCryptoSuites(mixed, true);
1204 SetDtlsSrtpCryptoSuites(mixed, false);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001205 TestHandshake();
1206
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001207 int client_cipher;
1208 ASSERT_TRUE(GetDtlsSrtpCryptoSuite(true, &client_cipher));
1209 int server_cipher;
1210 ASSERT_TRUE(GetDtlsSrtpCryptoSuite(false, &server_cipher));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001211
1212 ASSERT_EQ(client_cipher, server_cipher);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001213 ASSERT_EQ(client_cipher, rtc::SRTP_AES128_CM_SHA1_80);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001214};
1215
jbauchcb560652016-08-04 05:20:32 -07001216// Test DTLS-SRTP with all GCM-128 ciphers.
1217TEST_P(SSLStreamAdapterTestDTLS, TestDTLSSrtpGCM128) {
jbauchcb560652016-08-04 05:20:32 -07001218 std::vector<int> gcm128;
1219 gcm128.push_back(rtc::SRTP_AEAD_AES_128_GCM);
1220 SetDtlsSrtpCryptoSuites(gcm128, true);
1221 SetDtlsSrtpCryptoSuites(gcm128, false);
1222 TestHandshake();
1223
1224 int client_cipher;
1225 ASSERT_TRUE(GetDtlsSrtpCryptoSuite(true, &client_cipher));
1226 int server_cipher;
1227 ASSERT_TRUE(GetDtlsSrtpCryptoSuite(false, &server_cipher));
1228
1229 ASSERT_EQ(client_cipher, server_cipher);
1230 ASSERT_EQ(client_cipher, rtc::SRTP_AEAD_AES_128_GCM);
1231};
1232
1233// Test DTLS-SRTP with all GCM-256 ciphers.
1234TEST_P(SSLStreamAdapterTestDTLS, TestDTLSSrtpGCM256) {
jbauchcb560652016-08-04 05:20:32 -07001235 std::vector<int> gcm256;
1236 gcm256.push_back(rtc::SRTP_AEAD_AES_256_GCM);
1237 SetDtlsSrtpCryptoSuites(gcm256, true);
1238 SetDtlsSrtpCryptoSuites(gcm256, false);
1239 TestHandshake();
1240
1241 int client_cipher;
1242 ASSERT_TRUE(GetDtlsSrtpCryptoSuite(true, &client_cipher));
1243 int server_cipher;
1244 ASSERT_TRUE(GetDtlsSrtpCryptoSuite(false, &server_cipher));
1245
1246 ASSERT_EQ(client_cipher, server_cipher);
1247 ASSERT_EQ(client_cipher, rtc::SRTP_AEAD_AES_256_GCM);
1248};
1249
1250// Test DTLS-SRTP with mixed GCM-128/-256 ciphers -- should not converge.
1251TEST_P(SSLStreamAdapterTestDTLS, TestDTLSSrtpGCMMismatch) {
jbauchcb560652016-08-04 05:20:32 -07001252 std::vector<int> gcm128;
1253 gcm128.push_back(rtc::SRTP_AEAD_AES_128_GCM);
1254 std::vector<int> gcm256;
1255 gcm256.push_back(rtc::SRTP_AEAD_AES_256_GCM);
1256 SetDtlsSrtpCryptoSuites(gcm128, true);
1257 SetDtlsSrtpCryptoSuites(gcm256, false);
1258 TestHandshake();
1259
1260 int client_cipher;
1261 ASSERT_FALSE(GetDtlsSrtpCryptoSuite(true, &client_cipher));
1262 int server_cipher;
1263 ASSERT_FALSE(GetDtlsSrtpCryptoSuite(false, &server_cipher));
1264};
1265
1266// Test DTLS-SRTP with both GCM-128/-256 ciphers -- should select GCM-256.
1267TEST_P(SSLStreamAdapterTestDTLS, TestDTLSSrtpGCMMixed) {
jbauchcb560652016-08-04 05:20:32 -07001268 std::vector<int> gcmBoth;
1269 gcmBoth.push_back(rtc::SRTP_AEAD_AES_256_GCM);
1270 gcmBoth.push_back(rtc::SRTP_AEAD_AES_128_GCM);
1271 SetDtlsSrtpCryptoSuites(gcmBoth, true);
1272 SetDtlsSrtpCryptoSuites(gcmBoth, false);
1273 TestHandshake();
1274
1275 int client_cipher;
1276 ASSERT_TRUE(GetDtlsSrtpCryptoSuite(true, &client_cipher));
1277 int server_cipher;
1278 ASSERT_TRUE(GetDtlsSrtpCryptoSuite(false, &server_cipher));
1279
1280 ASSERT_EQ(client_cipher, server_cipher);
1281 ASSERT_EQ(client_cipher, rtc::SRTP_AEAD_AES_256_GCM);
1282};
1283
1284// Test SRTP cipher suite lengths.
1285TEST_P(SSLStreamAdapterTestDTLS, TestDTLSSrtpKeyAndSaltLengths) {
1286 int key_len;
1287 int salt_len;
1288
Yves Gerey665174f2018-06-19 15:03:05 +02001289 ASSERT_FALSE(rtc::GetSrtpKeyAndSaltLengths(rtc::SRTP_INVALID_CRYPTO_SUITE,
1290 &key_len, &salt_len));
jbauchcb560652016-08-04 05:20:32 -07001291
Yves Gerey665174f2018-06-19 15:03:05 +02001292 ASSERT_TRUE(rtc::GetSrtpKeyAndSaltLengths(rtc::SRTP_AES128_CM_SHA1_32,
1293 &key_len, &salt_len));
1294 ASSERT_EQ(128 / 8, key_len);
1295 ASSERT_EQ(112 / 8, salt_len);
jbauchcb560652016-08-04 05:20:32 -07001296
Yves Gerey665174f2018-06-19 15:03:05 +02001297 ASSERT_TRUE(rtc::GetSrtpKeyAndSaltLengths(rtc::SRTP_AES128_CM_SHA1_80,
1298 &key_len, &salt_len));
1299 ASSERT_EQ(128 / 8, key_len);
1300 ASSERT_EQ(112 / 8, salt_len);
jbauchcb560652016-08-04 05:20:32 -07001301
Yves Gerey665174f2018-06-19 15:03:05 +02001302 ASSERT_TRUE(rtc::GetSrtpKeyAndSaltLengths(rtc::SRTP_AEAD_AES_128_GCM,
1303 &key_len, &salt_len));
1304 ASSERT_EQ(128 / 8, key_len);
1305 ASSERT_EQ(96 / 8, salt_len);
jbauchcb560652016-08-04 05:20:32 -07001306
Yves Gerey665174f2018-06-19 15:03:05 +02001307 ASSERT_TRUE(rtc::GetSrtpKeyAndSaltLengths(rtc::SRTP_AEAD_AES_256_GCM,
1308 &key_len, &salt_len));
1309 ASSERT_EQ(256 / 8, key_len);
1310 ASSERT_EQ(96 / 8, salt_len);
jbauchcb560652016-08-04 05:20:32 -07001311};
1312
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001313// Test an exporter
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +02001314TEST_P(SSLStreamAdapterTestDTLS, TestDTLSExporter) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001315 TestHandshake();
1316 unsigned char client_out[20];
1317 unsigned char server_out[20];
1318
1319 bool result;
Yves Gerey665174f2018-06-19 15:03:05 +02001320 result = ExportKeyingMaterial(kExporterLabel, kExporterContext,
1321 kExporterContextLen, true, true, client_out,
1322 sizeof(client_out));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001323 ASSERT_TRUE(result);
1324
Yves Gerey665174f2018-06-19 15:03:05 +02001325 result = ExportKeyingMaterial(kExporterLabel, kExporterContext,
1326 kExporterContextLen, true, false, server_out,
1327 sizeof(server_out));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001328 ASSERT_TRUE(result);
1329
1330 ASSERT_TRUE(!memcmp(client_out, server_out, sizeof(client_out)));
1331}
1332
1333// Test not yet valid certificates are not rejected.
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +02001334TEST_P(SSLStreamAdapterTestDTLS, TestCertNotYetValid) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001335 long one_day = 60 * 60 * 24;
1336 // Make the certificates not valid until one day later.
1337 ResetIdentitiesWithValidity(one_day, one_day);
1338 TestHandshake();
1339}
1340
1341// Test expired certificates are not rejected.
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +02001342TEST_P(SSLStreamAdapterTestDTLS, TestCertExpired) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001343 long one_day = 60 * 60 * 24;
1344 // Make the certificates already expired.
1345 ResetIdentitiesWithValidity(-one_day, -one_day);
1346 TestHandshake();
1347}
1348
1349// Test data transfer using certs created from strings.
torbjorng7593aad2015-11-19 12:20:51 -08001350TEST_F(SSLStreamAdapterTestDTLSFromPEMStrings, TestTransfer) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001351 TestHandshake();
1352 TestTransfer(100);
1353}
1354
1355// Test getting the remote certificate.
torbjorng7593aad2015-11-19 12:20:51 -08001356TEST_F(SSLStreamAdapterTestDTLSFromPEMStrings, TestDTLSGetPeerCertificate) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001357 // Peer certificates haven't been received yet.
kwibergb4d01c42016-04-06 05:15:06 -07001358 ASSERT_FALSE(GetPeerCertificate(true));
1359 ASSERT_FALSE(GetPeerCertificate(false));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001360
1361 TestHandshake();
1362
1363 // The client should have a peer certificate after the handshake.
jbauch555604a2016-04-26 03:13:22 -07001364 std::unique_ptr<rtc::SSLCertificate> client_peer_cert =
kwibergb4d01c42016-04-06 05:15:06 -07001365 GetPeerCertificate(true);
1366 ASSERT_TRUE(client_peer_cert);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001367
1368 // It's not kCERT_PEM.
1369 std::string client_peer_string = client_peer_cert->ToPEMString();
1370 ASSERT_NE(kCERT_PEM, client_peer_string);
1371
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001372 // The server should have a peer certificate after the handshake.
jbauch555604a2016-04-26 03:13:22 -07001373 std::unique_ptr<rtc::SSLCertificate> server_peer_cert =
kwibergb4d01c42016-04-06 05:15:06 -07001374 GetPeerCertificate(false);
1375 ASSERT_TRUE(server_peer_cert);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001376
1377 // It's kCERT_PEM
1378 ASSERT_EQ(kCERT_PEM, server_peer_cert->ToPEMString());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001379}
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +00001380
1381// Test getting the used DTLS ciphers.
Joachim Bauch831c5582015-05-20 12:48:41 +02001382// DTLS 1.2 enabled for neither client nor server -> DTLS 1.0 will be used.
torbjorng43166b82016-03-11 00:06:47 -08001383TEST_P(SSLStreamAdapterTestDTLS, TestGetSslCipherSuite) {
Joachim Bauch831c5582015-05-20 12:48:41 +02001384 SetupProtocolVersions(rtc::SSL_PROTOCOL_DTLS_10, rtc::SSL_PROTOCOL_DTLS_10);
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +00001385 TestHandshake();
1386
Guo-wei Shieh6caafbe2015-10-05 12:43:27 -07001387 int client_cipher;
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001388 ASSERT_TRUE(GetSslCipherSuite(true, &client_cipher));
Guo-wei Shieh6caafbe2015-10-05 12:43:27 -07001389 int server_cipher;
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001390 ASSERT_TRUE(GetSslCipherSuite(false, &server_cipher));
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +00001391
torbjorng43166b82016-03-11 00:06:47 -08001392 ASSERT_EQ(rtc::SSL_PROTOCOL_DTLS_10, GetSslVersion(true));
1393 ASSERT_EQ(rtc::SSL_PROTOCOL_DTLS_10, GetSslVersion(false));
1394
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +00001395 ASSERT_EQ(client_cipher, server_cipher);
torbjorng43166b82016-03-11 00:06:47 -08001396 ASSERT_TRUE(rtc::SSLStreamAdapter::IsAcceptableCipher(
1397 server_cipher, ::testing::get<1>(GetParam()).type()));
Joachim Bauch831c5582015-05-20 12:48:41 +02001398}
1399
1400// Test getting the used DTLS 1.2 ciphers.
1401// DTLS 1.2 enabled for client and server -> DTLS 1.2 will be used.
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001402TEST_P(SSLStreamAdapterTestDTLS, TestGetSslCipherSuiteDtls12Both) {
Joachim Bauch831c5582015-05-20 12:48:41 +02001403 SetupProtocolVersions(rtc::SSL_PROTOCOL_DTLS_12, rtc::SSL_PROTOCOL_DTLS_12);
1404 TestHandshake();
1405
Guo-wei Shieh6caafbe2015-10-05 12:43:27 -07001406 int client_cipher;
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001407 ASSERT_TRUE(GetSslCipherSuite(true, &client_cipher));
Guo-wei Shieh6caafbe2015-10-05 12:43:27 -07001408 int server_cipher;
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001409 ASSERT_TRUE(GetSslCipherSuite(false, &server_cipher));
Joachim Bauch831c5582015-05-20 12:48:41 +02001410
torbjorng43166b82016-03-11 00:06:47 -08001411 ASSERT_EQ(rtc::SSL_PROTOCOL_DTLS_12, GetSslVersion(true));
1412 ASSERT_EQ(rtc::SSL_PROTOCOL_DTLS_12, GetSslVersion(false));
1413
Joachim Bauch831c5582015-05-20 12:48:41 +02001414 ASSERT_EQ(client_cipher, server_cipher);
torbjorng43166b82016-03-11 00:06:47 -08001415 ASSERT_TRUE(rtc::SSLStreamAdapter::IsAcceptableCipher(
1416 server_cipher, ::testing::get<1>(GetParam()).type()));
Joachim Bauch831c5582015-05-20 12:48:41 +02001417}
1418
1419// DTLS 1.2 enabled for client only -> DTLS 1.0 will be used.
torbjorng43166b82016-03-11 00:06:47 -08001420TEST_P(SSLStreamAdapterTestDTLS, TestGetSslCipherSuiteDtls12Client) {
Joachim Bauch831c5582015-05-20 12:48:41 +02001421 SetupProtocolVersions(rtc::SSL_PROTOCOL_DTLS_10, rtc::SSL_PROTOCOL_DTLS_12);
1422 TestHandshake();
1423
Guo-wei Shieh6caafbe2015-10-05 12:43:27 -07001424 int client_cipher;
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001425 ASSERT_TRUE(GetSslCipherSuite(true, &client_cipher));
Guo-wei Shieh6caafbe2015-10-05 12:43:27 -07001426 int server_cipher;
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001427 ASSERT_TRUE(GetSslCipherSuite(false, &server_cipher));
Joachim Bauch831c5582015-05-20 12:48:41 +02001428
torbjorng43166b82016-03-11 00:06:47 -08001429 ASSERT_EQ(rtc::SSL_PROTOCOL_DTLS_10, GetSslVersion(true));
1430 ASSERT_EQ(rtc::SSL_PROTOCOL_DTLS_10, GetSslVersion(false));
1431
Joachim Bauch831c5582015-05-20 12:48:41 +02001432 ASSERT_EQ(client_cipher, server_cipher);
torbjorng43166b82016-03-11 00:06:47 -08001433 ASSERT_TRUE(rtc::SSLStreamAdapter::IsAcceptableCipher(
1434 server_cipher, ::testing::get<1>(GetParam()).type()));
Joachim Bauch831c5582015-05-20 12:48:41 +02001435}
1436
1437// DTLS 1.2 enabled for server only -> DTLS 1.0 will be used.
torbjorng43166b82016-03-11 00:06:47 -08001438TEST_P(SSLStreamAdapterTestDTLS, TestGetSslCipherSuiteDtls12Server) {
Joachim Bauch831c5582015-05-20 12:48:41 +02001439 SetupProtocolVersions(rtc::SSL_PROTOCOL_DTLS_12, rtc::SSL_PROTOCOL_DTLS_10);
1440 TestHandshake();
1441
Guo-wei Shieh6caafbe2015-10-05 12:43:27 -07001442 int client_cipher;
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001443 ASSERT_TRUE(GetSslCipherSuite(true, &client_cipher));
Guo-wei Shieh6caafbe2015-10-05 12:43:27 -07001444 int server_cipher;
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001445 ASSERT_TRUE(GetSslCipherSuite(false, &server_cipher));
Joachim Bauch831c5582015-05-20 12:48:41 +02001446
torbjorng43166b82016-03-11 00:06:47 -08001447 ASSERT_EQ(rtc::SSL_PROTOCOL_DTLS_10, GetSslVersion(true));
1448 ASSERT_EQ(rtc::SSL_PROTOCOL_DTLS_10, GetSslVersion(false));
1449
Joachim Bauch831c5582015-05-20 12:48:41 +02001450 ASSERT_EQ(client_cipher, server_cipher);
torbjorng43166b82016-03-11 00:06:47 -08001451 ASSERT_TRUE(rtc::SSLStreamAdapter::IsAcceptableCipher(
1452 server_cipher, ::testing::get<1>(GetParam()).type()));
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +00001453}
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +02001454
torbjorng4e572472015-10-08 09:42:49 -07001455// The RSA keysizes here might look strange, why not include the RFC's size
1456// 2048?. The reason is test case slowness; testing two sizes to exercise
1457// parametrization is sufficient.
1458INSTANTIATE_TEST_CASE_P(
1459 SSLStreamAdapterTestsTLS,
1460 SSLStreamAdapterTestTLS,
1461 Combine(Values(rtc::KeyParams::RSA(1024, 65537),
1462 rtc::KeyParams::RSA(1152, 65537),
1463 rtc::KeyParams::ECDSA(rtc::EC_NIST_P256)),
1464 Values(rtc::KeyParams::RSA(1024, 65537),
1465 rtc::KeyParams::RSA(1152, 65537),
1466 rtc::KeyParams::ECDSA(rtc::EC_NIST_P256))));
1467INSTANTIATE_TEST_CASE_P(
1468 SSLStreamAdapterTestsDTLS,
1469 SSLStreamAdapterTestDTLS,
1470 Combine(Values(rtc::KeyParams::RSA(1024, 65537),
1471 rtc::KeyParams::RSA(1152, 65537),
1472 rtc::KeyParams::ECDSA(rtc::EC_NIST_P256)),
1473 Values(rtc::KeyParams::RSA(1024, 65537),
1474 rtc::KeyParams::RSA(1152, 65537),
1475 rtc::KeyParams::ECDSA(rtc::EC_NIST_P256))));