OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2011 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
| 11 #include <memory> |
11 #include <set> | 12 #include <set> |
12 | 13 |
13 #include "webrtc/p2p/base/dtlstransport.h" | 14 #include "webrtc/p2p/base/dtlstransport.h" |
14 #include "webrtc/p2p/base/faketransportcontroller.h" | 15 #include "webrtc/p2p/base/faketransportcontroller.h" |
15 #include "webrtc/base/common.h" | 16 #include "webrtc/base/common.h" |
16 #include "webrtc/base/dscp.h" | 17 #include "webrtc/base/dscp.h" |
17 #include "webrtc/base/gunit.h" | 18 #include "webrtc/base/gunit.h" |
18 #include "webrtc/base/helpers.h" | 19 #include "webrtc/base/helpers.h" |
19 #include "webrtc/base/scoped_ptr.h" | |
20 #include "webrtc/base/ssladapter.h" | 20 #include "webrtc/base/ssladapter.h" |
21 #include "webrtc/base/sslidentity.h" | 21 #include "webrtc/base/sslidentity.h" |
22 #include "webrtc/base/sslstreamadapter.h" | 22 #include "webrtc/base/sslstreamadapter.h" |
23 #include "webrtc/base/stringutils.h" | 23 #include "webrtc/base/stringutils.h" |
24 | 24 |
25 #define MAYBE_SKIP_TEST(feature) \ | 25 #define MAYBE_SKIP_TEST(feature) \ |
26 if (!(rtc::SSLStreamAdapter::feature())) { \ | 26 if (!(rtc::SSLStreamAdapter::feature())) { \ |
27 LOG(LS_INFO) << "Feature disabled... skipping"; \ | 27 LOG(LS_INFO) << "Feature disabled... skipping"; \ |
28 return; \ | 28 return; \ |
29 } | 29 } |
(...skipping 17 matching lines...) Expand all Loading... |
47 DtlsTestClient(const std::string& name) | 47 DtlsTestClient(const std::string& name) |
48 : name_(name), | 48 : name_(name), |
49 packet_size_(0), | 49 packet_size_(0), |
50 use_dtls_srtp_(false), | 50 use_dtls_srtp_(false), |
51 ssl_max_version_(rtc::SSL_PROTOCOL_DTLS_12), | 51 ssl_max_version_(rtc::SSL_PROTOCOL_DTLS_12), |
52 negotiated_dtls_(false), | 52 negotiated_dtls_(false), |
53 received_dtls_client_hello_(false), | 53 received_dtls_client_hello_(false), |
54 received_dtls_server_hello_(false) {} | 54 received_dtls_server_hello_(false) {} |
55 void CreateCertificate(rtc::KeyType key_type) { | 55 void CreateCertificate(rtc::KeyType key_type) { |
56 certificate_ = | 56 certificate_ = |
57 rtc::RTCCertificate::Create(rtc::scoped_ptr<rtc::SSLIdentity>( | 57 rtc::RTCCertificate::Create(std::unique_ptr<rtc::SSLIdentity>( |
58 rtc::SSLIdentity::Generate(name_, key_type))); | 58 rtc::SSLIdentity::Generate(name_, key_type))); |
59 } | 59 } |
60 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate() { | 60 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate() { |
61 return certificate_; | 61 return certificate_; |
62 } | 62 } |
63 void SetupSrtp() { | 63 void SetupSrtp() { |
64 ASSERT(certificate_); | 64 ASSERT(certificate_); |
65 use_dtls_srtp_ = true; | 65 use_dtls_srtp_ = true; |
66 } | 66 } |
67 void SetupMaxProtocolVersion(rtc::SSLProtocolVersion version) { | 67 void SetupMaxProtocolVersion(rtc::SSLProtocolVersion version) { |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 local_role, remote_role, flags); | 115 local_role, remote_role, flags); |
116 } | 116 } |
117 | 117 |
118 // Allow any DTLS configuration to be specified (including invalid ones). | 118 // Allow any DTLS configuration to be specified (including invalid ones). |
119 void Negotiate(const rtc::scoped_refptr<rtc::RTCCertificate>& local_cert, | 119 void Negotiate(const rtc::scoped_refptr<rtc::RTCCertificate>& local_cert, |
120 const rtc::scoped_refptr<rtc::RTCCertificate>& remote_cert, | 120 const rtc::scoped_refptr<rtc::RTCCertificate>& remote_cert, |
121 cricket::ContentAction action, | 121 cricket::ContentAction action, |
122 ConnectionRole local_role, | 122 ConnectionRole local_role, |
123 ConnectionRole remote_role, | 123 ConnectionRole remote_role, |
124 int flags) { | 124 int flags) { |
125 rtc::scoped_ptr<rtc::SSLFingerprint> local_fingerprint; | 125 std::unique_ptr<rtc::SSLFingerprint> local_fingerprint; |
126 rtc::scoped_ptr<rtc::SSLFingerprint> remote_fingerprint; | 126 std::unique_ptr<rtc::SSLFingerprint> remote_fingerprint; |
127 if (local_cert) { | 127 if (local_cert) { |
128 std::string digest_algorithm; | 128 std::string digest_algorithm; |
129 ASSERT_TRUE(local_cert->ssl_certificate().GetSignatureDigestAlgorithm( | 129 ASSERT_TRUE(local_cert->ssl_certificate().GetSignatureDigestAlgorithm( |
130 &digest_algorithm)); | 130 &digest_algorithm)); |
131 ASSERT_FALSE(digest_algorithm.empty()); | 131 ASSERT_FALSE(digest_algorithm.empty()); |
132 local_fingerprint.reset(rtc::SSLFingerprint::Create( | 132 local_fingerprint.reset(rtc::SSLFingerprint::Create( |
133 digest_algorithm, local_cert->identity())); | 133 digest_algorithm, local_cert->identity())); |
134 ASSERT_TRUE(local_fingerprint.get() != NULL); | 134 ASSERT_TRUE(local_fingerprint.get() != NULL); |
135 EXPECT_EQ(rtc::DIGEST_SHA_256, digest_algorithm); | 135 EXPECT_EQ(rtc::DIGEST_SHA_256, digest_algorithm); |
136 } | 136 } |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 EXPECT_TRUE( | 241 EXPECT_TRUE( |
242 rtc::SSLStreamAdapter::IsAcceptableCipher(cipher, rtc::KT_DEFAULT)); | 242 rtc::SSLStreamAdapter::IsAcceptableCipher(cipher, rtc::KT_DEFAULT)); |
243 } else { | 243 } else { |
244 ASSERT_FALSE(rv); | 244 ASSERT_FALSE(rv); |
245 } | 245 } |
246 } | 246 } |
247 } | 247 } |
248 | 248 |
249 void SendPackets(size_t channel, size_t size, size_t count, bool srtp) { | 249 void SendPackets(size_t channel, size_t size, size_t count, bool srtp) { |
250 ASSERT(channel < channels_.size()); | 250 ASSERT(channel < channels_.size()); |
251 rtc::scoped_ptr<char[]> packet(new char[size]); | 251 std::unique_ptr<char[]> packet(new char[size]); |
252 size_t sent = 0; | 252 size_t sent = 0; |
253 do { | 253 do { |
254 // Fill the packet with a known value and a sequence number to check | 254 // Fill the packet with a known value and a sequence number to check |
255 // against, and make sure that it doesn't look like DTLS. | 255 // against, and make sure that it doesn't look like DTLS. |
256 memset(packet.get(), sent & 0xff, size); | 256 memset(packet.get(), sent & 0xff, size); |
257 packet[0] = (srtp) ? 0x80 : 0x00; | 257 packet[0] = (srtp) ? 0x80 : 0x00; |
258 rtc::SetBE32(packet.get() + kPacketNumOffset, | 258 rtc::SetBE32(packet.get() + kPacketNumOffset, |
259 static_cast<uint32_t>(sent)); | 259 static_cast<uint32_t>(sent)); |
260 | 260 |
261 // Only set the bypass flag if we've activated DTLS. | 261 // Only set the bypass flag if we've activated DTLS. |
262 int flags = (certificate_ && srtp) ? cricket::PF_SRTP_BYPASS : 0; | 262 int flags = (certificate_ && srtp) ? cricket::PF_SRTP_BYPASS : 0; |
263 rtc::PacketOptions packet_options; | 263 rtc::PacketOptions packet_options; |
264 packet_options.packet_id = kFakePacketId; | 264 packet_options.packet_id = kFakePacketId; |
265 int rv = channels_[channel]->SendPacket( | 265 int rv = channels_[channel]->SendPacket( |
266 packet.get(), size, packet_options, flags); | 266 packet.get(), size, packet_options, flags); |
267 ASSERT_GT(rv, 0); | 267 ASSERT_GT(rv, 0); |
268 ASSERT_EQ(size, static_cast<size_t>(rv)); | 268 ASSERT_EQ(size, static_cast<size_t>(rv)); |
269 ++sent; | 269 ++sent; |
270 } while (sent < count); | 270 } while (sent < count); |
271 } | 271 } |
272 | 272 |
273 int SendInvalidSrtpPacket(size_t channel, size_t size) { | 273 int SendInvalidSrtpPacket(size_t channel, size_t size) { |
274 ASSERT(channel < channels_.size()); | 274 ASSERT(channel < channels_.size()); |
275 rtc::scoped_ptr<char[]> packet(new char[size]); | 275 std::unique_ptr<char[]> packet(new char[size]); |
276 // Fill the packet with 0 to form an invalid SRTP packet. | 276 // Fill the packet with 0 to form an invalid SRTP packet. |
277 memset(packet.get(), 0, size); | 277 memset(packet.get(), 0, size); |
278 | 278 |
279 rtc::PacketOptions packet_options; | 279 rtc::PacketOptions packet_options; |
280 return channels_[channel]->SendPacket( | 280 return channels_[channel]->SendPacket( |
281 packet.get(), size, packet_options, cricket::PF_SRTP_BYPASS); | 281 packet.get(), size, packet_options, cricket::PF_SRTP_BYPASS); |
282 } | 282 } |
283 | 283 |
284 void ExpectPackets(size_t channel, size_t size) { | 284 void ExpectPackets(size_t channel, size_t size) { |
285 packet_size_ = size; | 285 packet_size_ = size; |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
372 } else if (IsRtpLeadByte(data[0])) { | 372 } else if (IsRtpLeadByte(data[0])) { |
373 ASSERT_TRUE(VerifyPacket(data, size, NULL)); | 373 ASSERT_TRUE(VerifyPacket(data, size, NULL)); |
374 } | 374 } |
375 } | 375 } |
376 } | 376 } |
377 } | 377 } |
378 | 378 |
379 private: | 379 private: |
380 std::string name_; | 380 std::string name_; |
381 rtc::scoped_refptr<rtc::RTCCertificate> certificate_; | 381 rtc::scoped_refptr<rtc::RTCCertificate> certificate_; |
382 rtc::scoped_ptr<cricket::FakeTransport> transport_; | 382 std::unique_ptr<cricket::FakeTransport> transport_; |
383 std::vector<cricket::DtlsTransportChannelWrapper*> channels_; | 383 std::vector<cricket::DtlsTransportChannelWrapper*> channels_; |
384 size_t packet_size_; | 384 size_t packet_size_; |
385 std::set<int> received_; | 385 std::set<int> received_; |
386 bool use_dtls_srtp_; | 386 bool use_dtls_srtp_; |
387 rtc::SSLProtocolVersion ssl_max_version_; | 387 rtc::SSLProtocolVersion ssl_max_version_; |
388 bool negotiated_dtls_; | 388 bool negotiated_dtls_; |
389 bool received_dtls_client_hello_; | 389 bool received_dtls_client_hello_; |
390 bool received_dtls_server_hello_; | 390 bool received_dtls_server_hello_; |
391 rtc::SentPacket sent_packet_; | 391 rtc::SentPacket sent_packet_; |
392 }; | 392 }; |
(...skipping 446 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
839 } | 839 } |
840 | 840 |
841 // Test Certificates state after negotiation but before connection. | 841 // Test Certificates state after negotiation but before connection. |
842 TEST_F(DtlsTransportChannelTest, TestCertificatesBeforeConnect) { | 842 TEST_F(DtlsTransportChannelTest, TestCertificatesBeforeConnect) { |
843 MAYBE_SKIP_TEST(HaveDtls); | 843 MAYBE_SKIP_TEST(HaveDtls); |
844 PrepareDtls(true, true, rtc::KT_DEFAULT); | 844 PrepareDtls(true, true, rtc::KT_DEFAULT); |
845 Negotiate(); | 845 Negotiate(); |
846 | 846 |
847 rtc::scoped_refptr<rtc::RTCCertificate> certificate1; | 847 rtc::scoped_refptr<rtc::RTCCertificate> certificate1; |
848 rtc::scoped_refptr<rtc::RTCCertificate> certificate2; | 848 rtc::scoped_refptr<rtc::RTCCertificate> certificate2; |
849 rtc::scoped_ptr<rtc::SSLCertificate> remote_cert1; | 849 std::unique_ptr<rtc::SSLCertificate> remote_cert1; |
850 rtc::scoped_ptr<rtc::SSLCertificate> remote_cert2; | 850 std::unique_ptr<rtc::SSLCertificate> remote_cert2; |
851 | 851 |
852 // After negotiation, each side has a distinct local certificate, but still no | 852 // After negotiation, each side has a distinct local certificate, but still no |
853 // remote certificate, because connection has not yet occurred. | 853 // remote certificate, because connection has not yet occurred. |
854 ASSERT_TRUE(client1_.transport()->GetLocalCertificate(&certificate1)); | 854 ASSERT_TRUE(client1_.transport()->GetLocalCertificate(&certificate1)); |
855 ASSERT_TRUE(client2_.transport()->GetLocalCertificate(&certificate2)); | 855 ASSERT_TRUE(client2_.transport()->GetLocalCertificate(&certificate2)); |
856 ASSERT_NE(certificate1->ssl_certificate().ToPEMString(), | 856 ASSERT_NE(certificate1->ssl_certificate().ToPEMString(), |
857 certificate2->ssl_certificate().ToPEMString()); | 857 certificate2->ssl_certificate().ToPEMString()); |
858 ASSERT_FALSE(client1_.transport()->GetRemoteSSLCertificate()); | 858 ASSERT_FALSE(client1_.transport()->GetRemoteSSLCertificate()); |
859 ASSERT_FALSE(client2_.transport()->GetRemoteSSLCertificate()); | 859 ASSERT_FALSE(client2_.transport()->GetRemoteSSLCertificate()); |
860 } | 860 } |
861 | 861 |
862 // Test Certificates state after connection. | 862 // Test Certificates state after connection. |
863 TEST_F(DtlsTransportChannelTest, TestCertificatesAfterConnect) { | 863 TEST_F(DtlsTransportChannelTest, TestCertificatesAfterConnect) { |
864 MAYBE_SKIP_TEST(HaveDtls); | 864 MAYBE_SKIP_TEST(HaveDtls); |
865 PrepareDtls(true, true, rtc::KT_DEFAULT); | 865 PrepareDtls(true, true, rtc::KT_DEFAULT); |
866 ASSERT_TRUE(Connect()); | 866 ASSERT_TRUE(Connect()); |
867 | 867 |
868 rtc::scoped_refptr<rtc::RTCCertificate> certificate1; | 868 rtc::scoped_refptr<rtc::RTCCertificate> certificate1; |
869 rtc::scoped_refptr<rtc::RTCCertificate> certificate2; | 869 rtc::scoped_refptr<rtc::RTCCertificate> certificate2; |
870 | 870 |
871 // After connection, each side has a distinct local certificate. | 871 // After connection, each side has a distinct local certificate. |
872 ASSERT_TRUE(client1_.transport()->GetLocalCertificate(&certificate1)); | 872 ASSERT_TRUE(client1_.transport()->GetLocalCertificate(&certificate1)); |
873 ASSERT_TRUE(client2_.transport()->GetLocalCertificate(&certificate2)); | 873 ASSERT_TRUE(client2_.transport()->GetLocalCertificate(&certificate2)); |
874 ASSERT_NE(certificate1->ssl_certificate().ToPEMString(), | 874 ASSERT_NE(certificate1->ssl_certificate().ToPEMString(), |
875 certificate2->ssl_certificate().ToPEMString()); | 875 certificate2->ssl_certificate().ToPEMString()); |
876 | 876 |
877 // Each side's remote certificate is the other side's local certificate. | 877 // Each side's remote certificate is the other side's local certificate. |
878 rtc::scoped_ptr<rtc::SSLCertificate> remote_cert1 = | 878 std::unique_ptr<rtc::SSLCertificate> remote_cert1 = |
879 client1_.transport()->GetRemoteSSLCertificate(); | 879 client1_.transport()->GetRemoteSSLCertificate(); |
880 ASSERT_TRUE(remote_cert1); | 880 ASSERT_TRUE(remote_cert1); |
881 ASSERT_EQ(remote_cert1->ToPEMString(), | 881 ASSERT_EQ(remote_cert1->ToPEMString(), |
882 certificate2->ssl_certificate().ToPEMString()); | 882 certificate2->ssl_certificate().ToPEMString()); |
883 rtc::scoped_ptr<rtc::SSLCertificate> remote_cert2 = | 883 std::unique_ptr<rtc::SSLCertificate> remote_cert2 = |
884 client2_.transport()->GetRemoteSSLCertificate(); | 884 client2_.transport()->GetRemoteSSLCertificate(); |
885 ASSERT_TRUE(remote_cert2); | 885 ASSERT_TRUE(remote_cert2); |
886 ASSERT_EQ(remote_cert2->ToPEMString(), | 886 ASSERT_EQ(remote_cert2->ToPEMString(), |
887 certificate1->ssl_certificate().ToPEMString()); | 887 certificate1->ssl_certificate().ToPEMString()); |
888 } | 888 } |
OLD | NEW |