Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [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 | |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 11 | #include <memory> |
| 12 | |
Karl Wiberg | 1b0eae3 | 2017-10-17 14:48:54 +0200 | [diff] [blame] | 13 | #include "api/audio_codecs/builtin_audio_decoder_factory.h" |
| 14 | #include "api/audio_codecs/builtin_audio_encoder_factory.h" |
Mirko Bonadei | 2ff3f49 | 2018-11-22 09:00:13 +0100 | [diff] [blame] | 15 | #include "api/create_peerconnection_factory.h" |
Anders Carlsson | 6753795 | 2018-05-03 11:28:29 +0200 | [diff] [blame] | 16 | #include "api/video_codecs/builtin_video_decoder_factory.h" |
| 17 | #include "api/video_codecs/builtin_video_encoder_factory.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 18 | #include "p2p/base/fake_port_allocator.h" |
| 19 | #include "pc/media_session.h" |
| 20 | #include "pc/peer_connection_wrapper.h" |
| 21 | #include "pc/sdp_utils.h" |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 22 | #ifdef WEBRTC_ANDROID |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 23 | #include "pc/test/android_test_initializer.h" |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 24 | #endif |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 25 | #include "pc/test/fake_audio_capture_module.h" |
| 26 | #include "pc/test/fake_rtc_certificate_generator.h" |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 27 | #include "rtc_base/gunit.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 28 | #include "rtc_base/virtual_socket_server.h" |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 29 | |
| 30 | namespace webrtc { |
| 31 | |
| 32 | using RTCConfiguration = PeerConnectionInterface::RTCConfiguration; |
Steve Anton | 8a63f78 | 2017-10-23 13:08:53 -0700 | [diff] [blame] | 33 | using RTCOfferAnswerOptions = PeerConnectionInterface::RTCOfferAnswerOptions; |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 34 | using ::testing::Combine; |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 35 | using ::testing::Values; |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 36 | |
| 37 | constexpr int kGenerateCertTimeout = 1000; |
| 38 | |
Steve Anton | 71182f4 | 2018-01-19 14:59:54 -0800 | [diff] [blame] | 39 | class PeerConnectionCryptoBaseTest : public ::testing::Test { |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 40 | protected: |
| 41 | typedef std::unique_ptr<PeerConnectionWrapper> WrapperPtr; |
| 42 | |
Steve Anton | 71182f4 | 2018-01-19 14:59:54 -0800 | [diff] [blame] | 43 | explicit PeerConnectionCryptoBaseTest(SdpSemantics sdp_semantics) |
| 44 | : vss_(new rtc::VirtualSocketServer()), |
| 45 | main_(vss_.get()), |
| 46 | sdp_semantics_(sdp_semantics) { |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 47 | #ifdef WEBRTC_ANDROID |
| 48 | InitializeAndroidObjects(); |
| 49 | #endif |
| 50 | pc_factory_ = CreatePeerConnectionFactory( |
| 51 | rtc::Thread::Current(), rtc::Thread::Current(), rtc::Thread::Current(), |
Karl Wiberg | 1b0eae3 | 2017-10-17 14:48:54 +0200 | [diff] [blame] | 52 | FakeAudioCaptureModule::Create(), CreateBuiltinAudioEncoderFactory(), |
Anders Carlsson | 6753795 | 2018-05-03 11:28:29 +0200 | [diff] [blame] | 53 | CreateBuiltinAudioDecoderFactory(), CreateBuiltinVideoEncoderFactory(), |
| 54 | CreateBuiltinVideoDecoderFactory(), nullptr /* audio_mixer */, |
| 55 | nullptr /* audio_processing */); |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Steve Anton | 8a63f78 | 2017-10-23 13:08:53 -0700 | [diff] [blame] | 58 | WrapperPtr CreatePeerConnection() { |
| 59 | return CreatePeerConnection(RTCConfiguration()); |
| 60 | } |
| 61 | |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 62 | WrapperPtr CreatePeerConnection(const RTCConfiguration& config) { |
| 63 | return CreatePeerConnection(config, nullptr); |
| 64 | } |
| 65 | |
| 66 | WrapperPtr CreatePeerConnection( |
| 67 | const RTCConfiguration& config, |
| 68 | std::unique_ptr<rtc::RTCCertificateGeneratorInterface> cert_gen) { |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 69 | auto fake_port_allocator = std::make_unique<cricket::FakePortAllocator>( |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 70 | rtc::Thread::Current(), nullptr); |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 71 | auto observer = std::make_unique<MockPeerConnectionObserver>(); |
Steve Anton | 71182f4 | 2018-01-19 14:59:54 -0800 | [diff] [blame] | 72 | RTCConfiguration modified_config = config; |
| 73 | modified_config.sdp_semantics = sdp_semantics_; |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 74 | auto pc = pc_factory_->CreatePeerConnection( |
Steve Anton | 71182f4 | 2018-01-19 14:59:54 -0800 | [diff] [blame] | 75 | modified_config, std::move(fake_port_allocator), std::move(cert_gen), |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 76 | observer.get()); |
| 77 | if (!pc) { |
| 78 | return nullptr; |
| 79 | } |
| 80 | |
Yves Gerey | 4e93329 | 2018-10-31 15:36:05 +0100 | [diff] [blame] | 81 | observer->SetPeerConnectionInterface(pc.get()); |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 82 | return std::make_unique<PeerConnectionWrapper>(pc_factory_, pc, |
| 83 | std::move(observer)); |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | // Accepts the same arguments as CreatePeerConnection and adds default audio |
| 87 | // and video tracks. |
| 88 | template <typename... Args> |
| 89 | WrapperPtr CreatePeerConnectionWithAudioVideo(Args&&... args) { |
| 90 | auto wrapper = CreatePeerConnection(std::forward<Args>(args)...); |
| 91 | if (!wrapper) { |
| 92 | return nullptr; |
| 93 | } |
Steve Anton | 8d3444d | 2017-10-20 15:30:51 -0700 | [diff] [blame] | 94 | wrapper->AddAudioTrack("a"); |
| 95 | wrapper->AddVideoTrack("v"); |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 96 | return wrapper; |
| 97 | } |
| 98 | |
Steve Anton | 8a63f78 | 2017-10-23 13:08:53 -0700 | [diff] [blame] | 99 | cricket::ConnectionRole& AudioConnectionRole( |
| 100 | cricket::SessionDescription* desc) { |
| 101 | return ConnectionRoleFromContent(desc, cricket::GetFirstAudioContent(desc)); |
| 102 | } |
| 103 | |
| 104 | cricket::ConnectionRole& VideoConnectionRole( |
| 105 | cricket::SessionDescription* desc) { |
| 106 | return ConnectionRoleFromContent(desc, cricket::GetFirstVideoContent(desc)); |
| 107 | } |
| 108 | |
| 109 | cricket::ConnectionRole& ConnectionRoleFromContent( |
| 110 | cricket::SessionDescription* desc, |
| 111 | cricket::ContentInfo* content) { |
| 112 | RTC_DCHECK(content); |
| 113 | auto* transport_info = desc->GetTransportInfoByName(content->name); |
| 114 | RTC_DCHECK(transport_info); |
| 115 | return transport_info->description.connection_role; |
| 116 | } |
| 117 | |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 118 | std::unique_ptr<rtc::VirtualSocketServer> vss_; |
| 119 | rtc::AutoSocketServerThread main_; |
| 120 | rtc::scoped_refptr<PeerConnectionFactoryInterface> pc_factory_; |
Steve Anton | 71182f4 | 2018-01-19 14:59:54 -0800 | [diff] [blame] | 121 | const SdpSemantics sdp_semantics_; |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | SdpContentPredicate HaveDtlsFingerprint() { |
| 125 | return [](const cricket::ContentInfo* content, |
| 126 | const cricket::TransportInfo* transport) { |
| 127 | return transport->description.identity_fingerprint != nullptr; |
| 128 | }; |
| 129 | } |
| 130 | |
Harald Alvestrand | 0d01841 | 2021-11-04 13:52:31 +0000 | [diff] [blame] | 131 | SdpContentPredicate HaveSdesCryptos() { |
| 132 | return [](const cricket::ContentInfo* content, |
| 133 | const cricket::TransportInfo* transport) { |
| 134 | return !content->media_description()->cryptos().empty(); |
| 135 | }; |
| 136 | } |
| 137 | |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 138 | SdpContentPredicate HaveProtocol(const std::string& protocol) { |
| 139 | return [protocol](const cricket::ContentInfo* content, |
| 140 | const cricket::TransportInfo* transport) { |
Steve Anton | b1c1de1 | 2017-12-21 15:14:30 -0800 | [diff] [blame] | 141 | return content->media_description()->protocol() == protocol; |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 142 | }; |
| 143 | } |
| 144 | |
Harald Alvestrand | 0d01841 | 2021-11-04 13:52:31 +0000 | [diff] [blame] | 145 | SdpContentPredicate HaveSdesGcmCryptos(size_t num_crypto_suites) { |
| 146 | return [num_crypto_suites](const cricket::ContentInfo* content, |
| 147 | const cricket::TransportInfo* transport) { |
| 148 | const auto& cryptos = content->media_description()->cryptos(); |
| 149 | if (cryptos.size() != num_crypto_suites) { |
| 150 | return false; |
| 151 | } |
| 152 | for (size_t i = 0; i < cryptos.size(); ++i) { |
| 153 | if (cryptos[i].key_params.size() == 67U && |
| 154 | cryptos[i].cipher_suite == "AEAD_AES_256_GCM") |
| 155 | return true; |
| 156 | } |
| 157 | return false; |
| 158 | }; |
| 159 | } |
| 160 | |
Steve Anton | 71182f4 | 2018-01-19 14:59:54 -0800 | [diff] [blame] | 161 | class PeerConnectionCryptoTest |
| 162 | : public PeerConnectionCryptoBaseTest, |
| 163 | public ::testing::WithParamInterface<SdpSemantics> { |
| 164 | protected: |
| 165 | PeerConnectionCryptoTest() : PeerConnectionCryptoBaseTest(GetParam()) {} |
| 166 | }; |
| 167 | |
Harald Alvestrand | 0d01841 | 2021-11-04 13:52:31 +0000 | [diff] [blame] | 168 | SdpContentMutator RemoveSdesCryptos() { |
| 169 | return [](cricket::ContentInfo* content, cricket::TransportInfo* transport) { |
| 170 | content->media_description()->set_cryptos({}); |
| 171 | }; |
| 172 | } |
| 173 | |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 174 | SdpContentMutator RemoveDtlsFingerprint() { |
| 175 | return [](cricket::ContentInfo* content, cricket::TransportInfo* transport) { |
| 176 | transport->description.identity_fingerprint.reset(); |
| 177 | }; |
| 178 | } |
| 179 | |
Harald Alvestrand | 0d01841 | 2021-11-04 13:52:31 +0000 | [diff] [blame] | 180 | // When DTLS is enabled, the SDP offer/answer should have a DTLS fingerprint and |
| 181 | // no SDES cryptos. |
Steve Anton | 71182f4 | 2018-01-19 14:59:54 -0800 | [diff] [blame] | 182 | TEST_P(PeerConnectionCryptoTest, CorrectCryptoInOfferWhenDtlsEnabled) { |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 183 | RTCConfiguration config; |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 184 | auto caller = CreatePeerConnectionWithAudioVideo(config); |
| 185 | |
| 186 | auto offer = caller->CreateOffer(); |
| 187 | ASSERT_TRUE(offer); |
| 188 | |
| 189 | ASSERT_FALSE(offer->description()->contents().empty()); |
| 190 | EXPECT_TRUE(SdpContentsAll(HaveDtlsFingerprint(), offer->description())); |
Harald Alvestrand | 0d01841 | 2021-11-04 13:52:31 +0000 | [diff] [blame] | 191 | EXPECT_TRUE(SdpContentsNone(HaveSdesCryptos(), offer->description())); |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 192 | EXPECT_TRUE(SdpContentsAll(HaveProtocol(cricket::kMediaProtocolDtlsSavpf), |
| 193 | offer->description())); |
| 194 | } |
Steve Anton | 71182f4 | 2018-01-19 14:59:54 -0800 | [diff] [blame] | 195 | TEST_P(PeerConnectionCryptoTest, CorrectCryptoInAnswerWhenDtlsEnabled) { |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 196 | RTCConfiguration config; |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 197 | auto caller = CreatePeerConnectionWithAudioVideo(config); |
| 198 | auto callee = CreatePeerConnectionWithAudioVideo(config); |
| 199 | |
| 200 | callee->SetRemoteDescription(caller->CreateOffer()); |
| 201 | auto answer = callee->CreateAnswer(); |
| 202 | ASSERT_TRUE(answer); |
| 203 | |
| 204 | ASSERT_FALSE(answer->description()->contents().empty()); |
| 205 | EXPECT_TRUE(SdpContentsAll(HaveDtlsFingerprint(), answer->description())); |
Harald Alvestrand | 0d01841 | 2021-11-04 13:52:31 +0000 | [diff] [blame] | 206 | EXPECT_TRUE(SdpContentsNone(HaveSdesCryptos(), answer->description())); |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 207 | EXPECT_TRUE(SdpContentsAll(HaveProtocol(cricket::kMediaProtocolDtlsSavpf), |
| 208 | answer->description())); |
| 209 | } |
| 210 | |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 211 | // When encryption is disabled, the SDP offer/answer should have neither a DTLS |
| 212 | // fingerprint nor any SDES crypto options. |
Steve Anton | 71182f4 | 2018-01-19 14:59:54 -0800 | [diff] [blame] | 213 | TEST_P(PeerConnectionCryptoTest, CorrectCryptoInOfferWhenEncryptionDisabled) { |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 214 | PeerConnectionFactoryInterface::Options options; |
| 215 | options.disable_encryption = true; |
| 216 | pc_factory_->SetOptions(options); |
| 217 | |
| 218 | RTCConfiguration config; |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 219 | auto caller = CreatePeerConnectionWithAudioVideo(config); |
| 220 | |
| 221 | auto offer = caller->CreateOffer(); |
| 222 | ASSERT_TRUE(offer); |
| 223 | |
| 224 | ASSERT_FALSE(offer->description()->contents().empty()); |
Harald Alvestrand | 0d01841 | 2021-11-04 13:52:31 +0000 | [diff] [blame] | 225 | EXPECT_TRUE(SdpContentsNone(HaveSdesCryptos(), offer->description())); |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 226 | EXPECT_TRUE(SdpContentsNone(HaveDtlsFingerprint(), offer->description())); |
| 227 | EXPECT_TRUE(SdpContentsAll(HaveProtocol(cricket::kMediaProtocolAvpf), |
| 228 | offer->description())); |
| 229 | } |
Steve Anton | 71182f4 | 2018-01-19 14:59:54 -0800 | [diff] [blame] | 230 | TEST_P(PeerConnectionCryptoTest, CorrectCryptoInAnswerWhenEncryptionDisabled) { |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 231 | PeerConnectionFactoryInterface::Options options; |
| 232 | options.disable_encryption = true; |
| 233 | pc_factory_->SetOptions(options); |
| 234 | |
| 235 | RTCConfiguration config; |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 236 | auto caller = CreatePeerConnectionWithAudioVideo(config); |
| 237 | auto callee = CreatePeerConnectionWithAudioVideo(config); |
| 238 | |
| 239 | callee->SetRemoteDescription(caller->CreateOffer()); |
| 240 | auto answer = callee->CreateAnswer(); |
| 241 | ASSERT_TRUE(answer); |
| 242 | |
| 243 | ASSERT_FALSE(answer->description()->contents().empty()); |
Harald Alvestrand | 0d01841 | 2021-11-04 13:52:31 +0000 | [diff] [blame] | 244 | EXPECT_TRUE(SdpContentsNone(HaveSdesCryptos(), answer->description())); |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 245 | EXPECT_TRUE(SdpContentsNone(HaveDtlsFingerprint(), answer->description())); |
| 246 | EXPECT_TRUE(SdpContentsAll(HaveProtocol(cricket::kMediaProtocolAvpf), |
| 247 | answer->description())); |
| 248 | } |
| 249 | |
Steve Anton | 71182f4 | 2018-01-19 14:59:54 -0800 | [diff] [blame] | 250 | TEST_P(PeerConnectionCryptoTest, CanSetSdesGcmRemoteOfferAndLocalAnswer) { |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 251 | PeerConnectionFactoryInterface::Options options; |
Benjamin Wright | a54daf1 | 2018-10-11 15:33:17 -0700 | [diff] [blame] | 252 | options.crypto_options.srtp.enable_gcm_crypto_suites = true; |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 253 | pc_factory_->SetOptions(options); |
| 254 | |
| 255 | RTCConfiguration config; |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 256 | auto caller = CreatePeerConnectionWithAudioVideo(config); |
| 257 | auto callee = CreatePeerConnectionWithAudioVideo(config); |
| 258 | |
| 259 | auto offer = caller->CreateOffer(); |
| 260 | ASSERT_TRUE(offer); |
| 261 | ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer))); |
| 262 | |
| 263 | auto answer = callee->CreateAnswer(); |
| 264 | ASSERT_TRUE(answer); |
| 265 | ASSERT_TRUE(callee->SetLocalDescription(std::move(answer))); |
Harald Alvestrand | f9e502d | 2021-10-13 15:26:26 +0000 | [diff] [blame] | 266 | // Note - this test doesn't verify that Gcm is present, just that it |
| 267 | // does not caue a failure. |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | // The following group tests that two PeerConnections can successfully exchange |
| 271 | // an offer/answer when DTLS is on and that they will refuse any offer/answer |
| 272 | // applied locally/remotely if it does not include a DTLS fingerprint. |
Steve Anton | 71182f4 | 2018-01-19 14:59:54 -0800 | [diff] [blame] | 273 | TEST_P(PeerConnectionCryptoTest, ExchangeOfferAnswerWhenDtlsOn) { |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 274 | RTCConfiguration config; |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 275 | auto caller = CreatePeerConnectionWithAudioVideo(config); |
| 276 | auto callee = CreatePeerConnectionWithAudioVideo(config); |
| 277 | |
| 278 | auto offer = caller->CreateOfferAndSetAsLocal(); |
| 279 | ASSERT_TRUE(offer); |
| 280 | ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer))); |
| 281 | |
| 282 | auto answer = callee->CreateAnswerAndSetAsLocal(); |
| 283 | ASSERT_TRUE(answer); |
| 284 | ASSERT_TRUE(caller->SetRemoteDescription(std::move(answer))); |
| 285 | } |
Steve Anton | 71182f4 | 2018-01-19 14:59:54 -0800 | [diff] [blame] | 286 | TEST_P(PeerConnectionCryptoTest, |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 287 | FailToSetLocalOfferWithNoFingerprintWhenDtlsOn) { |
| 288 | RTCConfiguration config; |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 289 | auto caller = CreatePeerConnectionWithAudioVideo(config); |
| 290 | |
| 291 | auto offer = caller->CreateOffer(); |
| 292 | SdpContentsForEach(RemoveDtlsFingerprint(), offer->description()); |
| 293 | |
| 294 | EXPECT_FALSE(caller->SetLocalDescription(std::move(offer))); |
| 295 | } |
Steve Anton | 71182f4 | 2018-01-19 14:59:54 -0800 | [diff] [blame] | 296 | TEST_P(PeerConnectionCryptoTest, |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 297 | FailToSetRemoteOfferWithNoFingerprintWhenDtlsOn) { |
| 298 | RTCConfiguration config; |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 299 | auto caller = CreatePeerConnectionWithAudioVideo(config); |
| 300 | auto callee = CreatePeerConnectionWithAudioVideo(config); |
| 301 | |
| 302 | auto offer = caller->CreateOffer(); |
| 303 | SdpContentsForEach(RemoveDtlsFingerprint(), offer->description()); |
| 304 | |
| 305 | EXPECT_FALSE(callee->SetRemoteDescription(std::move(offer))); |
| 306 | } |
Steve Anton | 71182f4 | 2018-01-19 14:59:54 -0800 | [diff] [blame] | 307 | TEST_P(PeerConnectionCryptoTest, |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 308 | FailToSetLocalAnswerWithNoFingerprintWhenDtlsOn) { |
| 309 | RTCConfiguration config; |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 310 | auto caller = CreatePeerConnectionWithAudioVideo(config); |
| 311 | auto callee = CreatePeerConnectionWithAudioVideo(config); |
| 312 | |
| 313 | callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()); |
| 314 | auto answer = callee->CreateAnswer(); |
| 315 | SdpContentsForEach(RemoveDtlsFingerprint(), answer->description()); |
| 316 | } |
Steve Anton | 71182f4 | 2018-01-19 14:59:54 -0800 | [diff] [blame] | 317 | TEST_P(PeerConnectionCryptoTest, |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 318 | FailToSetRemoteAnswerWithNoFingerprintWhenDtlsOn) { |
| 319 | RTCConfiguration config; |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 320 | auto caller = CreatePeerConnectionWithAudioVideo(config); |
| 321 | auto callee = CreatePeerConnectionWithAudioVideo(config); |
| 322 | |
| 323 | callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()); |
| 324 | auto answer = callee->CreateAnswerAndSetAsLocal(); |
| 325 | SdpContentsForEach(RemoveDtlsFingerprint(), answer->description()); |
| 326 | |
| 327 | EXPECT_FALSE(caller->SetRemoteDescription(std::move(answer))); |
| 328 | } |
| 329 | |
| 330 | // Test that an offer/answer can be exchanged when encryption is disabled. |
Steve Anton | 71182f4 | 2018-01-19 14:59:54 -0800 | [diff] [blame] | 331 | TEST_P(PeerConnectionCryptoTest, ExchangeOfferAnswerWhenNoEncryption) { |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 332 | PeerConnectionFactoryInterface::Options options; |
| 333 | options.disable_encryption = true; |
| 334 | pc_factory_->SetOptions(options); |
| 335 | |
| 336 | RTCConfiguration config; |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 337 | auto caller = CreatePeerConnectionWithAudioVideo(config); |
| 338 | auto callee = CreatePeerConnectionWithAudioVideo(config); |
| 339 | |
| 340 | auto offer = caller->CreateOfferAndSetAsLocal(); |
| 341 | ASSERT_TRUE(offer); |
| 342 | ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer))); |
| 343 | |
| 344 | auto answer = callee->CreateAnswerAndSetAsLocal(); |
| 345 | ASSERT_TRUE(answer); |
| 346 | ASSERT_TRUE(caller->SetRemoteDescription(std::move(answer))); |
| 347 | } |
| 348 | |
| 349 | // Tests that a DTLS call can be established when the certificate is specified |
| 350 | // in the PeerConnection config and no certificate generator is specified. |
Steve Anton | 71182f4 | 2018-01-19 14:59:54 -0800 | [diff] [blame] | 351 | TEST_P(PeerConnectionCryptoTest, |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 352 | ExchangeOfferAnswerWhenDtlsCertificateInConfig) { |
| 353 | RTCConfiguration caller_config; |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 354 | caller_config.certificates.push_back( |
| 355 | FakeRTCCertificateGenerator::GenerateCertificate()); |
| 356 | auto caller = CreatePeerConnectionWithAudioVideo(caller_config); |
| 357 | |
| 358 | RTCConfiguration callee_config; |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 359 | callee_config.certificates.push_back( |
| 360 | FakeRTCCertificateGenerator::GenerateCertificate()); |
| 361 | auto callee = CreatePeerConnectionWithAudioVideo(callee_config); |
| 362 | |
| 363 | auto offer = caller->CreateOfferAndSetAsLocal(); |
| 364 | ASSERT_TRUE(offer); |
| 365 | ASSERT_TRUE(callee->SetRemoteDescription(std::move(offer))); |
| 366 | |
| 367 | auto answer = callee->CreateAnswerAndSetAsLocal(); |
| 368 | ASSERT_TRUE(answer); |
| 369 | ASSERT_TRUE(caller->SetRemoteDescription(std::move(answer))); |
| 370 | } |
| 371 | |
| 372 | // The following parameterized test verifies that CreateOffer/CreateAnswer |
| 373 | // returns successfully (or with failure if the underlying certificate generator |
| 374 | // fails) no matter when the DTLS certificate is generated. If multiple |
| 375 | // CreateOffer/CreateAnswer calls are made while waiting for the certificate, |
| 376 | // they all finish after the certificate is generated. |
| 377 | |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 378 | // Whether the certificate will be generated before calling CreateOffer or |
| 379 | // while CreateOffer is executing. |
| 380 | enum class CertGenTime { kBefore, kDuring }; |
| 381 | std::ostream& operator<<(std::ostream& out, CertGenTime value) { |
| 382 | switch (value) { |
| 383 | case CertGenTime::kBefore: |
| 384 | return out << "before"; |
| 385 | case CertGenTime::kDuring: |
| 386 | return out << "during"; |
| 387 | default: |
| 388 | return out << "unknown"; |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | // Whether the fake certificate generator will produce a certificate or fail. |
| 393 | enum class CertGenResult { kSucceed, kFail }; |
| 394 | std::ostream& operator<<(std::ostream& out, CertGenResult value) { |
| 395 | switch (value) { |
| 396 | case CertGenResult::kSucceed: |
| 397 | return out << "succeed"; |
| 398 | case CertGenResult::kFail: |
| 399 | return out << "fail"; |
| 400 | default: |
| 401 | return out << "unknown"; |
| 402 | } |
| 403 | } |
| 404 | |
Steve Anton | 71182f4 | 2018-01-19 14:59:54 -0800 | [diff] [blame] | 405 | class PeerConnectionCryptoDtlsCertGenTest |
| 406 | : public PeerConnectionCryptoBaseTest, |
| 407 | public ::testing::WithParamInterface<std::tuple<SdpSemantics, |
| 408 | SdpType, |
| 409 | CertGenTime, |
| 410 | CertGenResult, |
| 411 | size_t>> { |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 412 | protected: |
Steve Anton | 71182f4 | 2018-01-19 14:59:54 -0800 | [diff] [blame] | 413 | PeerConnectionCryptoDtlsCertGenTest() |
| 414 | : PeerConnectionCryptoBaseTest(std::get<0>(GetParam())) { |
| 415 | sdp_type_ = std::get<1>(GetParam()); |
| 416 | cert_gen_time_ = std::get<2>(GetParam()); |
| 417 | cert_gen_result_ = std::get<3>(GetParam()); |
| 418 | concurrent_calls_ = std::get<4>(GetParam()); |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | SdpType sdp_type_; |
| 422 | CertGenTime cert_gen_time_; |
| 423 | CertGenResult cert_gen_result_; |
| 424 | size_t concurrent_calls_; |
| 425 | }; |
| 426 | |
Steve Anton | 71182f4 | 2018-01-19 14:59:54 -0800 | [diff] [blame] | 427 | TEST_P(PeerConnectionCryptoDtlsCertGenTest, TestCertificateGeneration) { |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 428 | RTCConfiguration config; |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 429 | auto owned_fake_certificate_generator = |
Mirko Bonadei | 317a1f0 | 2019-09-17 17:06:18 +0200 | [diff] [blame] | 430 | std::make_unique<FakeRTCCertificateGenerator>(); |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 431 | auto* fake_certificate_generator = owned_fake_certificate_generator.get(); |
| 432 | fake_certificate_generator->set_should_fail(cert_gen_result_ == |
| 433 | CertGenResult::kFail); |
| 434 | fake_certificate_generator->set_should_wait(cert_gen_time_ == |
| 435 | CertGenTime::kDuring); |
| 436 | WrapperPtr pc; |
| 437 | if (sdp_type_ == SdpType::kOffer) { |
| 438 | pc = CreatePeerConnectionWithAudioVideo( |
| 439 | config, std::move(owned_fake_certificate_generator)); |
| 440 | } else { |
| 441 | auto caller = CreatePeerConnectionWithAudioVideo(config); |
| 442 | pc = CreatePeerConnectionWithAudioVideo( |
| 443 | config, std::move(owned_fake_certificate_generator)); |
| 444 | pc->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()); |
| 445 | } |
| 446 | if (cert_gen_time_ == CertGenTime::kBefore) { |
| 447 | ASSERT_TRUE_WAIT(fake_certificate_generator->generated_certificates() + |
| 448 | fake_certificate_generator->generated_failures() > |
| 449 | 0, |
| 450 | kGenerateCertTimeout); |
| 451 | } else { |
| 452 | ASSERT_EQ(fake_certificate_generator->generated_certificates(), 0); |
| 453 | fake_certificate_generator->set_should_wait(false); |
| 454 | } |
| 455 | std::vector<rtc::scoped_refptr<MockCreateSessionDescriptionObserver>> |
| 456 | observers; |
| 457 | for (size_t i = 0; i < concurrent_calls_; i++) { |
| 458 | rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observer = |
Tommi | 87f7090 | 2021-04-27 14:43:08 +0200 | [diff] [blame] | 459 | rtc::make_ref_counted<MockCreateSessionDescriptionObserver>(); |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 460 | observers.push_back(observer); |
| 461 | if (sdp_type_ == SdpType::kOffer) { |
Niels Möller | f06f923 | 2018-08-07 12:32:18 +0200 | [diff] [blame] | 462 | pc->pc()->CreateOffer(observer, |
| 463 | PeerConnectionInterface::RTCOfferAnswerOptions()); |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 464 | } else { |
Niels Möller | f06f923 | 2018-08-07 12:32:18 +0200 | [diff] [blame] | 465 | pc->pc()->CreateAnswer(observer, |
| 466 | PeerConnectionInterface::RTCOfferAnswerOptions()); |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 467 | } |
| 468 | } |
| 469 | for (auto& observer : observers) { |
| 470 | EXPECT_TRUE_WAIT(observer->called(), 1000); |
| 471 | if (cert_gen_result_ == CertGenResult::kSucceed) { |
| 472 | EXPECT_TRUE(observer->result()); |
| 473 | } else { |
| 474 | EXPECT_FALSE(observer->result()); |
| 475 | } |
| 476 | } |
| 477 | } |
| 478 | |
Mirko Bonadei | c84f661 | 2019-01-31 12:20:57 +0100 | [diff] [blame] | 479 | INSTANTIATE_TEST_SUITE_P( |
Steve Anton | 71182f4 | 2018-01-19 14:59:54 -0800 | [diff] [blame] | 480 | PeerConnectionCryptoTest, |
| 481 | PeerConnectionCryptoDtlsCertGenTest, |
| 482 | Combine(Values(SdpSemantics::kPlanB, SdpSemantics::kUnifiedPlan), |
| 483 | Values(SdpType::kOffer, SdpType::kAnswer), |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 484 | Values(CertGenTime::kBefore, CertGenTime::kDuring), |
| 485 | Values(CertGenResult::kSucceed, CertGenResult::kFail), |
| 486 | Values(1, 3))); |
| 487 | |
Steve Anton | 8a63f78 | 2017-10-23 13:08:53 -0700 | [diff] [blame] | 488 | // Test that we can create and set an answer correctly when different |
| 489 | // SSL roles have been negotiated for different transports. |
| 490 | // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=4525 |
Steve Anton | 71182f4 | 2018-01-19 14:59:54 -0800 | [diff] [blame] | 491 | TEST_P(PeerConnectionCryptoTest, CreateAnswerWithDifferentSslRoles) { |
Steve Anton | 8a63f78 | 2017-10-23 13:08:53 -0700 | [diff] [blame] | 492 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 493 | auto callee = CreatePeerConnectionWithAudioVideo(); |
| 494 | |
| 495 | RTCOfferAnswerOptions options_no_bundle; |
| 496 | options_no_bundle.use_rtp_mux = false; |
| 497 | |
| 498 | // First, negotiate different SSL roles for audio and video. |
| 499 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal())); |
| 500 | auto answer = callee->CreateAnswer(options_no_bundle); |
| 501 | |
| 502 | AudioConnectionRole(answer->description()) = cricket::CONNECTIONROLE_ACTIVE; |
| 503 | VideoConnectionRole(answer->description()) = cricket::CONNECTIONROLE_PASSIVE; |
| 504 | |
| 505 | ASSERT_TRUE( |
| 506 | callee->SetLocalDescription(CloneSessionDescription(answer.get()))); |
| 507 | ASSERT_TRUE(caller->SetRemoteDescription(std::move(answer))); |
| 508 | |
| 509 | // Now create an offer in the reverse direction, and ensure the initial |
| 510 | // offerer responds with an answer with the correct SSL roles. |
| 511 | ASSERT_TRUE(caller->SetRemoteDescription(callee->CreateOfferAndSetAsLocal())); |
| 512 | answer = caller->CreateAnswer(options_no_bundle); |
| 513 | |
| 514 | EXPECT_EQ(cricket::CONNECTIONROLE_PASSIVE, |
| 515 | AudioConnectionRole(answer->description())); |
| 516 | EXPECT_EQ(cricket::CONNECTIONROLE_ACTIVE, |
| 517 | VideoConnectionRole(answer->description())); |
| 518 | |
| 519 | ASSERT_TRUE( |
| 520 | caller->SetLocalDescription(CloneSessionDescription(answer.get()))); |
| 521 | ASSERT_TRUE(callee->SetRemoteDescription(std::move(answer))); |
| 522 | |
| 523 | // Lastly, start BUNDLE-ing on "audio", expecting that the "passive" role of |
| 524 | // audio is transferred over to video in the answer that completes the BUNDLE |
| 525 | // negotiation. |
| 526 | RTCOfferAnswerOptions options_bundle; |
| 527 | options_bundle.use_rtp_mux = true; |
| 528 | |
| 529 | ASSERT_TRUE(caller->SetRemoteDescription(callee->CreateOfferAndSetAsLocal())); |
| 530 | answer = caller->CreateAnswer(options_bundle); |
| 531 | |
| 532 | EXPECT_EQ(cricket::CONNECTIONROLE_PASSIVE, |
| 533 | AudioConnectionRole(answer->description())); |
| 534 | EXPECT_EQ(cricket::CONNECTIONROLE_PASSIVE, |
| 535 | VideoConnectionRole(answer->description())); |
| 536 | |
| 537 | ASSERT_TRUE( |
| 538 | caller->SetLocalDescription(CloneSessionDescription(answer.get()))); |
| 539 | ASSERT_TRUE(callee->SetRemoteDescription(std::move(answer))); |
| 540 | } |
| 541 | |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 542 | // Tests that if the DTLS fingerprint is invalid then all future calls to |
| 543 | // SetLocalDescription and SetRemoteDescription will fail due to a session |
| 544 | // error. |
| 545 | // This is a regression test for crbug.com/800775 |
| 546 | TEST_P(PeerConnectionCryptoTest, SessionErrorIfFingerprintInvalid) { |
| 547 | auto callee_certificate = rtc::RTCCertificate::FromPEM(kRsaPems[0]); |
| 548 | auto other_certificate = rtc::RTCCertificate::FromPEM(kRsaPems[1]); |
| 549 | |
| 550 | auto caller = CreatePeerConnectionWithAudioVideo(); |
| 551 | RTCConfiguration callee_config; |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 552 | callee_config.certificates.push_back(callee_certificate); |
| 553 | auto callee = CreatePeerConnectionWithAudioVideo(callee_config); |
| 554 | |
| 555 | ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal())); |
| 556 | |
| 557 | // Create an invalid answer with the other certificate's fingerprint. |
Steve Anton | 25ca0ac | 2019-06-25 10:40:48 -0700 | [diff] [blame] | 558 | auto valid_answer = callee->CreateAnswer(); |
| 559 | auto invalid_answer = CloneSessionDescription(valid_answer.get()); |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 560 | auto* audio_content = |
| 561 | cricket::GetFirstAudioContent(invalid_answer->description()); |
| 562 | ASSERT_TRUE(audio_content); |
| 563 | auto* audio_transport_info = |
| 564 | invalid_answer->description()->GetTransportInfoByName( |
| 565 | audio_content->name); |
| 566 | ASSERT_TRUE(audio_transport_info); |
Steve Anton | 4905edb | 2018-10-15 19:27:44 -0700 | [diff] [blame] | 567 | audio_transport_info->description.identity_fingerprint = |
| 568 | rtc::SSLFingerprint::CreateFromCertificate(*other_certificate); |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 569 | |
| 570 | // Set the invalid answer and expect a fingerprint error. |
| 571 | std::string error; |
| 572 | ASSERT_FALSE(callee->SetLocalDescription(std::move(invalid_answer), &error)); |
| 573 | EXPECT_PRED_FORMAT2(AssertStringContains, error, |
| 574 | "Local fingerprint does not match identity."); |
| 575 | |
| 576 | // Make sure that setting a valid remote offer or local answer also fails now. |
| 577 | ASSERT_FALSE(callee->SetRemoteDescription(caller->CreateOffer(), &error)); |
| 578 | EXPECT_PRED_FORMAT2(AssertStringContains, error, |
| 579 | "Session error code: ERROR_CONTENT."); |
Steve Anton | 25ca0ac | 2019-06-25 10:40:48 -0700 | [diff] [blame] | 580 | ASSERT_FALSE(callee->SetLocalDescription(std::move(valid_answer), &error)); |
Steve Anton | 80dd7b5 | 2018-02-16 17:08:42 -0800 | [diff] [blame] | 581 | EXPECT_PRED_FORMAT2(AssertStringContains, error, |
| 582 | "Session error code: ERROR_CONTENT."); |
| 583 | } |
| 584 | |
Mirko Bonadei | c84f661 | 2019-01-31 12:20:57 +0100 | [diff] [blame] | 585 | INSTANTIATE_TEST_SUITE_P(PeerConnectionCryptoTest, |
| 586 | PeerConnectionCryptoTest, |
| 587 | Values(SdpSemantics::kPlanB, |
| 588 | SdpSemantics::kUnifiedPlan)); |
Steve Anton | 71182f4 | 2018-01-19 14:59:54 -0800 | [diff] [blame] | 589 | |
Steve Anton | 6b63cd5 | 2017-10-06 11:20:31 -0700 | [diff] [blame] | 590 | } // namespace webrtc |