Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 186 | 188 |
| 187 EXPECT_TRUE(f2_.ProtectRtcp(rtcp_packet, rtcp_len, | 189 EXPECT_TRUE(f2_.ProtectRtcp(rtcp_packet, rtcp_len, |
| 188 static_cast<int>(rtcp_buffer.size()), | 190 static_cast<int>(rtcp_buffer.size()), |
| 189 &out_len)); | 191 &out_len)); |
| 190 EXPECT_EQ(out_len, rtcp_len + 4 + rtcp_auth_tag_len(cs2)); // NOLINT | 192 EXPECT_EQ(out_len, rtcp_len + 4 + rtcp_auth_tag_len(cs2)); // NOLINT |
| 191 EXPECT_NE(0, memcmp(rtcp_packet, kRtcpReport, rtcp_len)); | 193 EXPECT_NE(0, memcmp(rtcp_packet, kRtcpReport, rtcp_len)); |
| 192 EXPECT_TRUE(f1_.UnprotectRtcp(rtcp_packet, out_len, &out_len)); | 194 EXPECT_TRUE(f1_.UnprotectRtcp(rtcp_packet, out_len, &out_len)); |
| 193 EXPECT_EQ(rtcp_len, out_len); | 195 EXPECT_EQ(rtcp_len, out_len); |
| 194 EXPECT_EQ(0, memcmp(rtcp_packet, kRtcpReport, rtcp_len)); | 196 EXPECT_EQ(0, memcmp(rtcp_packet, kRtcpReport, rtcp_len)); |
| 195 } | 197 } |
| 198 void CompareHeaderExtensions(const char* packet1, const char* packet2, | |
| 199 const std::vector<int> encrypted_headers, bool expect_equal) { | |
| 200 // This expects both packets to be based on kPcmuWithExtensionsFrame. | |
|
Taylor Brandstetter
2017/03/22 18:00:11
nit: Comment may look better above method rather t
joachim
2017/03/23 00:04:34
Done.
| |
| 201 | |
| 202 // RTP header + extension header are the same. | |
| 203 EXPECT_EQ(0, memcmp(packet1, packet2, 12 + 4)); | |
| 204 // Check for one-byte header extensions. | |
| 205 EXPECT_EQ('\xBE', packet1[12]); | |
| 206 EXPECT_EQ('\xDE', packet1[13]); | |
| 207 // Determine position and size of extension headers. | |
| 208 size_t extension_words = packet1[14] << 8 | packet1[15]; | |
| 209 const char* extension_data1 = packet1 + 12 + 4; | |
| 210 const char* extension_end1 = extension_data1 + extension_words * 4; | |
| 211 const char* extension_data2 = packet2 + 12 + 4; | |
| 212 while (extension_data1 < extension_end1) { | |
| 213 uint8_t id = (*extension_data1 & 0xf0) >> 4; | |
| 214 uint8_t len = (*extension_data1 & 0x0f) +1; | |
| 215 extension_data1++; | |
| 216 extension_data2++; | |
| 217 EXPECT_LE(extension_data1, extension_end1); | |
| 218 if (id == 15) { | |
| 219 // Finished parsing. | |
| 220 break; | |
| 221 } | |
| 222 | |
| 223 // The header extension doesn't get encrypted if the id not in the list | |
| 224 // of header extensions to encrypt. | |
|
Taylor Brandstetter
2017/03/22 18:00:11
So, unencrypted extensions are always expected to
joachim
2017/03/23 00:04:34
Done.
| |
| 225 if (expect_equal || | |
| 226 std::find(encrypted_headers.begin(), encrypted_headers.end(), id) == | |
| 227 encrypted_headers.end()) { | |
| 228 EXPECT_EQ(0, memcmp(extension_data1, extension_data2, len)); | |
| 229 } else { | |
| 230 EXPECT_NE(0, memcmp(extension_data1, extension_data2, len)); | |
| 231 } | |
| 232 | |
| 233 extension_data1 += len; | |
| 234 extension_data2 += len; | |
| 235 // Skip padding. | |
| 236 while (extension_data1 < extension_end1 && *extension_data1 == 0) { | |
| 237 extension_data1++; | |
| 238 extension_data2++; | |
| 239 } | |
| 240 } | |
| 241 } | |
| 242 void TestProtectUnprotectHeaderEncryption(const std::string& cs1, | |
| 243 const std::string& cs2, const std::vector<int>& encrypted_headers) { | |
| 244 rtc::Buffer rtp_buffer(sizeof(kPcmuWithExtensionsFrame) + | |
| 245 rtp_auth_tag_len(cs1)); | |
| 246 char* rtp_packet = rtp_buffer.data<char>(); | |
| 247 char original_rtp_packet[sizeof(kPcmuWithExtensionsFrame)]; | |
| 248 int rtp_len = sizeof(kPcmuWithExtensionsFrame), out_len; | |
| 249 memcpy(rtp_packet, kPcmuWithExtensionsFrame, rtp_len); | |
| 250 // In order to be able to run this test function multiple times we can not | |
| 251 // use the same sequence number twice. Increase the sequence number by one. | |
| 252 rtc::SetBE16(reinterpret_cast<uint8_t*>(rtp_packet) + 2, | |
| 253 ++sequence_number_); | |
| 254 memcpy(original_rtp_packet, rtp_packet, rtp_len); | |
| 255 | |
| 256 EXPECT_TRUE(f1_.ProtectRtp(rtp_packet, rtp_len, | |
| 257 static_cast<int>(rtp_buffer.size()), | |
| 258 &out_len)); | |
| 259 EXPECT_EQ(out_len, rtp_len + rtp_auth_tag_len(cs1)); | |
| 260 EXPECT_NE(0, memcmp(rtp_packet, original_rtp_packet, rtp_len)); | |
| 261 CompareHeaderExtensions(rtp_packet, original_rtp_packet, encrypted_headers, | |
| 262 false); | |
| 263 EXPECT_TRUE(f2_.UnprotectRtp(rtp_packet, out_len, &out_len)); | |
| 264 EXPECT_EQ(rtp_len, out_len); | |
| 265 EXPECT_EQ(0, memcmp(rtp_packet, original_rtp_packet, rtp_len)); | |
| 266 CompareHeaderExtensions(rtp_packet, original_rtp_packet, encrypted_headers, | |
| 267 true); | |
| 268 | |
| 269 EXPECT_TRUE(f2_.ProtectRtp(rtp_packet, rtp_len, | |
| 270 static_cast<int>(rtp_buffer.size()), | |
| 271 &out_len)); | |
| 272 EXPECT_EQ(out_len, rtp_len + rtp_auth_tag_len(cs2)); | |
| 273 EXPECT_NE(0, memcmp(rtp_packet, original_rtp_packet, rtp_len)); | |
| 274 CompareHeaderExtensions(rtp_packet, original_rtp_packet, encrypted_headers, | |
| 275 false); | |
| 276 EXPECT_TRUE(f1_.UnprotectRtp(rtp_packet, out_len, &out_len)); | |
| 277 EXPECT_EQ(rtp_len, out_len); | |
| 278 EXPECT_EQ(0, memcmp(rtp_packet, original_rtp_packet, rtp_len)); | |
| 279 CompareHeaderExtensions(rtp_packet, original_rtp_packet, encrypted_headers, | |
| 280 true); | |
| 281 } | |
| 196 void TestProtectSetParamsDirect(bool enable_external_auth, int cs, | 282 void TestProtectSetParamsDirect(bool enable_external_auth, int cs, |
| 197 const uint8_t* key1, int key1_len, const uint8_t* key2, int key2_len, | 283 const uint8_t* key1, int key1_len, const uint8_t* key2, int key2_len, |
| 198 const std::string& cs_name) { | 284 const std::string& cs_name) { |
| 285 std::vector<int> no_encrypted_headers; | |
| 199 EXPECT_EQ(key1_len, key2_len); | 286 EXPECT_EQ(key1_len, key2_len); |
| 200 EXPECT_EQ(cs_name, rtc::SrtpCryptoSuiteToName(cs)); | 287 EXPECT_EQ(cs_name, rtc::SrtpCryptoSuiteToName(cs)); |
| 201 if (enable_external_auth) { | 288 if (enable_external_auth) { |
| 202 f1_.EnableExternalAuth(); | 289 f1_.EnableExternalAuth(); |
| 203 f2_.EnableExternalAuth(); | 290 f2_.EnableExternalAuth(); |
| 204 } | 291 } |
| 205 EXPECT_TRUE(f1_.SetRtpParams(cs, key1, key1_len, cs, key2, key2_len)); | 292 EXPECT_TRUE(f1_.SetRtpParams(cs, key1, key1_len, no_encrypted_headers, |
| 206 EXPECT_TRUE(f2_.SetRtpParams(cs, key2, key2_len, cs, key1, key1_len)); | 293 cs, key2, key2_len, no_encrypted_headers)); |
| 294 EXPECT_TRUE(f2_.SetRtpParams(cs, key2, key2_len, no_encrypted_headers, | |
| 295 cs, key1, key1_len, no_encrypted_headers)); | |
| 207 EXPECT_TRUE(f1_.SetRtcpParams(cs, key1, key1_len, cs, key2, key2_len)); | 296 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)); | 297 EXPECT_TRUE(f2_.SetRtcpParams(cs, key2, key2_len, cs, key1, key1_len)); |
| 209 EXPECT_TRUE(f1_.IsActive()); | 298 EXPECT_TRUE(f1_.IsActive()); |
| 210 EXPECT_TRUE(f2_.IsActive()); | 299 EXPECT_TRUE(f2_.IsActive()); |
| 211 if (rtc::IsGcmCryptoSuite(cs)) { | 300 if (rtc::IsGcmCryptoSuite(cs)) { |
| 212 EXPECT_FALSE(f1_.IsExternalAuthActive()); | 301 EXPECT_FALSE(f1_.IsExternalAuthActive()); |
| 213 EXPECT_FALSE(f2_.IsExternalAuthActive()); | 302 EXPECT_FALSE(f2_.IsExternalAuthActive()); |
| 214 } else if (enable_external_auth) { | 303 } else if (enable_external_auth) { |
| 215 EXPECT_TRUE(f1_.IsExternalAuthActive()); | 304 EXPECT_TRUE(f1_.IsExternalAuthActive()); |
| 216 EXPECT_TRUE(f2_.IsExternalAuthActive()); | 305 EXPECT_TRUE(f2_.IsExternalAuthActive()); |
| 217 } | 306 } |
| 218 TestProtectUnprotect(cs_name, cs_name); | 307 TestProtectUnprotect(cs_name, cs_name); |
| 219 } | 308 } |
| 309 void TestProtectSetParamsDirectHeaderEncryption(int cs, | |
| 310 const uint8_t* key1, int key1_len, const uint8_t* key2, int key2_len, | |
| 311 const std::string& cs_name) { | |
| 312 std::vector<int> encrypted_headers; | |
| 313 encrypted_headers.push_back(1); | |
| 314 // Don't encrypt header ids 2 and 3. | |
| 315 encrypted_headers.push_back(4); | |
| 316 EXPECT_EQ(key1_len, key2_len); | |
| 317 EXPECT_EQ(cs_name, rtc::SrtpCryptoSuiteToName(cs)); | |
| 318 EXPECT_TRUE(f1_.SetRtpParams(cs, key1, key1_len, encrypted_headers, | |
| 319 cs, key2, key2_len, encrypted_headers)); | |
| 320 EXPECT_TRUE(f2_.SetRtpParams(cs, key2, key2_len, encrypted_headers, | |
| 321 cs, key1, key1_len, encrypted_headers)); | |
| 322 EXPECT_TRUE(f1_.IsActive()); | |
| 323 EXPECT_TRUE(f2_.IsActive()); | |
| 324 EXPECT_FALSE(f1_.IsExternalAuthActive()); | |
| 325 EXPECT_FALSE(f2_.IsExternalAuthActive()); | |
| 326 TestProtectUnprotectHeaderEncryption(cs_name, cs_name, encrypted_headers); | |
| 327 } | |
| 220 cricket::SrtpFilter f1_; | 328 cricket::SrtpFilter f1_; |
| 221 cricket::SrtpFilter f2_; | 329 cricket::SrtpFilter f2_; |
| 222 int sequence_number_; | 330 int sequence_number_; |
| 223 }; | 331 }; |
| 224 | 332 |
| 225 // Test that we can set up the session and keys properly. | 333 // Test that we can set up the session and keys properly. |
| 226 TEST_F(SrtpFilterTest, TestGoodSetupOneCipherSuite) { | 334 TEST_F(SrtpFilterTest, TestGoodSetupOneCipherSuite) { |
| 227 EXPECT_TRUE(f1_.SetOffer(MakeVector(kTestCryptoParams1), CS_LOCAL)); | 335 EXPECT_TRUE(f1_.SetOffer(MakeVector(kTestCryptoParams1), CS_LOCAL)); |
| 228 EXPECT_FALSE(f1_.IsActive()); | 336 EXPECT_FALSE(f1_.IsActive()); |
| 229 EXPECT_TRUE(f1_.SetAnswer(MakeVector(kTestCryptoParams2), CS_REMOTE)); | 337 EXPECT_TRUE(f1_.SetAnswer(MakeVector(kTestCryptoParams2), CS_REMOTE)); |
| (...skipping 382 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 612 }; | 720 }; |
| 613 | 721 |
| 614 // Test directly setting the params with AES_CM_128_HMAC_SHA1_80. | 722 // Test directly setting the params with AES_CM_128_HMAC_SHA1_80. |
| 615 TEST_P(SrtpFilterProtectSetParamsDirectTest, Test_AES_CM_128_HMAC_SHA1_80) { | 723 TEST_P(SrtpFilterProtectSetParamsDirectTest, Test_AES_CM_128_HMAC_SHA1_80) { |
| 616 bool enable_external_auth = GetParam(); | 724 bool enable_external_auth = GetParam(); |
| 617 TestProtectSetParamsDirect(enable_external_auth, rtc::SRTP_AES128_CM_SHA1_80, | 725 TestProtectSetParamsDirect(enable_external_auth, rtc::SRTP_AES128_CM_SHA1_80, |
| 618 kTestKey1, kTestKeyLen, kTestKey2, kTestKeyLen, | 726 kTestKey1, kTestKeyLen, kTestKey2, kTestKeyLen, |
| 619 CS_AES_CM_128_HMAC_SHA1_80); | 727 CS_AES_CM_128_HMAC_SHA1_80); |
| 620 } | 728 } |
| 621 | 729 |
| 730 TEST_F(SrtpFilterTest, | |
| 731 TestProtectSetParamsDirectHeaderEncryption_AES_CM_128_HMAC_SHA1_80) { | |
| 732 TestProtectSetParamsDirectHeaderEncryption(rtc::SRTP_AES128_CM_SHA1_80, | |
| 733 kTestKey1, kTestKeyLen, kTestKey2, kTestKeyLen, | |
| 734 CS_AES_CM_128_HMAC_SHA1_80); | |
| 735 } | |
| 736 | |
| 622 // Test directly setting the params with AES_CM_128_HMAC_SHA1_32. | 737 // Test directly setting the params with AES_CM_128_HMAC_SHA1_32. |
| 623 TEST_P(SrtpFilterProtectSetParamsDirectTest, Test_AES_CM_128_HMAC_SHA1_32) { | 738 TEST_P(SrtpFilterProtectSetParamsDirectTest, Test_AES_CM_128_HMAC_SHA1_32) { |
| 624 bool enable_external_auth = GetParam(); | 739 bool enable_external_auth = GetParam(); |
| 625 TestProtectSetParamsDirect(enable_external_auth, rtc::SRTP_AES128_CM_SHA1_32, | 740 TestProtectSetParamsDirect(enable_external_auth, rtc::SRTP_AES128_CM_SHA1_32, |
| 626 kTestKey1, kTestKeyLen, kTestKey2, kTestKeyLen, | 741 kTestKey1, kTestKeyLen, kTestKey2, kTestKeyLen, |
| 627 CS_AES_CM_128_HMAC_SHA1_32); | 742 CS_AES_CM_128_HMAC_SHA1_32); |
| 628 } | 743 } |
| 629 | 744 |
| 745 TEST_F(SrtpFilterTest, | |
| 746 TestProtectSetParamsDirectHeaderEncryption_AES_CM_128_HMAC_SHA1_32) { | |
| 747 TestProtectSetParamsDirectHeaderEncryption(rtc::SRTP_AES128_CM_SHA1_32, | |
| 748 kTestKey1, kTestKeyLen, kTestKey2, kTestKeyLen, | |
| 749 CS_AES_CM_128_HMAC_SHA1_32); | |
| 750 } | |
| 751 | |
| 630 // Test directly setting the params with SRTP_AEAD_AES_128_GCM. | 752 // Test directly setting the params with SRTP_AEAD_AES_128_GCM. |
| 631 TEST_P(SrtpFilterProtectSetParamsDirectTest, Test_SRTP_AEAD_AES_128_GCM) { | 753 TEST_P(SrtpFilterProtectSetParamsDirectTest, Test_SRTP_AEAD_AES_128_GCM) { |
| 632 bool enable_external_auth = GetParam(); | 754 bool enable_external_auth = GetParam(); |
| 633 TestProtectSetParamsDirect(enable_external_auth, rtc::SRTP_AEAD_AES_128_GCM, | 755 TestProtectSetParamsDirect(enable_external_auth, rtc::SRTP_AEAD_AES_128_GCM, |
| 634 kTestKeyGcm128_1, kTestKeyGcm128Len, kTestKeyGcm128_2, kTestKeyGcm128Len, | 756 kTestKeyGcm128_1, kTestKeyGcm128Len, kTestKeyGcm128_2, kTestKeyGcm128Len, |
| 635 CS_AEAD_AES_128_GCM); | 757 CS_AEAD_AES_128_GCM); |
| 636 } | 758 } |
| 637 | 759 |
| 760 TEST_F(SrtpFilterTest, | |
| 761 TestProtectSetParamsDirectHeaderEncryption_SRTP_AEAD_AES_128_GCM) { | |
| 762 TestProtectSetParamsDirectHeaderEncryption(rtc::SRTP_AEAD_AES_128_GCM, | |
| 763 kTestKeyGcm128_1, kTestKeyGcm128Len, kTestKeyGcm128_2, kTestKeyGcm128Len, | |
| 764 CS_AEAD_AES_128_GCM); | |
| 765 } | |
| 766 | |
| 638 // Test directly setting the params with SRTP_AEAD_AES_256_GCM. | 767 // Test directly setting the params with SRTP_AEAD_AES_256_GCM. |
| 639 TEST_P(SrtpFilterProtectSetParamsDirectTest, Test_SRTP_AEAD_AES_256_GCM) { | 768 TEST_P(SrtpFilterProtectSetParamsDirectTest, Test_SRTP_AEAD_AES_256_GCM) { |
| 640 bool enable_external_auth = GetParam(); | 769 bool enable_external_auth = GetParam(); |
| 641 TestProtectSetParamsDirect(enable_external_auth, rtc::SRTP_AEAD_AES_256_GCM, | 770 TestProtectSetParamsDirect(enable_external_auth, rtc::SRTP_AEAD_AES_256_GCM, |
| 642 kTestKeyGcm256_1, kTestKeyGcm256Len, kTestKeyGcm256_2, kTestKeyGcm256Len, | 771 kTestKeyGcm256_1, kTestKeyGcm256Len, kTestKeyGcm256_2, kTestKeyGcm256Len, |
| 643 CS_AEAD_AES_256_GCM); | 772 CS_AEAD_AES_256_GCM); |
| 644 } | 773 } |
| 645 | 774 |
| 775 TEST_F(SrtpFilterTest, | |
| 776 TestProtectSetParamsDirectHeaderEncryption_SRTP_AEAD_AES_256_GCM) { | |
| 777 TestProtectSetParamsDirectHeaderEncryption(rtc::SRTP_AEAD_AES_256_GCM, | |
| 778 kTestKeyGcm256_1, kTestKeyGcm256Len, kTestKeyGcm256_2, kTestKeyGcm256Len, | |
| 779 CS_AEAD_AES_256_GCM); | |
| 780 } | |
| 781 | |
| 646 // Run all tests both with and without external auth enabled. | 782 // Run all tests both with and without external auth enabled. |
| 647 INSTANTIATE_TEST_CASE_P(ExternalAuth, | 783 INSTANTIATE_TEST_CASE_P(ExternalAuth, |
| 648 SrtpFilterProtectSetParamsDirectTest, | 784 SrtpFilterProtectSetParamsDirectTest, |
| 649 ::testing::Values(true, false)); | 785 ::testing::Values(true, false)); |
| 650 | 786 |
| 651 // Test directly setting the params with bogus keys. | 787 // Test directly setting the params with bogus keys. |
| 652 TEST_F(SrtpFilterTest, TestSetParamsKeyTooShort) { | 788 TEST_F(SrtpFilterTest, TestSetParamsKeyTooShort) { |
| 789 std::vector<int> no_encrypted_headers; | |
| 653 EXPECT_FALSE(f1_.SetRtpParams(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, | 790 EXPECT_FALSE(f1_.SetRtpParams(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, |
| 654 kTestKeyLen - 1, rtc::SRTP_AES128_CM_SHA1_80, | 791 kTestKeyLen - 1, no_encrypted_headers, |
| 655 kTestKey1, kTestKeyLen - 1)); | 792 rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, |
| 793 kTestKeyLen - 1, no_encrypted_headers)); | |
| 656 EXPECT_FALSE(f1_.SetRtcpParams(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, | 794 EXPECT_FALSE(f1_.SetRtcpParams(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, |
| 657 kTestKeyLen - 1, rtc::SRTP_AES128_CM_SHA1_80, | 795 kTestKeyLen - 1, rtc::SRTP_AES128_CM_SHA1_80, |
| 658 kTestKey1, kTestKeyLen - 1)); | 796 kTestKey1, kTestKeyLen - 1)); |
| 659 } | 797 } |
| 660 | 798 |
| 661 class SrtpSessionTest : public testing::Test { | 799 class SrtpSessionTest : public testing::Test { |
| 662 protected: | 800 protected: |
| 663 virtual void SetUp() { | 801 virtual void SetUp() { |
| 664 rtp_len_ = sizeof(kPcmuFrame); | 802 rtp_len_ = sizeof(kPcmuFrame); |
| 665 rtcp_len_ = sizeof(kRtcpReport); | 803 rtcp_len_ = sizeof(kRtcpReport); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 697 cricket::SrtpSession s1_; | 835 cricket::SrtpSession s1_; |
| 698 cricket::SrtpSession s2_; | 836 cricket::SrtpSession s2_; |
| 699 char rtp_packet_[sizeof(kPcmuFrame) + 10]; | 837 char rtp_packet_[sizeof(kPcmuFrame) + 10]; |
| 700 char rtcp_packet_[sizeof(kRtcpReport) + 4 + 10]; | 838 char rtcp_packet_[sizeof(kRtcpReport) + 4 + 10]; |
| 701 int rtp_len_; | 839 int rtp_len_; |
| 702 int rtcp_len_; | 840 int rtcp_len_; |
| 703 }; | 841 }; |
| 704 | 842 |
| 705 // Test that we can set up the session and keys properly. | 843 // Test that we can set up the session and keys properly. |
| 706 TEST_F(SrtpSessionTest, TestGoodSetup) { | 844 TEST_F(SrtpSessionTest, TestGoodSetup) { |
| 707 EXPECT_TRUE(s1_.SetSend(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen)); | 845 std::vector<int> no_encrypted_headers; |
| 708 EXPECT_TRUE(s2_.SetRecv(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen)); | 846 EXPECT_TRUE(s1_.SetSend(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen, |
| 847 no_encrypted_headers)); | |
| 848 EXPECT_TRUE(s2_.SetRecv(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen, | |
| 849 no_encrypted_headers)); | |
| 709 } | 850 } |
| 710 | 851 |
| 711 // Test that we can't change the keys once set. | 852 // Test that we can't change the keys once set. |
| 712 TEST_F(SrtpSessionTest, TestBadSetup) { | 853 TEST_F(SrtpSessionTest, TestBadSetup) { |
| 713 EXPECT_TRUE(s1_.SetSend(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen)); | 854 std::vector<int> no_encrypted_headers; |
| 714 EXPECT_TRUE(s2_.SetRecv(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen)); | 855 EXPECT_TRUE(s1_.SetSend(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen, |
| 856 no_encrypted_headers)); | |
| 857 EXPECT_TRUE(s2_.SetRecv(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen, | |
| 858 no_encrypted_headers)); | |
| 715 EXPECT_FALSE( | 859 EXPECT_FALSE( |
| 716 s1_.SetSend(rtc::SRTP_AES128_CM_SHA1_80, kTestKey2, kTestKeyLen)); | 860 s1_.SetSend(rtc::SRTP_AES128_CM_SHA1_80, kTestKey2, kTestKeyLen, |
| 861 no_encrypted_headers)); | |
| 717 EXPECT_FALSE( | 862 EXPECT_FALSE( |
| 718 s2_.SetRecv(rtc::SRTP_AES128_CM_SHA1_80, kTestKey2, kTestKeyLen)); | 863 s2_.SetRecv(rtc::SRTP_AES128_CM_SHA1_80, kTestKey2, kTestKeyLen, |
| 864 no_encrypted_headers)); | |
| 719 } | 865 } |
| 720 | 866 |
| 721 // Test that we fail keys of the wrong length. | 867 // Test that we fail keys of the wrong length. |
| 722 TEST_F(SrtpSessionTest, TestKeysTooShort) { | 868 TEST_F(SrtpSessionTest, TestKeysTooShort) { |
| 723 EXPECT_FALSE(s1_.SetSend(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, 1)); | 869 std::vector<int> no_encrypted_headers; |
| 724 EXPECT_FALSE(s2_.SetRecv(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, 1)); | 870 EXPECT_FALSE(s1_.SetSend(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, 1, |
| 871 no_encrypted_headers)); | |
| 872 EXPECT_FALSE(s2_.SetRecv(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, 1, | |
| 873 no_encrypted_headers)); | |
| 725 } | 874 } |
| 726 | 875 |
| 727 // Test that we can encrypt and decrypt RTP/RTCP using AES_CM_128_HMAC_SHA1_80. | 876 // 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) { | 877 TEST_F(SrtpSessionTest, TestProtect_AES_CM_128_HMAC_SHA1_80) { |
| 729 EXPECT_TRUE(s1_.SetSend(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen)); | 878 std::vector<int> no_encrypted_headers; |
| 730 EXPECT_TRUE(s2_.SetRecv(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen)); | 879 EXPECT_TRUE(s1_.SetSend(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen, |
| 880 no_encrypted_headers)); | |
| 881 EXPECT_TRUE(s2_.SetRecv(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen, | |
| 882 no_encrypted_headers)); | |
| 731 TestProtectRtp(CS_AES_CM_128_HMAC_SHA1_80); | 883 TestProtectRtp(CS_AES_CM_128_HMAC_SHA1_80); |
| 732 TestProtectRtcp(CS_AES_CM_128_HMAC_SHA1_80); | 884 TestProtectRtcp(CS_AES_CM_128_HMAC_SHA1_80); |
| 733 TestUnprotectRtp(CS_AES_CM_128_HMAC_SHA1_80); | 885 TestUnprotectRtp(CS_AES_CM_128_HMAC_SHA1_80); |
| 734 TestUnprotectRtcp(CS_AES_CM_128_HMAC_SHA1_80); | 886 TestUnprotectRtcp(CS_AES_CM_128_HMAC_SHA1_80); |
| 735 } | 887 } |
| 736 | 888 |
| 737 // Test that we can encrypt and decrypt RTP/RTCP using AES_CM_128_HMAC_SHA1_32. | 889 // 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) { | 890 TEST_F(SrtpSessionTest, TestProtect_AES_CM_128_HMAC_SHA1_32) { |
| 739 EXPECT_TRUE(s1_.SetSend(rtc::SRTP_AES128_CM_SHA1_32, kTestKey1, kTestKeyLen)); | 891 std::vector<int> no_encrypted_headers; |
| 740 EXPECT_TRUE(s2_.SetRecv(rtc::SRTP_AES128_CM_SHA1_32, kTestKey1, kTestKeyLen)); | 892 EXPECT_TRUE(s1_.SetSend(rtc::SRTP_AES128_CM_SHA1_32, kTestKey1, kTestKeyLen, |
| 893 no_encrypted_headers)); | |
| 894 EXPECT_TRUE(s2_.SetRecv(rtc::SRTP_AES128_CM_SHA1_32, kTestKey1, kTestKeyLen, | |
| 895 no_encrypted_headers)); | |
| 741 TestProtectRtp(CS_AES_CM_128_HMAC_SHA1_32); | 896 TestProtectRtp(CS_AES_CM_128_HMAC_SHA1_32); |
| 742 TestProtectRtcp(CS_AES_CM_128_HMAC_SHA1_32); | 897 TestProtectRtcp(CS_AES_CM_128_HMAC_SHA1_32); |
| 743 TestUnprotectRtp(CS_AES_CM_128_HMAC_SHA1_32); | 898 TestUnprotectRtp(CS_AES_CM_128_HMAC_SHA1_32); |
| 744 TestUnprotectRtcp(CS_AES_CM_128_HMAC_SHA1_32); | 899 TestUnprotectRtcp(CS_AES_CM_128_HMAC_SHA1_32); |
| 745 } | 900 } |
| 746 | 901 |
| 747 TEST_F(SrtpSessionTest, TestGetSendStreamPacketIndex) { | 902 TEST_F(SrtpSessionTest, TestGetSendStreamPacketIndex) { |
| 748 EXPECT_TRUE(s1_.SetSend(rtc::SRTP_AES128_CM_SHA1_32, kTestKey1, kTestKeyLen)); | 903 std::vector<int> no_encrypted_headers; |
| 904 EXPECT_TRUE(s1_.SetSend(rtc::SRTP_AES128_CM_SHA1_32, kTestKey1, kTestKeyLen, | |
| 905 no_encrypted_headers)); | |
| 749 int64_t index; | 906 int64_t index; |
| 750 int out_len = 0; | 907 int out_len = 0; |
| 751 EXPECT_TRUE(s1_.ProtectRtp(rtp_packet_, rtp_len_, | 908 EXPECT_TRUE(s1_.ProtectRtp(rtp_packet_, rtp_len_, |
| 752 sizeof(rtp_packet_), &out_len, &index)); | 909 sizeof(rtp_packet_), &out_len, &index)); |
| 753 // |index| will be shifted by 16. | 910 // |index| will be shifted by 16. |
| 754 int64_t be64_index = static_cast<int64_t>(rtc::NetworkToHost64(1 << 16)); | 911 int64_t be64_index = static_cast<int64_t>(rtc::NetworkToHost64(1 << 16)); |
| 755 EXPECT_EQ(be64_index, index); | 912 EXPECT_EQ(be64_index, index); |
| 756 } | 913 } |
| 757 | 914 |
| 758 // Test that we fail to unprotect if someone tampers with the RTP/RTCP paylaods. | 915 // Test that we fail to unprotect if someone tampers with the RTP/RTCP paylaods. |
| 759 TEST_F(SrtpSessionTest, TestTamperReject) { | 916 TEST_F(SrtpSessionTest, TestTamperReject) { |
| 917 std::vector<int> no_encrypted_headers; | |
| 760 int out_len; | 918 int out_len; |
| 761 EXPECT_TRUE(s1_.SetSend(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen)); | 919 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)); | 920 no_encrypted_headers)); |
| 921 EXPECT_TRUE(s2_.SetRecv(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen, | |
| 922 no_encrypted_headers)); | |
| 763 TestProtectRtp(CS_AES_CM_128_HMAC_SHA1_80); | 923 TestProtectRtp(CS_AES_CM_128_HMAC_SHA1_80); |
| 764 TestProtectRtcp(CS_AES_CM_128_HMAC_SHA1_80); | 924 TestProtectRtcp(CS_AES_CM_128_HMAC_SHA1_80); |
| 765 rtp_packet_[0] = 0x12; | 925 rtp_packet_[0] = 0x12; |
| 766 rtcp_packet_[1] = 0x34; | 926 rtcp_packet_[1] = 0x34; |
| 767 EXPECT_FALSE(s2_.UnprotectRtp(rtp_packet_, rtp_len_, &out_len)); | 927 EXPECT_FALSE(s2_.UnprotectRtp(rtp_packet_, rtp_len_, &out_len)); |
| 768 EXPECT_FALSE(s2_.UnprotectRtcp(rtcp_packet_, rtcp_len_, &out_len)); | 928 EXPECT_FALSE(s2_.UnprotectRtcp(rtcp_packet_, rtcp_len_, &out_len)); |
| 769 } | 929 } |
| 770 | 930 |
| 771 // Test that we fail to unprotect if the payloads are not authenticated. | 931 // Test that we fail to unprotect if the payloads are not authenticated. |
| 772 TEST_F(SrtpSessionTest, TestUnencryptReject) { | 932 TEST_F(SrtpSessionTest, TestUnencryptReject) { |
| 933 std::vector<int> no_encrypted_headers; | |
| 773 int out_len; | 934 int out_len; |
| 774 EXPECT_TRUE(s1_.SetSend(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen)); | 935 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)); | 936 no_encrypted_headers)); |
| 937 EXPECT_TRUE(s2_.SetRecv(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen, | |
| 938 no_encrypted_headers)); | |
| 776 EXPECT_FALSE(s2_.UnprotectRtp(rtp_packet_, rtp_len_, &out_len)); | 939 EXPECT_FALSE(s2_.UnprotectRtp(rtp_packet_, rtp_len_, &out_len)); |
| 777 EXPECT_FALSE(s2_.UnprotectRtcp(rtcp_packet_, rtcp_len_, &out_len)); | 940 EXPECT_FALSE(s2_.UnprotectRtcp(rtcp_packet_, rtcp_len_, &out_len)); |
| 778 } | 941 } |
| 779 | 942 |
| 780 // Test that we fail when using buffers that are too small. | 943 // Test that we fail when using buffers that are too small. |
| 781 TEST_F(SrtpSessionTest, TestBuffersTooSmall) { | 944 TEST_F(SrtpSessionTest, TestBuffersTooSmall) { |
| 945 std::vector<int> no_encrypted_headers; | |
| 782 int out_len; | 946 int out_len; |
| 783 EXPECT_TRUE(s1_.SetSend(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen)); | 947 EXPECT_TRUE(s1_.SetSend(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen, |
| 948 no_encrypted_headers)); | |
| 784 EXPECT_FALSE(s1_.ProtectRtp(rtp_packet_, rtp_len_, | 949 EXPECT_FALSE(s1_.ProtectRtp(rtp_packet_, rtp_len_, |
| 785 sizeof(rtp_packet_) - 10, &out_len)); | 950 sizeof(rtp_packet_) - 10, &out_len)); |
| 786 EXPECT_FALSE(s1_.ProtectRtcp(rtcp_packet_, rtcp_len_, | 951 EXPECT_FALSE(s1_.ProtectRtcp(rtcp_packet_, rtcp_len_, |
| 787 sizeof(rtcp_packet_) - 14, &out_len)); | 952 sizeof(rtcp_packet_) - 14, &out_len)); |
| 788 } | 953 } |
| 789 | 954 |
| 790 TEST_F(SrtpSessionTest, TestReplay) { | 955 TEST_F(SrtpSessionTest, TestReplay) { |
| 956 std::vector<int> no_encrypted_headers; | |
| 791 static const uint16_t kMaxSeqnum = static_cast<uint16_t>(-1); | 957 static const uint16_t kMaxSeqnum = static_cast<uint16_t>(-1); |
| 792 static const uint16_t seqnum_big = 62275; | 958 static const uint16_t seqnum_big = 62275; |
| 793 static const uint16_t seqnum_small = 10; | 959 static const uint16_t seqnum_small = 10; |
| 794 static const uint16_t replay_window = 1024; | 960 static const uint16_t replay_window = 1024; |
| 795 int out_len; | 961 int out_len; |
| 796 | 962 |
| 797 EXPECT_TRUE(s1_.SetSend(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen)); | 963 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)); | 964 no_encrypted_headers)); |
| 965 EXPECT_TRUE(s2_.SetRecv(rtc::SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen, | |
| 966 no_encrypted_headers)); | |
| 799 | 967 |
| 800 // Initial sequence number. | 968 // Initial sequence number. |
| 801 rtc::SetBE16(reinterpret_cast<uint8_t*>(rtp_packet_) + 2, seqnum_big); | 969 rtc::SetBE16(reinterpret_cast<uint8_t*>(rtp_packet_) + 2, seqnum_big); |
| 802 EXPECT_TRUE(s1_.ProtectRtp(rtp_packet_, rtp_len_, sizeof(rtp_packet_), | 970 EXPECT_TRUE(s1_.ProtectRtp(rtp_packet_, rtp_len_, sizeof(rtp_packet_), |
| 803 &out_len)); | 971 &out_len)); |
| 804 | 972 |
| 805 // Replay within the 1024 window should succeed. | 973 // Replay within the 1024 window should succeed. |
| 806 rtc::SetBE16(reinterpret_cast<uint8_t*>(rtp_packet_) + 2, | 974 rtc::SetBE16(reinterpret_cast<uint8_t*>(rtp_packet_) + 2, |
| 807 seqnum_big - replay_window + 1); | 975 seqnum_big - replay_window + 1); |
| 808 EXPECT_TRUE(s1_.ProtectRtp(rtp_packet_, rtp_len_, sizeof(rtp_packet_), | 976 EXPECT_TRUE(s1_.ProtectRtp(rtp_packet_, rtp_len_, sizeof(rtp_packet_), |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1003 srtp_stat_.AddUnprotectRtcpResult(srtp_err_status_fail); | 1171 srtp_stat_.AddUnprotectRtcpResult(srtp_err_status_fail); |
| 1004 EXPECT_EQ(-1, mode_); | 1172 EXPECT_EQ(-1, mode_); |
| 1005 EXPECT_EQ(cricket::SrtpFilter::ERROR_NONE, error_); | 1173 EXPECT_EQ(cricket::SrtpFilter::ERROR_NONE, error_); |
| 1006 // Now the error will be triggered again. | 1174 // Now the error will be triggered again. |
| 1007 Reset(); | 1175 Reset(); |
| 1008 rtc::Thread::Current()->SleepMs(210); | 1176 rtc::Thread::Current()->SleepMs(210); |
| 1009 srtp_stat_.AddUnprotectRtcpResult(srtp_err_status_fail); | 1177 srtp_stat_.AddUnprotectRtcpResult(srtp_err_status_fail); |
| 1010 EXPECT_EQ(cricket::SrtpFilter::UNPROTECT, mode_); | 1178 EXPECT_EQ(cricket::SrtpFilter::UNPROTECT, mode_); |
| 1011 EXPECT_EQ(cricket::SrtpFilter::ERROR_FAIL, error_); | 1179 EXPECT_EQ(cricket::SrtpFilter::ERROR_FAIL, error_); |
| 1012 } | 1180 } |
| OLD | NEW |