Zhi Huang | f2d7beb | 2017-11-20 14:35:11 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 | |
| 11 | #include "pc/dtlssrtptransport.h" |
| 12 | |
| 13 | #include <memory> |
| 14 | #include <utility> |
| 15 | |
| 16 | #include "media/base/fakertp.h" |
| 17 | #include "p2p/base/dtlstransportinternal.h" |
| 18 | #include "p2p/base/fakedtlstransport.h" |
| 19 | #include "p2p/base/fakepackettransport.h" |
| 20 | #include "p2p/base/p2pconstants.h" |
| 21 | #include "pc/rtptransport.h" |
| 22 | #include "pc/rtptransporttestutil.h" |
| 23 | #include "rtc_base/asyncpacketsocket.h" |
| 24 | #include "rtc_base/gunit.h" |
| 25 | #include "rtc_base/ptr_util.h" |
| 26 | #include "rtc_base/sslstreamadapter.h" |
| 27 | |
| 28 | using cricket::FakeDtlsTransport; |
| 29 | using cricket::FakeIceTransport; |
| 30 | using webrtc::DtlsSrtpTransport; |
| 31 | using webrtc::SrtpTransport; |
| 32 | using webrtc::RtpTransport; |
| 33 | |
| 34 | const int kRtpAuthTagLen = 10; |
| 35 | |
Zhi Huang | f2d7beb | 2017-11-20 14:35:11 -0800 | [diff] [blame] | 36 | class DtlsSrtpTransportTest : public testing::Test, |
| 37 | public sigslot::has_slots<> { |
| 38 | protected: |
| 39 | DtlsSrtpTransportTest() {} |
| 40 | |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 41 | ~DtlsSrtpTransportTest() { |
| 42 | if (dtls_srtp_transport1_) { |
| 43 | dtls_srtp_transport1_->UnregisterRtpDemuxerSink(&transport_observer1_); |
| 44 | } |
| 45 | if (dtls_srtp_transport2_) { |
| 46 | dtls_srtp_transport2_->UnregisterRtpDemuxerSink(&transport_observer2_); |
| 47 | } |
| 48 | } |
| 49 | |
Zhi Huang | f2d7beb | 2017-11-20 14:35:11 -0800 | [diff] [blame] | 50 | std::unique_ptr<DtlsSrtpTransport> MakeDtlsSrtpTransport( |
| 51 | FakeDtlsTransport* rtp_dtls, |
| 52 | FakeDtlsTransport* rtcp_dtls, |
| 53 | bool rtcp_mux_enabled) { |
Zhi Huang | f2d7beb | 2017-11-20 14:35:11 -0800 | [diff] [blame] | 54 | auto dtls_srtp_transport = |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 55 | rtc::MakeUnique<DtlsSrtpTransport>(rtcp_mux_enabled); |
Zhi Huang | f2d7beb | 2017-11-20 14:35:11 -0800 | [diff] [blame] | 56 | |
| 57 | dtls_srtp_transport->SetDtlsTransports(rtp_dtls, rtcp_dtls); |
| 58 | |
| 59 | return dtls_srtp_transport; |
| 60 | } |
| 61 | |
| 62 | void MakeDtlsSrtpTransports(FakeDtlsTransport* rtp_dtls1, |
| 63 | FakeDtlsTransport* rtcp_dtls1, |
| 64 | FakeDtlsTransport* rtp_dtls2, |
| 65 | FakeDtlsTransport* rtcp_dtls2, |
| 66 | bool rtcp_mux_enabled) { |
| 67 | dtls_srtp_transport1_ = |
| 68 | MakeDtlsSrtpTransport(rtp_dtls1, rtcp_dtls1, rtcp_mux_enabled); |
| 69 | dtls_srtp_transport2_ = |
| 70 | MakeDtlsSrtpTransport(rtp_dtls2, rtcp_dtls2, rtcp_mux_enabled); |
| 71 | |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 72 | dtls_srtp_transport1_->SignalRtcpPacketReceived.connect( |
| 73 | &transport_observer1_, |
| 74 | &webrtc::TransportObserver::OnRtcpPacketReceived); |
Zhi Huang | f2d7beb | 2017-11-20 14:35:11 -0800 | [diff] [blame] | 75 | dtls_srtp_transport1_->SignalReadyToSend.connect( |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 76 | &transport_observer1_, &webrtc::TransportObserver::OnReadyToSend); |
Zhi Huang | f2d7beb | 2017-11-20 14:35:11 -0800 | [diff] [blame] | 77 | |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 78 | dtls_srtp_transport2_->SignalRtcpPacketReceived.connect( |
| 79 | &transport_observer2_, |
| 80 | &webrtc::TransportObserver::OnRtcpPacketReceived); |
Zhi Huang | f2d7beb | 2017-11-20 14:35:11 -0800 | [diff] [blame] | 81 | dtls_srtp_transport2_->SignalReadyToSend.connect( |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 82 | &transport_observer2_, &webrtc::TransportObserver::OnReadyToSend); |
| 83 | webrtc::RtpDemuxerCriteria demuxer_criteria; |
| 84 | // 0x00 is the payload type used in kPcmuFrame. |
| 85 | demuxer_criteria.payload_types = {0x00}; |
| 86 | dtls_srtp_transport1_->RegisterRtpDemuxerSink(demuxer_criteria, |
| 87 | &transport_observer1_); |
| 88 | dtls_srtp_transport2_->RegisterRtpDemuxerSink(demuxer_criteria, |
| 89 | &transport_observer2_); |
Zhi Huang | f2d7beb | 2017-11-20 14:35:11 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | void CompleteDtlsHandshake(FakeDtlsTransport* fake_dtls1, |
| 93 | FakeDtlsTransport* fake_dtls2) { |
| 94 | auto cert1 = rtc::RTCCertificate::Create(std::unique_ptr<rtc::SSLIdentity>( |
| 95 | rtc::SSLIdentity::Generate("session1", rtc::KT_DEFAULT))); |
| 96 | fake_dtls1->SetLocalCertificate(cert1); |
| 97 | auto cert2 = rtc::RTCCertificate::Create(std::unique_ptr<rtc::SSLIdentity>( |
| 98 | rtc::SSLIdentity::Generate("session1", rtc::KT_DEFAULT))); |
| 99 | fake_dtls2->SetLocalCertificate(cert2); |
| 100 | fake_dtls1->SetDestination(fake_dtls2); |
| 101 | } |
| 102 | |
| 103 | void SendRecvRtpPackets() { |
| 104 | ASSERT_TRUE(dtls_srtp_transport1_); |
| 105 | ASSERT_TRUE(dtls_srtp_transport2_); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 106 | ASSERT_TRUE(dtls_srtp_transport1_->IsSrtpActive()); |
| 107 | ASSERT_TRUE(dtls_srtp_transport2_->IsSrtpActive()); |
Zhi Huang | f2d7beb | 2017-11-20 14:35:11 -0800 | [diff] [blame] | 108 | |
| 109 | size_t rtp_len = sizeof(kPcmuFrame); |
| 110 | size_t packet_size = rtp_len + kRtpAuthTagLen; |
| 111 | rtc::Buffer rtp_packet_buffer(packet_size); |
| 112 | char* rtp_packet_data = rtp_packet_buffer.data<char>(); |
| 113 | memcpy(rtp_packet_data, kPcmuFrame, rtp_len); |
| 114 | // In order to be able to run this test function multiple times we can not |
| 115 | // use the same sequence number twice. Increase the sequence number by one. |
| 116 | rtc::SetBE16(reinterpret_cast<uint8_t*>(rtp_packet_data) + 2, |
| 117 | ++sequence_number_); |
| 118 | rtc::CopyOnWriteBuffer rtp_packet1to2(rtp_packet_data, rtp_len, |
| 119 | packet_size); |
| 120 | rtc::CopyOnWriteBuffer rtp_packet2to1(rtp_packet_data, rtp_len, |
| 121 | packet_size); |
| 122 | |
| 123 | rtc::PacketOptions options; |
| 124 | // Send a packet from |srtp_transport1_| to |srtp_transport2_| and verify |
| 125 | // that the packet can be successfully received and decrypted. |
Taylor Brandstetter | 53e43b3 | 2018-04-19 14:44:12 -0700 | [diff] [blame] | 126 | int prev_received_packets = transport_observer2_.rtp_count(); |
Zhi Huang | f2d7beb | 2017-11-20 14:35:11 -0800 | [diff] [blame] | 127 | ASSERT_TRUE(dtls_srtp_transport1_->SendRtpPacket(&rtp_packet1to2, options, |
| 128 | cricket::PF_SRTP_BYPASS)); |
| 129 | ASSERT_TRUE(transport_observer2_.last_recv_rtp_packet().data()); |
| 130 | EXPECT_EQ(0, memcmp(transport_observer2_.last_recv_rtp_packet().data(), |
| 131 | kPcmuFrame, rtp_len)); |
Taylor Brandstetter | 53e43b3 | 2018-04-19 14:44:12 -0700 | [diff] [blame] | 132 | EXPECT_EQ(prev_received_packets + 1, transport_observer2_.rtp_count()); |
| 133 | |
| 134 | prev_received_packets = transport_observer1_.rtp_count(); |
Zhi Huang | f2d7beb | 2017-11-20 14:35:11 -0800 | [diff] [blame] | 135 | ASSERT_TRUE(dtls_srtp_transport2_->SendRtpPacket(&rtp_packet2to1, options, |
| 136 | cricket::PF_SRTP_BYPASS)); |
| 137 | ASSERT_TRUE(transport_observer1_.last_recv_rtp_packet().data()); |
| 138 | EXPECT_EQ(0, memcmp(transport_observer1_.last_recv_rtp_packet().data(), |
| 139 | kPcmuFrame, rtp_len)); |
Taylor Brandstetter | 53e43b3 | 2018-04-19 14:44:12 -0700 | [diff] [blame] | 140 | EXPECT_EQ(prev_received_packets + 1, transport_observer1_.rtp_count()); |
Zhi Huang | f2d7beb | 2017-11-20 14:35:11 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | void SendRecvRtcpPackets() { |
| 144 | size_t rtcp_len = sizeof(kRtcpReport); |
| 145 | size_t packet_size = rtcp_len + 4 + kRtpAuthTagLen; |
| 146 | rtc::Buffer rtcp_packet_buffer(packet_size); |
| 147 | |
| 148 | // TODO(zhihuang): Remove the extra copy when the SendRtpPacket method |
| 149 | // doesn't take the CopyOnWriteBuffer by pointer. |
| 150 | rtc::CopyOnWriteBuffer rtcp_packet1to2(kRtcpReport, rtcp_len, packet_size); |
| 151 | rtc::CopyOnWriteBuffer rtcp_packet2to1(kRtcpReport, rtcp_len, packet_size); |
| 152 | |
| 153 | rtc::PacketOptions options; |
| 154 | // Send a packet from |srtp_transport1_| to |srtp_transport2_| and verify |
| 155 | // that the packet can be successfully received and decrypted. |
Taylor Brandstetter | 53e43b3 | 2018-04-19 14:44:12 -0700 | [diff] [blame] | 156 | int prev_received_packets = transport_observer2_.rtcp_count(); |
Zhi Huang | f2d7beb | 2017-11-20 14:35:11 -0800 | [diff] [blame] | 157 | ASSERT_TRUE(dtls_srtp_transport1_->SendRtcpPacket(&rtcp_packet1to2, options, |
| 158 | cricket::PF_SRTP_BYPASS)); |
| 159 | ASSERT_TRUE(transport_observer2_.last_recv_rtcp_packet().data()); |
| 160 | EXPECT_EQ(0, memcmp(transport_observer2_.last_recv_rtcp_packet().data(), |
| 161 | kRtcpReport, rtcp_len)); |
Taylor Brandstetter | 53e43b3 | 2018-04-19 14:44:12 -0700 | [diff] [blame] | 162 | EXPECT_EQ(prev_received_packets + 1, transport_observer2_.rtcp_count()); |
Zhi Huang | f2d7beb | 2017-11-20 14:35:11 -0800 | [diff] [blame] | 163 | |
| 164 | // Do the same thing in the opposite direction; |
Taylor Brandstetter | 53e43b3 | 2018-04-19 14:44:12 -0700 | [diff] [blame] | 165 | prev_received_packets = transport_observer1_.rtcp_count(); |
Zhi Huang | f2d7beb | 2017-11-20 14:35:11 -0800 | [diff] [blame] | 166 | ASSERT_TRUE(dtls_srtp_transport2_->SendRtcpPacket(&rtcp_packet2to1, options, |
| 167 | cricket::PF_SRTP_BYPASS)); |
| 168 | ASSERT_TRUE(transport_observer1_.last_recv_rtcp_packet().data()); |
| 169 | EXPECT_EQ(0, memcmp(transport_observer1_.last_recv_rtcp_packet().data(), |
| 170 | kRtcpReport, rtcp_len)); |
Taylor Brandstetter | 53e43b3 | 2018-04-19 14:44:12 -0700 | [diff] [blame] | 171 | EXPECT_EQ(prev_received_packets + 1, transport_observer1_.rtcp_count()); |
Zhi Huang | f2d7beb | 2017-11-20 14:35:11 -0800 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | void SendRecvRtpPacketsWithHeaderExtension( |
| 175 | const std::vector<int>& encrypted_header_ids) { |
| 176 | ASSERT_TRUE(dtls_srtp_transport1_); |
| 177 | ASSERT_TRUE(dtls_srtp_transport2_); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 178 | ASSERT_TRUE(dtls_srtp_transport1_->IsSrtpActive()); |
| 179 | ASSERT_TRUE(dtls_srtp_transport2_->IsSrtpActive()); |
Zhi Huang | f2d7beb | 2017-11-20 14:35:11 -0800 | [diff] [blame] | 180 | |
| 181 | size_t rtp_len = sizeof(kPcmuFrameWithExtensions); |
| 182 | size_t packet_size = rtp_len + kRtpAuthTagLen; |
| 183 | rtc::Buffer rtp_packet_buffer(packet_size); |
| 184 | char* rtp_packet_data = rtp_packet_buffer.data<char>(); |
| 185 | memcpy(rtp_packet_data, kPcmuFrameWithExtensions, rtp_len); |
| 186 | // In order to be able to run this test function multiple times we can not |
| 187 | // use the same sequence number twice. Increase the sequence number by one. |
| 188 | rtc::SetBE16(reinterpret_cast<uint8_t*>(rtp_packet_data) + 2, |
| 189 | ++sequence_number_); |
| 190 | rtc::CopyOnWriteBuffer rtp_packet1to2(rtp_packet_data, rtp_len, |
| 191 | packet_size); |
| 192 | rtc::CopyOnWriteBuffer rtp_packet2to1(rtp_packet_data, rtp_len, |
| 193 | packet_size); |
| 194 | |
| 195 | char original_rtp_data[sizeof(kPcmuFrameWithExtensions)]; |
| 196 | memcpy(original_rtp_data, rtp_packet_data, rtp_len); |
| 197 | |
| 198 | rtc::PacketOptions options; |
| 199 | // Send a packet from |srtp_transport1_| to |srtp_transport2_| and verify |
| 200 | // that the packet can be successfully received and decrypted. |
| 201 | ASSERT_TRUE(dtls_srtp_transport1_->SendRtpPacket(&rtp_packet1to2, options, |
| 202 | cricket::PF_SRTP_BYPASS)); |
| 203 | ASSERT_TRUE(transport_observer2_.last_recv_rtp_packet().data()); |
| 204 | EXPECT_EQ(0, memcmp(transport_observer2_.last_recv_rtp_packet().data(), |
| 205 | original_rtp_data, rtp_len)); |
| 206 | // Get the encrypted packet from underneath packet transport and verify the |
| 207 | // data and header extension are actually encrypted. |
| 208 | auto fake_dtls_transport = static_cast<FakeDtlsTransport*>( |
| 209 | dtls_srtp_transport1_->rtp_packet_transport()); |
| 210 | auto fake_ice_transport = |
| 211 | static_cast<FakeIceTransport*>(fake_dtls_transport->ice_transport()); |
| 212 | EXPECT_NE(0, memcmp(fake_ice_transport->last_sent_packet().data(), |
| 213 | original_rtp_data, rtp_len)); |
| 214 | CompareHeaderExtensions(reinterpret_cast<const char*>( |
| 215 | fake_ice_transport->last_sent_packet().data()), |
| 216 | fake_ice_transport->last_sent_packet().size(), |
| 217 | original_rtp_data, rtp_len, encrypted_header_ids, |
| 218 | false); |
| 219 | |
| 220 | // Do the same thing in the opposite direction. |
| 221 | ASSERT_TRUE(dtls_srtp_transport2_->SendRtpPacket(&rtp_packet2to1, options, |
| 222 | cricket::PF_SRTP_BYPASS)); |
| 223 | ASSERT_TRUE(transport_observer1_.last_recv_rtp_packet().data()); |
| 224 | EXPECT_EQ(0, memcmp(transport_observer1_.last_recv_rtp_packet().data(), |
| 225 | original_rtp_data, rtp_len)); |
| 226 | // Get the encrypted packet from underneath packet transport and verify the |
| 227 | // data and header extension are actually encrypted. |
| 228 | fake_dtls_transport = static_cast<FakeDtlsTransport*>( |
| 229 | dtls_srtp_transport2_->rtp_packet_transport()); |
| 230 | fake_ice_transport = |
| 231 | static_cast<FakeIceTransport*>(fake_dtls_transport->ice_transport()); |
| 232 | EXPECT_NE(0, memcmp(fake_ice_transport->last_sent_packet().data(), |
| 233 | original_rtp_data, rtp_len)); |
| 234 | CompareHeaderExtensions(reinterpret_cast<const char*>( |
| 235 | fake_ice_transport->last_sent_packet().data()), |
| 236 | fake_ice_transport->last_sent_packet().size(), |
| 237 | original_rtp_data, rtp_len, encrypted_header_ids, |
| 238 | false); |
| 239 | } |
| 240 | |
| 241 | void SendRecvPackets() { |
| 242 | SendRecvRtpPackets(); |
| 243 | SendRecvRtcpPackets(); |
| 244 | } |
| 245 | |
| 246 | std::unique_ptr<DtlsSrtpTransport> dtls_srtp_transport1_; |
| 247 | std::unique_ptr<DtlsSrtpTransport> dtls_srtp_transport2_; |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 248 | webrtc::TransportObserver transport_observer1_; |
| 249 | webrtc::TransportObserver transport_observer2_; |
Zhi Huang | f2d7beb | 2017-11-20 14:35:11 -0800 | [diff] [blame] | 250 | |
| 251 | int sequence_number_ = 0; |
| 252 | }; |
| 253 | |
| 254 | // Tests that if RTCP muxing is enabled and transports are set after RTP |
| 255 | // transport finished the handshake, SRTP is set up. |
| 256 | TEST_F(DtlsSrtpTransportTest, SetTransportsAfterHandshakeCompleteWithRtcpMux) { |
| 257 | auto rtp_dtls1 = rtc::MakeUnique<FakeDtlsTransport>( |
| 258 | "video", cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 259 | auto rtp_dtls2 = rtc::MakeUnique<FakeDtlsTransport>( |
| 260 | "video", cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 261 | |
| 262 | MakeDtlsSrtpTransports(rtp_dtls1.get(), nullptr, rtp_dtls2.get(), nullptr, |
| 263 | /*rtcp_mux_enabled=*/true); |
| 264 | |
| 265 | auto rtp_dtls3 = rtc::MakeUnique<FakeDtlsTransport>( |
| 266 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 267 | auto rtp_dtls4 = rtc::MakeUnique<FakeDtlsTransport>( |
| 268 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 269 | |
| 270 | CompleteDtlsHandshake(rtp_dtls3.get(), rtp_dtls4.get()); |
| 271 | |
| 272 | dtls_srtp_transport1_->SetDtlsTransports(rtp_dtls3.get(), nullptr); |
| 273 | dtls_srtp_transport2_->SetDtlsTransports(rtp_dtls4.get(), nullptr); |
| 274 | |
| 275 | SendRecvPackets(); |
| 276 | } |
| 277 | |
| 278 | // Tests that if RTCP muxing is not enabled and transports are set after both |
| 279 | // RTP and RTCP transports finished the handshake, SRTP is set up. |
| 280 | TEST_F(DtlsSrtpTransportTest, |
| 281 | SetTransportsAfterHandshakeCompleteWithoutRtcpMux) { |
| 282 | auto rtp_dtls1 = rtc::MakeUnique<FakeDtlsTransport>( |
| 283 | "video", cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 284 | auto rtcp_dtls1 = rtc::MakeUnique<FakeDtlsTransport>( |
| 285 | "video", cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 286 | auto rtp_dtls2 = rtc::MakeUnique<FakeDtlsTransport>( |
| 287 | "video", cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 288 | auto rtcp_dtls2 = rtc::MakeUnique<FakeDtlsTransport>( |
| 289 | "video", cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 290 | |
| 291 | MakeDtlsSrtpTransports(rtp_dtls1.get(), rtcp_dtls1.get(), rtp_dtls2.get(), |
| 292 | rtcp_dtls2.get(), /*rtcp_mux_enabled=*/false); |
| 293 | |
| 294 | auto rtp_dtls3 = rtc::MakeUnique<FakeDtlsTransport>( |
| 295 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 296 | auto rtcp_dtls3 = rtc::MakeUnique<FakeDtlsTransport>( |
| 297 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 298 | auto rtp_dtls4 = rtc::MakeUnique<FakeDtlsTransport>( |
| 299 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 300 | auto rtcp_dtls4 = rtc::MakeUnique<FakeDtlsTransport>( |
| 301 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 302 | CompleteDtlsHandshake(rtp_dtls3.get(), rtp_dtls4.get()); |
| 303 | CompleteDtlsHandshake(rtcp_dtls3.get(), rtcp_dtls4.get()); |
| 304 | |
| 305 | dtls_srtp_transport1_->SetDtlsTransports(rtp_dtls3.get(), rtcp_dtls3.get()); |
| 306 | dtls_srtp_transport2_->SetDtlsTransports(rtp_dtls4.get(), rtcp_dtls4.get()); |
| 307 | |
| 308 | SendRecvPackets(); |
| 309 | } |
| 310 | |
| 311 | // Tests if RTCP muxing is enabled, SRTP is set up as soon as the RTP DTLS |
| 312 | // handshake is finished. |
| 313 | TEST_F(DtlsSrtpTransportTest, SetTransportsBeforeHandshakeCompleteWithRtcpMux) { |
| 314 | auto rtp_dtls1 = rtc::MakeUnique<FakeDtlsTransport>( |
| 315 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 316 | auto rtcp_dtls1 = rtc::MakeUnique<FakeDtlsTransport>( |
| 317 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 318 | auto rtp_dtls2 = rtc::MakeUnique<FakeDtlsTransport>( |
| 319 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 320 | auto rtcp_dtls2 = rtc::MakeUnique<FakeDtlsTransport>( |
| 321 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 322 | |
| 323 | MakeDtlsSrtpTransports(rtp_dtls1.get(), rtcp_dtls1.get(), rtp_dtls2.get(), |
| 324 | rtcp_dtls2.get(), |
| 325 | /*rtcp_mux_enabled=*/false); |
| 326 | |
| 327 | dtls_srtp_transport1_->SetRtcpMuxEnabled(true); |
| 328 | dtls_srtp_transport2_->SetRtcpMuxEnabled(true); |
| 329 | CompleteDtlsHandshake(rtp_dtls1.get(), rtp_dtls2.get()); |
| 330 | SendRecvPackets(); |
| 331 | } |
| 332 | |
| 333 | // Tests if RTCP muxing is not enabled, SRTP is set up when both the RTP and |
| 334 | // RTCP DTLS handshake are finished. |
| 335 | TEST_F(DtlsSrtpTransportTest, |
| 336 | SetTransportsBeforeHandshakeCompleteWithoutRtcpMux) { |
| 337 | auto rtp_dtls1 = rtc::MakeUnique<FakeDtlsTransport>( |
| 338 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 339 | auto rtcp_dtls1 = rtc::MakeUnique<FakeDtlsTransport>( |
| 340 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 341 | auto rtp_dtls2 = rtc::MakeUnique<FakeDtlsTransport>( |
| 342 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 343 | auto rtcp_dtls2 = rtc::MakeUnique<FakeDtlsTransport>( |
| 344 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 345 | |
| 346 | MakeDtlsSrtpTransports(rtp_dtls1.get(), rtcp_dtls1.get(), rtp_dtls2.get(), |
| 347 | rtcp_dtls2.get(), /*rtcp_mux_enabled=*/false); |
| 348 | |
| 349 | CompleteDtlsHandshake(rtp_dtls1.get(), rtp_dtls2.get()); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 350 | EXPECT_FALSE(dtls_srtp_transport1_->IsSrtpActive()); |
| 351 | EXPECT_FALSE(dtls_srtp_transport2_->IsSrtpActive()); |
Zhi Huang | f2d7beb | 2017-11-20 14:35:11 -0800 | [diff] [blame] | 352 | CompleteDtlsHandshake(rtcp_dtls1.get(), rtcp_dtls2.get()); |
| 353 | SendRecvPackets(); |
| 354 | } |
| 355 | |
| 356 | // Tests that if the DtlsTransport underneath is changed, the previous DTLS-SRTP |
| 357 | // context will be reset and will be re-setup once the new transports' handshake |
| 358 | // complete. |
| 359 | TEST_F(DtlsSrtpTransportTest, DtlsSrtpResetAfterDtlsTransportChange) { |
| 360 | auto rtp_dtls1 = rtc::MakeUnique<FakeDtlsTransport>( |
| 361 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 362 | auto rtp_dtls2 = rtc::MakeUnique<FakeDtlsTransport>( |
| 363 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 364 | |
| 365 | MakeDtlsSrtpTransports(rtp_dtls1.get(), nullptr, rtp_dtls2.get(), nullptr, |
| 366 | /*rtcp_mux_enabled=*/true); |
| 367 | |
| 368 | CompleteDtlsHandshake(rtp_dtls1.get(), rtp_dtls2.get()); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 369 | EXPECT_TRUE(dtls_srtp_transport1_->IsSrtpActive()); |
| 370 | EXPECT_TRUE(dtls_srtp_transport2_->IsSrtpActive()); |
Zhi Huang | f2d7beb | 2017-11-20 14:35:11 -0800 | [diff] [blame] | 371 | |
| 372 | auto rtp_dtls3 = rtc::MakeUnique<FakeDtlsTransport>( |
| 373 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 374 | auto rtp_dtls4 = rtc::MakeUnique<FakeDtlsTransport>( |
| 375 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 376 | |
| 377 | // The previous context is reset. |
| 378 | dtls_srtp_transport1_->SetDtlsTransports(rtp_dtls3.get(), nullptr); |
| 379 | dtls_srtp_transport2_->SetDtlsTransports(rtp_dtls4.get(), nullptr); |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 380 | EXPECT_FALSE(dtls_srtp_transport1_->IsSrtpActive()); |
| 381 | EXPECT_FALSE(dtls_srtp_transport2_->IsSrtpActive()); |
Zhi Huang | f2d7beb | 2017-11-20 14:35:11 -0800 | [diff] [blame] | 382 | |
| 383 | // Re-setup. |
| 384 | CompleteDtlsHandshake(rtp_dtls3.get(), rtp_dtls4.get()); |
| 385 | SendRecvPackets(); |
| 386 | } |
| 387 | |
| 388 | // Tests if only the RTP DTLS handshake complete, and then RTCP muxing is |
| 389 | // enabled, SRTP is set up. |
| 390 | TEST_F(DtlsSrtpTransportTest, |
| 391 | RtcpMuxEnabledAfterRtpTransportHandshakeComplete) { |
| 392 | auto rtp_dtls1 = rtc::MakeUnique<FakeDtlsTransport>( |
| 393 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 394 | auto rtcp_dtls1 = rtc::MakeUnique<FakeDtlsTransport>( |
| 395 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 396 | auto rtp_dtls2 = rtc::MakeUnique<FakeDtlsTransport>( |
| 397 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 398 | auto rtcp_dtls2 = rtc::MakeUnique<FakeDtlsTransport>( |
| 399 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 400 | |
| 401 | MakeDtlsSrtpTransports(rtp_dtls1.get(), rtcp_dtls1.get(), rtp_dtls2.get(), |
| 402 | rtcp_dtls2.get(), /*rtcp_mux_enabled=*/false); |
| 403 | |
| 404 | CompleteDtlsHandshake(rtp_dtls1.get(), rtp_dtls2.get()); |
| 405 | // Inactive because the RTCP transport handshake didn't complete. |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 406 | EXPECT_FALSE(dtls_srtp_transport1_->IsSrtpActive()); |
| 407 | EXPECT_FALSE(dtls_srtp_transport2_->IsSrtpActive()); |
Zhi Huang | f2d7beb | 2017-11-20 14:35:11 -0800 | [diff] [blame] | 408 | |
| 409 | dtls_srtp_transport1_->SetRtcpMuxEnabled(true); |
| 410 | dtls_srtp_transport2_->SetRtcpMuxEnabled(true); |
| 411 | // The transports should be active and be able to send packets when the |
| 412 | // RTCP muxing is enabled. |
| 413 | SendRecvPackets(); |
| 414 | } |
| 415 | |
| 416 | // Tests that when SetSend/RecvEncryptedHeaderExtensionIds is called, the SRTP |
| 417 | // sessions are updated with new encryped header extension IDs immediately. |
| 418 | TEST_F(DtlsSrtpTransportTest, EncryptedHeaderExtensionIdUpdated) { |
| 419 | auto rtp_dtls1 = rtc::MakeUnique<FakeDtlsTransport>( |
| 420 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 421 | auto rtp_dtls2 = rtc::MakeUnique<FakeDtlsTransport>( |
| 422 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 423 | |
| 424 | MakeDtlsSrtpTransports(rtp_dtls1.get(), nullptr, rtp_dtls2.get(), nullptr, |
| 425 | /*rtcp_mux_enabled=*/true); |
| 426 | CompleteDtlsHandshake(rtp_dtls1.get(), rtp_dtls2.get()); |
| 427 | |
| 428 | std::vector<int> encrypted_headers; |
| 429 | encrypted_headers.push_back(kHeaderExtensionIDs[0]); |
| 430 | encrypted_headers.push_back(kHeaderExtensionIDs[1]); |
Zhi Huang | f2d7beb | 2017-11-20 14:35:11 -0800 | [diff] [blame] | 431 | |
Zhi Huang | cd3fc5d | 2017-11-29 10:41:57 -0800 | [diff] [blame] | 432 | dtls_srtp_transport1_->UpdateSendEncryptedHeaderExtensionIds( |
| 433 | encrypted_headers); |
| 434 | dtls_srtp_transport1_->UpdateRecvEncryptedHeaderExtensionIds( |
| 435 | encrypted_headers); |
| 436 | dtls_srtp_transport2_->UpdateSendEncryptedHeaderExtensionIds( |
| 437 | encrypted_headers); |
| 438 | dtls_srtp_transport2_->UpdateRecvEncryptedHeaderExtensionIds( |
| 439 | encrypted_headers); |
Zhi Huang | f2d7beb | 2017-11-20 14:35:11 -0800 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | // Tests if RTCP muxing is enabled. DtlsSrtpTransport is ready to send once the |
| 443 | // RTP DtlsTransport is ready. |
| 444 | TEST_F(DtlsSrtpTransportTest, SignalReadyToSendFiredWithRtcpMux) { |
| 445 | auto rtp_dtls1 = rtc::MakeUnique<FakeDtlsTransport>( |
| 446 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 447 | auto rtp_dtls2 = rtc::MakeUnique<FakeDtlsTransport>( |
| 448 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 449 | |
| 450 | MakeDtlsSrtpTransports(rtp_dtls1.get(), nullptr, rtp_dtls2.get(), nullptr, |
| 451 | /*rtcp_mux_enabled=*/true); |
| 452 | |
| 453 | rtp_dtls1->SetDestination(rtp_dtls2.get()); |
| 454 | EXPECT_TRUE(transport_observer1_.ready_to_send()); |
| 455 | EXPECT_TRUE(transport_observer2_.ready_to_send()); |
| 456 | } |
| 457 | |
| 458 | // Tests if RTCP muxing is not enabled. DtlsSrtpTransport is ready to send once |
| 459 | // both the RTP and RTCP DtlsTransport are ready. |
| 460 | TEST_F(DtlsSrtpTransportTest, SignalReadyToSendFiredWithoutRtcpMux) { |
| 461 | auto rtp_dtls1 = rtc::MakeUnique<FakeDtlsTransport>( |
| 462 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 463 | auto rtcp_dtls1 = rtc::MakeUnique<FakeDtlsTransport>( |
| 464 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 465 | auto rtp_dtls2 = rtc::MakeUnique<FakeDtlsTransport>( |
| 466 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 467 | auto rtcp_dtls2 = rtc::MakeUnique<FakeDtlsTransport>( |
| 468 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 469 | |
| 470 | MakeDtlsSrtpTransports(rtp_dtls1.get(), rtcp_dtls1.get(), rtp_dtls2.get(), |
| 471 | rtcp_dtls2.get(), /*rtcp_mux_enabled=*/false); |
| 472 | |
| 473 | rtp_dtls1->SetDestination(rtp_dtls2.get()); |
| 474 | EXPECT_FALSE(transport_observer1_.ready_to_send()); |
| 475 | EXPECT_FALSE(transport_observer2_.ready_to_send()); |
| 476 | |
| 477 | rtcp_dtls1->SetDestination(rtcp_dtls2.get()); |
| 478 | EXPECT_TRUE(transport_observer1_.ready_to_send()); |
| 479 | EXPECT_TRUE(transport_observer2_.ready_to_send()); |
| 480 | } |
Taylor Brandstetter | 53e43b3 | 2018-04-19 14:44:12 -0700 | [diff] [blame] | 481 | |
| 482 | // Test that if an endpoint "fully" enables RTCP mux, setting the RTCP |
| 483 | // transport to null, it *doesn't* reset its SRTP context. That would cause the |
| 484 | // ROC and SRTCP index to be reset, causing replay detection and other errors |
| 485 | // when attempting to unprotect packets. |
| 486 | // Regression test for bugs.webrtc.org/8996 |
| 487 | TEST_F(DtlsSrtpTransportTest, SrtpSessionNotResetWhenRtcpTransportRemoved) { |
| 488 | auto rtp_dtls1 = rtc::MakeUnique<FakeDtlsTransport>( |
| 489 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 490 | auto rtcp_dtls1 = rtc::MakeUnique<FakeDtlsTransport>( |
| 491 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 492 | auto rtp_dtls2 = rtc::MakeUnique<FakeDtlsTransport>( |
| 493 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 494 | auto rtcp_dtls2 = rtc::MakeUnique<FakeDtlsTransport>( |
| 495 | "audio", cricket::ICE_CANDIDATE_COMPONENT_RTCP); |
| 496 | |
| 497 | MakeDtlsSrtpTransports(rtp_dtls1.get(), rtcp_dtls1.get(), rtp_dtls2.get(), |
| 498 | rtcp_dtls2.get(), /*rtcp_mux_enabled=*/true); |
| 499 | CompleteDtlsHandshake(rtp_dtls1.get(), rtp_dtls2.get()); |
| 500 | CompleteDtlsHandshake(rtcp_dtls1.get(), rtcp_dtls2.get()); |
| 501 | |
| 502 | // Send some RTCP packets, causing the SRTCP index to be incremented. |
| 503 | SendRecvRtcpPackets(); |
| 504 | |
| 505 | // Set RTCP transport to null, which previously would trigger this problem. |
| 506 | dtls_srtp_transport1_->SetDtlsTransports(rtp_dtls1.get(), nullptr); |
| 507 | |
| 508 | // Attempt to send more RTCP packets. If the issue occurred, one side would |
| 509 | // reset its context while the other would not, causing replay detection |
| 510 | // errors when a packet with a duplicate SRTCP index is received. |
| 511 | SendRecvRtcpPackets(); |
| 512 | } |