blob: 2ac64c84f6f399191b7a7b4998e9374a187b9fe8 [file] [log] [blame]
zstein398c3fd2017-07-19 13:38:02 -07001/*
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
Steve Anton36b29d12017-10-30 09:57:42 -070011#include <vector>
12
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "pc/srtptransport.h"
zstein398c3fd2017-07-19 13:38:02 -070014
Karl Wiberg918f50c2018-07-05 11:40:33 +020015#include "absl/memory/memory.h"
Zhi Huangcf990f52017-09-22 12:12:30 -070016#include "media/base/fakertp.h"
17#include "p2p/base/dtlstransportinternal.h"
18#include "p2p/base/fakepackettransport.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "pc/rtptransport.h"
20#include "pc/rtptransporttestutil.h"
Zhi Huangcf990f52017-09-22 12:12:30 -070021#include "pc/srtptestutil.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/asyncpacketsocket.h"
23#include "rtc_base/gunit.h"
Zhi Huangcf990f52017-09-22 12:12:30 -070024#include "rtc_base/sslstreamadapter.h"
25
26using rtc::kTestKey1;
27using rtc::kTestKey2;
28using rtc::kTestKeyLen;
29using rtc::SRTP_AEAD_AES_128_GCM;
zstein398c3fd2017-07-19 13:38:02 -070030
31namespace webrtc {
Zhi Huangcf990f52017-09-22 12:12:30 -070032static const uint8_t kTestKeyGcm128_1[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ12";
33static const uint8_t kTestKeyGcm128_2[] = "21ZYXWVUTSRQPONMLKJIHGFEDCBA";
34static const int kTestKeyGcm128Len = 28; // 128 bits key + 96 bits salt.
35static const uint8_t kTestKeyGcm256_1[] =
36 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqr";
37static const uint8_t kTestKeyGcm256_2[] =
38 "rqponmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA";
39static const int kTestKeyGcm256Len = 44; // 256 bits key + 96 bits salt.
zstein398c3fd2017-07-19 13:38:02 -070040
Zhi Huangcf990f52017-09-22 12:12:30 -070041class SrtpTransportTest : public testing::Test, public sigslot::has_slots<> {
42 protected:
43 SrtpTransportTest() {
44 bool rtcp_mux_enabled = true;
zstein398c3fd2017-07-19 13:38:02 -070045
Zhi Huangcf990f52017-09-22 12:12:30 -070046 rtp_packet_transport1_ =
Karl Wiberg918f50c2018-07-05 11:40:33 +020047 absl::make_unique<rtc::FakePacketTransport>("fake_packet_transport1");
Zhi Huangcf990f52017-09-22 12:12:30 -070048 rtp_packet_transport2_ =
Karl Wiberg918f50c2018-07-05 11:40:33 +020049 absl::make_unique<rtc::FakePacketTransport>("fake_packet_transport2");
zstein398c3fd2017-07-19 13:38:02 -070050
Zhi Huangcf990f52017-09-22 12:12:30 -070051 bool asymmetric = false;
52 rtp_packet_transport1_->SetDestination(rtp_packet_transport2_.get(),
53 asymmetric);
zstein398c3fd2017-07-19 13:38:02 -070054
Karl Wiberg918f50c2018-07-05 11:40:33 +020055 srtp_transport1_ = absl::make_unique<SrtpTransport>(rtcp_mux_enabled);
56 srtp_transport2_ = absl::make_unique<SrtpTransport>(rtcp_mux_enabled);
Zhi Huangcf990f52017-09-22 12:12:30 -070057
Zhi Huang365381f2018-04-13 16:44:34 -070058 srtp_transport1_->SetRtpPacketTransport(rtp_packet_transport1_.get());
59 srtp_transport2_->SetRtpPacketTransport(rtp_packet_transport2_.get());
Zhi Huangcf990f52017-09-22 12:12:30 -070060
Zhi Huang365381f2018-04-13 16:44:34 -070061 srtp_transport1_->SignalRtcpPacketReceived.connect(
62 &rtp_sink1_, &TransportObserver::OnRtcpPacketReceived);
63 srtp_transport2_->SignalRtcpPacketReceived.connect(
64 &rtp_sink2_, &TransportObserver::OnRtcpPacketReceived);
65
66 RtpDemuxerCriteria demuxer_criteria;
67 // 0x00 is the payload type used in kPcmuFrame.
68 demuxer_criteria.payload_types = {0x00};
69
70 srtp_transport1_->RegisterRtpDemuxerSink(demuxer_criteria, &rtp_sink1_);
71 srtp_transport2_->RegisterRtpDemuxerSink(demuxer_criteria, &rtp_sink2_);
zstein398c3fd2017-07-19 13:38:02 -070072 }
Zhi Huangcf990f52017-09-22 12:12:30 -070073
Zhi Huang365381f2018-04-13 16:44:34 -070074 ~SrtpTransportTest() {
75 if (srtp_transport1_) {
76 srtp_transport1_->UnregisterRtpDemuxerSink(&rtp_sink1_);
77 }
78 if (srtp_transport2_) {
79 srtp_transport2_->UnregisterRtpDemuxerSink(&rtp_sink2_);
80 }
Zhi Huangcf990f52017-09-22 12:12:30 -070081 }
82
83 // With external auth enabled, SRTP doesn't write the auth tag and
84 // unprotect would fail. Check accessing the information about the
85 // tag instead, similar to what the actual code would do that relies
86 // on external auth.
87 void TestRtpAuthParams(SrtpTransport* transport, const std::string& cs) {
88 int overhead;
89 EXPECT_TRUE(transport->GetSrtpOverhead(&overhead));
90 switch (rtc::SrtpCryptoSuiteFromName(cs)) {
91 case rtc::SRTP_AES128_CM_SHA1_32:
92 EXPECT_EQ(32 / 8, overhead); // 32-bit tag.
93 break;
94 case rtc::SRTP_AES128_CM_SHA1_80:
95 EXPECT_EQ(80 / 8, overhead); // 80-bit tag.
96 break;
97 default:
98 RTC_NOTREACHED();
99 break;
100 }
101
102 uint8_t* auth_key = nullptr;
103 int key_len = 0;
104 int tag_len = 0;
105 EXPECT_TRUE(transport->GetRtpAuthParams(&auth_key, &key_len, &tag_len));
106 EXPECT_NE(nullptr, auth_key);
107 EXPECT_EQ(160 / 8, key_len); // Length of SHA-1 is 160 bits.
108 EXPECT_EQ(overhead, tag_len);
109 }
110
111 void TestSendRecvRtpPacket(const std::string& cipher_suite_name) {
112 size_t rtp_len = sizeof(kPcmuFrame);
113 size_t packet_size = rtp_len + rtc::rtp_auth_tag_len(cipher_suite_name);
114 rtc::Buffer rtp_packet_buffer(packet_size);
115 char* rtp_packet_data = rtp_packet_buffer.data<char>();
116 memcpy(rtp_packet_data, kPcmuFrame, rtp_len);
117 // In order to be able to run this test function multiple times we can not
118 // use the same sequence number twice. Increase the sequence number by one.
119 rtc::SetBE16(reinterpret_cast<uint8_t*>(rtp_packet_data) + 2,
120 ++sequence_number_);
121 rtc::CopyOnWriteBuffer rtp_packet1to2(rtp_packet_data, rtp_len,
122 packet_size);
123 rtc::CopyOnWriteBuffer rtp_packet2to1(rtp_packet_data, rtp_len,
124 packet_size);
125
126 char original_rtp_data[sizeof(kPcmuFrame)];
127 memcpy(original_rtp_data, rtp_packet_data, rtp_len);
128
129 rtc::PacketOptions options;
130 // Send a packet from |srtp_transport1_| to |srtp_transport2_| and verify
131 // that the packet can be successfully received and decrypted.
132 ASSERT_TRUE(srtp_transport1_->SendRtpPacket(&rtp_packet1to2, options,
133 cricket::PF_SRTP_BYPASS));
134 if (srtp_transport1_->IsExternalAuthActive()) {
135 TestRtpAuthParams(srtp_transport1_.get(), cipher_suite_name);
136 } else {
Zhi Huang365381f2018-04-13 16:44:34 -0700137 ASSERT_TRUE(rtp_sink2_.last_recv_rtp_packet().data());
138 EXPECT_EQ(0, memcmp(rtp_sink2_.last_recv_rtp_packet().data(),
139 original_rtp_data, rtp_len));
Zhi Huangcf990f52017-09-22 12:12:30 -0700140 // Get the encrypted packet from underneath packet transport and verify
141 // the data is actually encrypted.
142 auto fake_rtp_packet_transport = static_cast<rtc::FakePacketTransport*>(
143 srtp_transport1_->rtp_packet_transport());
Steve Anton36b29d12017-10-30 09:57:42 -0700144 EXPECT_NE(0, memcmp(fake_rtp_packet_transport->last_sent_packet()->data(),
145 original_rtp_data, rtp_len));
Zhi Huangcf990f52017-09-22 12:12:30 -0700146 }
147
148 // Do the same thing in the opposite direction;
149 ASSERT_TRUE(srtp_transport2_->SendRtpPacket(&rtp_packet2to1, options,
150 cricket::PF_SRTP_BYPASS));
151 if (srtp_transport2_->IsExternalAuthActive()) {
152 TestRtpAuthParams(srtp_transport2_.get(), cipher_suite_name);
153 } else {
Zhi Huang365381f2018-04-13 16:44:34 -0700154 ASSERT_TRUE(rtp_sink1_.last_recv_rtp_packet().data());
155 EXPECT_EQ(0, memcmp(rtp_sink1_.last_recv_rtp_packet().data(),
156 original_rtp_data, rtp_len));
Zhi Huangcf990f52017-09-22 12:12:30 -0700157 auto fake_rtp_packet_transport = static_cast<rtc::FakePacketTransport*>(
158 srtp_transport2_->rtp_packet_transport());
Steve Anton36b29d12017-10-30 09:57:42 -0700159 EXPECT_NE(0, memcmp(fake_rtp_packet_transport->last_sent_packet()->data(),
160 original_rtp_data, rtp_len));
Zhi Huangcf990f52017-09-22 12:12:30 -0700161 }
162 }
163
164 void TestSendRecvRtcpPacket(const std::string& cipher_suite_name) {
Zhi Huang365381f2018-04-13 16:44:34 -0700165 size_t rtcp_len = sizeof(::kRtcpReport);
Zhi Huangcf990f52017-09-22 12:12:30 -0700166 size_t packet_size =
167 rtcp_len + 4 + rtc::rtcp_auth_tag_len(cipher_suite_name);
168 rtc::Buffer rtcp_packet_buffer(packet_size);
169 char* rtcp_packet_data = rtcp_packet_buffer.data<char>();
Zhi Huang365381f2018-04-13 16:44:34 -0700170 memcpy(rtcp_packet_data, ::kRtcpReport, rtcp_len);
Zhi Huangcf990f52017-09-22 12:12:30 -0700171
172 rtc::CopyOnWriteBuffer rtcp_packet1to2(rtcp_packet_data, rtcp_len,
173 packet_size);
174 rtc::CopyOnWriteBuffer rtcp_packet2to1(rtcp_packet_data, rtcp_len,
175 packet_size);
176
177 rtc::PacketOptions options;
178 // Send a packet from |srtp_transport1_| to |srtp_transport2_| and verify
179 // that the packet can be successfully received and decrypted.
180 ASSERT_TRUE(srtp_transport1_->SendRtcpPacket(&rtcp_packet1to2, options,
181 cricket::PF_SRTP_BYPASS));
Zhi Huang365381f2018-04-13 16:44:34 -0700182 ASSERT_TRUE(rtp_sink2_.last_recv_rtcp_packet().data());
183 EXPECT_EQ(0, memcmp(rtp_sink2_.last_recv_rtcp_packet().data(),
184 rtcp_packet_data, rtcp_len));
Zhi Huangcf990f52017-09-22 12:12:30 -0700185 // Get the encrypted packet from underneath packet transport and verify the
186 // data is actually encrypted.
187 auto fake_rtp_packet_transport = static_cast<rtc::FakePacketTransport*>(
188 srtp_transport1_->rtp_packet_transport());
Steve Anton36b29d12017-10-30 09:57:42 -0700189 EXPECT_NE(0, memcmp(fake_rtp_packet_transport->last_sent_packet()->data(),
190 rtcp_packet_data, rtcp_len));
Zhi Huangcf990f52017-09-22 12:12:30 -0700191
192 // Do the same thing in the opposite direction;
193 ASSERT_TRUE(srtp_transport2_->SendRtcpPacket(&rtcp_packet2to1, options,
194 cricket::PF_SRTP_BYPASS));
Zhi Huang365381f2018-04-13 16:44:34 -0700195 ASSERT_TRUE(rtp_sink1_.last_recv_rtcp_packet().data());
196 EXPECT_EQ(0, memcmp(rtp_sink1_.last_recv_rtcp_packet().data(),
197 rtcp_packet_data, rtcp_len));
Zhi Huangcf990f52017-09-22 12:12:30 -0700198 fake_rtp_packet_transport = static_cast<rtc::FakePacketTransport*>(
199 srtp_transport2_->rtp_packet_transport());
Steve Anton36b29d12017-10-30 09:57:42 -0700200 EXPECT_NE(0, memcmp(fake_rtp_packet_transport->last_sent_packet()->data(),
201 rtcp_packet_data, rtcp_len));
Zhi Huangcf990f52017-09-22 12:12:30 -0700202 }
203
204 void TestSendRecvPacket(bool enable_external_auth,
205 int cs,
206 const uint8_t* key1,
207 int key1_len,
208 const uint8_t* key2,
209 int key2_len,
210 const std::string& cipher_suite_name) {
211 EXPECT_EQ(key1_len, key2_len);
212 EXPECT_EQ(cipher_suite_name, rtc::SrtpCryptoSuiteToName(cs));
213 if (enable_external_auth) {
214 srtp_transport1_->EnableExternalAuth();
215 srtp_transport2_->EnableExternalAuth();
216 }
Zhi Huangc99b6c72017-11-10 16:44:46 -0800217 std::vector<int> extension_ids;
218 EXPECT_TRUE(srtp_transport1_->SetRtpParams(
219 cs, key1, key1_len, extension_ids, cs, key2, key2_len, extension_ids));
220 EXPECT_TRUE(srtp_transport2_->SetRtpParams(
221 cs, key2, key2_len, extension_ids, cs, key1, key1_len, extension_ids));
222 EXPECT_TRUE(srtp_transport1_->SetRtcpParams(
223 cs, key1, key1_len, extension_ids, cs, key2, key2_len, extension_ids));
224 EXPECT_TRUE(srtp_transport2_->SetRtcpParams(
225 cs, key2, key2_len, extension_ids, cs, key1, key1_len, extension_ids));
Zhi Huange830e682018-03-30 10:48:35 -0700226 EXPECT_TRUE(srtp_transport1_->IsSrtpActive());
227 EXPECT_TRUE(srtp_transport2_->IsSrtpActive());
Zhi Huangcf990f52017-09-22 12:12:30 -0700228 if (rtc::IsGcmCryptoSuite(cs)) {
229 EXPECT_FALSE(srtp_transport1_->IsExternalAuthActive());
230 EXPECT_FALSE(srtp_transport2_->IsExternalAuthActive());
231 } else if (enable_external_auth) {
232 EXPECT_TRUE(srtp_transport1_->IsExternalAuthActive());
233 EXPECT_TRUE(srtp_transport2_->IsExternalAuthActive());
234 }
235 TestSendRecvRtpPacket(cipher_suite_name);
236 TestSendRecvRtcpPacket(cipher_suite_name);
237 }
238
239 void TestSendRecvPacketWithEncryptedHeaderExtension(
240 const std::string& cs,
241 const std::vector<int>& encrypted_header_ids) {
242 size_t rtp_len = sizeof(kPcmuFrameWithExtensions);
243 size_t packet_size = rtp_len + rtc::rtp_auth_tag_len(cs);
244 rtc::Buffer rtp_packet_buffer(packet_size);
245 char* rtp_packet_data = rtp_packet_buffer.data<char>();
246 memcpy(rtp_packet_data, kPcmuFrameWithExtensions, rtp_len);
247 // In order to be able to run this test function multiple times we can not
248 // use the same sequence number twice. Increase the sequence number by one.
249 rtc::SetBE16(reinterpret_cast<uint8_t*>(rtp_packet_data) + 2,
250 ++sequence_number_);
251 rtc::CopyOnWriteBuffer rtp_packet1to2(rtp_packet_data, rtp_len,
252 packet_size);
253 rtc::CopyOnWriteBuffer rtp_packet2to1(rtp_packet_data, rtp_len,
254 packet_size);
255
256 char original_rtp_data[sizeof(kPcmuFrameWithExtensions)];
257 memcpy(original_rtp_data, rtp_packet_data, rtp_len);
258
259 rtc::PacketOptions options;
260 // Send a packet from |srtp_transport1_| to |srtp_transport2_| and verify
261 // that the packet can be successfully received and decrypted.
262 ASSERT_TRUE(srtp_transport1_->SendRtpPacket(&rtp_packet1to2, options,
263 cricket::PF_SRTP_BYPASS));
Zhi Huang365381f2018-04-13 16:44:34 -0700264 ASSERT_TRUE(rtp_sink2_.last_recv_rtp_packet().data());
265 EXPECT_EQ(0, memcmp(rtp_sink2_.last_recv_rtp_packet().data(),
266 original_rtp_data, rtp_len));
Zhi Huangcf990f52017-09-22 12:12:30 -0700267 // Get the encrypted packet from underneath packet transport and verify the
268 // data and header extension are actually encrypted.
269 auto fake_rtp_packet_transport = static_cast<rtc::FakePacketTransport*>(
270 srtp_transport1_->rtp_packet_transport());
Steve Anton36b29d12017-10-30 09:57:42 -0700271 EXPECT_NE(0, memcmp(fake_rtp_packet_transport->last_sent_packet()->data(),
272 original_rtp_data, rtp_len));
Zhi Huangcf990f52017-09-22 12:12:30 -0700273 CompareHeaderExtensions(
274 reinterpret_cast<const char*>(
275 fake_rtp_packet_transport->last_sent_packet()->data()),
276 fake_rtp_packet_transport->last_sent_packet()->size(),
277 original_rtp_data, rtp_len, encrypted_header_ids, false);
278
279 // Do the same thing in the opposite direction;
280 ASSERT_TRUE(srtp_transport2_->SendRtpPacket(&rtp_packet2to1, options,
281 cricket::PF_SRTP_BYPASS));
Zhi Huang365381f2018-04-13 16:44:34 -0700282 ASSERT_TRUE(rtp_sink1_.last_recv_rtp_packet().data());
283 EXPECT_EQ(0, memcmp(rtp_sink1_.last_recv_rtp_packet().data(),
284 original_rtp_data, rtp_len));
Zhi Huangcf990f52017-09-22 12:12:30 -0700285 fake_rtp_packet_transport = static_cast<rtc::FakePacketTransport*>(
286 srtp_transport2_->rtp_packet_transport());
Steve Anton36b29d12017-10-30 09:57:42 -0700287 EXPECT_NE(0, memcmp(fake_rtp_packet_transport->last_sent_packet()->data(),
288 original_rtp_data, rtp_len));
Zhi Huangcf990f52017-09-22 12:12:30 -0700289 CompareHeaderExtensions(
290 reinterpret_cast<const char*>(
291 fake_rtp_packet_transport->last_sent_packet()->data()),
292 fake_rtp_packet_transport->last_sent_packet()->size(),
293 original_rtp_data, rtp_len, encrypted_header_ids, false);
294 }
295
296 void TestSendRecvEncryptedHeaderExtension(int cs,
297 const uint8_t* key1,
298 int key1_len,
299 const uint8_t* key2,
300 int key2_len,
301 const std::string& cs_name) {
302 std::vector<int> encrypted_headers;
Zhi Huangf2d7beb2017-11-20 14:35:11 -0800303 encrypted_headers.push_back(kHeaderExtensionIDs[0]);
Zhi Huangcf990f52017-09-22 12:12:30 -0700304 // Don't encrypt header ids 2 and 3.
Zhi Huangf2d7beb2017-11-20 14:35:11 -0800305 encrypted_headers.push_back(kHeaderExtensionIDs[1]);
Zhi Huangcf990f52017-09-22 12:12:30 -0700306 EXPECT_EQ(key1_len, key2_len);
307 EXPECT_EQ(cs_name, rtc::SrtpCryptoSuiteToName(cs));
Zhi Huangc99b6c72017-11-10 16:44:46 -0800308 EXPECT_TRUE(srtp_transport1_->SetRtpParams(cs, key1, key1_len,
309 encrypted_headers, cs, key2,
310 key2_len, encrypted_headers));
311 EXPECT_TRUE(srtp_transport2_->SetRtpParams(cs, key2, key2_len,
312 encrypted_headers, cs, key1,
313 key1_len, encrypted_headers));
Zhi Huange830e682018-03-30 10:48:35 -0700314 EXPECT_TRUE(srtp_transport1_->IsSrtpActive());
315 EXPECT_TRUE(srtp_transport2_->IsSrtpActive());
Zhi Huangcf990f52017-09-22 12:12:30 -0700316 EXPECT_FALSE(srtp_transport1_->IsExternalAuthActive());
317 EXPECT_FALSE(srtp_transport2_->IsExternalAuthActive());
318 TestSendRecvPacketWithEncryptedHeaderExtension(cs_name, encrypted_headers);
319 }
320
321 std::unique_ptr<SrtpTransport> srtp_transport1_;
322 std::unique_ptr<SrtpTransport> srtp_transport2_;
323
324 std::unique_ptr<rtc::FakePacketTransport> rtp_packet_transport1_;
325 std::unique_ptr<rtc::FakePacketTransport> rtp_packet_transport2_;
326
Zhi Huang365381f2018-04-13 16:44:34 -0700327 TransportObserver rtp_sink1_;
328 TransportObserver rtp_sink2_;
329
Zhi Huangcf990f52017-09-22 12:12:30 -0700330 int sequence_number_ = 0;
zstein398c3fd2017-07-19 13:38:02 -0700331};
332
Zhi Huangcf990f52017-09-22 12:12:30 -0700333class SrtpTransportTestWithExternalAuth
334 : public SrtpTransportTest,
335 public testing::WithParamInterface<bool> {};
zstein398c3fd2017-07-19 13:38:02 -0700336
Zhi Huangcf990f52017-09-22 12:12:30 -0700337TEST_P(SrtpTransportTestWithExternalAuth,
338 SendAndRecvPacket_AES_CM_128_HMAC_SHA1_80) {
339 bool enable_external_auth = GetParam();
340 TestSendRecvPacket(enable_external_auth, rtc::SRTP_AES128_CM_SHA1_80,
341 kTestKey1, kTestKeyLen, kTestKey2, kTestKeyLen,
342 rtc::CS_AES_CM_128_HMAC_SHA1_80);
zstein398c3fd2017-07-19 13:38:02 -0700343}
344
Zhi Huangcf990f52017-09-22 12:12:30 -0700345TEST_F(SrtpTransportTest,
346 SendAndRecvPacketWithHeaderExtension_AES_CM_128_HMAC_SHA1_80) {
347 TestSendRecvEncryptedHeaderExtension(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1,
348 kTestKeyLen, kTestKey2, kTestKeyLen,
349 rtc::CS_AES_CM_128_HMAC_SHA1_80);
350}
zstein398c3fd2017-07-19 13:38:02 -0700351
Zhi Huangcf990f52017-09-22 12:12:30 -0700352TEST_P(SrtpTransportTestWithExternalAuth,
353 SendAndRecvPacket_AES_CM_128_HMAC_SHA1_32) {
354 bool enable_external_auth = GetParam();
355 TestSendRecvPacket(enable_external_auth, rtc::SRTP_AES128_CM_SHA1_32,
356 kTestKey1, kTestKeyLen, kTestKey2, kTestKeyLen,
357 rtc::CS_AES_CM_128_HMAC_SHA1_32);
358}
zstein398c3fd2017-07-19 13:38:02 -0700359
Zhi Huangcf990f52017-09-22 12:12:30 -0700360TEST_F(SrtpTransportTest,
361 SendAndRecvPacketWithHeaderExtension_AES_CM_128_HMAC_SHA1_32) {
362 TestSendRecvEncryptedHeaderExtension(rtc::SRTP_AES128_CM_SHA1_32, kTestKey1,
363 kTestKeyLen, kTestKey2, kTestKeyLen,
364 rtc::CS_AES_CM_128_HMAC_SHA1_32);
365}
zstein398c3fd2017-07-19 13:38:02 -0700366
Zhi Huangcf990f52017-09-22 12:12:30 -0700367TEST_P(SrtpTransportTestWithExternalAuth,
368 SendAndRecvPacket_SRTP_AEAD_AES_128_GCM) {
369 bool enable_external_auth = GetParam();
370 TestSendRecvPacket(enable_external_auth, rtc::SRTP_AEAD_AES_128_GCM,
371 kTestKeyGcm128_1, kTestKeyGcm128Len, kTestKeyGcm128_2,
372 kTestKeyGcm128Len, rtc::CS_AEAD_AES_128_GCM);
373}
zstein398c3fd2017-07-19 13:38:02 -0700374
Zhi Huangcf990f52017-09-22 12:12:30 -0700375TEST_F(SrtpTransportTest,
376 SendAndRecvPacketWithHeaderExtension_SRTP_AEAD_AES_128_GCM) {
377 TestSendRecvEncryptedHeaderExtension(
378 rtc::SRTP_AEAD_AES_128_GCM, kTestKeyGcm128_1, kTestKeyGcm128Len,
379 kTestKeyGcm128_2, kTestKeyGcm128Len, rtc::CS_AEAD_AES_128_GCM);
380}
381
382TEST_P(SrtpTransportTestWithExternalAuth,
383 SendAndRecvPacket_SRTP_AEAD_AES_256_GCM) {
384 bool enable_external_auth = GetParam();
385 TestSendRecvPacket(enable_external_auth, rtc::SRTP_AEAD_AES_256_GCM,
386 kTestKeyGcm256_1, kTestKeyGcm256Len, kTestKeyGcm256_2,
387 kTestKeyGcm256Len, rtc::CS_AEAD_AES_256_GCM);
388}
389
390TEST_F(SrtpTransportTest,
391 SendAndRecvPacketWithHeaderExtension_SRTP_AEAD_AES_256_GCM) {
392 TestSendRecvEncryptedHeaderExtension(
393 rtc::SRTP_AEAD_AES_256_GCM, kTestKeyGcm256_1, kTestKeyGcm256Len,
394 kTestKeyGcm256_2, kTestKeyGcm256Len, rtc::CS_AEAD_AES_256_GCM);
395}
396
397// Run all tests both with and without external auth enabled.
398INSTANTIATE_TEST_CASE_P(ExternalAuth,
399 SrtpTransportTestWithExternalAuth,
400 ::testing::Values(true, false));
401
402// Test directly setting the params with bogus keys.
403TEST_F(SrtpTransportTest, TestSetParamsKeyTooShort) {
Zhi Huangc99b6c72017-11-10 16:44:46 -0800404 std::vector<int> extension_ids;
Zhi Huangcf990f52017-09-22 12:12:30 -0700405 EXPECT_FALSE(srtp_transport1_->SetRtpParams(
Zhi Huangc99b6c72017-11-10 16:44:46 -0800406 rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen - 1, extension_ids,
407 rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen - 1, extension_ids));
Zhi Huangcf990f52017-09-22 12:12:30 -0700408 EXPECT_FALSE(srtp_transport1_->SetRtcpParams(
Zhi Huangc99b6c72017-11-10 16:44:46 -0800409 rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen - 1, extension_ids,
410 rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen - 1, extension_ids));
zstein398c3fd2017-07-19 13:38:02 -0700411}
412
413} // namespace webrtc