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

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

Issue 3012333003: Revert of Completed the functionalities of SrtpTransport. (Closed)
Patch Set: Created 3 years, 3 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
« no previous file with comments | « webrtc/pc/srtpfilter.cc ('k') | webrtc/pc/srtptransport.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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> 11 #include <algorithm>
12 12
13 #include "webrtc/pc/srtpfilter.h" 13 #include "webrtc/pc/srtpfilter.h"
14 14
15 #include "webrtc/media/base/cryptoparams.h" 15 #include "webrtc/media/base/cryptoparams.h"
16 #include "webrtc/media/base/fakertp.h"
17 #include "webrtc/p2p/base/sessiondescription.h"
18 #include "webrtc/pc/srtptestutil.h"
19 #include "webrtc/rtc_base/buffer.h"
20 #include "webrtc/rtc_base/byteorder.h"
21 #include "webrtc/rtc_base/constructormagic.h"
16 #include "webrtc/rtc_base/gunit.h" 22 #include "webrtc/rtc_base/gunit.h"
23 #include "webrtc/rtc_base/thread.h"
17 24
18 using cricket::CryptoParams; 25 using cricket::CryptoParams;
19 using cricket::CS_LOCAL; 26 using cricket::CS_LOCAL;
20 using cricket::CS_REMOTE; 27 using cricket::CS_REMOTE;
21 28
22 namespace rtc { 29 namespace rtc {
23 30
31 static const uint8_t kTestKeyGcm128_1[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ12";
32 static const uint8_t kTestKeyGcm128_2[] = "21ZYXWVUTSRQPONMLKJIHGFEDCBA";
33 static const int kTestKeyGcm128Len = 28; // 128 bits key + 96 bits salt.
34 static const uint8_t kTestKeyGcm256_1[] =
35 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqr";
36 static const uint8_t kTestKeyGcm256_2[] =
37 "rqponmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA";
38 static const int kTestKeyGcm256Len = 44; // 256 bits key + 96 bits salt.
24 static const std::string kTestKeyParams1 = 39 static const std::string kTestKeyParams1 =
25 "inline:WVNfX19zZW1jdGwgKCkgewkyMjA7fQp9CnVubGVz"; 40 "inline:WVNfX19zZW1jdGwgKCkgewkyMjA7fQp9CnVubGVz";
26 static const std::string kTestKeyParams2 = 41 static const std::string kTestKeyParams2 =
27 "inline:PS1uQCVeeCFCanVmcjkpPywjNWhcYD0mXXtxaVBR"; 42 "inline:PS1uQCVeeCFCanVmcjkpPywjNWhcYD0mXXtxaVBR";
28 static const std::string kTestKeyParams3 = 43 static const std::string kTestKeyParams3 =
29 "inline:1234X19zZW1jdGwgKCkgewkyMjA7fQp9CnVubGVz"; 44 "inline:1234X19zZW1jdGwgKCkgewkyMjA7fQp9CnVubGVz";
30 static const std::string kTestKeyParams4 = 45 static const std::string kTestKeyParams4 =
31 "inline:4567QCVeeCFCanVmcjkpPywjNWhcYD0mXXtxaVBR"; 46 "inline:4567QCVeeCFCanVmcjkpPywjNWhcYD0mXXtxaVBR";
32 static const std::string kTestKeyParamsGcm1 = 47 static const std::string kTestKeyParamsGcm1 =
33 "inline:e166KFlKzJsGW0d5apX+rrI05vxbrvMJEzFI14aTDCa63IRTlLK4iH66uOI="; 48 "inline:e166KFlKzJsGW0d5apX+rrI05vxbrvMJEzFI14aTDCa63IRTlLK4iH66uOI=";
(...skipping 11 matching lines...) Expand all
45 1, "AEAD_AES_256_GCM", kTestKeyParamsGcm1, ""); 60 1, "AEAD_AES_256_GCM", kTestKeyParamsGcm1, "");
46 static const cricket::CryptoParams kTestCryptoParamsGcm2( 61 static const cricket::CryptoParams kTestCryptoParamsGcm2(
47 1, "AEAD_AES_256_GCM", kTestKeyParamsGcm2, ""); 62 1, "AEAD_AES_256_GCM", kTestKeyParamsGcm2, "");
48 static const cricket::CryptoParams kTestCryptoParamsGcm3( 63 static const cricket::CryptoParams kTestCryptoParamsGcm3(
49 1, "AEAD_AES_128_GCM", kTestKeyParamsGcm3, ""); 64 1, "AEAD_AES_128_GCM", kTestKeyParamsGcm3, "");
50 static const cricket::CryptoParams kTestCryptoParamsGcm4( 65 static const cricket::CryptoParams kTestCryptoParamsGcm4(
51 1, "AEAD_AES_128_GCM", kTestKeyParamsGcm4, ""); 66 1, "AEAD_AES_128_GCM", kTestKeyParamsGcm4, "");
52 67
53 class SrtpFilterTest : public testing::Test { 68 class SrtpFilterTest : public testing::Test {
54 protected: 69 protected:
55 SrtpFilterTest() {} 70 SrtpFilterTest()
71 // Need to initialize |sequence_number_|, the value does not matter.
72 : sequence_number_(1) {
73 }
56 static std::vector<CryptoParams> MakeVector(const CryptoParams& params) { 74 static std::vector<CryptoParams> MakeVector(const CryptoParams& params) {
57 std::vector<CryptoParams> vec; 75 std::vector<CryptoParams> vec;
58 vec.push_back(params); 76 vec.push_back(params);
59 return vec; 77 return vec;
60 } 78 }
61
62 void TestSetParams(const std::vector<CryptoParams>& params1, 79 void TestSetParams(const std::vector<CryptoParams>& params1,
63 const std::vector<CryptoParams>& params2) { 80 const std::vector<CryptoParams>& params2) {
64 EXPECT_TRUE(f1_.SetOffer(params1, CS_LOCAL)); 81 EXPECT_TRUE(f1_.SetOffer(params1, CS_LOCAL));
65 EXPECT_TRUE(f2_.SetOffer(params1, CS_REMOTE)); 82 EXPECT_TRUE(f2_.SetOffer(params1, CS_REMOTE));
66 EXPECT_FALSE(f1_.IsActive()); 83 EXPECT_FALSE(f1_.IsActive());
67 EXPECT_FALSE(f2_.IsActive()); 84 EXPECT_FALSE(f2_.IsActive());
68 EXPECT_TRUE(f2_.SetAnswer(params2, CS_LOCAL)); 85 EXPECT_TRUE(f2_.SetAnswer(params2, CS_LOCAL));
69 EXPECT_TRUE(f1_.SetAnswer(params2, CS_REMOTE)); 86 EXPECT_TRUE(f1_.SetAnswer(params2, CS_REMOTE));
70 EXPECT_TRUE(f1_.IsActive()); 87 EXPECT_TRUE(f1_.IsActive());
71 EXPECT_TRUE(f2_.IsActive()); 88 EXPECT_TRUE(f2_.IsActive());
72 } 89 }
90 void TestRtpAuthParams(cricket::SrtpFilter* filter, const std::string& cs) {
91 int overhead;
92 EXPECT_TRUE(filter->GetSrtpOverhead(&overhead));
93 switch (SrtpCryptoSuiteFromName(cs)) {
94 case SRTP_AES128_CM_SHA1_32:
95 EXPECT_EQ(32/8, overhead); // 32-bit tag.
96 break;
97 case SRTP_AES128_CM_SHA1_80:
98 EXPECT_EQ(80/8, overhead); // 80-bit tag.
99 break;
100 default:
101 RTC_NOTREACHED();
102 break;
103 }
73 104
74 void VerifyCryptoParamsMatch(const std::string& cs1, const std::string& cs2) { 105 uint8_t* auth_key = nullptr;
75 EXPECT_EQ(rtc::SrtpCryptoSuiteFromName(cs1), f1_.send_cipher_suite()); 106 int key_len = 0;
76 EXPECT_EQ(rtc::SrtpCryptoSuiteFromName(cs2), f2_.send_cipher_suite()); 107 int tag_len = 0;
77 EXPECT_TRUE(f1_.send_key() == f2_.recv_key()); 108 EXPECT_TRUE(filter->GetRtpAuthParams(&auth_key, &key_len, &tag_len));
78 EXPECT_TRUE(f2_.send_key() == f1_.recv_key()); 109 EXPECT_NE(nullptr, auth_key);
110 EXPECT_EQ(160/8, key_len); // Length of SHA-1 is 160 bits.
111 EXPECT_EQ(overhead, tag_len);
79 } 112 }
113 void TestProtectUnprotect(const std::string& cs1, const std::string& cs2) {
114 Buffer rtp_buffer(sizeof(kPcmuFrame) + rtp_auth_tag_len(cs1));
115 char* rtp_packet = rtp_buffer.data<char>();
116 char original_rtp_packet[sizeof(kPcmuFrame)];
117 Buffer rtcp_buffer(sizeof(kRtcpReport) + 4 + rtcp_auth_tag_len(cs2));
118 char* rtcp_packet = rtcp_buffer.data<char>();
119 int rtp_len = sizeof(kPcmuFrame), rtcp_len = sizeof(kRtcpReport), out_len;
120 memcpy(rtp_packet, kPcmuFrame, rtp_len);
121 // In order to be able to run this test function multiple times we can not
122 // use the same sequence number twice. Increase the sequence number by one.
123 SetBE16(reinterpret_cast<uint8_t*>(rtp_packet) + 2, ++sequence_number_);
124 memcpy(original_rtp_packet, rtp_packet, rtp_len);
125 memcpy(rtcp_packet, kRtcpReport, rtcp_len);
80 126
127 EXPECT_TRUE(f1_.ProtectRtp(rtp_packet, rtp_len,
128 static_cast<int>(rtp_buffer.size()),
129 &out_len));
130 EXPECT_EQ(out_len, rtp_len + rtp_auth_tag_len(cs1));
131 EXPECT_NE(0, memcmp(rtp_packet, original_rtp_packet, rtp_len));
132 if (!f1_.IsExternalAuthActive()) {
133 EXPECT_TRUE(f2_.UnprotectRtp(rtp_packet, out_len, &out_len));
134 EXPECT_EQ(rtp_len, out_len);
135 EXPECT_EQ(0, memcmp(rtp_packet, original_rtp_packet, rtp_len));
136 } else {
137 // With external auth enabled, SRTP doesn't write the auth tag and
138 // unprotect would fail. Check accessing the information about the
139 // tag instead, similar to what the actual code would do that relies
140 // on external auth.
141 TestRtpAuthParams(&f1_, cs1);
142 }
143
144 EXPECT_TRUE(f2_.ProtectRtp(rtp_packet, rtp_len,
145 static_cast<int>(rtp_buffer.size()),
146 &out_len));
147 EXPECT_EQ(out_len, rtp_len + rtp_auth_tag_len(cs2));
148 EXPECT_NE(0, memcmp(rtp_packet, original_rtp_packet, rtp_len));
149 if (!f2_.IsExternalAuthActive()) {
150 EXPECT_TRUE(f1_.UnprotectRtp(rtp_packet, out_len, &out_len));
151 EXPECT_EQ(rtp_len, out_len);
152 EXPECT_EQ(0, memcmp(rtp_packet, original_rtp_packet, rtp_len));
153 } else {
154 TestRtpAuthParams(&f2_, cs2);
155 }
156
157 EXPECT_TRUE(f1_.ProtectRtcp(rtcp_packet, rtcp_len,
158 static_cast<int>(rtcp_buffer.size()),
159 &out_len));
160 EXPECT_EQ(out_len, rtcp_len + 4 + rtcp_auth_tag_len(cs1)); // NOLINT
161 EXPECT_NE(0, memcmp(rtcp_packet, kRtcpReport, rtcp_len));
162 EXPECT_TRUE(f2_.UnprotectRtcp(rtcp_packet, out_len, &out_len));
163 EXPECT_EQ(rtcp_len, out_len);
164 EXPECT_EQ(0, memcmp(rtcp_packet, kRtcpReport, rtcp_len));
165
166 EXPECT_TRUE(f2_.ProtectRtcp(rtcp_packet, rtcp_len,
167 static_cast<int>(rtcp_buffer.size()),
168 &out_len));
169 EXPECT_EQ(out_len, rtcp_len + 4 + rtcp_auth_tag_len(cs2)); // NOLINT
170 EXPECT_NE(0, memcmp(rtcp_packet, kRtcpReport, rtcp_len));
171 EXPECT_TRUE(f1_.UnprotectRtcp(rtcp_packet, out_len, &out_len));
172 EXPECT_EQ(rtcp_len, out_len);
173 EXPECT_EQ(0, memcmp(rtcp_packet, kRtcpReport, rtcp_len));
174 }
175 void TestProtectUnprotectHeaderEncryption(const std::string& cs1,
176 const std::string& cs2,
177 const std::vector<int>& encrypted_header_ids) {
178 Buffer rtp_buffer(sizeof(kPcmuFrameWithExtensions) + rtp_auth_tag_len(cs1));
179 char* rtp_packet = rtp_buffer.data<char>();
180 size_t rtp_packet_size = rtp_buffer.size();
181 char original_rtp_packet[sizeof(kPcmuFrameWithExtensions)];
182 size_t original_rtp_packet_size = sizeof(original_rtp_packet);
183 int rtp_len = sizeof(kPcmuFrameWithExtensions), out_len;
184 memcpy(rtp_packet, kPcmuFrameWithExtensions, rtp_len);
185 // In order to be able to run this test function multiple times we can not
186 // use the same sequence number twice. Increase the sequence number by one.
187 SetBE16(reinterpret_cast<uint8_t*>(rtp_packet) + 2, ++sequence_number_);
188 memcpy(original_rtp_packet, rtp_packet, rtp_len);
189
190 EXPECT_TRUE(f1_.ProtectRtp(rtp_packet, rtp_len,
191 static_cast<int>(rtp_buffer.size()),
192 &out_len));
193 EXPECT_EQ(out_len, rtp_len + rtp_auth_tag_len(cs1));
194 EXPECT_NE(0, memcmp(rtp_packet, original_rtp_packet, rtp_len));
195 CompareHeaderExtensions(rtp_packet, rtp_packet_size,
196 original_rtp_packet, original_rtp_packet_size,
197 encrypted_header_ids, false);
198 EXPECT_TRUE(f2_.UnprotectRtp(rtp_packet, out_len, &out_len));
199 EXPECT_EQ(rtp_len, out_len);
200 EXPECT_EQ(0, memcmp(rtp_packet, original_rtp_packet, rtp_len));
201 CompareHeaderExtensions(rtp_packet, rtp_packet_size,
202 original_rtp_packet, original_rtp_packet_size,
203 encrypted_header_ids, true);
204
205 EXPECT_TRUE(f2_.ProtectRtp(rtp_packet, rtp_len,
206 static_cast<int>(rtp_buffer.size()),
207 &out_len));
208 EXPECT_EQ(out_len, rtp_len + rtp_auth_tag_len(cs2));
209 EXPECT_NE(0, memcmp(rtp_packet, original_rtp_packet, rtp_len));
210 CompareHeaderExtensions(rtp_packet, rtp_packet_size,
211 original_rtp_packet, original_rtp_packet_size,
212 encrypted_header_ids, false);
213 EXPECT_TRUE(f1_.UnprotectRtp(rtp_packet, out_len, &out_len));
214 EXPECT_EQ(rtp_len, out_len);
215 EXPECT_EQ(0, memcmp(rtp_packet, original_rtp_packet, rtp_len));
216 CompareHeaderExtensions(rtp_packet, rtp_packet_size,
217 original_rtp_packet, original_rtp_packet_size,
218 encrypted_header_ids, true);
219 }
220 void TestProtectSetParamsDirect(bool enable_external_auth, int cs,
221 const uint8_t* key1, int key1_len, const uint8_t* key2, int key2_len,
222 const std::string& cs_name) {
223 EXPECT_EQ(key1_len, key2_len);
224 EXPECT_EQ(cs_name, SrtpCryptoSuiteToName(cs));
225 if (enable_external_auth) {
226 f1_.EnableExternalAuth();
227 f2_.EnableExternalAuth();
228 }
229 EXPECT_TRUE(f1_.SetRtpParams(cs, key1, key1_len, cs, key2, key2_len));
230 EXPECT_TRUE(f2_.SetRtpParams(cs, key2, key2_len, cs, key1, key1_len));
231 EXPECT_TRUE(f1_.SetRtcpParams(cs, key1, key1_len, cs, key2, key2_len));
232 EXPECT_TRUE(f2_.SetRtcpParams(cs, key2, key2_len, cs, key1, key1_len));
233 EXPECT_TRUE(f1_.IsActive());
234 EXPECT_TRUE(f2_.IsActive());
235 if (IsGcmCryptoSuite(cs)) {
236 EXPECT_FALSE(f1_.IsExternalAuthActive());
237 EXPECT_FALSE(f2_.IsExternalAuthActive());
238 } else if (enable_external_auth) {
239 EXPECT_TRUE(f1_.IsExternalAuthActive());
240 EXPECT_TRUE(f2_.IsExternalAuthActive());
241 }
242 TestProtectUnprotect(cs_name, cs_name);
243 }
244 void TestProtectSetParamsDirectHeaderEncryption(int cs,
245 const uint8_t* key1, int key1_len, const uint8_t* key2, int key2_len,
246 const std::string& cs_name) {
247 std::vector<int> encrypted_headers;
248 encrypted_headers.push_back(1);
249 // Don't encrypt header ids 2 and 3.
250 encrypted_headers.push_back(4);
251 EXPECT_EQ(key1_len, key2_len);
252 EXPECT_EQ(cs_name, SrtpCryptoSuiteToName(cs));
253 f1_.SetEncryptedHeaderExtensionIds(CS_LOCAL, encrypted_headers);
254 f1_.SetEncryptedHeaderExtensionIds(CS_REMOTE, encrypted_headers);
255 f2_.SetEncryptedHeaderExtensionIds(CS_LOCAL, encrypted_headers);
256 f2_.SetEncryptedHeaderExtensionIds(CS_REMOTE, encrypted_headers);
257 EXPECT_TRUE(f1_.SetRtpParams(cs, key1, key1_len, cs, key2, key2_len));
258 EXPECT_TRUE(f2_.SetRtpParams(cs, key2, key2_len, cs, key1, key1_len));
259 EXPECT_TRUE(f1_.IsActive());
260 EXPECT_TRUE(f2_.IsActive());
261 EXPECT_FALSE(f1_.IsExternalAuthActive());
262 EXPECT_FALSE(f2_.IsExternalAuthActive());
263 TestProtectUnprotectHeaderEncryption(cs_name, cs_name, encrypted_headers);
264 }
81 cricket::SrtpFilter f1_; 265 cricket::SrtpFilter f1_;
82 cricket::SrtpFilter f2_; 266 cricket::SrtpFilter f2_;
267 int sequence_number_;
83 }; 268 };
84 269
85 // Test that we can set up the session and keys properly. 270 // Test that we can set up the session and keys properly.
86 TEST_F(SrtpFilterTest, TestGoodSetupOneCipherSuite) { 271 TEST_F(SrtpFilterTest, TestGoodSetupOneCipherSuite) {
87 EXPECT_TRUE(f1_.SetOffer(MakeVector(kTestCryptoParams1), CS_LOCAL)); 272 EXPECT_TRUE(f1_.SetOffer(MakeVector(kTestCryptoParams1), CS_LOCAL));
88 EXPECT_FALSE(f1_.IsActive()); 273 EXPECT_FALSE(f1_.IsActive());
89 EXPECT_TRUE(f1_.SetAnswer(MakeVector(kTestCryptoParams2), CS_REMOTE)); 274 EXPECT_TRUE(f1_.SetAnswer(MakeVector(kTestCryptoParams2), CS_REMOTE));
90 EXPECT_TRUE(f1_.IsActive()); 275 EXPECT_TRUE(f1_.IsActive());
91 } 276 }
92 277
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after
286 TEST_F(SrtpFilterTest, TestUnsupportedOptions) { 471 TEST_F(SrtpFilterTest, TestUnsupportedOptions) {
287 std::vector<CryptoParams> offer(MakeVector(kTestCryptoParams1)); 472 std::vector<CryptoParams> offer(MakeVector(kTestCryptoParams1));
288 std::vector<CryptoParams> answer(MakeVector(kTestCryptoParams2)); 473 std::vector<CryptoParams> answer(MakeVector(kTestCryptoParams2));
289 answer[0].key_params = 474 answer[0].key_params =
290 "inline:PS1uQCVeeCFCanVmcjkpPywjNWhcYD0mXXtxaVBR|2^20|1:4"; 475 "inline:PS1uQCVeeCFCanVmcjkpPywjNWhcYD0mXXtxaVBR|2^20|1:4";
291 EXPECT_TRUE(f1_.SetOffer(offer, CS_LOCAL)); 476 EXPECT_TRUE(f1_.SetOffer(offer, CS_LOCAL));
292 EXPECT_FALSE(f1_.SetAnswer(answer, CS_REMOTE)); 477 EXPECT_FALSE(f1_.SetAnswer(answer, CS_REMOTE));
293 EXPECT_FALSE(f1_.IsActive()); 478 EXPECT_FALSE(f1_.IsActive());
294 } 479 }
295 480
481 // Test that we can encrypt/decrypt after setting the same CryptoParams again on
482 // one side.
483 TEST_F(SrtpFilterTest, TestSettingSameKeyOnOneSide) {
484 std::vector<CryptoParams> offer(MakeVector(kTestCryptoParams1));
485 std::vector<CryptoParams> answer(MakeVector(kTestCryptoParams2));
486 TestSetParams(offer, answer);
487
488 TestProtectUnprotect(CS_AES_CM_128_HMAC_SHA1_80,
489 CS_AES_CM_128_HMAC_SHA1_80);
490
491 // Re-applying the same keys on one end and it should not reset the ROC.
492 EXPECT_TRUE(f2_.SetOffer(offer, CS_REMOTE));
493 EXPECT_TRUE(f2_.SetAnswer(answer, CS_LOCAL));
494 TestProtectUnprotect(CS_AES_CM_128_HMAC_SHA1_80, CS_AES_CM_128_HMAC_SHA1_80);
495 }
496
296 // Test that we can encrypt/decrypt after negotiating AES_CM_128_HMAC_SHA1_80. 497 // Test that we can encrypt/decrypt after negotiating AES_CM_128_HMAC_SHA1_80.
297 TEST_F(SrtpFilterTest, TestProtect_AES_CM_128_HMAC_SHA1_80) { 498 TEST_F(SrtpFilterTest, TestProtect_AES_CM_128_HMAC_SHA1_80) {
298 std::vector<CryptoParams> offer(MakeVector(kTestCryptoParams1)); 499 std::vector<CryptoParams> offer(MakeVector(kTestCryptoParams1));
299 std::vector<CryptoParams> answer(MakeVector(kTestCryptoParams2)); 500 std::vector<CryptoParams> answer(MakeVector(kTestCryptoParams2));
300 offer.push_back(kTestCryptoParams1); 501 offer.push_back(kTestCryptoParams1);
301 offer[1].tag = 2; 502 offer[1].tag = 2;
302 offer[1].cipher_suite = CS_AES_CM_128_HMAC_SHA1_32; 503 offer[1].cipher_suite = CS_AES_CM_128_HMAC_SHA1_32;
303 TestSetParams(offer, answer); 504 TestSetParams(offer, answer);
304 VerifyCryptoParamsMatch(CS_AES_CM_128_HMAC_SHA1_80, 505 TestProtectUnprotect(CS_AES_CM_128_HMAC_SHA1_80, CS_AES_CM_128_HMAC_SHA1_80);
305 CS_AES_CM_128_HMAC_SHA1_80);
306 } 506 }
307 507
308 // Test that we can encrypt/decrypt after negotiating AES_CM_128_HMAC_SHA1_32. 508 // Test that we can encrypt/decrypt after negotiating AES_CM_128_HMAC_SHA1_32.
309 TEST_F(SrtpFilterTest, TestProtect_AES_CM_128_HMAC_SHA1_32) { 509 TEST_F(SrtpFilterTest, TestProtect_AES_CM_128_HMAC_SHA1_32) {
310 std::vector<CryptoParams> offer(MakeVector(kTestCryptoParams1)); 510 std::vector<CryptoParams> offer(MakeVector(kTestCryptoParams1));
311 std::vector<CryptoParams> answer(MakeVector(kTestCryptoParams2)); 511 std::vector<CryptoParams> answer(MakeVector(kTestCryptoParams2));
312 offer.push_back(kTestCryptoParams1); 512 offer.push_back(kTestCryptoParams1);
313 offer[1].tag = 2; 513 offer[1].tag = 2;
314 offer[1].cipher_suite = CS_AES_CM_128_HMAC_SHA1_32; 514 offer[1].cipher_suite = CS_AES_CM_128_HMAC_SHA1_32;
315 answer[0].tag = 2; 515 answer[0].tag = 2;
316 answer[0].cipher_suite = CS_AES_CM_128_HMAC_SHA1_32; 516 answer[0].cipher_suite = CS_AES_CM_128_HMAC_SHA1_32;
317 TestSetParams(offer, answer); 517 TestSetParams(offer, answer);
318 VerifyCryptoParamsMatch(CS_AES_CM_128_HMAC_SHA1_32, 518 TestProtectUnprotect(CS_AES_CM_128_HMAC_SHA1_32, CS_AES_CM_128_HMAC_SHA1_32);
319 CS_AES_CM_128_HMAC_SHA1_32);
320 } 519 }
321 520
322 // Test that we can change encryption parameters. 521 // Test that we can change encryption parameters.
323 TEST_F(SrtpFilterTest, TestChangeParameters) { 522 TEST_F(SrtpFilterTest, TestChangeParameters) {
324 std::vector<CryptoParams> offer(MakeVector(kTestCryptoParams1)); 523 std::vector<CryptoParams> offer(MakeVector(kTestCryptoParams1));
325 std::vector<CryptoParams> answer(MakeVector(kTestCryptoParams2)); 524 std::vector<CryptoParams> answer(MakeVector(kTestCryptoParams2));
326 525
327 TestSetParams(offer, answer); 526 TestSetParams(offer, answer);
328 VerifyCryptoParamsMatch(CS_AES_CM_128_HMAC_SHA1_80, 527 TestProtectUnprotect(CS_AES_CM_128_HMAC_SHA1_80, CS_AES_CM_128_HMAC_SHA1_80);
329 CS_AES_CM_128_HMAC_SHA1_80);
330 528
331 // Change the key parameters and cipher_suite. 529 // Change the key parameters and cipher_suite.
332 offer[0].key_params = kTestKeyParams3; 530 offer[0].key_params = kTestKeyParams3;
333 offer[0].cipher_suite = CS_AES_CM_128_HMAC_SHA1_32; 531 offer[0].cipher_suite = CS_AES_CM_128_HMAC_SHA1_32;
334 answer[0].key_params = kTestKeyParams4; 532 answer[0].key_params = kTestKeyParams4;
335 answer[0].cipher_suite = CS_AES_CM_128_HMAC_SHA1_32; 533 answer[0].cipher_suite = CS_AES_CM_128_HMAC_SHA1_32;
336 534
337 EXPECT_TRUE(f1_.SetOffer(offer, CS_LOCAL)); 535 EXPECT_TRUE(f1_.SetOffer(offer, CS_LOCAL));
338 EXPECT_TRUE(f2_.SetOffer(offer, CS_REMOTE)); 536 EXPECT_TRUE(f2_.SetOffer(offer, CS_REMOTE));
339 EXPECT_TRUE(f1_.IsActive()); 537 EXPECT_TRUE(f1_.IsActive());
340 EXPECT_TRUE(f1_.IsActive()); 538 EXPECT_TRUE(f1_.IsActive());
341 539
342 // Test that the old keys are valid until the negotiation is complete. 540 // Test that the old keys are valid until the negotiation is complete.
343 VerifyCryptoParamsMatch(CS_AES_CM_128_HMAC_SHA1_80, 541 TestProtectUnprotect(CS_AES_CM_128_HMAC_SHA1_80, CS_AES_CM_128_HMAC_SHA1_80);
344 CS_AES_CM_128_HMAC_SHA1_80);
345 542
346 // Complete the negotiation and test that we can still understand each other. 543 // Complete the negotiation and test that we can still understand each other.
347 EXPECT_TRUE(f2_.SetAnswer(answer, CS_LOCAL)); 544 EXPECT_TRUE(f2_.SetAnswer(answer, CS_LOCAL));
348 EXPECT_TRUE(f1_.SetAnswer(answer, CS_REMOTE)); 545 EXPECT_TRUE(f1_.SetAnswer(answer, CS_REMOTE));
349 546
350 VerifyCryptoParamsMatch(CS_AES_CM_128_HMAC_SHA1_32, 547 TestProtectUnprotect(CS_AES_CM_128_HMAC_SHA1_32, CS_AES_CM_128_HMAC_SHA1_32);
351 CS_AES_CM_128_HMAC_SHA1_32);
352 } 548 }
353 549
354 // Test that we can send and receive provisional answers with crypto enabled. 550 // Test that we can send and receive provisional answers with crypto enabled.
355 // Also test that we can change the crypto. 551 // Also test that we can change the crypto.
356 TEST_F(SrtpFilterTest, TestProvisionalAnswer) { 552 TEST_F(SrtpFilterTest, TestProvisionalAnswer) {
357 std::vector<CryptoParams> offer(MakeVector(kTestCryptoParams1)); 553 std::vector<CryptoParams> offer(MakeVector(kTestCryptoParams1));
358 offer.push_back(kTestCryptoParams1); 554 offer.push_back(kTestCryptoParams1);
359 offer[1].tag = 2; 555 offer[1].tag = 2;
360 offer[1].cipher_suite = CS_AES_CM_128_HMAC_SHA1_32; 556 offer[1].cipher_suite = CS_AES_CM_128_HMAC_SHA1_32;
361 std::vector<CryptoParams> answer(MakeVector(kTestCryptoParams2)); 557 std::vector<CryptoParams> answer(MakeVector(kTestCryptoParams2));
362 558
363 EXPECT_TRUE(f1_.SetOffer(offer, CS_LOCAL)); 559 EXPECT_TRUE(f1_.SetOffer(offer, CS_LOCAL));
364 EXPECT_TRUE(f2_.SetOffer(offer, CS_REMOTE)); 560 EXPECT_TRUE(f2_.SetOffer(offer, CS_REMOTE));
365 EXPECT_FALSE(f1_.IsActive()); 561 EXPECT_FALSE(f1_.IsActive());
366 EXPECT_FALSE(f2_.IsActive()); 562 EXPECT_FALSE(f2_.IsActive());
367 EXPECT_TRUE(f2_.SetProvisionalAnswer(answer, CS_LOCAL)); 563 EXPECT_TRUE(f2_.SetProvisionalAnswer(answer, CS_LOCAL));
368 EXPECT_TRUE(f1_.SetProvisionalAnswer(answer, CS_REMOTE)); 564 EXPECT_TRUE(f1_.SetProvisionalAnswer(answer, CS_REMOTE));
369 EXPECT_TRUE(f1_.IsActive()); 565 EXPECT_TRUE(f1_.IsActive());
370 EXPECT_TRUE(f2_.IsActive()); 566 EXPECT_TRUE(f2_.IsActive());
371 VerifyCryptoParamsMatch(CS_AES_CM_128_HMAC_SHA1_80, 567 TestProtectUnprotect(CS_AES_CM_128_HMAC_SHA1_80, CS_AES_CM_128_HMAC_SHA1_80);
372 CS_AES_CM_128_HMAC_SHA1_80);
373 568
374 answer[0].key_params = kTestKeyParams4; 569 answer[0].key_params = kTestKeyParams4;
375 answer[0].tag = 2; 570 answer[0].tag = 2;
376 answer[0].cipher_suite = CS_AES_CM_128_HMAC_SHA1_32; 571 answer[0].cipher_suite = CS_AES_CM_128_HMAC_SHA1_32;
377 EXPECT_TRUE(f2_.SetAnswer(answer, CS_LOCAL)); 572 EXPECT_TRUE(f2_.SetAnswer(answer, CS_LOCAL));
378 EXPECT_TRUE(f1_.SetAnswer(answer, CS_REMOTE)); 573 EXPECT_TRUE(f1_.SetAnswer(answer, CS_REMOTE));
379 EXPECT_TRUE(f1_.IsActive()); 574 EXPECT_TRUE(f1_.IsActive());
380 EXPECT_TRUE(f2_.IsActive()); 575 EXPECT_TRUE(f2_.IsActive());
381 VerifyCryptoParamsMatch(CS_AES_CM_128_HMAC_SHA1_32, 576 TestProtectUnprotect(CS_AES_CM_128_HMAC_SHA1_32, CS_AES_CM_128_HMAC_SHA1_32);
382 CS_AES_CM_128_HMAC_SHA1_32);
383 } 577 }
384 578
385 // Test that a provisional answer doesn't need to contain a crypto. 579 // Test that a provisional answer doesn't need to contain a crypto.
386 TEST_F(SrtpFilterTest, TestProvisionalAnswerWithoutCrypto) { 580 TEST_F(SrtpFilterTest, TestProvisionalAnswerWithoutCrypto) {
387 std::vector<CryptoParams> offer(MakeVector(kTestCryptoParams1)); 581 std::vector<CryptoParams> offer(MakeVector(kTestCryptoParams1));
388 std::vector<CryptoParams> answer; 582 std::vector<CryptoParams> answer;
389 583
390 EXPECT_TRUE(f1_.SetOffer(offer, CS_LOCAL)); 584 EXPECT_TRUE(f1_.SetOffer(offer, CS_LOCAL));
391 EXPECT_TRUE(f2_.SetOffer(offer, CS_REMOTE)); 585 EXPECT_TRUE(f2_.SetOffer(offer, CS_REMOTE));
392 EXPECT_FALSE(f1_.IsActive()); 586 EXPECT_FALSE(f1_.IsActive());
393 EXPECT_FALSE(f2_.IsActive()); 587 EXPECT_FALSE(f2_.IsActive());
394 EXPECT_TRUE(f2_.SetProvisionalAnswer(answer, CS_LOCAL)); 588 EXPECT_TRUE(f2_.SetProvisionalAnswer(answer, CS_LOCAL));
395 EXPECT_TRUE(f1_.SetProvisionalAnswer(answer, CS_REMOTE)); 589 EXPECT_TRUE(f1_.SetProvisionalAnswer(answer, CS_REMOTE));
396 EXPECT_FALSE(f1_.IsActive()); 590 EXPECT_FALSE(f1_.IsActive());
397 EXPECT_FALSE(f2_.IsActive()); 591 EXPECT_FALSE(f2_.IsActive());
398 592
399 answer.push_back(kTestCryptoParams2); 593 answer.push_back(kTestCryptoParams2);
400 EXPECT_TRUE(f2_.SetAnswer(answer, CS_LOCAL)); 594 EXPECT_TRUE(f2_.SetAnswer(answer, CS_LOCAL));
401 EXPECT_TRUE(f1_.SetAnswer(answer, CS_REMOTE)); 595 EXPECT_TRUE(f1_.SetAnswer(answer, CS_REMOTE));
402 EXPECT_TRUE(f1_.IsActive()); 596 EXPECT_TRUE(f1_.IsActive());
403 EXPECT_TRUE(f2_.IsActive()); 597 EXPECT_TRUE(f2_.IsActive());
404 VerifyCryptoParamsMatch(CS_AES_CM_128_HMAC_SHA1_80, 598 TestProtectUnprotect(CS_AES_CM_128_HMAC_SHA1_80, CS_AES_CM_128_HMAC_SHA1_80);
405 CS_AES_CM_128_HMAC_SHA1_80);
406 } 599 }
407 600
408 // Test that if we get a new local offer after a provisional answer 601 // Test that if we get a new local offer after a provisional answer
409 // with no crypto, that we are in an inactive state. 602 // with no crypto, that we are in an inactive state.
410 TEST_F(SrtpFilterTest, TestLocalOfferAfterProvisionalAnswerWithoutCrypto) { 603 TEST_F(SrtpFilterTest, TestLocalOfferAfterProvisionalAnswerWithoutCrypto) {
411 std::vector<CryptoParams> offer(MakeVector(kTestCryptoParams1)); 604 std::vector<CryptoParams> offer(MakeVector(kTestCryptoParams1));
412 std::vector<CryptoParams> answer; 605 std::vector<CryptoParams> answer;
413 606
414 EXPECT_TRUE(f1_.SetOffer(offer, CS_LOCAL)); 607 EXPECT_TRUE(f1_.SetOffer(offer, CS_LOCAL));
415 EXPECT_TRUE(f2_.SetOffer(offer, CS_REMOTE)); 608 EXPECT_TRUE(f2_.SetOffer(offer, CS_REMOTE));
416 EXPECT_TRUE(f1_.SetProvisionalAnswer(answer, CS_REMOTE)); 609 EXPECT_TRUE(f1_.SetProvisionalAnswer(answer, CS_REMOTE));
417 EXPECT_TRUE(f2_.SetProvisionalAnswer(answer, CS_LOCAL)); 610 EXPECT_TRUE(f2_.SetProvisionalAnswer(answer, CS_LOCAL));
418 EXPECT_FALSE(f1_.IsActive()); 611 EXPECT_FALSE(f1_.IsActive());
419 EXPECT_FALSE(f2_.IsActive()); 612 EXPECT_FALSE(f2_.IsActive());
420 // The calls to set an offer after a provisional answer fail, so the 613 // The calls to set an offer after a provisional answer fail, so the
421 // state doesn't change. 614 // state doesn't change.
422 EXPECT_FALSE(f1_.SetOffer(offer, CS_LOCAL)); 615 EXPECT_FALSE(f1_.SetOffer(offer, CS_LOCAL));
423 EXPECT_FALSE(f2_.SetOffer(offer, CS_REMOTE)); 616 EXPECT_FALSE(f2_.SetOffer(offer, CS_REMOTE));
424 EXPECT_FALSE(f1_.IsActive()); 617 EXPECT_FALSE(f1_.IsActive());
425 EXPECT_FALSE(f2_.IsActive()); 618 EXPECT_FALSE(f2_.IsActive());
426 619
427 answer.push_back(kTestCryptoParams2); 620 answer.push_back(kTestCryptoParams2);
428 EXPECT_TRUE(f2_.SetAnswer(answer, CS_LOCAL)); 621 EXPECT_TRUE(f2_.SetAnswer(answer, CS_LOCAL));
429 EXPECT_TRUE(f1_.SetAnswer(answer, CS_REMOTE)); 622 EXPECT_TRUE(f1_.SetAnswer(answer, CS_REMOTE));
430 EXPECT_TRUE(f1_.IsActive()); 623 EXPECT_TRUE(f1_.IsActive());
431 EXPECT_TRUE(f2_.IsActive()); 624 EXPECT_TRUE(f2_.IsActive());
432 VerifyCryptoParamsMatch(CS_AES_CM_128_HMAC_SHA1_80, 625 TestProtectUnprotect(CS_AES_CM_128_HMAC_SHA1_80, CS_AES_CM_128_HMAC_SHA1_80);
433 CS_AES_CM_128_HMAC_SHA1_80);
434 } 626 }
435 627
436 // Test that we can disable encryption. 628 // Test that we can disable encryption.
437 TEST_F(SrtpFilterTest, TestDisableEncryption) { 629 TEST_F(SrtpFilterTest, TestDisableEncryption) {
438 std::vector<CryptoParams> offer(MakeVector(kTestCryptoParams1)); 630 std::vector<CryptoParams> offer(MakeVector(kTestCryptoParams1));
439 std::vector<CryptoParams> answer(MakeVector(kTestCryptoParams2)); 631 std::vector<CryptoParams> answer(MakeVector(kTestCryptoParams2));
440 632
441 TestSetParams(offer, answer); 633 TestSetParams(offer, answer);
442 VerifyCryptoParamsMatch(CS_AES_CM_128_HMAC_SHA1_80, 634 TestProtectUnprotect(CS_AES_CM_128_HMAC_SHA1_80, CS_AES_CM_128_HMAC_SHA1_80);
443 CS_AES_CM_128_HMAC_SHA1_80);
444 635
445 offer.clear(); 636 offer.clear();
446 answer.clear(); 637 answer.clear();
447 EXPECT_TRUE(f1_.SetOffer(offer, CS_LOCAL)); 638 EXPECT_TRUE(f1_.SetOffer(offer, CS_LOCAL));
448 EXPECT_TRUE(f2_.SetOffer(offer, CS_REMOTE)); 639 EXPECT_TRUE(f2_.SetOffer(offer, CS_REMOTE));
449 EXPECT_TRUE(f1_.IsActive()); 640 EXPECT_TRUE(f1_.IsActive());
450 EXPECT_TRUE(f2_.IsActive()); 641 EXPECT_TRUE(f2_.IsActive());
451 642
452 // Test that the old keys are valid until the negotiation is complete. 643 // Test that the old keys are valid until the negotiation is complete.
453 VerifyCryptoParamsMatch(CS_AES_CM_128_HMAC_SHA1_80, 644 TestProtectUnprotect(CS_AES_CM_128_HMAC_SHA1_80, CS_AES_CM_128_HMAC_SHA1_80);
454 CS_AES_CM_128_HMAC_SHA1_80);
455 645
456 // Complete the negotiation. 646 // Complete the negotiation.
457 EXPECT_TRUE(f2_.SetAnswer(answer, CS_LOCAL)); 647 EXPECT_TRUE(f2_.SetAnswer(answer, CS_LOCAL));
458 EXPECT_TRUE(f1_.SetAnswer(answer, CS_REMOTE)); 648 EXPECT_TRUE(f1_.SetAnswer(answer, CS_REMOTE));
459 649
460 EXPECT_FALSE(f1_.IsActive()); 650 EXPECT_FALSE(f1_.IsActive());
461 EXPECT_FALSE(f2_.IsActive()); 651 EXPECT_FALSE(f2_.IsActive());
462 } 652 }
463 653
654 class SrtpFilterProtectSetParamsDirectTest
655 : public SrtpFilterTest,
656 public testing::WithParamInterface<bool> {
657 };
658
659 // Test directly setting the params with AES_CM_128_HMAC_SHA1_80.
660 TEST_P(SrtpFilterProtectSetParamsDirectTest, Test_AES_CM_128_HMAC_SHA1_80) {
661 bool enable_external_auth = GetParam();
662 TestProtectSetParamsDirect(enable_external_auth, SRTP_AES128_CM_SHA1_80,
663 kTestKey1, kTestKeyLen, kTestKey2, kTestKeyLen,
664 CS_AES_CM_128_HMAC_SHA1_80);
665 }
666
667 TEST_F(SrtpFilterTest,
668 TestProtectSetParamsDirectHeaderEncryption_AES_CM_128_HMAC_SHA1_80) {
669 TestProtectSetParamsDirectHeaderEncryption(
670 SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen, kTestKey2, kTestKeyLen,
671 CS_AES_CM_128_HMAC_SHA1_80);
672 }
673
674 // Test directly setting the params with AES_CM_128_HMAC_SHA1_32.
675 TEST_P(SrtpFilterProtectSetParamsDirectTest, Test_AES_CM_128_HMAC_SHA1_32) {
676 bool enable_external_auth = GetParam();
677 TestProtectSetParamsDirect(enable_external_auth, SRTP_AES128_CM_SHA1_32,
678 kTestKey1, kTestKeyLen, kTestKey2, kTestKeyLen,
679 CS_AES_CM_128_HMAC_SHA1_32);
680 }
681
682 TEST_F(SrtpFilterTest,
683 TestProtectSetParamsDirectHeaderEncryption_AES_CM_128_HMAC_SHA1_32) {
684 TestProtectSetParamsDirectHeaderEncryption(
685 SRTP_AES128_CM_SHA1_32, kTestKey1, kTestKeyLen, kTestKey2, kTestKeyLen,
686 CS_AES_CM_128_HMAC_SHA1_32);
687 }
688
689 // Test directly setting the params with SRTP_AEAD_AES_128_GCM.
690 TEST_P(SrtpFilterProtectSetParamsDirectTest, Test_SRTP_AEAD_AES_128_GCM) {
691 bool enable_external_auth = GetParam();
692 TestProtectSetParamsDirect(enable_external_auth, SRTP_AEAD_AES_128_GCM,
693 kTestKeyGcm128_1, kTestKeyGcm128Len,
694 kTestKeyGcm128_2, kTestKeyGcm128Len,
695 CS_AEAD_AES_128_GCM);
696 }
697
698 TEST_F(SrtpFilterTest,
699 TestProtectSetParamsDirectHeaderEncryption_SRTP_AEAD_AES_128_GCM) {
700 TestProtectSetParamsDirectHeaderEncryption(
701 SRTP_AEAD_AES_128_GCM, kTestKeyGcm128_1, kTestKeyGcm128Len,
702 kTestKeyGcm128_2, kTestKeyGcm128Len, CS_AEAD_AES_128_GCM);
703 }
704
705 // Test directly setting the params with SRTP_AEAD_AES_256_GCM.
706 TEST_P(SrtpFilterProtectSetParamsDirectTest, Test_SRTP_AEAD_AES_256_GCM) {
707 bool enable_external_auth = GetParam();
708 TestProtectSetParamsDirect(enable_external_auth, SRTP_AEAD_AES_256_GCM,
709 kTestKeyGcm256_1, kTestKeyGcm256Len,
710 kTestKeyGcm256_2, kTestKeyGcm256Len,
711 CS_AEAD_AES_256_GCM);
712 }
713
714 TEST_F(SrtpFilterTest,
715 TestProtectSetParamsDirectHeaderEncryption_SRTP_AEAD_AES_256_GCM) {
716 TestProtectSetParamsDirectHeaderEncryption(
717 SRTP_AEAD_AES_256_GCM, kTestKeyGcm256_1, kTestKeyGcm256Len,
718 kTestKeyGcm256_2, kTestKeyGcm256Len, CS_AEAD_AES_256_GCM);
719 }
720
721 // Run all tests both with and without external auth enabled.
722 INSTANTIATE_TEST_CASE_P(ExternalAuth,
723 SrtpFilterProtectSetParamsDirectTest,
724 ::testing::Values(true, false));
725
726 // Test directly setting the params with bogus keys.
727 TEST_F(SrtpFilterTest, TestSetParamsKeyTooShort) {
728 EXPECT_FALSE(f1_.SetRtpParams(SRTP_AES128_CM_SHA1_80, kTestKey1,
729 kTestKeyLen - 1, SRTP_AES128_CM_SHA1_80,
730 kTestKey1, kTestKeyLen - 1));
731 EXPECT_FALSE(f1_.SetRtcpParams(SRTP_AES128_CM_SHA1_80, kTestKey1,
732 kTestKeyLen - 1, SRTP_AES128_CM_SHA1_80,
733 kTestKey1, kTestKeyLen - 1));
734 }
735
464 } // namespace rtc 736 } // namespace rtc
OLDNEW
« no previous file with comments | « webrtc/pc/srtpfilter.cc ('k') | webrtc/pc/srtptransport.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698