blob: 6fbb1d7c2cc09822d26ba67e9a70ea52c315a71a [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"
Niels Möllere7547d52018-11-01 09:33:08 +010020#include "rtc_base/memory_stream.h"
Yves Gerey2e00abc2018-10-05 15:39:24 +020021#include "rtc_base/messagedigest.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/ssladapter.h"
23#include "rtc_base/sslidentity.h"
24#include "rtc_base/sslstreamadapter.h"
25#include "rtc_base/stream.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000026
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +020027using ::testing::WithParamInterface;
28using ::testing::Values;
29using ::testing::Combine;
30using ::testing::tuple;
31
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000032static const int kBlockSize = 4096;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000033static const char kExporterLabel[] = "label";
34static const unsigned char kExporterContext[] = "context";
35static int kExporterContextLen = sizeof(kExporterContext);
36
37static const char kRSA_PRIVATE_KEY_PEM[] =
38 "-----BEGIN RSA PRIVATE KEY-----\n"
39 "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMYRkbhmI7kVA/rM\n"
40 "czsZ+6JD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wv1EvlC5RG\n"
126 "hLX+TXaWqFxB1Hd8ebKRR40mboFG6KcUI3lLBthDvQE7jnq48QfZMjlMQK0ZF1l7\n"
127 "SrlwRXWA74bU8CLJvnZKKo9p4TsTiDYGSYC6tNHKj5s3TGWL46oqGyZ0KdGNhrtC\n"
128 "rIGenMhth1vPYjyy0XuGBndXT85yi+IM2l8g8oU845+plxIhgpSI8bbC0oLwnhQ5\n"
129 "ARfsiYLkXDE7imSS0CSUmye76372mlzAIB1is4bBB/SzpPQtBuB9LDKtONgpSGHn\n"
130 "dGaXBy+qbVXVyGXaeEbIRjtJ6m92\n"
131 "-----END CERTIFICATE-----\n";
132
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000133class SSLStreamAdapterTestBase;
134
jbauche488a0d2015-11-19 05:17:58 -0800135class SSLDummyStreamBase : public rtc::StreamInterface,
136 public sigslot::has_slots<> {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000137 public:
jbauche488a0d2015-11-19 05:17:58 -0800138 SSLDummyStreamBase(SSLStreamAdapterTestBase* test,
Yves Gerey665174f2018-06-19 15:03:05 +0200139 const std::string& side,
jbauche488a0d2015-11-19 05:17:58 -0800140 rtc::StreamInterface* in,
Yves Gerey665174f2018-06-19 15:03:05 +0200141 rtc::StreamInterface* out)
142 : test_base_(test), side_(side), in_(in), out_(out), first_packet_(true) {
jbauche488a0d2015-11-19 05:17:58 -0800143 in_->SignalEvent.connect(this, &SSLDummyStreamBase::OnEventIn);
144 out_->SignalEvent.connect(this, &SSLDummyStreamBase::OnEventOut);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000145 }
146
jbauche488a0d2015-11-19 05:17:58 -0800147 rtc::StreamState GetState() const override { return rtc::SS_OPEN; }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000148
Yves Gerey665174f2018-06-19 15:03:05 +0200149 rtc::StreamResult Read(void* buffer,
150 size_t buffer_len,
151 size_t* read,
152 int* error) override {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000153 rtc::StreamResult r;
154
155 r = in_->Read(buffer, buffer_len, read, error);
156 if (r == rtc::SR_BLOCK)
157 return rtc::SR_BLOCK;
158 if (r == rtc::SR_EOS)
159 return rtc::SR_EOS;
160
161 if (r != rtc::SR_SUCCESS) {
162 ADD_FAILURE();
163 return rtc::SR_ERROR;
164 }
165
166 return rtc::SR_SUCCESS;
167 }
168
169 // Catch readability events on in and pass them up.
jbauche488a0d2015-11-19 05:17:58 -0800170 void OnEventIn(rtc::StreamInterface* stream, int sig, int err) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000171 int mask = (rtc::SE_READ | rtc::SE_CLOSE);
172
173 if (sig & mask) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100174 RTC_LOG(LS_VERBOSE) << "SSLDummyStreamBase::OnEvent side=" << side_
175 << " sig=" << sig << " forwarding upward";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000176 PostEvent(sig & mask, 0);
177 }
178 }
179
180 // Catch writeability events on out and pass them up.
jbauche488a0d2015-11-19 05:17:58 -0800181 void OnEventOut(rtc::StreamInterface* stream, int sig, int err) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000182 if (sig & rtc::SE_WRITE) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100183 RTC_LOG(LS_VERBOSE) << "SSLDummyStreamBase::OnEvent side=" << side_
184 << " sig=" << sig << " forwarding upward";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000185
186 PostEvent(sig & rtc::SE_WRITE, 0);
187 }
188 }
189
190 // Write to the outgoing FifoBuffer
Yves Gerey665174f2018-06-19 15:03:05 +0200191 rtc::StreamResult WriteData(const void* data,
192 size_t data_len,
193 size_t* written,
194 int* error) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000195 return out_->Write(data, data_len, written, error);
196 }
197
Yves Gerey665174f2018-06-19 15:03:05 +0200198 rtc::StreamResult Write(const void* data,
199 size_t data_len,
200 size_t* written,
201 int* error) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000202
jbauche488a0d2015-11-19 05:17:58 -0800203 void Close() override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100204 RTC_LOG(LS_INFO) << "Closing outbound stream";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000205 out_->Close();
206 }
207
jbauche488a0d2015-11-19 05:17:58 -0800208 protected:
209 SSLStreamAdapterTestBase* test_base_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000210 const std::string side_;
jbauche488a0d2015-11-19 05:17:58 -0800211 rtc::StreamInterface* in_;
212 rtc::StreamInterface* out_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000213 bool first_packet_;
214};
215
jbauche488a0d2015-11-19 05:17:58 -0800216class SSLDummyStreamTLS : public SSLDummyStreamBase {
217 public:
218 SSLDummyStreamTLS(SSLStreamAdapterTestBase* test,
219 const std::string& side,
220 rtc::FifoBuffer* in,
Yves Gerey665174f2018-06-19 15:03:05 +0200221 rtc::FifoBuffer* out)
222 : SSLDummyStreamBase(test, side, in, out) {}
jbauche488a0d2015-11-19 05:17:58 -0800223};
224
Yves Gerey665174f2018-06-19 15:03:05 +0200225class BufferQueueStream : public rtc::BufferQueue, public rtc::StreamInterface {
jbauche488a0d2015-11-19 05:17:58 -0800226 public:
227 BufferQueueStream(size_t capacity, size_t default_size)
Yves Gerey665174f2018-06-19 15:03:05 +0200228 : rtc::BufferQueue(capacity, default_size) {}
jbauche488a0d2015-11-19 05:17:58 -0800229
230 // Implementation of abstract StreamInterface methods.
231
232 // A buffer queue stream is always "open".
233 rtc::StreamState GetState() const override { return rtc::SS_OPEN; }
234
235 // Reading a buffer queue stream will either succeed or block.
Yves Gerey665174f2018-06-19 15:03:05 +0200236 rtc::StreamResult Read(void* buffer,
237 size_t buffer_len,
238 size_t* read,
239 int* error) override {
jbauche488a0d2015-11-19 05:17:58 -0800240 if (!ReadFront(buffer, buffer_len, read)) {
241 return rtc::SR_BLOCK;
242 }
243 return rtc::SR_SUCCESS;
244 }
245
246 // Writing to a buffer queue stream will either succeed or block.
Yves Gerey665174f2018-06-19 15:03:05 +0200247 rtc::StreamResult Write(const void* data,
248 size_t data_len,
249 size_t* written,
250 int* error) override {
jbauche488a0d2015-11-19 05:17:58 -0800251 if (!WriteBack(data, data_len, written)) {
252 return rtc::SR_BLOCK;
253 }
254 return rtc::SR_SUCCESS;
255 }
256
257 // A buffer queue stream can not be closed.
258 void Close() override {}
259
260 protected:
Yves Gerey665174f2018-06-19 15:03:05 +0200261 void NotifyReadableForTest() override { PostEvent(rtc::SE_READ, 0); }
jbauche488a0d2015-11-19 05:17:58 -0800262
Yves Gerey665174f2018-06-19 15:03:05 +0200263 void NotifyWritableForTest() override { PostEvent(rtc::SE_WRITE, 0); }
jbauche488a0d2015-11-19 05:17:58 -0800264};
265
266class SSLDummyStreamDTLS : public SSLDummyStreamBase {
267 public:
268 SSLDummyStreamDTLS(SSLStreamAdapterTestBase* test,
269 const std::string& side,
270 BufferQueueStream* in,
Yves Gerey665174f2018-06-19 15:03:05 +0200271 BufferQueueStream* out)
272 : SSLDummyStreamBase(test, side, in, out) {}
jbauche488a0d2015-11-19 05:17:58 -0800273};
274
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000275static const int kFifoBufferSize = 4096;
jbauche488a0d2015-11-19 05:17:58 -0800276static const int kBufferCapacity = 1;
277static const size_t kDefaultBufferSize = 2048;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000278
279class SSLStreamAdapterTestBase : public testing::Test,
280 public sigslot::has_slots<> {
281 public:
torbjorng4e572472015-10-08 09:42:49 -0700282 SSLStreamAdapterTestBase(
283 const std::string& client_cert_pem,
284 const std::string& client_private_key_pem,
285 bool dtls,
286 rtc::KeyParams client_key_type = rtc::KeyParams(rtc::KT_DEFAULT),
287 rtc::KeyParams server_key_type = rtc::KeyParams(rtc::KT_DEFAULT))
jbauche488a0d2015-11-19 05:17:58 -0800288 : client_cert_pem_(client_cert_pem),
289 client_private_key_pem_(client_private_key_pem),
290 client_key_type_(client_key_type),
291 server_key_type_(server_key_type),
deadbeef37f5ecf2017-02-27 14:06:41 -0800292 client_stream_(nullptr),
293 server_stream_(nullptr),
294 client_identity_(nullptr),
295 server_identity_(nullptr),
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200296 delay_(0),
297 mtu_(1460),
298 loss_(0),
299 lose_first_packet_(false),
300 damage_(false),
301 dtls_(dtls),
302 handshake_wait_(5000),
303 identities_set_(false) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000304 // Set use of the test RNG to get predictable loss patterns.
305 rtc::SetRandomTestMode(true);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000306 }
307
Steve Anton9de3aac2017-10-24 10:08:26 -0700308 ~SSLStreamAdapterTestBase() override {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000309 // Put it back for the next test.
310 rtc::SetRandomTestMode(false);
311 }
312
torbjorng7593aad2015-11-19 12:20:51 -0800313 void SetUp() override {
jbauche488a0d2015-11-19 05:17:58 -0800314 CreateStreams();
315
316 client_ssl_.reset(rtc::SSLStreamAdapter::Create(client_stream_));
317 server_ssl_.reset(rtc::SSLStreamAdapter::Create(server_stream_));
318
319 // Set up the slots
320 client_ssl_->SignalEvent.connect(this, &SSLStreamAdapterTestBase::OnEvent);
321 server_ssl_->SignalEvent.connect(this, &SSLStreamAdapterTestBase::OnEvent);
322
323 if (!client_cert_pem_.empty() && !client_private_key_pem_.empty()) {
324 client_identity_ = rtc::SSLIdentity::FromPEMStrings(
325 client_private_key_pem_, client_cert_pem_);
326 } else {
327 client_identity_ = rtc::SSLIdentity::Generate("client", client_key_type_);
328 }
329 server_identity_ = rtc::SSLIdentity::Generate("server", server_key_type_);
330
331 client_ssl_->SetIdentity(client_identity_);
332 server_ssl_->SetIdentity(server_identity_);
333 }
334
torbjorng7593aad2015-11-19 12:20:51 -0800335 void TearDown() override {
jbauche488a0d2015-11-19 05:17:58 -0800336 client_ssl_.reset(nullptr);
337 server_ssl_.reset(nullptr);
338 }
339
340 virtual void CreateStreams() = 0;
341
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000342 // Recreate the client/server identities with the specified validity period.
343 // |not_before| and |not_after| are offsets from the current time in number
344 // of seconds.
345 void ResetIdentitiesWithValidity(int not_before, int not_after) {
jbauche488a0d2015-11-19 05:17:58 -0800346 CreateStreams();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000347
348 client_ssl_.reset(rtc::SSLStreamAdapter::Create(client_stream_));
349 server_ssl_.reset(rtc::SSLStreamAdapter::Create(server_stream_));
350
351 client_ssl_->SignalEvent.connect(this, &SSLStreamAdapterTestBase::OnEvent);
352 server_ssl_->SignalEvent.connect(this, &SSLStreamAdapterTestBase::OnEvent);
353
Torbjorn Granlund46c9cc02015-12-01 13:06:34 +0100354 time_t now = time(nullptr);
355
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000356 rtc::SSLIdentityParams client_params;
torbjorng4e572472015-10-08 09:42:49 -0700357 client_params.key_params = rtc::KeyParams(rtc::KT_DEFAULT);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000358 client_params.common_name = "client";
Torbjorn Granlund46c9cc02015-12-01 13:06:34 +0100359 client_params.not_before = now + not_before;
360 client_params.not_after = now + not_after;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000361 client_identity_ = rtc::SSLIdentity::GenerateForTest(client_params);
362
363 rtc::SSLIdentityParams server_params;
torbjorng4e572472015-10-08 09:42:49 -0700364 server_params.key_params = rtc::KeyParams(rtc::KT_DEFAULT);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000365 server_params.common_name = "server";
Torbjorn Granlund46c9cc02015-12-01 13:06:34 +0100366 server_params.not_before = now + not_before;
367 server_params.not_after = now + not_after;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000368 server_identity_ = rtc::SSLIdentity::GenerateForTest(server_params);
369
370 client_ssl_->SetIdentity(client_identity_);
371 server_ssl_->SetIdentity(server_identity_);
372 }
373
Yves Gerey665174f2018-06-19 15:03:05 +0200374 virtual void OnEvent(rtc::StreamInterface* stream, int sig, int err) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100375 RTC_LOG(LS_VERBOSE) << "SSLStreamAdapterTestBase::OnEvent sig=" << sig;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000376
377 if (sig & rtc::SE_READ) {
378 ReadData(stream);
379 }
380
381 if ((stream == client_ssl_.get()) && (sig & rtc::SE_WRITE)) {
382 WriteData();
383 }
384 }
385
deadbeef89824f62016-09-30 11:55:43 -0700386 void SetPeerIdentitiesByDigest(bool correct, bool expect_success) {
387 unsigned char server_digest[20];
388 size_t server_digest_len;
389 unsigned char client_digest[20];
390 size_t client_digest_len;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000391 bool rv;
deadbeef89824f62016-09-30 11:55:43 -0700392 rtc::SSLPeerCertificateDigestError err;
393 rtc::SSLPeerCertificateDigestError expected_err =
394 expect_success
395 ? rtc::SSLPeerCertificateDigestError::NONE
396 : rtc::SSLPeerCertificateDigestError::VERIFICATION_FAILED;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000397
Mirko Bonadei675513b2017-11-09 11:09:25 +0100398 RTC_LOG(LS_INFO) << "Setting peer identities by digest";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000399
deadbeef89824f62016-09-30 11:55:43 -0700400 rv = server_identity_->certificate().ComputeDigest(
401 rtc::DIGEST_SHA_1, server_digest, 20, &server_digest_len);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000402 ASSERT_TRUE(rv);
deadbeef89824f62016-09-30 11:55:43 -0700403 rv = client_identity_->certificate().ComputeDigest(
404 rtc::DIGEST_SHA_1, client_digest, 20, &client_digest_len);
405 ASSERT_TRUE(rv);
406
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000407 if (!correct) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100408 RTC_LOG(LS_INFO) << "Setting bogus digest for server cert";
deadbeef89824f62016-09-30 11:55:43 -0700409 server_digest[0]++;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000410 }
deadbeef89824f62016-09-30 11:55:43 -0700411 rv = client_ssl_->SetPeerCertificateDigest(rtc::DIGEST_SHA_1, server_digest,
412 server_digest_len, &err);
413 EXPECT_EQ(expected_err, err);
414 EXPECT_EQ(expect_success, rv);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000415
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000416 if (!correct) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100417 RTC_LOG(LS_INFO) << "Setting bogus digest for client cert";
deadbeef89824f62016-09-30 11:55:43 -0700418 client_digest[0]++;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000419 }
deadbeef89824f62016-09-30 11:55:43 -0700420 rv = server_ssl_->SetPeerCertificateDigest(rtc::DIGEST_SHA_1, client_digest,
421 client_digest_len, &err);
422 EXPECT_EQ(expected_err, err);
423 EXPECT_EQ(expect_success, rv);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000424
425 identities_set_ = true;
426 }
427
Joachim Bauch831c5582015-05-20 12:48:41 +0200428 void SetupProtocolVersions(rtc::SSLProtocolVersion server_version,
429 rtc::SSLProtocolVersion client_version) {
430 server_ssl_->SetMaxProtocolVersion(server_version);
431 client_ssl_->SetMaxProtocolVersion(client_version);
432 }
433
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000434 void TestHandshake(bool expect_success = true) {
Yves Gerey665174f2018-06-19 15:03:05 +0200435 server_ssl_->SetMode(dtls_ ? rtc::SSL_MODE_DTLS : rtc::SSL_MODE_TLS);
436 client_ssl_->SetMode(dtls_ ? rtc::SSL_MODE_DTLS : rtc::SSL_MODE_TLS);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000437
438 if (!dtls_) {
439 // Make sure we simulate a reliable network for TLS.
440 // This is just a check to make sure that people don't write wrong
441 // tests.
nissec16fa5e2017-02-07 07:18:43 -0800442 RTC_CHECK_EQ(1460, mtu_);
443 RTC_CHECK(!loss_);
444 RTC_CHECK(!lose_first_packet_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000445 }
446
447 if (!identities_set_)
deadbeef89824f62016-09-30 11:55:43 -0700448 SetPeerIdentitiesByDigest(true, true);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000449
450 // Start the handshake
451 int rv;
452
453 server_ssl_->SetServerRole();
Taylor Brandstetterc8762a82016-08-11 12:01:49 -0700454 rv = server_ssl_->StartSSL();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000455 ASSERT_EQ(0, rv);
456
Taylor Brandstetterc8762a82016-08-11 12:01:49 -0700457 rv = client_ssl_->StartSSL();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000458 ASSERT_EQ(0, rv);
459
460 // Now run the handshake
461 if (expect_success) {
Yves Gerey665174f2018-06-19 15:03:05 +0200462 EXPECT_TRUE_WAIT((client_ssl_->GetState() == rtc::SS_OPEN) &&
463 (server_ssl_->GetState() == rtc::SS_OPEN),
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000464 handshake_wait_);
465 } else {
466 EXPECT_TRUE_WAIT(client_ssl_->GetState() == rtc::SS_CLOSED,
467 handshake_wait_);
468 }
469 }
470
deadbeef89824f62016-09-30 11:55:43 -0700471 // This tests that the handshake can complete before the identity is
472 // verified, and the identity will be verified after the fact.
473 void TestHandshakeWithDelayedIdentity(bool valid_identity) {
474 server_ssl_->SetMode(dtls_ ? rtc::SSL_MODE_DTLS : rtc::SSL_MODE_TLS);
475 client_ssl_->SetMode(dtls_ ? rtc::SSL_MODE_DTLS : rtc::SSL_MODE_TLS);
476
477 if (!dtls_) {
478 // Make sure we simulate a reliable network for TLS.
479 // This is just a check to make sure that people don't write wrong
480 // tests.
nissec16fa5e2017-02-07 07:18:43 -0800481 RTC_CHECK_EQ(1460, mtu_);
482 RTC_CHECK(!loss_);
483 RTC_CHECK(!lose_first_packet_);
deadbeef89824f62016-09-30 11:55:43 -0700484 }
485
486 // Start the handshake
487 int rv;
488
489 server_ssl_->SetServerRole();
490 rv = server_ssl_->StartSSL();
491 ASSERT_EQ(0, rv);
492
493 rv = client_ssl_->StartSSL();
494 ASSERT_EQ(0, rv);
495
496 // Now run the handshake.
497 EXPECT_TRUE_WAIT(
498 client_ssl_->IsTlsConnected() && server_ssl_->IsTlsConnected(),
499 handshake_wait_);
500
501 // Until the identity has been verified, the state should still be
502 // SS_OPENING and writes should return SR_BLOCK.
503 EXPECT_EQ(rtc::SS_OPENING, client_ssl_->GetState());
504 EXPECT_EQ(rtc::SS_OPENING, server_ssl_->GetState());
505 unsigned char packet[1];
506 size_t sent;
507 EXPECT_EQ(rtc::SR_BLOCK, client_ssl_->Write(&packet, 1, &sent, 0));
508 EXPECT_EQ(rtc::SR_BLOCK, server_ssl_->Write(&packet, 1, &sent, 0));
509
510 // If we set an invalid identity at this point, SetPeerCertificateDigest
511 // should return false.
512 SetPeerIdentitiesByDigest(valid_identity, valid_identity);
513 // State should then transition to SS_OPEN or SS_CLOSED based on validation
514 // of the identity.
515 if (valid_identity) {
516 EXPECT_EQ(rtc::SS_OPEN, client_ssl_->GetState());
517 EXPECT_EQ(rtc::SS_OPEN, server_ssl_->GetState());
518 } else {
519 EXPECT_EQ(rtc::SS_CLOSED, client_ssl_->GetState());
520 EXPECT_EQ(rtc::SS_CLOSED, server_ssl_->GetState());
521 }
522 }
523
Yves Gerey665174f2018-06-19 15:03:05 +0200524 rtc::StreamResult DataWritten(SSLDummyStreamBase* from,
525 const void* data,
526 size_t data_len,
527 size_t* written,
528 int* error) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000529 // Randomly drop loss_ percent of packets
Peter Boström0c4e06b2015-10-07 12:23:21 +0200530 if (rtc::CreateRandomId() % 100 < static_cast<uint32_t>(loss_)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100531 RTC_LOG(LS_VERBOSE) << "Randomly dropping packet, size=" << data_len;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000532 *written = data_len;
533 return rtc::SR_SUCCESS;
534 }
535 if (dtls_ && (data_len > mtu_)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100536 RTC_LOG(LS_VERBOSE) << "Dropping packet > mtu, size=" << data_len;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000537 *written = data_len;
538 return rtc::SR_SUCCESS;
539 }
540
541 // Optionally damage application data (type 23). Note that we don't damage
542 // handshake packets and we damage the last byte to keep the header
543 // intact but break the MAC.
Yves Gerey665174f2018-06-19 15:03:05 +0200544 if (damage_ && (*static_cast<const unsigned char*>(data) == 23)) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000545 std::vector<char> buf(data_len);
546
Mirko Bonadei675513b2017-11-09 11:09:25 +0100547 RTC_LOG(LS_VERBOSE) << "Damaging packet";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000548
549 memcpy(&buf[0], data, data_len);
550 buf[data_len - 1]++;
551
552 return from->WriteData(&buf[0], data_len, written, error);
553 }
554
555 return from->WriteData(data, data_len, written, error);
556 }
557
Yves Gerey665174f2018-06-19 15:03:05 +0200558 void SetDelay(int delay) { delay_ = delay; }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000559 int GetDelay() { return delay_; }
560
Yves Gerey665174f2018-06-19 15:03:05 +0200561 void SetLoseFirstPacket(bool lose) { lose_first_packet_ = lose; }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000562 bool GetLoseFirstPacket() { return lose_first_packet_; }
563
Yves Gerey665174f2018-06-19 15:03:05 +0200564 void SetLoss(int percent) { loss_ = percent; }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000565
Yves Gerey665174f2018-06-19 15:03:05 +0200566 void SetDamage() { damage_ = true; }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000567
Yves Gerey665174f2018-06-19 15:03:05 +0200568 void SetMtu(size_t mtu) { mtu_ = mtu; }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000569
Yves Gerey665174f2018-06-19 15:03:05 +0200570 void SetHandshakeWait(int wait) { handshake_wait_ = wait; }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000571
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800572 void SetDtlsSrtpCryptoSuites(const std::vector<int>& ciphers, bool client) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000573 if (client)
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800574 client_ssl_->SetDtlsSrtpCryptoSuites(ciphers);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000575 else
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800576 server_ssl_->SetDtlsSrtpCryptoSuites(ciphers);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000577 }
578
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800579 bool GetDtlsSrtpCryptoSuite(bool client, int* retval) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000580 if (client)
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800581 return client_ssl_->GetDtlsSrtpCryptoSuite(retval);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000582 else
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800583 return server_ssl_->GetDtlsSrtpCryptoSuite(retval);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000584 }
585
jbauch555604a2016-04-26 03:13:22 -0700586 std::unique_ptr<rtc::SSLCertificate> GetPeerCertificate(bool client) {
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800587 std::unique_ptr<rtc::SSLCertChain> chain;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000588 if (client)
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800589 chain = client_ssl_->GetPeerSSLCertChain();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000590 else
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800591 chain = server_ssl_->GetPeerSSLCertChain();
Steve Antonf25303e2018-10-16 15:23:31 -0700592 return (chain && chain->GetSize()) ? chain->Get(0).Clone() : nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000593 }
594
Guo-wei Shieh6caafbe2015-10-05 12:43:27 -0700595 bool GetSslCipherSuite(bool client, int* retval) {
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000596 if (client)
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700597 return client_ssl_->GetSslCipherSuite(retval);
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000598 else
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700599 return server_ssl_->GetSslCipherSuite(retval);
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +0000600 }
601
torbjorng43166b82016-03-11 00:06:47 -0800602 int GetSslVersion(bool client) {
603 if (client)
604 return client_ssl_->GetSslVersion();
605 else
606 return server_ssl_->GetSslVersion();
607 }
608
Yves Gerey665174f2018-06-19 15:03:05 +0200609 bool ExportKeyingMaterial(const char* label,
610 const unsigned char* context,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000611 size_t context_len,
612 bool use_context,
613 bool client,
Yves Gerey665174f2018-06-19 15:03:05 +0200614 unsigned char* result,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000615 size_t result_len) {
616 if (client)
Yves Gerey665174f2018-06-19 15:03:05 +0200617 return client_ssl_->ExportKeyingMaterial(label, context, context_len,
618 use_context, result, result_len);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000619 else
Yves Gerey665174f2018-06-19 15:03:05 +0200620 return server_ssl_->ExportKeyingMaterial(label, context, context_len,
621 use_context, result, result_len);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000622 }
623
624 // To be implemented by subclasses.
625 virtual void WriteData() = 0;
Yves Gerey665174f2018-06-19 15:03:05 +0200626 virtual void ReadData(rtc::StreamInterface* stream) = 0;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000627 virtual void TestTransfer(int size) = 0;
628
629 protected:
jbauche488a0d2015-11-19 05:17:58 -0800630 std::string client_cert_pem_;
631 std::string client_private_key_pem_;
632 rtc::KeyParams client_key_type_;
633 rtc::KeyParams server_key_type_;
Yves Gerey665174f2018-06-19 15:03:05 +0200634 SSLDummyStreamBase* client_stream_; // freed by client_ssl_ destructor
635 SSLDummyStreamBase* server_stream_; // freed by server_ssl_ destructor
jbauch555604a2016-04-26 03:13:22 -0700636 std::unique_ptr<rtc::SSLStreamAdapter> client_ssl_;
637 std::unique_ptr<rtc::SSLStreamAdapter> server_ssl_;
Yves Gerey665174f2018-06-19 15:03:05 +0200638 rtc::SSLIdentity* client_identity_; // freed by client_ssl_ destructor
639 rtc::SSLIdentity* server_identity_; // freed by server_ssl_ destructor
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000640 int delay_;
641 size_t mtu_;
642 int loss_;
643 bool lose_first_packet_;
644 bool damage_;
645 bool dtls_;
646 int handshake_wait_;
647 bool identities_set_;
648};
649
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200650class SSLStreamAdapterTestTLS
651 : public SSLStreamAdapterTestBase,
torbjorng4e572472015-10-08 09:42:49 -0700652 public WithParamInterface<tuple<rtc::KeyParams, rtc::KeyParams>> {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000653 public:
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200654 SSLStreamAdapterTestTLS()
655 : SSLStreamAdapterTestBase("",
656 "",
657 false,
658 ::testing::get<0>(GetParam()),
jbauche488a0d2015-11-19 05:17:58 -0800659 ::testing::get<1>(GetParam())),
660 client_buffer_(kFifoBufferSize),
Yves Gerey665174f2018-06-19 15:03:05 +0200661 server_buffer_(kFifoBufferSize) {}
jbauche488a0d2015-11-19 05:17:58 -0800662
torbjorng7593aad2015-11-19 12:20:51 -0800663 void CreateStreams() override {
jbauche488a0d2015-11-19 05:17:58 -0800664 client_stream_ =
665 new SSLDummyStreamTLS(this, "c2s", &client_buffer_, &server_buffer_);
666 server_stream_ =
667 new SSLDummyStreamTLS(this, "s2c", &server_buffer_, &client_buffer_);
668 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000669
670 // Test data transfer for TLS
nisseef8b61e2016-04-29 06:09:15 -0700671 void TestTransfer(int size) override {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100672 RTC_LOG(LS_INFO) << "Starting transfer test with " << size << " bytes";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000673 // Create some dummy data to send.
674 size_t received;
675
676 send_stream_.ReserveSize(size);
677 for (int i = 0; i < size; ++i) {
678 char ch = static_cast<char>(i);
deadbeef37f5ecf2017-02-27 14:06:41 -0800679 send_stream_.Write(&ch, 1, nullptr, nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000680 }
681 send_stream_.Rewind();
682
683 // Prepare the receive stream.
684 recv_stream_.ReserveSize(size);
685
686 // Start sending
687 WriteData();
688
689 // Wait for the client to close
690 EXPECT_TRUE_WAIT(server_ssl_->GetState() == rtc::SS_CLOSED, 10000);
691
692 // Now check the data
693 recv_stream_.GetSize(&received);
694
695 EXPECT_EQ(static_cast<size_t>(size), received);
Yves Gerey665174f2018-06-19 15:03:05 +0200696 EXPECT_EQ(0,
697 memcmp(send_stream_.GetBuffer(), recv_stream_.GetBuffer(), size));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000698 }
699
nisseef8b61e2016-04-29 06:09:15 -0700700 void WriteData() override {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000701 size_t position, tosend, size;
702 rtc::StreamResult rv;
703 size_t sent;
704 char block[kBlockSize];
705
706 send_stream_.GetSize(&size);
707 if (!size)
708 return;
709
710 for (;;) {
711 send_stream_.GetPosition(&position);
deadbeef37f5ecf2017-02-27 14:06:41 -0800712 if (send_stream_.Read(block, sizeof(block), &tosend, nullptr) !=
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000713 rtc::SR_EOS) {
714 rv = client_ssl_->Write(block, tosend, &sent, 0);
715
716 if (rv == rtc::SR_SUCCESS) {
717 send_stream_.SetPosition(position + sent);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100718 RTC_LOG(LS_VERBOSE) << "Sent: " << position + sent;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000719 } else if (rv == rtc::SR_BLOCK) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100720 RTC_LOG(LS_VERBOSE) << "Blocked...";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000721 send_stream_.SetPosition(position);
722 break;
723 } else {
724 ADD_FAILURE();
725 break;
726 }
727 } else {
728 // Now close
Mirko Bonadei675513b2017-11-09 11:09:25 +0100729 RTC_LOG(LS_INFO) << "Wrote " << position << " bytes. Closing";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000730 client_ssl_->Close();
731 break;
732 }
733 }
734 };
735
Yves Gerey665174f2018-06-19 15:03:05 +0200736 void ReadData(rtc::StreamInterface* stream) override {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000737 char buffer[1600];
738 size_t bread;
739 int err2;
740 rtc::StreamResult r;
741
742 for (;;) {
743 r = stream->Read(buffer, sizeof(buffer), &bread, &err2);
744
745 if (r == rtc::SR_ERROR || r == rtc::SR_EOS) {
746 // Unfortunately, errors are the way that the stream adapter
torbjorng7593aad2015-11-19 12:20:51 -0800747 // signals close in OpenSSL.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000748 stream->Close();
749 return;
750 }
751
752 if (r == rtc::SR_BLOCK)
753 break;
754
755 ASSERT_EQ(rtc::SR_SUCCESS, r);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100756 RTC_LOG(LS_VERBOSE) << "Read " << bread;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000757
deadbeef37f5ecf2017-02-27 14:06:41 -0800758 recv_stream_.Write(buffer, bread, nullptr, nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000759 }
760 }
761
762 private:
jbauche488a0d2015-11-19 05:17:58 -0800763 rtc::FifoBuffer client_buffer_;
764 rtc::FifoBuffer server_buffer_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000765 rtc::MemoryStream send_stream_;
766 rtc::MemoryStream recv_stream_;
767};
768
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200769class SSLStreamAdapterTestDTLS
770 : public SSLStreamAdapterTestBase,
torbjorng4e572472015-10-08 09:42:49 -0700771 public WithParamInterface<tuple<rtc::KeyParams, rtc::KeyParams>> {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000772 public:
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200773 SSLStreamAdapterTestDTLS()
774 : SSLStreamAdapterTestBase("",
775 "",
776 true,
777 ::testing::get<0>(GetParam()),
778 ::testing::get<1>(GetParam())),
jbauche488a0d2015-11-19 05:17:58 -0800779 client_buffer_(kBufferCapacity, kDefaultBufferSize),
780 server_buffer_(kBufferCapacity, kDefaultBufferSize),
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200781 packet_size_(1000),
782 count_(0),
783 sent_(0) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000784
785 SSLStreamAdapterTestDTLS(const std::string& cert_pem,
Yves Gerey665174f2018-06-19 15:03:05 +0200786 const std::string& private_key_pem)
787 : SSLStreamAdapterTestBase(cert_pem, private_key_pem, true),
788 client_buffer_(kBufferCapacity, kDefaultBufferSize),
789 server_buffer_(kBufferCapacity, kDefaultBufferSize),
790 packet_size_(1000),
791 count_(0),
792 sent_(0) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000793
torbjorng7593aad2015-11-19 12:20:51 -0800794 void CreateStreams() override {
jbauche488a0d2015-11-19 05:17:58 -0800795 client_stream_ =
796 new SSLDummyStreamDTLS(this, "c2s", &client_buffer_, &server_buffer_);
797 server_stream_ =
798 new SSLDummyStreamDTLS(this, "s2c", &server_buffer_, &client_buffer_);
799 }
800
nisseef8b61e2016-04-29 06:09:15 -0700801 void WriteData() override {
Yves Gerey665174f2018-06-19 15:03:05 +0200802 unsigned char* packet = new unsigned char[1600];
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000803
jbauche488a0d2015-11-19 05:17:58 -0800804 while (sent_ < count_) {
torbjorng7593aad2015-11-19 12:20:51 -0800805 unsigned int rand_state = sent_;
806 packet[0] = sent_;
807 for (size_t i = 1; i < packet_size_; i++) {
808 // This is a simple LC PRNG. Keep in synch with identical code below.
809 rand_state = (rand_state * 251 + 19937) >> 7;
810 packet[i] = rand_state & 0xff;
811 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000812
813 size_t sent;
torbjorng7593aad2015-11-19 12:20:51 -0800814 rtc::StreamResult rv = client_ssl_->Write(packet, packet_size_, &sent, 0);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000815 if (rv == rtc::SR_SUCCESS) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100816 RTC_LOG(LS_VERBOSE) << "Sent: " << sent_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000817 sent_++;
818 } else if (rv == rtc::SR_BLOCK) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100819 RTC_LOG(LS_VERBOSE) << "Blocked...";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000820 break;
821 } else {
822 ADD_FAILURE();
823 break;
824 }
jbauche488a0d2015-11-19 05:17:58 -0800825 }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000826
Yves Gerey665174f2018-06-19 15:03:05 +0200827 delete[] packet;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000828 }
829
Yves Gerey665174f2018-06-19 15:03:05 +0200830 void ReadData(rtc::StreamInterface* stream) override {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000831 unsigned char buffer[2000];
832 size_t bread;
833 int err2;
834 rtc::StreamResult r;
835
836 for (;;) {
837 r = stream->Read(buffer, 2000, &bread, &err2);
838
839 if (r == rtc::SR_ERROR) {
840 // Unfortunately, errors are the way that the stream adapter
841 // signals close right now
842 stream->Close();
843 return;
844 }
845
846 if (r == rtc::SR_BLOCK)
847 break;
848
849 ASSERT_EQ(rtc::SR_SUCCESS, r);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100850 RTC_LOG(LS_VERBOSE) << "Read " << bread;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000851
852 // Now parse the datagram
853 ASSERT_EQ(packet_size_, bread);
torbjorng7593aad2015-11-19 12:20:51 -0800854 unsigned char packet_num = buffer[0];
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000855
torbjorng7593aad2015-11-19 12:20:51 -0800856 unsigned int rand_state = packet_num;
857 for (size_t i = 1; i < packet_size_; i++) {
858 // This is a simple LC PRNG. Keep in synch with identical code above.
859 rand_state = (rand_state * 251 + 19937) >> 7;
860 ASSERT_EQ(rand_state & 0xff, buffer[i]);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000861 }
862 received_.insert(packet_num);
863 }
864 }
865
nisseef8b61e2016-04-29 06:09:15 -0700866 void TestTransfer(int count) override {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000867 count_ = count;
868
869 WriteData();
870
871 EXPECT_TRUE_WAIT(sent_ == count_, 10000);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100872 RTC_LOG(LS_INFO) << "sent_ == " << sent_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000873
874 if (damage_) {
875 WAIT(false, 2000);
876 EXPECT_EQ(0U, received_.size());
877 } else if (loss_ == 0) {
Yves Gerey665174f2018-06-19 15:03:05 +0200878 EXPECT_EQ_WAIT(static_cast<size_t>(sent_), received_.size(), 1000);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000879 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100880 RTC_LOG(LS_INFO) << "Sent " << sent_ << " packets; received "
881 << received_.size();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000882 }
883 };
884
885 private:
jbauche488a0d2015-11-19 05:17:58 -0800886 BufferQueueStream client_buffer_;
887 BufferQueueStream server_buffer_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000888 size_t packet_size_;
889 int count_;
890 int sent_;
891 std::set<int> received_;
892};
893
Yves Gerey665174f2018-06-19 15:03:05 +0200894rtc::StreamResult SSLDummyStreamBase::Write(const void* data,
895 size_t data_len,
896 size_t* written,
897 int* error) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100898 RTC_LOG(LS_VERBOSE) << "Writing to loopback " << data_len;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000899
900 if (first_packet_) {
901 first_packet_ = false;
jbauche488a0d2015-11-19 05:17:58 -0800902 if (test_base_->GetLoseFirstPacket()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100903 RTC_LOG(LS_INFO) << "Losing initial packet of length " << data_len;
torbjorng7593aad2015-11-19 12:20:51 -0800904 *written = data_len; // Fake successful writing also to writer.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000905 return rtc::SR_SUCCESS;
906 }
907 }
908
jbauche488a0d2015-11-19 05:17:58 -0800909 return test_base_->DataWritten(this, data, data_len, written, error);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000910};
911
912class SSLStreamAdapterTestDTLSFromPEMStrings : public SSLStreamAdapterTestDTLS {
913 public:
Yves Gerey665174f2018-06-19 15:03:05 +0200914 SSLStreamAdapterTestDTLSFromPEMStrings()
915 : SSLStreamAdapterTestDTLS(kCERT_PEM, kRSA_PRIVATE_KEY_PEM) {}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000916};
917
Jian Cui0a8798b2017-11-16 16:58:02 -0800918// Test fixture for certificate chaining. Server will push more than one
919// certificate.
920class SSLStreamAdapterTestDTLSCertChain : public SSLStreamAdapterTestDTLS {
921 public:
922 SSLStreamAdapterTestDTLSCertChain() : SSLStreamAdapterTestDTLS("", ""){};
923 void SetUp() override {
924 CreateStreams();
925
926 client_ssl_.reset(rtc::SSLStreamAdapter::Create(client_stream_));
927 server_ssl_.reset(rtc::SSLStreamAdapter::Create(server_stream_));
928
929 // Set up the slots
930 client_ssl_->SignalEvent.connect(
931 reinterpret_cast<SSLStreamAdapterTestBase*>(this),
932 &SSLStreamAdapterTestBase::OnEvent);
933 server_ssl_->SignalEvent.connect(
934 reinterpret_cast<SSLStreamAdapterTestBase*>(this),
935 &SSLStreamAdapterTestBase::OnEvent);
936
937 if (!client_cert_pem_.empty() && !client_private_key_pem_.empty()) {
938 client_identity_ = rtc::SSLIdentity::FromPEMStrings(
939 client_private_key_pem_, client_cert_pem_);
940 } else {
941 client_identity_ = rtc::SSLIdentity::Generate("client", client_key_type_);
942 }
943
944 client_ssl_->SetIdentity(client_identity_);
945 }
946};
947
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000948// Basic tests: TLS
949
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000950// Test that we can make a handshake work
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +0200951TEST_P(SSLStreamAdapterTestTLS, TestTLSConnect) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000952 TestHandshake();
953};
954
Jian Cui0a8798b2017-11-16 16:58:02 -0800955TEST_P(SSLStreamAdapterTestTLS, GetPeerCertChainWithOneCertificate) {
956 TestHandshake();
957 std::unique_ptr<rtc::SSLCertChain> cert_chain =
958 client_ssl_->GetPeerSSLCertChain();
Jian Cui0a8798b2017-11-16 16:58:02 -0800959 ASSERT_NE(nullptr, cert_chain);
960 EXPECT_EQ(1u, cert_chain->GetSize());
Taylor Brandstetterc3928662018-02-23 13:04:51 -0800961 EXPECT_EQ(cert_chain->Get(0).ToPEMString(),
962 server_identity_->certificate().ToPEMString());
Jian Cui0a8798b2017-11-16 16:58:02 -0800963}
964
965TEST_F(SSLStreamAdapterTestDTLSCertChain, TwoCertHandshake) {
966 server_identity_ = rtc::SSLIdentity::FromPEMChainStrings(
967 kRSA_PRIVATE_KEY_PEM, std::string(kCERT_PEM) + kCACert);
968 server_ssl_->SetIdentity(server_identity_);
969 TestHandshake();
970 std::unique_ptr<rtc::SSLCertChain> peer_cert_chain =
971 client_ssl_->GetPeerSSLCertChain();
972 ASSERT_NE(nullptr, peer_cert_chain);
973 ASSERT_EQ(2u, peer_cert_chain->GetSize());
974 EXPECT_EQ(kCERT_PEM, peer_cert_chain->Get(0).ToPEMString());
975 EXPECT_EQ(kCACert, peer_cert_chain->Get(1).ToPEMString());
976}
977
David Benjaminea84b6b2017-12-01 17:25:45 -0500978TEST_F(SSLStreamAdapterTestDTLSCertChain, TwoCertHandshakeWithCopy) {
979 std::unique_ptr<rtc::SSLIdentity> identity(
980 rtc::SSLIdentity::FromPEMChainStrings(kRSA_PRIVATE_KEY_PEM,
981 std::string(kCERT_PEM) + kCACert));
982 server_identity_ = identity->GetReference();
983 server_ssl_->SetIdentity(server_identity_);
984 TestHandshake();
985 std::unique_ptr<rtc::SSLCertChain> peer_cert_chain =
986 client_ssl_->GetPeerSSLCertChain();
987 ASSERT_NE(nullptr, peer_cert_chain);
988 ASSERT_EQ(2u, peer_cert_chain->GetSize());
989 EXPECT_EQ(kCERT_PEM, peer_cert_chain->Get(0).ToPEMString());
990 EXPECT_EQ(kCACert, peer_cert_chain->Get(1).ToPEMString());
991}
992
Jian Cui0a8798b2017-11-16 16:58:02 -0800993TEST_F(SSLStreamAdapterTestDTLSCertChain, ThreeCertHandshake) {
994 server_identity_ = rtc::SSLIdentity::FromPEMChainStrings(
995 kRSA_PRIVATE_KEY_PEM, std::string(kCERT_PEM) + kIntCert1 + kCACert);
996 server_ssl_->SetIdentity(server_identity_);
997 TestHandshake();
998 std::unique_ptr<rtc::SSLCertChain> peer_cert_chain =
999 client_ssl_->GetPeerSSLCertChain();
1000 ASSERT_NE(nullptr, peer_cert_chain);
1001 ASSERT_EQ(3u, peer_cert_chain->GetSize());
1002 EXPECT_EQ(kCERT_PEM, peer_cert_chain->Get(0).ToPEMString());
1003 EXPECT_EQ(kIntCert1, peer_cert_chain->Get(1).ToPEMString());
1004 EXPECT_EQ(kCACert, peer_cert_chain->Get(2).ToPEMString());
1005}
1006
jiayl@webrtc.orgf1d751c2014-09-25 16:38:46 +00001007// Test that closing the connection on one side updates the other side.
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +02001008TEST_P(SSLStreamAdapterTestTLS, TestTLSClose) {
jiayl@webrtc.orgf1d751c2014-09-25 16:38:46 +00001009 TestHandshake();
1010 client_ssl_->Close();
1011 EXPECT_EQ_WAIT(rtc::SS_CLOSED, server_ssl_->GetState(), handshake_wait_);
1012};
1013
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001014// Test transfer -- trivial
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +02001015TEST_P(SSLStreamAdapterTestTLS, TestTLSTransfer) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001016 TestHandshake();
1017 TestTransfer(100000);
1018};
1019
1020// Test read-write after close.
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +02001021TEST_P(SSLStreamAdapterTestTLS, ReadWriteAfterClose) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001022 TestHandshake();
1023 TestTransfer(100000);
1024 client_ssl_->Close();
1025
1026 rtc::StreamResult rv;
1027 char block[kBlockSize];
1028 size_t dummy;
1029
1030 // It's an error to write after closed.
deadbeef37f5ecf2017-02-27 14:06:41 -08001031 rv = client_ssl_->Write(block, sizeof(block), &dummy, nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001032 ASSERT_EQ(rtc::SR_ERROR, rv);
1033
1034 // But after closed read gives you EOS.
deadbeef37f5ecf2017-02-27 14:06:41 -08001035 rv = client_ssl_->Read(block, sizeof(block), &dummy, nullptr);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001036 ASSERT_EQ(rtc::SR_EOS, rv);
1037};
1038
1039// Test a handshake with a bogus peer digest
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +02001040TEST_P(SSLStreamAdapterTestTLS, TestTLSBogusDigest) {
deadbeef89824f62016-09-30 11:55:43 -07001041 SetPeerIdentitiesByDigest(false, true);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001042 TestHandshake(false);
1043};
1044
deadbeef89824f62016-09-30 11:55:43 -07001045TEST_P(SSLStreamAdapterTestTLS, TestTLSDelayedIdentity) {
1046 TestHandshakeWithDelayedIdentity(true);
1047};
1048
1049TEST_P(SSLStreamAdapterTestTLS, TestTLSDelayedIdentityWithBogusDigest) {
1050 TestHandshakeWithDelayedIdentity(false);
1051};
1052
1053// Test that the correct error is returned when SetPeerCertificateDigest is
1054// called with an unknown algorithm.
1055TEST_P(SSLStreamAdapterTestTLS,
1056 TestSetPeerCertificateDigestWithUnknownAlgorithm) {
1057 unsigned char server_digest[20];
1058 size_t server_digest_len;
1059 bool rv;
1060 rtc::SSLPeerCertificateDigestError err;
1061
1062 rv = server_identity_->certificate().ComputeDigest(
1063 rtc::DIGEST_SHA_1, server_digest, 20, &server_digest_len);
1064 ASSERT_TRUE(rv);
1065
1066 rv = client_ssl_->SetPeerCertificateDigest("unknown algorithm", server_digest,
1067 server_digest_len, &err);
1068 EXPECT_EQ(rtc::SSLPeerCertificateDigestError::UNKNOWN_ALGORITHM, err);
1069 EXPECT_FALSE(rv);
1070}
1071
1072// Test that the correct error is returned when SetPeerCertificateDigest is
1073// called with an invalid digest length.
1074TEST_P(SSLStreamAdapterTestTLS, TestSetPeerCertificateDigestWithInvalidLength) {
1075 unsigned char server_digest[20];
1076 size_t server_digest_len;
1077 bool rv;
1078 rtc::SSLPeerCertificateDigestError err;
1079
1080 rv = server_identity_->certificate().ComputeDigest(
1081 rtc::DIGEST_SHA_1, server_digest, 20, &server_digest_len);
1082 ASSERT_TRUE(rv);
1083
1084 rv = client_ssl_->SetPeerCertificateDigest(rtc::DIGEST_SHA_1, server_digest,
1085 server_digest_len - 1, &err);
1086 EXPECT_EQ(rtc::SSLPeerCertificateDigestError::INVALID_LENGTH, err);
1087 EXPECT_FALSE(rv);
1088}
1089
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001090// Test moving a bunch of data
1091
1092// Basic tests: DTLS
1093// Test that we can make a handshake work
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +02001094TEST_P(SSLStreamAdapterTestDTLS, TestDTLSConnect) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001095 TestHandshake();
1096};
1097
1098// Test that we can make a handshake work if the first packet in
1099// each direction is lost. This gives us predictable loss
1100// rather than having to tune random
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +02001101TEST_P(SSLStreamAdapterTestDTLS, TestDTLSConnectWithLostFirstPacket) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001102 SetLoseFirstPacket(true);
1103 TestHandshake();
1104};
1105
1106// Test a handshake with loss and delay
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +02001107TEST_P(SSLStreamAdapterTestDTLS, TestDTLSConnectWithLostFirstPacketDelay2s) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001108 SetLoseFirstPacket(true);
1109 SetDelay(2000);
1110 SetHandshakeWait(20000);
1111 TestHandshake();
1112};
1113
1114// Test a handshake with small MTU
pbos@webrtc.org127ca3f2014-10-09 07:52:03 +00001115// Disabled due to https://code.google.com/p/webrtc/issues/detail?id=3910
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +02001116TEST_P(SSLStreamAdapterTestDTLS, DISABLED_TestDTLSConnectWithSmallMtu) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001117 SetMtu(700);
1118 SetHandshakeWait(20000);
1119 TestHandshake();
1120};
1121
1122// Test transfer -- trivial
jbauche488a0d2015-11-19 05:17:58 -08001123TEST_P(SSLStreamAdapterTestDTLS, TestDTLSTransfer) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001124 TestHandshake();
1125 TestTransfer(100);
1126};
1127
jbauche488a0d2015-11-19 05:17:58 -08001128TEST_P(SSLStreamAdapterTestDTLS, TestDTLSTransferWithLoss) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001129 TestHandshake();
1130 SetLoss(10);
1131 TestTransfer(100);
1132};
1133
jbauche488a0d2015-11-19 05:17:58 -08001134TEST_P(SSLStreamAdapterTestDTLS, TestDTLSTransferWithDamage) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001135 SetDamage(); // Must be called first because first packet
1136 // write happens at end of handshake.
1137 TestHandshake();
1138 TestTransfer(100);
1139};
1140
deadbeef89824f62016-09-30 11:55:43 -07001141TEST_P(SSLStreamAdapterTestDTLS, TestDTLSDelayedIdentity) {
1142 TestHandshakeWithDelayedIdentity(true);
1143};
1144
1145TEST_P(SSLStreamAdapterTestDTLS, TestDTLSDelayedIdentityWithBogusDigest) {
1146 TestHandshakeWithDelayedIdentity(false);
1147};
1148
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001149// Test DTLS-SRTP with all high ciphers
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +02001150TEST_P(SSLStreamAdapterTestDTLS, TestDTLSSrtpHigh) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001151 std::vector<int> high;
1152 high.push_back(rtc::SRTP_AES128_CM_SHA1_80);
1153 SetDtlsSrtpCryptoSuites(high, true);
1154 SetDtlsSrtpCryptoSuites(high, false);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001155 TestHandshake();
1156
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001157 int client_cipher;
1158 ASSERT_TRUE(GetDtlsSrtpCryptoSuite(true, &client_cipher));
1159 int server_cipher;
1160 ASSERT_TRUE(GetDtlsSrtpCryptoSuite(false, &server_cipher));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001161
1162 ASSERT_EQ(client_cipher, server_cipher);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001163 ASSERT_EQ(client_cipher, rtc::SRTP_AES128_CM_SHA1_80);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001164};
1165
1166// Test DTLS-SRTP with all low ciphers
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +02001167TEST_P(SSLStreamAdapterTestDTLS, TestDTLSSrtpLow) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001168 std::vector<int> low;
1169 low.push_back(rtc::SRTP_AES128_CM_SHA1_32);
1170 SetDtlsSrtpCryptoSuites(low, true);
1171 SetDtlsSrtpCryptoSuites(low, false);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001172 TestHandshake();
1173
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001174 int client_cipher;
1175 ASSERT_TRUE(GetDtlsSrtpCryptoSuite(true, &client_cipher));
1176 int server_cipher;
1177 ASSERT_TRUE(GetDtlsSrtpCryptoSuite(false, &server_cipher));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001178
1179 ASSERT_EQ(client_cipher, server_cipher);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001180 ASSERT_EQ(client_cipher, rtc::SRTP_AES128_CM_SHA1_32);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001181};
1182
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001183// Test DTLS-SRTP with a mismatch -- should not converge
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +02001184TEST_P(SSLStreamAdapterTestDTLS, TestDTLSSrtpHighLow) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001185 std::vector<int> high;
1186 high.push_back(rtc::SRTP_AES128_CM_SHA1_80);
1187 std::vector<int> low;
1188 low.push_back(rtc::SRTP_AES128_CM_SHA1_32);
1189 SetDtlsSrtpCryptoSuites(high, true);
1190 SetDtlsSrtpCryptoSuites(low, false);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001191 TestHandshake();
1192
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001193 int client_cipher;
1194 ASSERT_FALSE(GetDtlsSrtpCryptoSuite(true, &client_cipher));
1195 int server_cipher;
1196 ASSERT_FALSE(GetDtlsSrtpCryptoSuite(false, &server_cipher));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001197};
1198
1199// Test DTLS-SRTP with each side being mixed -- should select high
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +02001200TEST_P(SSLStreamAdapterTestDTLS, TestDTLSSrtpMixed) {
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001201 std::vector<int> mixed;
1202 mixed.push_back(rtc::SRTP_AES128_CM_SHA1_80);
1203 mixed.push_back(rtc::SRTP_AES128_CM_SHA1_32);
1204 SetDtlsSrtpCryptoSuites(mixed, true);
1205 SetDtlsSrtpCryptoSuites(mixed, false);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001206 TestHandshake();
1207
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001208 int client_cipher;
1209 ASSERT_TRUE(GetDtlsSrtpCryptoSuite(true, &client_cipher));
1210 int server_cipher;
1211 ASSERT_TRUE(GetDtlsSrtpCryptoSuite(false, &server_cipher));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001212
1213 ASSERT_EQ(client_cipher, server_cipher);
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -08001214 ASSERT_EQ(client_cipher, rtc::SRTP_AES128_CM_SHA1_80);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001215};
1216
jbauchcb560652016-08-04 05:20:32 -07001217// Test DTLS-SRTP with all GCM-128 ciphers.
1218TEST_P(SSLStreamAdapterTestDTLS, TestDTLSSrtpGCM128) {
jbauchcb560652016-08-04 05:20:32 -07001219 std::vector<int> gcm128;
1220 gcm128.push_back(rtc::SRTP_AEAD_AES_128_GCM);
1221 SetDtlsSrtpCryptoSuites(gcm128, true);
1222 SetDtlsSrtpCryptoSuites(gcm128, false);
1223 TestHandshake();
1224
1225 int client_cipher;
1226 ASSERT_TRUE(GetDtlsSrtpCryptoSuite(true, &client_cipher));
1227 int server_cipher;
1228 ASSERT_TRUE(GetDtlsSrtpCryptoSuite(false, &server_cipher));
1229
1230 ASSERT_EQ(client_cipher, server_cipher);
1231 ASSERT_EQ(client_cipher, rtc::SRTP_AEAD_AES_128_GCM);
1232};
1233
1234// Test DTLS-SRTP with all GCM-256 ciphers.
1235TEST_P(SSLStreamAdapterTestDTLS, TestDTLSSrtpGCM256) {
jbauchcb560652016-08-04 05:20:32 -07001236 std::vector<int> gcm256;
1237 gcm256.push_back(rtc::SRTP_AEAD_AES_256_GCM);
1238 SetDtlsSrtpCryptoSuites(gcm256, true);
1239 SetDtlsSrtpCryptoSuites(gcm256, false);
1240 TestHandshake();
1241
1242 int client_cipher;
1243 ASSERT_TRUE(GetDtlsSrtpCryptoSuite(true, &client_cipher));
1244 int server_cipher;
1245 ASSERT_TRUE(GetDtlsSrtpCryptoSuite(false, &server_cipher));
1246
1247 ASSERT_EQ(client_cipher, server_cipher);
1248 ASSERT_EQ(client_cipher, rtc::SRTP_AEAD_AES_256_GCM);
1249};
1250
1251// Test DTLS-SRTP with mixed GCM-128/-256 ciphers -- should not converge.
1252TEST_P(SSLStreamAdapterTestDTLS, TestDTLSSrtpGCMMismatch) {
jbauchcb560652016-08-04 05:20:32 -07001253 std::vector<int> gcm128;
1254 gcm128.push_back(rtc::SRTP_AEAD_AES_128_GCM);
1255 std::vector<int> gcm256;
1256 gcm256.push_back(rtc::SRTP_AEAD_AES_256_GCM);
1257 SetDtlsSrtpCryptoSuites(gcm128, true);
1258 SetDtlsSrtpCryptoSuites(gcm256, false);
1259 TestHandshake();
1260
1261 int client_cipher;
1262 ASSERT_FALSE(GetDtlsSrtpCryptoSuite(true, &client_cipher));
1263 int server_cipher;
1264 ASSERT_FALSE(GetDtlsSrtpCryptoSuite(false, &server_cipher));
1265};
1266
1267// Test DTLS-SRTP with both GCM-128/-256 ciphers -- should select GCM-256.
1268TEST_P(SSLStreamAdapterTestDTLS, TestDTLSSrtpGCMMixed) {
jbauchcb560652016-08-04 05:20:32 -07001269 std::vector<int> gcmBoth;
1270 gcmBoth.push_back(rtc::SRTP_AEAD_AES_256_GCM);
1271 gcmBoth.push_back(rtc::SRTP_AEAD_AES_128_GCM);
1272 SetDtlsSrtpCryptoSuites(gcmBoth, true);
1273 SetDtlsSrtpCryptoSuites(gcmBoth, false);
1274 TestHandshake();
1275
1276 int client_cipher;
1277 ASSERT_TRUE(GetDtlsSrtpCryptoSuite(true, &client_cipher));
1278 int server_cipher;
1279 ASSERT_TRUE(GetDtlsSrtpCryptoSuite(false, &server_cipher));
1280
1281 ASSERT_EQ(client_cipher, server_cipher);
1282 ASSERT_EQ(client_cipher, rtc::SRTP_AEAD_AES_256_GCM);
1283};
1284
1285// Test SRTP cipher suite lengths.
1286TEST_P(SSLStreamAdapterTestDTLS, TestDTLSSrtpKeyAndSaltLengths) {
1287 int key_len;
1288 int salt_len;
1289
Yves Gerey665174f2018-06-19 15:03:05 +02001290 ASSERT_FALSE(rtc::GetSrtpKeyAndSaltLengths(rtc::SRTP_INVALID_CRYPTO_SUITE,
1291 &key_len, &salt_len));
jbauchcb560652016-08-04 05:20:32 -07001292
Yves Gerey665174f2018-06-19 15:03:05 +02001293 ASSERT_TRUE(rtc::GetSrtpKeyAndSaltLengths(rtc::SRTP_AES128_CM_SHA1_32,
1294 &key_len, &salt_len));
1295 ASSERT_EQ(128 / 8, key_len);
1296 ASSERT_EQ(112 / 8, salt_len);
jbauchcb560652016-08-04 05:20:32 -07001297
Yves Gerey665174f2018-06-19 15:03:05 +02001298 ASSERT_TRUE(rtc::GetSrtpKeyAndSaltLengths(rtc::SRTP_AES128_CM_SHA1_80,
1299 &key_len, &salt_len));
1300 ASSERT_EQ(128 / 8, key_len);
1301 ASSERT_EQ(112 / 8, salt_len);
jbauchcb560652016-08-04 05:20:32 -07001302
Yves Gerey665174f2018-06-19 15:03:05 +02001303 ASSERT_TRUE(rtc::GetSrtpKeyAndSaltLengths(rtc::SRTP_AEAD_AES_128_GCM,
1304 &key_len, &salt_len));
1305 ASSERT_EQ(128 / 8, key_len);
1306 ASSERT_EQ(96 / 8, salt_len);
jbauchcb560652016-08-04 05:20:32 -07001307
Yves Gerey665174f2018-06-19 15:03:05 +02001308 ASSERT_TRUE(rtc::GetSrtpKeyAndSaltLengths(rtc::SRTP_AEAD_AES_256_GCM,
1309 &key_len, &salt_len));
1310 ASSERT_EQ(256 / 8, key_len);
1311 ASSERT_EQ(96 / 8, salt_len);
jbauchcb560652016-08-04 05:20:32 -07001312};
1313
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001314// Test an exporter
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +02001315TEST_P(SSLStreamAdapterTestDTLS, TestDTLSExporter) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001316 TestHandshake();
1317 unsigned char client_out[20];
1318 unsigned char server_out[20];
1319
1320 bool result;
Yves Gerey665174f2018-06-19 15:03:05 +02001321 result = ExportKeyingMaterial(kExporterLabel, kExporterContext,
1322 kExporterContextLen, true, true, client_out,
1323 sizeof(client_out));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001324 ASSERT_TRUE(result);
1325
Yves Gerey665174f2018-06-19 15:03:05 +02001326 result = ExportKeyingMaterial(kExporterLabel, kExporterContext,
1327 kExporterContextLen, true, false, server_out,
1328 sizeof(server_out));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001329 ASSERT_TRUE(result);
1330
1331 ASSERT_TRUE(!memcmp(client_out, server_out, sizeof(client_out)));
1332}
1333
1334// Test not yet valid certificates are not rejected.
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +02001335TEST_P(SSLStreamAdapterTestDTLS, TestCertNotYetValid) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001336 long one_day = 60 * 60 * 24;
1337 // Make the certificates not valid until one day later.
1338 ResetIdentitiesWithValidity(one_day, one_day);
1339 TestHandshake();
1340}
1341
1342// Test expired certificates are not rejected.
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +02001343TEST_P(SSLStreamAdapterTestDTLS, TestCertExpired) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001344 long one_day = 60 * 60 * 24;
1345 // Make the certificates already expired.
1346 ResetIdentitiesWithValidity(-one_day, -one_day);
1347 TestHandshake();
1348}
1349
1350// Test data transfer using certs created from strings.
torbjorng7593aad2015-11-19 12:20:51 -08001351TEST_F(SSLStreamAdapterTestDTLSFromPEMStrings, TestTransfer) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001352 TestHandshake();
1353 TestTransfer(100);
1354}
1355
1356// Test getting the remote certificate.
torbjorng7593aad2015-11-19 12:20:51 -08001357TEST_F(SSLStreamAdapterTestDTLSFromPEMStrings, TestDTLSGetPeerCertificate) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001358 // Peer certificates haven't been received yet.
kwibergb4d01c42016-04-06 05:15:06 -07001359 ASSERT_FALSE(GetPeerCertificate(true));
1360 ASSERT_FALSE(GetPeerCertificate(false));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001361
1362 TestHandshake();
1363
1364 // The client should have a peer certificate after the handshake.
jbauch555604a2016-04-26 03:13:22 -07001365 std::unique_ptr<rtc::SSLCertificate> client_peer_cert =
kwibergb4d01c42016-04-06 05:15:06 -07001366 GetPeerCertificate(true);
1367 ASSERT_TRUE(client_peer_cert);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001368
1369 // It's not kCERT_PEM.
1370 std::string client_peer_string = client_peer_cert->ToPEMString();
1371 ASSERT_NE(kCERT_PEM, client_peer_string);
1372
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001373 // The server should have a peer certificate after the handshake.
jbauch555604a2016-04-26 03:13:22 -07001374 std::unique_ptr<rtc::SSLCertificate> server_peer_cert =
kwibergb4d01c42016-04-06 05:15:06 -07001375 GetPeerCertificate(false);
1376 ASSERT_TRUE(server_peer_cert);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001377
1378 // It's kCERT_PEM
1379 ASSERT_EQ(kCERT_PEM, server_peer_cert->ToPEMString());
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001380}
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +00001381
1382// Test getting the used DTLS ciphers.
Joachim Bauch831c5582015-05-20 12:48:41 +02001383// DTLS 1.2 enabled for neither client nor server -> DTLS 1.0 will be used.
torbjorng43166b82016-03-11 00:06:47 -08001384TEST_P(SSLStreamAdapterTestDTLS, TestGetSslCipherSuite) {
Joachim Bauch831c5582015-05-20 12:48:41 +02001385 SetupProtocolVersions(rtc::SSL_PROTOCOL_DTLS_10, rtc::SSL_PROTOCOL_DTLS_10);
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +00001386 TestHandshake();
1387
Guo-wei Shieh6caafbe2015-10-05 12:43:27 -07001388 int client_cipher;
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001389 ASSERT_TRUE(GetSslCipherSuite(true, &client_cipher));
Guo-wei Shieh6caafbe2015-10-05 12:43:27 -07001390 int server_cipher;
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001391 ASSERT_TRUE(GetSslCipherSuite(false, &server_cipher));
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +00001392
torbjorng43166b82016-03-11 00:06:47 -08001393 ASSERT_EQ(rtc::SSL_PROTOCOL_DTLS_10, GetSslVersion(true));
1394 ASSERT_EQ(rtc::SSL_PROTOCOL_DTLS_10, GetSslVersion(false));
1395
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +00001396 ASSERT_EQ(client_cipher, server_cipher);
torbjorng43166b82016-03-11 00:06:47 -08001397 ASSERT_TRUE(rtc::SSLStreamAdapter::IsAcceptableCipher(
1398 server_cipher, ::testing::get<1>(GetParam()).type()));
Joachim Bauch831c5582015-05-20 12:48:41 +02001399}
1400
1401// Test getting the used DTLS 1.2 ciphers.
1402// DTLS 1.2 enabled for client and server -> DTLS 1.2 will be used.
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001403TEST_P(SSLStreamAdapterTestDTLS, TestGetSslCipherSuiteDtls12Both) {
Joachim Bauch831c5582015-05-20 12:48:41 +02001404 SetupProtocolVersions(rtc::SSL_PROTOCOL_DTLS_12, rtc::SSL_PROTOCOL_DTLS_12);
1405 TestHandshake();
1406
Guo-wei Shieh6caafbe2015-10-05 12:43:27 -07001407 int client_cipher;
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001408 ASSERT_TRUE(GetSslCipherSuite(true, &client_cipher));
Guo-wei Shieh6caafbe2015-10-05 12:43:27 -07001409 int server_cipher;
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001410 ASSERT_TRUE(GetSslCipherSuite(false, &server_cipher));
Joachim Bauch831c5582015-05-20 12:48:41 +02001411
torbjorng43166b82016-03-11 00:06:47 -08001412 ASSERT_EQ(rtc::SSL_PROTOCOL_DTLS_12, GetSslVersion(true));
1413 ASSERT_EQ(rtc::SSL_PROTOCOL_DTLS_12, GetSslVersion(false));
1414
Joachim Bauch831c5582015-05-20 12:48:41 +02001415 ASSERT_EQ(client_cipher, server_cipher);
torbjorng43166b82016-03-11 00:06:47 -08001416 ASSERT_TRUE(rtc::SSLStreamAdapter::IsAcceptableCipher(
1417 server_cipher, ::testing::get<1>(GetParam()).type()));
Joachim Bauch831c5582015-05-20 12:48:41 +02001418}
1419
1420// DTLS 1.2 enabled for client only -> DTLS 1.0 will be used.
torbjorng43166b82016-03-11 00:06:47 -08001421TEST_P(SSLStreamAdapterTestDTLS, TestGetSslCipherSuiteDtls12Client) {
Joachim Bauch831c5582015-05-20 12:48:41 +02001422 SetupProtocolVersions(rtc::SSL_PROTOCOL_DTLS_10, rtc::SSL_PROTOCOL_DTLS_12);
1423 TestHandshake();
1424
Guo-wei Shieh6caafbe2015-10-05 12:43:27 -07001425 int client_cipher;
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001426 ASSERT_TRUE(GetSslCipherSuite(true, &client_cipher));
Guo-wei Shieh6caafbe2015-10-05 12:43:27 -07001427 int server_cipher;
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001428 ASSERT_TRUE(GetSslCipherSuite(false, &server_cipher));
Joachim Bauch831c5582015-05-20 12:48:41 +02001429
torbjorng43166b82016-03-11 00:06:47 -08001430 ASSERT_EQ(rtc::SSL_PROTOCOL_DTLS_10, GetSslVersion(true));
1431 ASSERT_EQ(rtc::SSL_PROTOCOL_DTLS_10, GetSslVersion(false));
1432
Joachim Bauch831c5582015-05-20 12:48:41 +02001433 ASSERT_EQ(client_cipher, server_cipher);
torbjorng43166b82016-03-11 00:06:47 -08001434 ASSERT_TRUE(rtc::SSLStreamAdapter::IsAcceptableCipher(
1435 server_cipher, ::testing::get<1>(GetParam()).type()));
Joachim Bauch831c5582015-05-20 12:48:41 +02001436}
1437
1438// DTLS 1.2 enabled for server only -> DTLS 1.0 will be used.
torbjorng43166b82016-03-11 00:06:47 -08001439TEST_P(SSLStreamAdapterTestDTLS, TestGetSslCipherSuiteDtls12Server) {
Joachim Bauch831c5582015-05-20 12:48:41 +02001440 SetupProtocolVersions(rtc::SSL_PROTOCOL_DTLS_12, rtc::SSL_PROTOCOL_DTLS_10);
1441 TestHandshake();
1442
Guo-wei Shieh6caafbe2015-10-05 12:43:27 -07001443 int client_cipher;
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001444 ASSERT_TRUE(GetSslCipherSuite(true, &client_cipher));
Guo-wei Shieh6caafbe2015-10-05 12:43:27 -07001445 int server_cipher;
Guo-wei Shieh456696a2015-09-30 21:48:54 -07001446 ASSERT_TRUE(GetSslCipherSuite(false, &server_cipher));
Joachim Bauch831c5582015-05-20 12:48:41 +02001447
torbjorng43166b82016-03-11 00:06:47 -08001448 ASSERT_EQ(rtc::SSL_PROTOCOL_DTLS_10, GetSslVersion(true));
1449 ASSERT_EQ(rtc::SSL_PROTOCOL_DTLS_10, GetSslVersion(false));
1450
Joachim Bauch831c5582015-05-20 12:48:41 +02001451 ASSERT_EQ(client_cipher, server_cipher);
torbjorng43166b82016-03-11 00:06:47 -08001452 ASSERT_TRUE(rtc::SSLStreamAdapter::IsAcceptableCipher(
1453 server_cipher, ::testing::get<1>(GetParam()).type()));
pthatcher@webrtc.org3ee4fe52015-02-11 22:34:36 +00001454}
Torbjorn Granlundb6d4ec42015-08-17 14:08:59 +02001455
torbjorng4e572472015-10-08 09:42:49 -07001456// The RSA keysizes here might look strange, why not include the RFC's size
1457// 2048?. The reason is test case slowness; testing two sizes to exercise
1458// parametrization is sufficient.
1459INSTANTIATE_TEST_CASE_P(
1460 SSLStreamAdapterTestsTLS,
1461 SSLStreamAdapterTestTLS,
1462 Combine(Values(rtc::KeyParams::RSA(1024, 65537),
1463 rtc::KeyParams::RSA(1152, 65537),
1464 rtc::KeyParams::ECDSA(rtc::EC_NIST_P256)),
1465 Values(rtc::KeyParams::RSA(1024, 65537),
1466 rtc::KeyParams::RSA(1152, 65537),
1467 rtc::KeyParams::ECDSA(rtc::EC_NIST_P256))));
1468INSTANTIATE_TEST_CASE_P(
1469 SSLStreamAdapterTestsDTLS,
1470 SSLStreamAdapterTestDTLS,
1471 Combine(Values(rtc::KeyParams::RSA(1024, 65537),
1472 rtc::KeyParams::RSA(1152, 65537),
1473 rtc::KeyParams::ECDSA(rtc::EC_NIST_P256)),
1474 Values(rtc::KeyParams::RSA(1024, 65537),
1475 rtc::KeyParams::RSA(1152, 65537),
1476 rtc::KeyParams::ECDSA(rtc::EC_NIST_P256))));