Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(13)

Side by Side Diff: webrtc/pc/srtpfilter_unittest.cc

Issue 2761143002: Support encrypted RTP extensions (RFC 6904) (Closed)
Patch Set: Various changes based on feedback from Peter and Taylor. Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright 2004 The WebRTC project authors. All Rights Reserved. 2 * Copyright 2004 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 <algorithm>
12
11 #include "webrtc/pc/srtpfilter.h" 13 #include "webrtc/pc/srtpfilter.h"
12 14
13 #include "third_party/libsrtp/include/srtp.h" 15 #include "third_party/libsrtp/include/srtp.h"
14 #include "webrtc/base/buffer.h" 16 #include "webrtc/base/buffer.h"
15 #include "webrtc/base/byteorder.h" 17 #include "webrtc/base/byteorder.h"
16 #include "webrtc/base/constructormagic.h" 18 #include "webrtc/base/constructormagic.h"
17 #include "webrtc/base/gunit.h" 19 #include "webrtc/base/gunit.h"
18 #include "webrtc/base/thread.h" 20 #include "webrtc/base/thread.h"
19 #include "webrtc/media/base/cryptoparams.h" 21 #include "webrtc/media/base/cryptoparams.h"
20 #include "webrtc/media/base/fakertp.h" 22 #include "webrtc/media/base/fakertp.h"
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
60 static const cricket::CryptoParams kTestCryptoParams2( 62 static const cricket::CryptoParams kTestCryptoParams2(
61 1, "AES_CM_128_HMAC_SHA1_80", kTestKeyParams2, ""); 63 1, "AES_CM_128_HMAC_SHA1_80", kTestKeyParams2, "");
62 static const cricket::CryptoParams kTestCryptoParamsGcm1( 64 static const cricket::CryptoParams kTestCryptoParamsGcm1(
63 1, "AEAD_AES_256_GCM", kTestKeyParamsGcm1, ""); 65 1, "AEAD_AES_256_GCM", kTestKeyParamsGcm1, "");
64 static const cricket::CryptoParams kTestCryptoParamsGcm2( 66 static const cricket::CryptoParams kTestCryptoParamsGcm2(
65 1, "AEAD_AES_256_GCM", kTestKeyParamsGcm2, ""); 67 1, "AEAD_AES_256_GCM", kTestKeyParamsGcm2, "");
66 static const cricket::CryptoParams kTestCryptoParamsGcm3( 68 static const cricket::CryptoParams kTestCryptoParamsGcm3(
67 1, "AEAD_AES_128_GCM", kTestKeyParamsGcm3, ""); 69 1, "AEAD_AES_128_GCM", kTestKeyParamsGcm3, "");
68 static const cricket::CryptoParams kTestCryptoParamsGcm4( 70 static const cricket::CryptoParams kTestCryptoParamsGcm4(
69 1, "AEAD_AES_128_GCM", kTestKeyParamsGcm4, ""); 71 1, "AEAD_AES_128_GCM", kTestKeyParamsGcm4, "");
72 static const char kDummyExtensionUri1[] = "dummy-extension-uri-1";
73 static const char kDummyExtensionUri4[] = "dummy-extension-uri-4";
70 74
71 static int rtp_auth_tag_len(const std::string& cs) { 75 static int rtp_auth_tag_len(const std::string& cs) {
72 if (cs == CS_AES_CM_128_HMAC_SHA1_32) { 76 if (cs == CS_AES_CM_128_HMAC_SHA1_32) {
73 return 4; 77 return 4;
74 } else if (cs == CS_AEAD_AES_128_GCM || cs == CS_AEAD_AES_256_GCM) { 78 } else if (cs == CS_AEAD_AES_128_GCM || cs == CS_AEAD_AES_256_GCM) {
75 return 16; 79 return 16;
76 } else { 80 } else {
77 return 10; 81 return 10;
78 } 82 }
79 } 83 }
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 190
187 EXPECT_TRUE(f2_.ProtectRtcp(rtcp_packet, rtcp_len, 191 EXPECT_TRUE(f2_.ProtectRtcp(rtcp_packet, rtcp_len,
188 static_cast<int>(rtcp_buffer.size()), 192 static_cast<int>(rtcp_buffer.size()),
189 &out_len)); 193 &out_len));
190 EXPECT_EQ(out_len, rtcp_len + 4 + rtcp_auth_tag_len(cs2)); // NOLINT 194 EXPECT_EQ(out_len, rtcp_len + 4 + rtcp_auth_tag_len(cs2)); // NOLINT
191 EXPECT_NE(0, memcmp(rtcp_packet, kRtcpReport, rtcp_len)); 195 EXPECT_NE(0, memcmp(rtcp_packet, kRtcpReport, rtcp_len));
192 EXPECT_TRUE(f1_.UnprotectRtcp(rtcp_packet, out_len, &out_len)); 196 EXPECT_TRUE(f1_.UnprotectRtcp(rtcp_packet, out_len, &out_len));
193 EXPECT_EQ(rtcp_len, out_len); 197 EXPECT_EQ(rtcp_len, out_len);
194 EXPECT_EQ(0, memcmp(rtcp_packet, kRtcpReport, rtcp_len)); 198 EXPECT_EQ(0, memcmp(rtcp_packet, kRtcpReport, rtcp_len));
195 } 199 }
200 // This expects both packets to be based on kPcmuFrameWithExtensions.
201 // Header extensions with an id in "encrypted_headers" are expected to be
202 // different in the packets unless "expect_equal" is set to "true".
203 void CompareHeaderExtensions(const char* packet1, const char* packet2,
204 const std::vector<int> encrypted_headers,
205 bool expect_equal) {
206 // RTP header + extension header are the same.
207 EXPECT_EQ(0, memcmp(packet1, packet2, 12 + 4));
208 // Check for one-byte header extensions.
209 EXPECT_EQ('\xBE', packet1[12]);
210 EXPECT_EQ('\xDE', packet1[13]);
211 // Determine position and size of extension headers.
212 size_t extension_words = packet1[14] << 8 | packet1[15];
213 const char* extension_data1 = packet1 + 12 + 4;
214 const char* extension_end1 = extension_data1 + extension_words * 4;
215 const char* extension_data2 = packet2 + 12 + 4;
216 while (extension_data1 < extension_end1) {
217 uint8_t id = (*extension_data1 & 0xf0) >> 4;
218 uint8_t len = (*extension_data1 & 0x0f) +1;
219 extension_data1++;
220 extension_data2++;
221 EXPECT_LE(extension_data1, extension_end1);
222 if (id == 15) {
223 // Finished parsing.
224 break;
225 }
226
227 // The header extension doesn't get encrypted if the id not in the list
228 // of header extensions to encrypt.
229 if (expect_equal ||
230 std::find(encrypted_headers.begin(), encrypted_headers.end(), id) ==
231 encrypted_headers.end()) {
232 EXPECT_EQ(0, memcmp(extension_data1, extension_data2, len));
233 } else {
234 EXPECT_NE(0, memcmp(extension_data1, extension_data2, len));
235 }
236
237 extension_data1 += len;
238 extension_data2 += len;
239 // Skip padding.
240 while (extension_data1 < extension_end1 && *extension_data1 == 0) {
241 extension_data1++;
242 extension_data2++;
243 }
244 }
245 }
246 void TestProtectUnprotectHeaderEncryption(const std::string& cs1,
247 const std::string& cs2,
248 const std::vector<webrtc::RtpExtension>& encrypted_headers) {
249 rtc::Buffer rtp_buffer(sizeof(kPcmuFrameWithExtensions) +
250 rtp_auth_tag_len(cs1));
251 char* rtp_packet = rtp_buffer.data<char>();
252 char original_rtp_packet[sizeof(kPcmuFrameWithExtensions)];
253 int rtp_len = sizeof(kPcmuFrameWithExtensions), out_len;
254 memcpy(rtp_packet, kPcmuFrameWithExtensions, rtp_len);
255 // In order to be able to run this test function multiple times we can not
256 // use the same sequence number twice. Increase the sequence number by one.
257 rtc::SetBE16(reinterpret_cast<uint8_t*>(rtp_packet) + 2,
258 ++sequence_number_);
259 memcpy(original_rtp_packet, rtp_packet, rtp_len);
260
261 std::vector<int> encrypted_header_ids;
262 for (const webrtc::RtpExtension& extension : encrypted_headers) {
263 EXPECT_TRUE(extension.encrypt);
264 encrypted_header_ids.push_back(extension.id);
265 }
266
267 EXPECT_TRUE(f1_.ProtectRtp(rtp_packet, rtp_len,
268 static_cast<int>(rtp_buffer.size()),
269 &out_len));
270 EXPECT_EQ(out_len, rtp_len + rtp_auth_tag_len(cs1));
271 EXPECT_NE(0, memcmp(rtp_packet, original_rtp_packet, rtp_len));
272 CompareHeaderExtensions(rtp_packet, original_rtp_packet,
273 encrypted_header_ids, false);
274 EXPECT_TRUE(f2_.UnprotectRtp(rtp_packet, out_len, &out_len));
275 EXPECT_EQ(rtp_len, out_len);
276 EXPECT_EQ(0, memcmp(rtp_packet, original_rtp_packet, rtp_len));
277 CompareHeaderExtensions(rtp_packet, original_rtp_packet,
278 encrypted_header_ids, true);
279
280 EXPECT_TRUE(f2_.ProtectRtp(rtp_packet, rtp_len,
281 static_cast<int>(rtp_buffer.size()),
282 &out_len));
283 EXPECT_EQ(out_len, rtp_len + rtp_auth_tag_len(cs2));
284 EXPECT_NE(0, memcmp(rtp_packet, original_rtp_packet, rtp_len));
285 CompareHeaderExtensions(rtp_packet, original_rtp_packet,
286 encrypted_header_ids, false);
287 EXPECT_TRUE(f1_.UnprotectRtp(rtp_packet, out_len, &out_len));
288 EXPECT_EQ(rtp_len, out_len);
289 EXPECT_EQ(0, memcmp(rtp_packet, original_rtp_packet, rtp_len));
290 CompareHeaderExtensions(rtp_packet, original_rtp_packet,
291 encrypted_header_ids, true);
292 }
196 void TestProtectSetParamsDirect(bool enable_external_auth, int cs, 293 void TestProtectSetParamsDirect(bool enable_external_auth, int cs,
197 const uint8_t* key1, int key1_len, const uint8_t* key2, int key2_len, 294 const uint8_t* key1, int key1_len, const uint8_t* key2, int key2_len,
198 const std::string& cs_name) { 295 const std::string& cs_name) {
199 EXPECT_EQ(key1_len, key2_len); 296 EXPECT_EQ(key1_len, key2_len);
200 EXPECT_EQ(cs_name, rtc::SrtpCryptoSuiteToName(cs)); 297 EXPECT_EQ(cs_name, rtc::SrtpCryptoSuiteToName(cs));
201 if (enable_external_auth) { 298 if (enable_external_auth) {
202 f1_.EnableExternalAuth(); 299 f1_.EnableExternalAuth();
203 f2_.EnableExternalAuth(); 300 f2_.EnableExternalAuth();
204 } 301 }
205 EXPECT_TRUE(f1_.SetRtpParams(cs, key1, key1_len, cs, key2, key2_len)); 302 EXPECT_TRUE(f1_.SetRtpParams(cs, key1, key1_len, cs, key2, key2_len));
206 EXPECT_TRUE(f2_.SetRtpParams(cs, key2, key2_len, cs, key1, key1_len)); 303 EXPECT_TRUE(f2_.SetRtpParams(cs, key2, key2_len, cs, key1, key1_len));
207 EXPECT_TRUE(f1_.SetRtcpParams(cs, key1, key1_len, cs, key2, key2_len)); 304 EXPECT_TRUE(f1_.SetRtcpParams(cs, key1, key1_len, cs, key2, key2_len));
208 EXPECT_TRUE(f2_.SetRtcpParams(cs, key2, key2_len, cs, key1, key1_len)); 305 EXPECT_TRUE(f2_.SetRtcpParams(cs, key2, key2_len, cs, key1, key1_len));
209 EXPECT_TRUE(f1_.IsActive()); 306 EXPECT_TRUE(f1_.IsActive());
210 EXPECT_TRUE(f2_.IsActive()); 307 EXPECT_TRUE(f2_.IsActive());
211 if (rtc::IsGcmCryptoSuite(cs)) { 308 if (rtc::IsGcmCryptoSuite(cs)) {
212 EXPECT_FALSE(f1_.IsExternalAuthActive()); 309 EXPECT_FALSE(f1_.IsExternalAuthActive());
213 EXPECT_FALSE(f2_.IsExternalAuthActive()); 310 EXPECT_FALSE(f2_.IsExternalAuthActive());
214 } else if (enable_external_auth) { 311 } else if (enable_external_auth) {
215 EXPECT_TRUE(f1_.IsExternalAuthActive()); 312 EXPECT_TRUE(f1_.IsExternalAuthActive());
216 EXPECT_TRUE(f2_.IsExternalAuthActive()); 313 EXPECT_TRUE(f2_.IsExternalAuthActive());
217 } 314 }
218 TestProtectUnprotect(cs_name, cs_name); 315 TestProtectUnprotect(cs_name, cs_name);
219 } 316 }
317 void TestProtectSetParamsDirectHeaderEncryption(int cs,
318 const uint8_t* key1, int key1_len, const uint8_t* key2, int key2_len,
319 const std::string& cs_name) {
320 std::vector<webrtc::RtpExtension> encrypted_headers;
321 encrypted_headers.push_back(
322 webrtc::RtpExtension(kDummyExtensionUri1, 1, true));
323 // Don't encrypt header ids 2 and 3.
324 encrypted_headers.push_back(
325 webrtc::RtpExtension(kDummyExtensionUri4, 4, true));
326 EXPECT_EQ(key1_len, key2_len);
327 EXPECT_EQ(cs_name, rtc::SrtpCryptoSuiteToName(cs));
328 f1_.SetEncryptedHeaderExtensions(CS_LOCAL, encrypted_headers);
329 f1_.SetEncryptedHeaderExtensions(CS_REMOTE, encrypted_headers);
330 f2_.SetEncryptedHeaderExtensions(CS_LOCAL, encrypted_headers);
331 f2_.SetEncryptedHeaderExtensions(CS_REMOTE, encrypted_headers);
332 EXPECT_TRUE(f1_.SetRtpParams(cs, key1, key1_len, cs, key2, key2_len));
333 EXPECT_TRUE(f2_.SetRtpParams(cs, key2, key2_len, cs, key1, key1_len));
334 EXPECT_TRUE(f1_.IsActive());
335 EXPECT_TRUE(f2_.IsActive());
336 EXPECT_FALSE(f1_.IsExternalAuthActive());
337 EXPECT_FALSE(f2_.IsExternalAuthActive());
338 TestProtectUnprotectHeaderEncryption(cs_name, cs_name, encrypted_headers);
339 }
220 cricket::SrtpFilter f1_; 340 cricket::SrtpFilter f1_;
221 cricket::SrtpFilter f2_; 341 cricket::SrtpFilter f2_;
222 int sequence_number_; 342 int sequence_number_;
223 }; 343 };
224 344
225 // Test that we can set up the session and keys properly. 345 // Test that we can set up the session and keys properly.
226 TEST_F(SrtpFilterTest, TestGoodSetupOneCipherSuite) { 346 TEST_F(SrtpFilterTest, TestGoodSetupOneCipherSuite) {
227 EXPECT_TRUE(f1_.SetOffer(MakeVector(kTestCryptoParams1), CS_LOCAL)); 347 EXPECT_TRUE(f1_.SetOffer(MakeVector(kTestCryptoParams1), CS_LOCAL));
228 EXPECT_FALSE(f1_.IsActive()); 348 EXPECT_FALSE(f1_.IsActive());
229 EXPECT_TRUE(f1_.SetAnswer(MakeVector(kTestCryptoParams2), CS_REMOTE)); 349 EXPECT_TRUE(f1_.SetAnswer(MakeVector(kTestCryptoParams2), CS_REMOTE));
(...skipping 382 matching lines...) Expand 10 before | Expand all | Expand 10 after
612 }; 732 };
613 733
614 // Test directly setting the params with AES_CM_128_HMAC_SHA1_80. 734 // Test directly setting the params with AES_CM_128_HMAC_SHA1_80.
615 TEST_P(SrtpFilterProtectSetParamsDirectTest, Test_AES_CM_128_HMAC_SHA1_80) { 735 TEST_P(SrtpFilterProtectSetParamsDirectTest, Test_AES_CM_128_HMAC_SHA1_80) {
616 bool enable_external_auth = GetParam(); 736 bool enable_external_auth = GetParam();
617 TestProtectSetParamsDirect(enable_external_auth, rtc::SRTP_AES128_CM_SHA1_80, 737 TestProtectSetParamsDirect(enable_external_auth, rtc::SRTP_AES128_CM_SHA1_80,
618 kTestKey1, kTestKeyLen, kTestKey2, kTestKeyLen, 738 kTestKey1, kTestKeyLen, kTestKey2, kTestKeyLen,
619 CS_AES_CM_128_HMAC_SHA1_80); 739 CS_AES_CM_128_HMAC_SHA1_80);
620 } 740 }
621 741
742 TEST_F(SrtpFilterTest,
743 TestProtectSetParamsDirectHeaderEncryption_AES_CM_128_HMAC_SHA1_80) {
744 TestProtectSetParamsDirectHeaderEncryption(rtc::SRTP_AES128_CM_SHA1_80,
745 kTestKey1, kTestKeyLen, kTestKey2, kTestKeyLen,
746 CS_AES_CM_128_HMAC_SHA1_80);
747 }
748
622 // Test directly setting the params with AES_CM_128_HMAC_SHA1_32. 749 // Test directly setting the params with AES_CM_128_HMAC_SHA1_32.
623 TEST_P(SrtpFilterProtectSetParamsDirectTest, Test_AES_CM_128_HMAC_SHA1_32) { 750 TEST_P(SrtpFilterProtectSetParamsDirectTest, Test_AES_CM_128_HMAC_SHA1_32) {
624 bool enable_external_auth = GetParam(); 751 bool enable_external_auth = GetParam();
625 TestProtectSetParamsDirect(enable_external_auth, rtc::SRTP_AES128_CM_SHA1_32, 752 TestProtectSetParamsDirect(enable_external_auth, rtc::SRTP_AES128_CM_SHA1_32,
626 kTestKey1, kTestKeyLen, kTestKey2, kTestKeyLen, 753 kTestKey1, kTestKeyLen, kTestKey2, kTestKeyLen,
627 CS_AES_CM_128_HMAC_SHA1_32); 754 CS_AES_CM_128_HMAC_SHA1_32);
628 } 755 }
629 756
757 TEST_F(SrtpFilterTest,
758 TestProtectSetParamsDirectHeaderEncryption_AES_CM_128_HMAC_SHA1_32) {
759 TestProtectSetParamsDirectHeaderEncryption(rtc::SRTP_AES128_CM_SHA1_32,
760 kTestKey1, kTestKeyLen, kTestKey2, kTestKeyLen,
761 CS_AES_CM_128_HMAC_SHA1_32);
762 }
763
630 // Test directly setting the params with SRTP_AEAD_AES_128_GCM. 764 // Test directly setting the params with SRTP_AEAD_AES_128_GCM.
631 TEST_P(SrtpFilterProtectSetParamsDirectTest, Test_SRTP_AEAD_AES_128_GCM) { 765 TEST_P(SrtpFilterProtectSetParamsDirectTest, Test_SRTP_AEAD_AES_128_GCM) {
632 bool enable_external_auth = GetParam(); 766 bool enable_external_auth = GetParam();
633 TestProtectSetParamsDirect(enable_external_auth, rtc::SRTP_AEAD_AES_128_GCM, 767 TestProtectSetParamsDirect(enable_external_auth, rtc::SRTP_AEAD_AES_128_GCM,
634 kTestKeyGcm128_1, kTestKeyGcm128Len, kTestKeyGcm128_2, kTestKeyGcm128Len, 768 kTestKeyGcm128_1, kTestKeyGcm128Len, kTestKeyGcm128_2, kTestKeyGcm128Len,
635 CS_AEAD_AES_128_GCM); 769 CS_AEAD_AES_128_GCM);
636 } 770 }
637 771
772 TEST_F(SrtpFilterTest,
773 TestProtectSetParamsDirectHeaderEncryption_SRTP_AEAD_AES_128_GCM) {
774 TestProtectSetParamsDirectHeaderEncryption(rtc::SRTP_AEAD_AES_128_GCM,
775 kTestKeyGcm128_1, kTestKeyGcm128Len, kTestKeyGcm128_2, kTestKeyGcm128Len,
776 CS_AEAD_AES_128_GCM);
777 }
778
638 // Test directly setting the params with SRTP_AEAD_AES_256_GCM. 779 // Test directly setting the params with SRTP_AEAD_AES_256_GCM.
639 TEST_P(SrtpFilterProtectSetParamsDirectTest, Test_SRTP_AEAD_AES_256_GCM) { 780 TEST_P(SrtpFilterProtectSetParamsDirectTest, Test_SRTP_AEAD_AES_256_GCM) {
640 bool enable_external_auth = GetParam(); 781 bool enable_external_auth = GetParam();
641 TestProtectSetParamsDirect(enable_external_auth, rtc::SRTP_AEAD_AES_256_GCM, 782 TestProtectSetParamsDirect(enable_external_auth, rtc::SRTP_AEAD_AES_256_GCM,
642 kTestKeyGcm256_1, kTestKeyGcm256Len, kTestKeyGcm256_2, kTestKeyGcm256Len, 783 kTestKeyGcm256_1, kTestKeyGcm256Len, kTestKeyGcm256_2, kTestKeyGcm256Len,
643 CS_AEAD_AES_256_GCM); 784 CS_AEAD_AES_256_GCM);
644 } 785 }
645 786
787 TEST_F(SrtpFilterTest,
788 TestProtectSetParamsDirectHeaderEncryption_SRTP_AEAD_AES_256_GCM) {
789 TestProtectSetParamsDirectHeaderEncryption(rtc::SRTP_AEAD_AES_256_GCM,
790 kTestKeyGcm256_1, kTestKeyGcm256Len, kTestKeyGcm256_2, kTestKeyGcm256Len,
791 CS_AEAD_AES_256_GCM);
792 }
793
646 // Run all tests both with and without external auth enabled. 794 // Run all tests both with and without external auth enabled.
647 INSTANTIATE_TEST_CASE_P(ExternalAuth, 795 INSTANTIATE_TEST_CASE_P(ExternalAuth,
648 SrtpFilterProtectSetParamsDirectTest, 796 SrtpFilterProtectSetParamsDirectTest,
649 ::testing::Values(true, false)); 797 ::testing::Values(true, false));
650 798
651 // Test directly setting the params with bogus keys. 799 // Test directly setting the params with bogus keys.
652 TEST_F(SrtpFilterTest, TestSetParamsKeyTooShort) { 800 TEST_F(SrtpFilterTest, TestSetParamsKeyTooShort) {
653 EXPECT_FALSE(f1_.SetRtpParams(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, 801 EXPECT_FALSE(f1_.SetRtpParams(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1,
654 kTestKeyLen - 1, rtc::SRTP_AES128_CM_SHA1_80, 802 kTestKeyLen - 1, rtc::SRTP_AES128_CM_SHA1_80,
655 kTestKey1, kTestKeyLen - 1)); 803 kTestKey1, kTestKeyLen - 1));
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
697 cricket::SrtpSession s1_; 845 cricket::SrtpSession s1_;
698 cricket::SrtpSession s2_; 846 cricket::SrtpSession s2_;
699 char rtp_packet_[sizeof(kPcmuFrame) + 10]; 847 char rtp_packet_[sizeof(kPcmuFrame) + 10];
700 char rtcp_packet_[sizeof(kRtcpReport) + 4 + 10]; 848 char rtcp_packet_[sizeof(kRtcpReport) + 4 + 10];
701 int rtp_len_; 849 int rtp_len_;
702 int rtcp_len_; 850 int rtcp_len_;
703 }; 851 };
704 852
705 // Test that we can set up the session and keys properly. 853 // Test that we can set up the session and keys properly.
706 TEST_F(SrtpSessionTest, TestGoodSetup) { 854 TEST_F(SrtpSessionTest, TestGoodSetup) {
707 EXPECT_TRUE(s1_.SetSend(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen)); 855 std::vector<int> no_encrypted_headers;
708 EXPECT_TRUE(s2_.SetRecv(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen)); 856 EXPECT_TRUE(s1_.SetSend(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen,
857 no_encrypted_headers));
858 EXPECT_TRUE(s2_.SetRecv(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen,
859 no_encrypted_headers));
709 } 860 }
710 861
711 // Test that we can't change the keys once set. 862 // Test that we can't change the keys once set.
712 TEST_F(SrtpSessionTest, TestBadSetup) { 863 TEST_F(SrtpSessionTest, TestBadSetup) {
713 EXPECT_TRUE(s1_.SetSend(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen)); 864 std::vector<int> no_encrypted_headers;
714 EXPECT_TRUE(s2_.SetRecv(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen)); 865 EXPECT_TRUE(s1_.SetSend(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen,
866 no_encrypted_headers));
867 EXPECT_TRUE(s2_.SetRecv(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen,
868 no_encrypted_headers));
715 EXPECT_FALSE( 869 EXPECT_FALSE(
716 s1_.SetSend(rtc::SRTP_AES128_CM_SHA1_80, kTestKey2, kTestKeyLen)); 870 s1_.SetSend(rtc::SRTP_AES128_CM_SHA1_80, kTestKey2, kTestKeyLen,
871 no_encrypted_headers));
717 EXPECT_FALSE( 872 EXPECT_FALSE(
718 s2_.SetRecv(rtc::SRTP_AES128_CM_SHA1_80, kTestKey2, kTestKeyLen)); 873 s2_.SetRecv(rtc::SRTP_AES128_CM_SHA1_80, kTestKey2, kTestKeyLen,
874 no_encrypted_headers));
719 } 875 }
720 876
721 // Test that we fail keys of the wrong length. 877 // Test that we fail keys of the wrong length.
722 TEST_F(SrtpSessionTest, TestKeysTooShort) { 878 TEST_F(SrtpSessionTest, TestKeysTooShort) {
723 EXPECT_FALSE(s1_.SetSend(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, 1)); 879 std::vector<int> no_encrypted_headers;
724 EXPECT_FALSE(s2_.SetRecv(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, 1)); 880 EXPECT_FALSE(s1_.SetSend(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, 1,
881 no_encrypted_headers));
882 EXPECT_FALSE(s2_.SetRecv(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, 1,
883 no_encrypted_headers));
725 } 884 }
726 885
727 // Test that we can encrypt and decrypt RTP/RTCP using AES_CM_128_HMAC_SHA1_80. 886 // Test that we can encrypt and decrypt RTP/RTCP using AES_CM_128_HMAC_SHA1_80.
728 TEST_F(SrtpSessionTest, TestProtect_AES_CM_128_HMAC_SHA1_80) { 887 TEST_F(SrtpSessionTest, TestProtect_AES_CM_128_HMAC_SHA1_80) {
729 EXPECT_TRUE(s1_.SetSend(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen)); 888 std::vector<int> no_encrypted_headers;
730 EXPECT_TRUE(s2_.SetRecv(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen)); 889 EXPECT_TRUE(s1_.SetSend(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen,
890 no_encrypted_headers));
891 EXPECT_TRUE(s2_.SetRecv(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen,
892 no_encrypted_headers));
731 TestProtectRtp(CS_AES_CM_128_HMAC_SHA1_80); 893 TestProtectRtp(CS_AES_CM_128_HMAC_SHA1_80);
732 TestProtectRtcp(CS_AES_CM_128_HMAC_SHA1_80); 894 TestProtectRtcp(CS_AES_CM_128_HMAC_SHA1_80);
733 TestUnprotectRtp(CS_AES_CM_128_HMAC_SHA1_80); 895 TestUnprotectRtp(CS_AES_CM_128_HMAC_SHA1_80);
734 TestUnprotectRtcp(CS_AES_CM_128_HMAC_SHA1_80); 896 TestUnprotectRtcp(CS_AES_CM_128_HMAC_SHA1_80);
735 } 897 }
736 898
737 // Test that we can encrypt and decrypt RTP/RTCP using AES_CM_128_HMAC_SHA1_32. 899 // Test that we can encrypt and decrypt RTP/RTCP using AES_CM_128_HMAC_SHA1_32.
738 TEST_F(SrtpSessionTest, TestProtect_AES_CM_128_HMAC_SHA1_32) { 900 TEST_F(SrtpSessionTest, TestProtect_AES_CM_128_HMAC_SHA1_32) {
739 EXPECT_TRUE(s1_.SetSend(rtc::SRTP_AES128_CM_SHA1_32, kTestKey1, kTestKeyLen)); 901 std::vector<int> no_encrypted_headers;
740 EXPECT_TRUE(s2_.SetRecv(rtc::SRTP_AES128_CM_SHA1_32, kTestKey1, kTestKeyLen)); 902 EXPECT_TRUE(s1_.SetSend(rtc::SRTP_AES128_CM_SHA1_32, kTestKey1, kTestKeyLen,
903 no_encrypted_headers));
904 EXPECT_TRUE(s2_.SetRecv(rtc::SRTP_AES128_CM_SHA1_32, kTestKey1, kTestKeyLen,
905 no_encrypted_headers));
741 TestProtectRtp(CS_AES_CM_128_HMAC_SHA1_32); 906 TestProtectRtp(CS_AES_CM_128_HMAC_SHA1_32);
742 TestProtectRtcp(CS_AES_CM_128_HMAC_SHA1_32); 907 TestProtectRtcp(CS_AES_CM_128_HMAC_SHA1_32);
743 TestUnprotectRtp(CS_AES_CM_128_HMAC_SHA1_32); 908 TestUnprotectRtp(CS_AES_CM_128_HMAC_SHA1_32);
744 TestUnprotectRtcp(CS_AES_CM_128_HMAC_SHA1_32); 909 TestUnprotectRtcp(CS_AES_CM_128_HMAC_SHA1_32);
745 } 910 }
746 911
747 TEST_F(SrtpSessionTest, TestGetSendStreamPacketIndex) { 912 TEST_F(SrtpSessionTest, TestGetSendStreamPacketIndex) {
748 EXPECT_TRUE(s1_.SetSend(rtc::SRTP_AES128_CM_SHA1_32, kTestKey1, kTestKeyLen)); 913 std::vector<int> no_encrypted_headers;
914 EXPECT_TRUE(s1_.SetSend(rtc::SRTP_AES128_CM_SHA1_32, kTestKey1, kTestKeyLen,
915 no_encrypted_headers));
749 int64_t index; 916 int64_t index;
750 int out_len = 0; 917 int out_len = 0;
751 EXPECT_TRUE(s1_.ProtectRtp(rtp_packet_, rtp_len_, 918 EXPECT_TRUE(s1_.ProtectRtp(rtp_packet_, rtp_len_,
752 sizeof(rtp_packet_), &out_len, &index)); 919 sizeof(rtp_packet_), &out_len, &index));
753 // |index| will be shifted by 16. 920 // |index| will be shifted by 16.
754 int64_t be64_index = static_cast<int64_t>(rtc::NetworkToHost64(1 << 16)); 921 int64_t be64_index = static_cast<int64_t>(rtc::NetworkToHost64(1 << 16));
755 EXPECT_EQ(be64_index, index); 922 EXPECT_EQ(be64_index, index);
756 } 923 }
757 924
758 // Test that we fail to unprotect if someone tampers with the RTP/RTCP paylaods. 925 // Test that we fail to unprotect if someone tampers with the RTP/RTCP paylaods.
759 TEST_F(SrtpSessionTest, TestTamperReject) { 926 TEST_F(SrtpSessionTest, TestTamperReject) {
927 std::vector<int> no_encrypted_headers;
760 int out_len; 928 int out_len;
761 EXPECT_TRUE(s1_.SetSend(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen)); 929 EXPECT_TRUE(s1_.SetSend(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen,
762 EXPECT_TRUE(s2_.SetRecv(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen)); 930 no_encrypted_headers));
931 EXPECT_TRUE(s2_.SetRecv(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen,
932 no_encrypted_headers));
763 TestProtectRtp(CS_AES_CM_128_HMAC_SHA1_80); 933 TestProtectRtp(CS_AES_CM_128_HMAC_SHA1_80);
764 TestProtectRtcp(CS_AES_CM_128_HMAC_SHA1_80); 934 TestProtectRtcp(CS_AES_CM_128_HMAC_SHA1_80);
765 rtp_packet_[0] = 0x12; 935 rtp_packet_[0] = 0x12;
766 rtcp_packet_[1] = 0x34; 936 rtcp_packet_[1] = 0x34;
767 EXPECT_FALSE(s2_.UnprotectRtp(rtp_packet_, rtp_len_, &out_len)); 937 EXPECT_FALSE(s2_.UnprotectRtp(rtp_packet_, rtp_len_, &out_len));
768 EXPECT_FALSE(s2_.UnprotectRtcp(rtcp_packet_, rtcp_len_, &out_len)); 938 EXPECT_FALSE(s2_.UnprotectRtcp(rtcp_packet_, rtcp_len_, &out_len));
769 } 939 }
770 940
771 // Test that we fail to unprotect if the payloads are not authenticated. 941 // Test that we fail to unprotect if the payloads are not authenticated.
772 TEST_F(SrtpSessionTest, TestUnencryptReject) { 942 TEST_F(SrtpSessionTest, TestUnencryptReject) {
943 std::vector<int> no_encrypted_headers;
773 int out_len; 944 int out_len;
774 EXPECT_TRUE(s1_.SetSend(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen)); 945 EXPECT_TRUE(s1_.SetSend(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen,
775 EXPECT_TRUE(s2_.SetRecv(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen)); 946 no_encrypted_headers));
947 EXPECT_TRUE(s2_.SetRecv(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen,
948 no_encrypted_headers));
776 EXPECT_FALSE(s2_.UnprotectRtp(rtp_packet_, rtp_len_, &out_len)); 949 EXPECT_FALSE(s2_.UnprotectRtp(rtp_packet_, rtp_len_, &out_len));
777 EXPECT_FALSE(s2_.UnprotectRtcp(rtcp_packet_, rtcp_len_, &out_len)); 950 EXPECT_FALSE(s2_.UnprotectRtcp(rtcp_packet_, rtcp_len_, &out_len));
778 } 951 }
779 952
780 // Test that we fail when using buffers that are too small. 953 // Test that we fail when using buffers that are too small.
781 TEST_F(SrtpSessionTest, TestBuffersTooSmall) { 954 TEST_F(SrtpSessionTest, TestBuffersTooSmall) {
955 std::vector<int> no_encrypted_headers;
782 int out_len; 956 int out_len;
783 EXPECT_TRUE(s1_.SetSend(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen)); 957 EXPECT_TRUE(s1_.SetSend(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen,
958 no_encrypted_headers));
784 EXPECT_FALSE(s1_.ProtectRtp(rtp_packet_, rtp_len_, 959 EXPECT_FALSE(s1_.ProtectRtp(rtp_packet_, rtp_len_,
785 sizeof(rtp_packet_) - 10, &out_len)); 960 sizeof(rtp_packet_) - 10, &out_len));
786 EXPECT_FALSE(s1_.ProtectRtcp(rtcp_packet_, rtcp_len_, 961 EXPECT_FALSE(s1_.ProtectRtcp(rtcp_packet_, rtcp_len_,
787 sizeof(rtcp_packet_) - 14, &out_len)); 962 sizeof(rtcp_packet_) - 14, &out_len));
788 } 963 }
789 964
790 TEST_F(SrtpSessionTest, TestReplay) { 965 TEST_F(SrtpSessionTest, TestReplay) {
966 std::vector<int> no_encrypted_headers;
791 static const uint16_t kMaxSeqnum = static_cast<uint16_t>(-1); 967 static const uint16_t kMaxSeqnum = static_cast<uint16_t>(-1);
792 static const uint16_t seqnum_big = 62275; 968 static const uint16_t seqnum_big = 62275;
793 static const uint16_t seqnum_small = 10; 969 static const uint16_t seqnum_small = 10;
794 static const uint16_t replay_window = 1024; 970 static const uint16_t replay_window = 1024;
795 int out_len; 971 int out_len;
796 972
797 EXPECT_TRUE(s1_.SetSend(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen)); 973 EXPECT_TRUE(s1_.SetSend(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen,
798 EXPECT_TRUE(s2_.SetRecv(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen)); 974 no_encrypted_headers));
975 EXPECT_TRUE(s2_.SetRecv(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen,
976 no_encrypted_headers));
799 977
800 // Initial sequence number. 978 // Initial sequence number.
801 rtc::SetBE16(reinterpret_cast<uint8_t*>(rtp_packet_) + 2, seqnum_big); 979 rtc::SetBE16(reinterpret_cast<uint8_t*>(rtp_packet_) + 2, seqnum_big);
802 EXPECT_TRUE(s1_.ProtectRtp(rtp_packet_, rtp_len_, sizeof(rtp_packet_), 980 EXPECT_TRUE(s1_.ProtectRtp(rtp_packet_, rtp_len_, sizeof(rtp_packet_),
803 &out_len)); 981 &out_len));
804 982
805 // Replay within the 1024 window should succeed. 983 // Replay within the 1024 window should succeed.
806 rtc::SetBE16(reinterpret_cast<uint8_t*>(rtp_packet_) + 2, 984 rtc::SetBE16(reinterpret_cast<uint8_t*>(rtp_packet_) + 2,
807 seqnum_big - replay_window + 1); 985 seqnum_big - replay_window + 1);
808 EXPECT_TRUE(s1_.ProtectRtp(rtp_packet_, rtp_len_, sizeof(rtp_packet_), 986 EXPECT_TRUE(s1_.ProtectRtp(rtp_packet_, rtp_len_, sizeof(rtp_packet_),
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after
1003 srtp_stat_.AddUnprotectRtcpResult(srtp_err_status_fail); 1181 srtp_stat_.AddUnprotectRtcpResult(srtp_err_status_fail);
1004 EXPECT_EQ(-1, mode_); 1182 EXPECT_EQ(-1, mode_);
1005 EXPECT_EQ(cricket::SrtpFilter::ERROR_NONE, error_); 1183 EXPECT_EQ(cricket::SrtpFilter::ERROR_NONE, error_);
1006 // Now the error will be triggered again. 1184 // Now the error will be triggered again.
1007 Reset(); 1185 Reset();
1008 rtc::Thread::Current()->SleepMs(210); 1186 rtc::Thread::Current()->SleepMs(210);
1009 srtp_stat_.AddUnprotectRtcpResult(srtp_err_status_fail); 1187 srtp_stat_.AddUnprotectRtcpResult(srtp_err_status_fail);
1010 EXPECT_EQ(cricket::SrtpFilter::UNPROTECT, mode_); 1188 EXPECT_EQ(cricket::SrtpFilter::UNPROTECT, mode_);
1011 EXPECT_EQ(cricket::SrtpFilter::ERROR_FAIL, error_); 1189 EXPECT_EQ(cricket::SrtpFilter::ERROR_FAIL, error_);
1012 } 1190 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698