3 * Copyright 2011, Google Inc.
4 * Portions Copyright 2011, RTFM, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
20 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include "talk/base/gunit.h"
35 #include "talk/base/helpers.h"
36 #include "talk/base/scoped_ptr.h"
37 #include "talk/base/ssladapter.h"
38 #include "talk/base/sslconfig.h"
39 #include "talk/base/sslidentity.h"
40 #include "talk/base/sslstreamadapter.h"
41 #include "talk/base/stream.h"
43 static const int kBlockSize = 4096;
44 static const char kAES_CM_HMAC_SHA1_80[] = "AES_CM_128_HMAC_SHA1_80";
45 static const char kAES_CM_HMAC_SHA1_32[] = "AES_CM_128_HMAC_SHA1_32";
46 static const char kExporterLabel[] = "label";
47 static const unsigned char kExporterContext[] = "context";
48 static int kExporterContextLen = sizeof(kExporterContext);
50 static const char kRSA_PRIVATE_KEY_PEM[] =
51 "-----BEGIN RSA PRIVATE KEY-----\n"
52 "MIICXQIBAAKBgQDCueE4a9hDMZ3sbVZdlXOz9ZA+cvzie3zJ9gXnT/BCt9P4b9HE\n"
53 "vD/tr73YBqD3Wr5ZWScmyGYF9EMn0r3rzBxv6oooLU5TdUvOm4rzUjkCLQaQML8o\n"
54 "NxXq+qW/j3zUKGikLhaaAl/amaX2zSWUsRQ1CpngQ3+tmDNH4/25TncNmQIDAQAB\n"
55 "AoGAUcuU0Id0k10fMjYHZk4mCPzot2LD2Tr4Aznl5vFMQipHzv7hhZtx2xzMSRcX\n"
56 "vG+Qr6VkbcUWHgApyWubvZXCh3+N7Vo2aYdMAQ8XqmFpBdIrL5CVdVfqFfEMlgEy\n"
57 "LSZNG5klnrIfl3c7zQVovLr4eMqyl2oGfAqPQz75+fecv1UCQQD6wNHch9NbAG1q\n"
58 "yuFEhMARB6gDXb+5SdzFjjtTWW5uJfm4DcZLoYyaIZm0uxOwsUKd0Rsma+oGitS1\n"
59 "CXmuqfpPAkEAxszyN3vIdpD44SREEtyKZBMNOk5pEIIGdbeMJC5/XHvpxww9xkoC\n"
60 "+39NbvUZYd54uT+rafbx4QZKc0h9xA/HlwJBAL37lYVWy4XpPv1olWCKi9LbUCqs\n"
61 "vvQtyD1N1BkEayy9TQRsO09WKOcmigRqsTJwOx7DLaTgokEuspYvhagWVPUCQE/y\n"
62 "0+YkTbYBD1Xbs9SyBKXCU6uDJRWSdO6aZi2W1XloC9gUwDMiSJjD1Wwt/YsyYPJ+\n"
63 "/Hyc5yFL2l0KZimW/vkCQQCjuZ/lPcH46EuzhdbRfumDOG5N3ld7UhGI1TIRy17W\n"
64 "dGF90cG33/L6BfS8Ll+fkkW/2AMRk8FDvF4CZi2nfW4L\n"
65 "-----END RSA PRIVATE KEY-----\n";
67 static const char kCERT_PEM[] =
68 "-----BEGIN CERTIFICATE-----\n"
69 "MIIBmTCCAQICCQCPNJORW/M13DANBgkqhkiG9w0BAQUFADARMQ8wDQYDVQQDDAZ3\n"
70 "ZWJydGMwHhcNMTMwNjE0MjIzMDAxWhcNMTQwNjE0MjIzMDAxWjARMQ8wDQYDVQQD\n"
71 "DAZ3ZWJydGMwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMK54Thr2EMxnext\n"
72 "Vl2Vc7P1kD5y/OJ7fMn2BedP8EK30/hv0cS8P+2vvdgGoPdavllZJybIZgX0QyfS\n"
73 "vevMHG/qiigtTlN1S86bivNSOQItBpAwvyg3Fer6pb+PfNQoaKQuFpoCX9qZpfbN\n"
74 "JZSxFDUKmeBDf62YM0fj/blOdw2ZAgMBAAEwDQYJKoZIhvcNAQEFBQADgYEAECMt\n"
75 "UZb35H8TnjGx4XPzco/kbnurMLFFWcuve/DwTsuf10Ia9N4md8LY0UtgIgtyNqWc\n"
76 "ZwyRMwxONF6ty3wcaIiPbGqiAa55T3YRuPibkRmck9CjrmM9JAtyvqHnpHd2TsBD\n"
77 "qCV42aXS3onOXDQ1ibuWq0fr0//aj0wo4KV474c=\n"
78 "-----END CERTIFICATE-----\n";
80 #define MAYBE_SKIP_TEST(feature) \
81 if (!(talk_base::SSLStreamAdapter::feature())) { \
82 LOG(LS_INFO) << "Feature disabled... skipping"; \
86 class SSLStreamAdapterTestBase;
88 class SSLDummyStream : public talk_base::StreamInterface,
89 public sigslot::has_slots<> {
91 explicit SSLDummyStream(SSLStreamAdapterTestBase *test,
92 const std::string &side,
93 talk_base::FifoBuffer *in,
94 talk_base::FifoBuffer *out) :
100 in_->SignalEvent.connect(this, &SSLDummyStream::OnEventIn);
101 out_->SignalEvent.connect(this, &SSLDummyStream::OnEventOut);
104 virtual talk_base::StreamState GetState() const { return talk_base::SS_OPEN; }
106 virtual talk_base::StreamResult Read(void* buffer, size_t buffer_len,
107 size_t* read, int* error) {
108 talk_base::StreamResult r;
110 r = in_->Read(buffer, buffer_len, read, error);
111 if (r == talk_base::SR_BLOCK)
112 return talk_base::SR_BLOCK;
113 if (r == talk_base::SR_EOS)
114 return talk_base::SR_EOS;
116 if (r != talk_base::SR_SUCCESS) {
118 return talk_base::SR_ERROR;
121 return talk_base::SR_SUCCESS;
124 // Catch readability events on in and pass them up.
125 virtual void OnEventIn(talk_base::StreamInterface *stream, int sig,
127 int mask = (talk_base::SE_READ | talk_base::SE_CLOSE);
130 LOG(LS_INFO) << "SSLDummyStream::OnEvent side=" << side_ << " sig="
131 << sig << " forwarding upward";
132 PostEvent(sig & mask, 0);
136 // Catch writeability events on out and pass them up.
137 virtual void OnEventOut(talk_base::StreamInterface *stream, int sig,
139 if (sig & talk_base::SE_WRITE) {
140 LOG(LS_INFO) << "SSLDummyStream::OnEvent side=" << side_ << " sig="
141 << sig << " forwarding upward";
143 PostEvent(sig & talk_base::SE_WRITE, 0);
147 // Write to the outgoing FifoBuffer
148 talk_base::StreamResult WriteData(const void* data, size_t data_len,
149 size_t* written, int* error) {
150 return out_->Write(data, data_len, written, error);
154 virtual talk_base::StreamResult Write(const void* data, size_t data_len,
155 size_t* written, int* error);
157 virtual void Close() {
158 LOG(LS_INFO) << "Closing outbound stream";
163 SSLStreamAdapterTestBase *test_;
164 const std::string side_;
165 talk_base::FifoBuffer *in_;
166 talk_base::FifoBuffer *out_;
170 static const int kFifoBufferSize = 4096;
172 class SSLStreamAdapterTestBase : public testing::Test,
173 public sigslot::has_slots<> {
175 SSLStreamAdapterTestBase(const std::string& client_cert_pem,
176 const std::string& client_private_key_pem,
178 client_buffer_(kFifoBufferSize), server_buffer_(kFifoBufferSize),
180 new SSLDummyStream(this, "c2s", &client_buffer_, &server_buffer_)),
182 new SSLDummyStream(this, "s2c", &server_buffer_, &client_buffer_)),
183 client_ssl_(talk_base::SSLStreamAdapter::Create(client_stream_)),
184 server_ssl_(talk_base::SSLStreamAdapter::Create(server_stream_)),
185 client_identity_(NULL), server_identity_(NULL),
186 delay_(0), mtu_(1460), loss_(0), lose_first_packet_(false),
187 damage_(false), dtls_(dtls),
188 handshake_wait_(5000), identities_set_(false) {
189 // Set use of the test RNG to get predictable loss patterns.
190 talk_base::SetRandomTestMode(true);
193 client_ssl_->SignalEvent.connect(this, &SSLStreamAdapterTestBase::OnEvent);
194 server_ssl_->SignalEvent.connect(this, &SSLStreamAdapterTestBase::OnEvent);
196 if (!client_cert_pem.empty() && !client_private_key_pem.empty()) {
197 client_identity_ = talk_base::SSLIdentity::FromPEMStrings(
198 client_private_key_pem, client_cert_pem);
200 client_identity_ = talk_base::SSLIdentity::Generate("client");
202 server_identity_ = talk_base::SSLIdentity::Generate("server");
204 client_ssl_->SetIdentity(client_identity_);
205 server_ssl_->SetIdentity(server_identity_);
208 ~SSLStreamAdapterTestBase() {
209 // Put it back for the next test.
210 talk_base::SetRandomTestMode(false);
211 talk_base::CleanupSSL();
214 static void SetUpTestCase() {
215 talk_base::InitializeSSL();
218 virtual void OnEvent(talk_base::StreamInterface *stream, int sig, int err) {
219 LOG(LS_INFO) << "SSLStreamAdapterTestBase::OnEvent sig=" << sig;
221 if (sig & talk_base::SE_READ) {
225 if ((stream == client_ssl_.get()) && (sig & talk_base::SE_WRITE)) {
230 void SetPeerIdentitiesByCertificate(bool correct) {
231 LOG(LS_INFO) << "Setting peer identities by certificate";
234 client_ssl_->SetPeerCertificate(server_identity_->certificate().
236 server_ssl_->SetPeerCertificate(client_identity_->certificate().
239 // If incorrect, set up to expect our own certificate at the peer
240 client_ssl_->SetPeerCertificate(client_identity_->certificate().
242 server_ssl_->SetPeerCertificate(server_identity_->certificate().
245 identities_set_ = true;
248 void SetPeerIdentitiesByDigest(bool correct) {
249 unsigned char digest[20];
253 LOG(LS_INFO) << "Setting peer identities by digest";
255 rv = server_identity_->certificate().ComputeDigest(talk_base::DIGEST_SHA_1,
260 LOG(LS_INFO) << "Setting bogus digest for server cert";
263 rv = client_ssl_->SetPeerCertificateDigest(talk_base::DIGEST_SHA_1, digest,
268 rv = client_identity_->certificate().ComputeDigest(talk_base::DIGEST_SHA_1,
269 digest, 20, &digest_len);
272 LOG(LS_INFO) << "Setting bogus digest for client cert";
275 rv = server_ssl_->SetPeerCertificateDigest(talk_base::DIGEST_SHA_1, digest,
279 identities_set_ = true;
282 void TestHandshake(bool expect_success = true) {
283 server_ssl_->SetMode(dtls_ ? talk_base::SSL_MODE_DTLS :
284 talk_base::SSL_MODE_TLS);
285 client_ssl_->SetMode(dtls_ ? talk_base::SSL_MODE_DTLS :
286 talk_base::SSL_MODE_TLS);
289 // Make sure we simulate a reliable network for TLS.
290 // This is just a check to make sure that people don't write wrong
292 ASSERT((mtu_ == 1460) && (loss_ == 0) && (lose_first_packet_ == 0));
295 if (!identities_set_)
296 SetPeerIdentitiesByDigest(true);
298 // Start the handshake
301 server_ssl_->SetServerRole();
302 rv = server_ssl_->StartSSLWithPeer();
305 rv = client_ssl_->StartSSLWithPeer();
308 // Now run the handshake
309 if (expect_success) {
310 EXPECT_TRUE_WAIT((client_ssl_->GetState() == talk_base::SS_OPEN)
311 && (server_ssl_->GetState() == talk_base::SS_OPEN),
314 EXPECT_TRUE_WAIT(client_ssl_->GetState() == talk_base::SS_CLOSED,
319 talk_base::StreamResult DataWritten(SSLDummyStream *from, const void *data,
320 size_t data_len, size_t *written,
322 // Randomly drop loss_ percent of packets
323 if (talk_base::CreateRandomId() % 100 < static_cast<uint32>(loss_)) {
324 LOG(LS_INFO) << "Randomly dropping packet, size=" << data_len;
326 return talk_base::SR_SUCCESS;
328 if (dtls_ && (data_len > mtu_)) {
329 LOG(LS_INFO) << "Dropping packet > mtu, size=" << data_len;
331 return talk_base::SR_SUCCESS;
334 // Optionally damage application data (type 23). Note that we don't damage
335 // handshake packets and we damage the last byte to keep the header
336 // intact but break the MAC.
337 if (damage_ && (*static_cast<const unsigned char *>(data) == 23)) {
338 std::vector<char> buf(data_len);
340 LOG(LS_INFO) << "Damaging packet";
342 memcpy(&buf[0], data, data_len);
345 return from->WriteData(&buf[0], data_len, written, error);
348 return from->WriteData(data, data_len, written, error);
351 void SetDelay(int delay) {
354 int GetDelay() { return delay_; }
356 void SetLoseFirstPacket(bool lose) {
357 lose_first_packet_ = lose;
359 bool GetLoseFirstPacket() { return lose_first_packet_; }
361 void SetLoss(int percent) {
369 void SetMtu(size_t mtu) {
373 void SetHandshakeWait(int wait) {
374 handshake_wait_ = wait;
377 void SetDtlsSrtpCiphers(const std::vector<std::string> &ciphers,
380 client_ssl_->SetDtlsSrtpCiphers(ciphers);
382 server_ssl_->SetDtlsSrtpCiphers(ciphers);
385 bool GetDtlsSrtpCipher(bool client, std::string *retval) {
387 return client_ssl_->GetDtlsSrtpCipher(retval);
389 return server_ssl_->GetDtlsSrtpCipher(retval);
392 bool GetPeerCertificate(bool client, talk_base::SSLCertificate** cert) {
394 return client_ssl_->GetPeerCertificate(cert);
396 return server_ssl_->GetPeerCertificate(cert);
399 bool ExportKeyingMaterial(const char *label,
400 const unsigned char *context,
404 unsigned char *result,
407 return client_ssl_->ExportKeyingMaterial(label,
408 context, context_len,
412 return server_ssl_->ExportKeyingMaterial(label,
413 context, context_len,
418 // To be implemented by subclasses.
419 virtual void WriteData() = 0;
420 virtual void ReadData(talk_base::StreamInterface *stream) = 0;
421 virtual void TestTransfer(int size) = 0;
424 talk_base::FifoBuffer client_buffer_;
425 talk_base::FifoBuffer server_buffer_;
426 SSLDummyStream *client_stream_; // freed by client_ssl_ destructor
427 SSLDummyStream *server_stream_; // freed by server_ssl_ destructor
428 talk_base::scoped_ptr<talk_base::SSLStreamAdapter> client_ssl_;
429 talk_base::scoped_ptr<talk_base::SSLStreamAdapter> server_ssl_;
430 talk_base::SSLIdentity *client_identity_; // freed by client_ssl_ destructor
431 talk_base::SSLIdentity *server_identity_; // freed by server_ssl_ destructor
435 bool lose_first_packet_;
439 bool identities_set_;
442 class SSLStreamAdapterTestTLS : public SSLStreamAdapterTestBase {
444 SSLStreamAdapterTestTLS() :
445 SSLStreamAdapterTestBase("", "", false) {
448 // Test data transfer for TLS
449 virtual void TestTransfer(int size) {
450 LOG(LS_INFO) << "Starting transfer test with " << size << " bytes";
451 // Create some dummy data to send.
454 send_stream_.ReserveSize(size);
455 for (int i = 0; i < size; ++i) {
456 char ch = static_cast<char>(i);
457 send_stream_.Write(&ch, 1, NULL, NULL);
459 send_stream_.Rewind();
461 // Prepare the receive stream.
462 recv_stream_.ReserveSize(size);
467 // Wait for the client to close
468 EXPECT_TRUE_WAIT(server_ssl_->GetState() == talk_base::SS_CLOSED, 10000);
470 // Now check the data
471 recv_stream_.GetSize(&received);
473 EXPECT_EQ(static_cast<size_t>(size), received);
474 EXPECT_EQ(0, memcmp(send_stream_.GetBuffer(),
475 recv_stream_.GetBuffer(), size));
479 size_t position, tosend, size;
480 talk_base::StreamResult rv;
482 char block[kBlockSize];
484 send_stream_.GetSize(&size);
489 send_stream_.GetPosition(&position);
490 if (send_stream_.Read(block, sizeof(block), &tosend, NULL) !=
492 rv = client_ssl_->Write(block, tosend, &sent, 0);
494 if (rv == talk_base::SR_SUCCESS) {
495 send_stream_.SetPosition(position + sent);
496 LOG(LS_VERBOSE) << "Sent: " << position + sent;
497 } else if (rv == talk_base::SR_BLOCK) {
498 LOG(LS_VERBOSE) << "Blocked...";
499 send_stream_.SetPosition(position);
507 LOG(LS_INFO) << "Wrote " << position << " bytes. Closing";
508 client_ssl_->Close();
514 virtual void ReadData(talk_base::StreamInterface *stream) {
518 talk_base::StreamResult r;
521 r = stream->Read(buffer, sizeof(buffer), &bread, &err2);
523 if (r == talk_base::SR_ERROR || r == talk_base::SR_EOS) {
524 // Unfortunately, errors are the way that the stream adapter
525 // signals close in OpenSSL
530 if (r == talk_base::SR_BLOCK)
533 ASSERT_EQ(talk_base::SR_SUCCESS, r);
534 LOG(LS_INFO) << "Read " << bread;
536 recv_stream_.Write(buffer, bread, NULL, NULL);
541 talk_base::MemoryStream send_stream_;
542 talk_base::MemoryStream recv_stream_;
545 class SSLStreamAdapterTestDTLS : public SSLStreamAdapterTestBase {
547 SSLStreamAdapterTestDTLS() :
548 SSLStreamAdapterTestBase("", "", true),
549 packet_size_(1000), count_(0), sent_(0) {
552 SSLStreamAdapterTestDTLS(const std::string& cert_pem,
553 const std::string& private_key_pem) :
554 SSLStreamAdapterTestBase(cert_pem, private_key_pem, true),
555 packet_size_(1000), count_(0), sent_(0) {
558 virtual void WriteData() {
559 unsigned char *packet = new unsigned char[1600];
562 memset(packet, sent_ & 0xff, packet_size_);
563 *(reinterpret_cast<uint32_t *>(packet)) = sent_;
566 int rv = client_ssl_->Write(packet, packet_size_, &sent, 0);
567 if (rv == talk_base::SR_SUCCESS) {
568 LOG(LS_VERBOSE) << "Sent: " << sent_;
570 } else if (rv == talk_base::SR_BLOCK) {
571 LOG(LS_VERBOSE) << "Blocked...";
577 } while (sent_ < count_);
582 virtual void ReadData(talk_base::StreamInterface *stream) {
583 unsigned char buffer[2000];
586 talk_base::StreamResult r;
589 r = stream->Read(buffer, 2000, &bread, &err2);
591 if (r == talk_base::SR_ERROR) {
592 // Unfortunately, errors are the way that the stream adapter
593 // signals close right now
598 if (r == talk_base::SR_BLOCK)
601 ASSERT_EQ(talk_base::SR_SUCCESS, r);
602 LOG(LS_INFO) << "Read " << bread;
604 // Now parse the datagram
605 ASSERT_EQ(packet_size_, bread);
606 unsigned char* ptr_to_buffer = buffer;
607 uint32_t packet_num = *(reinterpret_cast<uint32_t *>(ptr_to_buffer));
609 for (size_t i = 4; i < packet_size_; i++) {
610 ASSERT_EQ((packet_num & 0xff), buffer[i]);
612 received_.insert(packet_num);
616 virtual void TestTransfer(int count) {
621 EXPECT_TRUE_WAIT(sent_ == count_, 10000);
622 LOG(LS_INFO) << "sent_ == " << sent_;
626 EXPECT_EQ(0U, received_.size());
627 } else if (loss_ == 0) {
628 EXPECT_EQ_WAIT(static_cast<size_t>(sent_), received_.size(), 1000);
630 LOG(LS_INFO) << "Sent " << sent_ << " packets; received " <<
639 std::set<int> received_;
643 talk_base::StreamResult SSLDummyStream::Write(const void* data, size_t data_len,
644 size_t* written, int* error) {
647 LOG(LS_INFO) << "Writing to loopback " << data_len;
650 first_packet_ = false;
651 if (test_->GetLoseFirstPacket()) {
652 LOG(LS_INFO) << "Losing initial packet of length " << data_len;
653 return talk_base::SR_SUCCESS;
657 return test_->DataWritten(this, data, data_len, written, error);
659 return talk_base::SR_SUCCESS;
662 class SSLStreamAdapterTestDTLSFromPEMStrings : public SSLStreamAdapterTestDTLS {
664 SSLStreamAdapterTestDTLSFromPEMStrings() :
665 SSLStreamAdapterTestDTLS(kCERT_PEM, kRSA_PRIVATE_KEY_PEM) {
671 // Test that we cannot read/write if we have not yet handshaked.
672 // This test only applies to NSS because OpenSSL has passthrough
673 // semantics for I/O before the handshake is started.
675 TEST_F(SSLStreamAdapterTestTLS, TestNoReadWriteBeforeConnect) {
676 talk_base::StreamResult rv;
677 char block[kBlockSize];
680 rv = client_ssl_->Write(block, sizeof(block), &dummy, NULL);
681 ASSERT_EQ(talk_base::SR_BLOCK, rv);
683 rv = client_ssl_->Read(block, sizeof(block), &dummy, NULL);
684 ASSERT_EQ(talk_base::SR_BLOCK, rv);
689 // Test that we can make a handshake work
690 TEST_F(SSLStreamAdapterTestTLS, TestTLSConnect) {
694 // Test transfer -- trivial
695 TEST_F(SSLStreamAdapterTestTLS, TestTLSTransfer) {
697 TestTransfer(100000);
700 // Test read-write after close.
701 TEST_F(SSLStreamAdapterTestTLS, ReadWriteAfterClose) {
703 TestTransfer(100000);
704 client_ssl_->Close();
706 talk_base::StreamResult rv;
707 char block[kBlockSize];
710 // It's an error to write after closed.
711 rv = client_ssl_->Write(block, sizeof(block), &dummy, NULL);
712 ASSERT_EQ(talk_base::SR_ERROR, rv);
714 // But after closed read gives you EOS.
715 rv = client_ssl_->Read(block, sizeof(block), &dummy, NULL);
716 ASSERT_EQ(talk_base::SR_EOS, rv);
719 // Test a handshake with a bogus peer digest
720 TEST_F(SSLStreamAdapterTestTLS, TestTLSBogusDigest) {
721 SetPeerIdentitiesByDigest(false);
722 TestHandshake(false);
725 // Test a handshake with a peer certificate
726 TEST_F(SSLStreamAdapterTestTLS, TestTLSPeerCertificate) {
727 SetPeerIdentitiesByCertificate(true);
731 // Test a handshake with a bogus peer certificate
732 TEST_F(SSLStreamAdapterTestTLS, TestTLSBogusPeerCertificate) {
733 SetPeerIdentitiesByCertificate(false);
734 TestHandshake(false);
736 // Test moving a bunch of data
739 // Test that we can make a handshake work
740 TEST_F(SSLStreamAdapterTestDTLS, TestDTLSConnect) {
741 MAYBE_SKIP_TEST(HaveDtls);
745 // Test that we can make a handshake work if the first packet in
746 // each direction is lost. This gives us predictable loss
747 // rather than having to tune random
748 TEST_F(SSLStreamAdapterTestDTLS, TestDTLSConnectWithLostFirstPacket) {
749 MAYBE_SKIP_TEST(HaveDtls);
750 SetLoseFirstPacket(true);
754 // Test a handshake with loss and delay
755 TEST_F(SSLStreamAdapterTestDTLS,
756 TestDTLSConnectWithLostFirstPacketDelay2s) {
757 MAYBE_SKIP_TEST(HaveDtls);
758 SetLoseFirstPacket(true);
760 SetHandshakeWait(20000);
764 // Test a handshake with small MTU
765 TEST_F(SSLStreamAdapterTestDTLS, TestDTLSConnectWithSmallMtu) {
766 MAYBE_SKIP_TEST(HaveDtls);
768 SetHandshakeWait(20000);
772 // Test transfer -- trivial
773 TEST_F(SSLStreamAdapterTestDTLS, TestDTLSTransfer) {
774 MAYBE_SKIP_TEST(HaveDtls);
779 TEST_F(SSLStreamAdapterTestDTLS, TestDTLSTransferWithLoss) {
780 MAYBE_SKIP_TEST(HaveDtls);
786 TEST_F(SSLStreamAdapterTestDTLS, TestDTLSTransferWithDamage) {
787 MAYBE_SKIP_TEST(HaveDtls);
788 SetDamage(); // Must be called first because first packet
789 // write happens at end of handshake.
794 // Test DTLS-SRTP with all high ciphers
795 TEST_F(SSLStreamAdapterTestDTLS, TestDTLSSrtpHigh) {
796 MAYBE_SKIP_TEST(HaveDtlsSrtp);
797 std::vector<std::string> high;
798 high.push_back(kAES_CM_HMAC_SHA1_80);
799 SetDtlsSrtpCiphers(high, true);
800 SetDtlsSrtpCiphers(high, false);
803 std::string client_cipher;
804 ASSERT_TRUE(GetDtlsSrtpCipher(true, &client_cipher));
805 std::string server_cipher;
806 ASSERT_TRUE(GetDtlsSrtpCipher(false, &server_cipher));
808 ASSERT_EQ(client_cipher, server_cipher);
809 ASSERT_EQ(client_cipher, kAES_CM_HMAC_SHA1_80);
812 // Test DTLS-SRTP with all low ciphers
813 TEST_F(SSLStreamAdapterTestDTLS, TestDTLSSrtpLow) {
814 MAYBE_SKIP_TEST(HaveDtlsSrtp);
815 std::vector<std::string> low;
816 low.push_back(kAES_CM_HMAC_SHA1_32);
817 SetDtlsSrtpCiphers(low, true);
818 SetDtlsSrtpCiphers(low, false);
821 std::string client_cipher;
822 ASSERT_TRUE(GetDtlsSrtpCipher(true, &client_cipher));
823 std::string server_cipher;
824 ASSERT_TRUE(GetDtlsSrtpCipher(false, &server_cipher));
826 ASSERT_EQ(client_cipher, server_cipher);
827 ASSERT_EQ(client_cipher, kAES_CM_HMAC_SHA1_32);
831 // Test DTLS-SRTP with a mismatch -- should not converge
832 TEST_F(SSLStreamAdapterTestDTLS, TestDTLSSrtpHighLow) {
833 MAYBE_SKIP_TEST(HaveDtlsSrtp);
834 std::vector<std::string> high;
835 high.push_back(kAES_CM_HMAC_SHA1_80);
836 std::vector<std::string> low;
837 low.push_back(kAES_CM_HMAC_SHA1_32);
838 SetDtlsSrtpCiphers(high, true);
839 SetDtlsSrtpCiphers(low, false);
842 std::string client_cipher;
843 ASSERT_FALSE(GetDtlsSrtpCipher(true, &client_cipher));
844 std::string server_cipher;
845 ASSERT_FALSE(GetDtlsSrtpCipher(false, &server_cipher));
848 // Test DTLS-SRTP with each side being mixed -- should select high
849 TEST_F(SSLStreamAdapterTestDTLS, TestDTLSSrtpMixed) {
850 MAYBE_SKIP_TEST(HaveDtlsSrtp);
851 std::vector<std::string> mixed;
852 mixed.push_back(kAES_CM_HMAC_SHA1_80);
853 mixed.push_back(kAES_CM_HMAC_SHA1_32);
854 SetDtlsSrtpCiphers(mixed, true);
855 SetDtlsSrtpCiphers(mixed, false);
858 std::string client_cipher;
859 ASSERT_TRUE(GetDtlsSrtpCipher(true, &client_cipher));
860 std::string server_cipher;
861 ASSERT_TRUE(GetDtlsSrtpCipher(false, &server_cipher));
863 ASSERT_EQ(client_cipher, server_cipher);
864 ASSERT_EQ(client_cipher, kAES_CM_HMAC_SHA1_80);
868 TEST_F(SSLStreamAdapterTestDTLS, TestDTLSExporter) {
869 MAYBE_SKIP_TEST(HaveExporter);
871 unsigned char client_out[20];
872 unsigned char server_out[20];
875 result = ExportKeyingMaterial(kExporterLabel,
876 kExporterContext, kExporterContextLen,
878 client_out, sizeof(client_out));
881 result = ExportKeyingMaterial(kExporterLabel,
882 kExporterContext, kExporterContextLen,
884 server_out, sizeof(server_out));
887 ASSERT_TRUE(!memcmp(client_out, server_out, sizeof(client_out)));
890 // Test data transfer using certs created from strings.
891 TEST_F(SSLStreamAdapterTestDTLSFromPEMStrings, TestTransfer) {
892 MAYBE_SKIP_TEST(HaveDtls);
897 // Test getting the remote certificate.
898 TEST_F(SSLStreamAdapterTestDTLSFromPEMStrings, TestDTLSGetPeerCertificate) {
899 MAYBE_SKIP_TEST(HaveDtls);
901 // Peer certificates haven't been received yet.
902 talk_base::scoped_ptr<talk_base::SSLCertificate> client_peer_cert;
903 ASSERT_FALSE(GetPeerCertificate(true, client_peer_cert.accept()));
904 ASSERT_FALSE(client_peer_cert != NULL);
906 talk_base::scoped_ptr<talk_base::SSLCertificate> server_peer_cert;
907 ASSERT_FALSE(GetPeerCertificate(false, server_peer_cert.accept()));
908 ASSERT_FALSE(server_peer_cert != NULL);
912 // The client should have a peer certificate after the handshake.
913 ASSERT_TRUE(GetPeerCertificate(true, client_peer_cert.accept()));
914 ASSERT_TRUE(client_peer_cert != NULL);
916 // It's not kCERT_PEM.
917 std::string client_peer_string = client_peer_cert->ToPEMString();
918 ASSERT_NE(kCERT_PEM, client_peer_string);
920 // It must not have a chain, because the test certs are self-signed.
921 talk_base::SSLCertChain* client_peer_chain;
922 ASSERT_FALSE(client_peer_cert->GetChain(&client_peer_chain));
924 // The server should have a peer certificate after the handshake.
925 ASSERT_TRUE(GetPeerCertificate(false, server_peer_cert.accept()));
926 ASSERT_TRUE(server_peer_cert != NULL);
929 ASSERT_EQ(kCERT_PEM, server_peer_cert->ToPEMString());
931 // It must not have a chain, because the test certs are self-signed.
932 talk_base::SSLCertChain* server_peer_chain;
933 ASSERT_FALSE(server_peer_cert->GetChain(&server_peer_chain));