blob: d73bcbe8f7ee3d123b28f2ca67b437f36e88460e [file] [log] [blame]
zstein4dde3df2017-07-07 14:26:25 -07001/*
2 * Copyright 2004 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "pc/srtpsession.h"
zstein4dde3df2017-07-07 14:26:25 -070012
13#include <string>
14
Steve Antondb67ba12018-03-19 17:41:42 -070015#include "api/fakemetricsobserver.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "media/base/fakertp.h"
17#include "pc/srtptestutil.h"
18#include "rtc_base/gunit.h"
Steve Antondb67ba12018-03-19 17:41:42 -070019#include "rtc_base/ptr_util.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "rtc_base/sslstreamadapter.h" // For rtc::SRTP_*
Steve Antondb67ba12018-03-19 17:41:42 -070021#include "third_party/libsrtp/include/srtp.h"
zstein4dde3df2017-07-07 14:26:25 -070022
23namespace rtc {
24
Steve Antondb67ba12018-03-19 17:41:42 -070025using webrtc::FakeMetricsObserver;
26
Zhi Huangc99b6c72017-11-10 16:44:46 -080027std::vector<int> kEncryptedHeaderExtensionIds;
28
zstein4dde3df2017-07-07 14:26:25 -070029class SrtpSessionTest : public testing::Test {
30 protected:
31 virtual void SetUp() {
32 rtp_len_ = sizeof(kPcmuFrame);
33 rtcp_len_ = sizeof(kRtcpReport);
34 memcpy(rtp_packet_, kPcmuFrame, rtp_len_);
35 memcpy(rtcp_packet_, kRtcpReport, rtcp_len_);
36 }
37 void TestProtectRtp(const std::string& cs) {
38 int out_len = 0;
39 EXPECT_TRUE(
40 s1_.ProtectRtp(rtp_packet_, rtp_len_, sizeof(rtp_packet_), &out_len));
41 EXPECT_EQ(out_len, rtp_len_ + rtp_auth_tag_len(cs));
42 EXPECT_NE(0, memcmp(rtp_packet_, kPcmuFrame, rtp_len_));
43 rtp_len_ = out_len;
44 }
45 void TestProtectRtcp(const std::string& cs) {
46 int out_len = 0;
47 EXPECT_TRUE(s1_.ProtectRtcp(rtcp_packet_, rtcp_len_, sizeof(rtcp_packet_),
48 &out_len));
49 EXPECT_EQ(out_len, rtcp_len_ + 4 + rtcp_auth_tag_len(cs)); // NOLINT
50 EXPECT_NE(0, memcmp(rtcp_packet_, kRtcpReport, rtcp_len_));
51 rtcp_len_ = out_len;
52 }
53 void TestUnprotectRtp(const std::string& cs) {
54 int out_len = 0, expected_len = sizeof(kPcmuFrame);
55 EXPECT_TRUE(s2_.UnprotectRtp(rtp_packet_, rtp_len_, &out_len));
56 EXPECT_EQ(expected_len, out_len);
57 EXPECT_EQ(0, memcmp(rtp_packet_, kPcmuFrame, out_len));
58 }
59 void TestUnprotectRtcp(const std::string& cs) {
60 int out_len = 0, expected_len = sizeof(kRtcpReport);
61 EXPECT_TRUE(s2_.UnprotectRtcp(rtcp_packet_, rtcp_len_, &out_len));
62 EXPECT_EQ(expected_len, out_len);
63 EXPECT_EQ(0, memcmp(rtcp_packet_, kRtcpReport, out_len));
64 }
65 cricket::SrtpSession s1_;
66 cricket::SrtpSession s2_;
67 char rtp_packet_[sizeof(kPcmuFrame) + 10];
68 char rtcp_packet_[sizeof(kRtcpReport) + 4 + 10];
69 int rtp_len_;
70 int rtcp_len_;
71};
72
73// Test that we can set up the session and keys properly.
74TEST_F(SrtpSessionTest, TestGoodSetup) {
Zhi Huangc99b6c72017-11-10 16:44:46 -080075 EXPECT_TRUE(s1_.SetSend(SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen,
76 kEncryptedHeaderExtensionIds));
77 EXPECT_TRUE(s2_.SetRecv(SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen,
78 kEncryptedHeaderExtensionIds));
zstein4dde3df2017-07-07 14:26:25 -070079}
80
81// Test that we can't change the keys once set.
82TEST_F(SrtpSessionTest, TestBadSetup) {
Zhi Huangc99b6c72017-11-10 16:44:46 -080083 EXPECT_TRUE(s1_.SetSend(SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen,
84 kEncryptedHeaderExtensionIds));
85 EXPECT_TRUE(s2_.SetRecv(SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen,
86 kEncryptedHeaderExtensionIds));
87 EXPECT_FALSE(s1_.SetSend(SRTP_AES128_CM_SHA1_80, kTestKey2, kTestKeyLen,
88 kEncryptedHeaderExtensionIds));
89 EXPECT_FALSE(s2_.SetRecv(SRTP_AES128_CM_SHA1_80, kTestKey2, kTestKeyLen,
90 kEncryptedHeaderExtensionIds));
zstein4dde3df2017-07-07 14:26:25 -070091}
92
93// Test that we fail keys of the wrong length.
94TEST_F(SrtpSessionTest, TestKeysTooShort) {
Zhi Huangc99b6c72017-11-10 16:44:46 -080095 EXPECT_FALSE(s1_.SetSend(SRTP_AES128_CM_SHA1_80, kTestKey1, 1,
96 kEncryptedHeaderExtensionIds));
97 EXPECT_FALSE(s2_.SetRecv(SRTP_AES128_CM_SHA1_80, kTestKey1, 1,
98 kEncryptedHeaderExtensionIds));
zstein4dde3df2017-07-07 14:26:25 -070099}
100
101// Test that we can encrypt and decrypt RTP/RTCP using AES_CM_128_HMAC_SHA1_80.
102TEST_F(SrtpSessionTest, TestProtect_AES_CM_128_HMAC_SHA1_80) {
Zhi Huangc99b6c72017-11-10 16:44:46 -0800103 EXPECT_TRUE(s1_.SetSend(SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen,
104 kEncryptedHeaderExtensionIds));
105 EXPECT_TRUE(s2_.SetRecv(SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen,
106 kEncryptedHeaderExtensionIds));
zstein4dde3df2017-07-07 14:26:25 -0700107 TestProtectRtp(CS_AES_CM_128_HMAC_SHA1_80);
108 TestProtectRtcp(CS_AES_CM_128_HMAC_SHA1_80);
109 TestUnprotectRtp(CS_AES_CM_128_HMAC_SHA1_80);
110 TestUnprotectRtcp(CS_AES_CM_128_HMAC_SHA1_80);
111}
112
113// Test that we can encrypt and decrypt RTP/RTCP using AES_CM_128_HMAC_SHA1_32.
114TEST_F(SrtpSessionTest, TestProtect_AES_CM_128_HMAC_SHA1_32) {
Zhi Huangc99b6c72017-11-10 16:44:46 -0800115 EXPECT_TRUE(s1_.SetSend(SRTP_AES128_CM_SHA1_32, kTestKey1, kTestKeyLen,
116 kEncryptedHeaderExtensionIds));
117 EXPECT_TRUE(s2_.SetRecv(SRTP_AES128_CM_SHA1_32, kTestKey1, kTestKeyLen,
118 kEncryptedHeaderExtensionIds));
zstein4dde3df2017-07-07 14:26:25 -0700119 TestProtectRtp(CS_AES_CM_128_HMAC_SHA1_32);
120 TestProtectRtcp(CS_AES_CM_128_HMAC_SHA1_32);
121 TestUnprotectRtp(CS_AES_CM_128_HMAC_SHA1_32);
122 TestUnprotectRtcp(CS_AES_CM_128_HMAC_SHA1_32);
123}
124
125TEST_F(SrtpSessionTest, TestGetSendStreamPacketIndex) {
Zhi Huangc99b6c72017-11-10 16:44:46 -0800126 EXPECT_TRUE(s1_.SetSend(SRTP_AES128_CM_SHA1_32, kTestKey1, kTestKeyLen,
127 kEncryptedHeaderExtensionIds));
zstein4dde3df2017-07-07 14:26:25 -0700128 int64_t index;
129 int out_len = 0;
130 EXPECT_TRUE(s1_.ProtectRtp(rtp_packet_, rtp_len_, sizeof(rtp_packet_),
131 &out_len, &index));
132 // |index| will be shifted by 16.
133 int64_t be64_index = static_cast<int64_t>(NetworkToHost64(1 << 16));
134 EXPECT_EQ(be64_index, index);
135}
136
137// Test that we fail to unprotect if someone tampers with the RTP/RTCP paylaods.
138TEST_F(SrtpSessionTest, TestTamperReject) {
Steve Antondb67ba12018-03-19 17:41:42 -0700139 rtc::scoped_refptr<FakeMetricsObserver> metrics_observer(
140 new rtc::RefCountedObject<FakeMetricsObserver>());
141 s2_.SetMetricsObserver(metrics_observer);
zstein4dde3df2017-07-07 14:26:25 -0700142 int out_len;
Zhi Huangc99b6c72017-11-10 16:44:46 -0800143 EXPECT_TRUE(s1_.SetSend(SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen,
144 kEncryptedHeaderExtensionIds));
145 EXPECT_TRUE(s2_.SetRecv(SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen,
146 kEncryptedHeaderExtensionIds));
zstein4dde3df2017-07-07 14:26:25 -0700147 TestProtectRtp(CS_AES_CM_128_HMAC_SHA1_80);
148 TestProtectRtcp(CS_AES_CM_128_HMAC_SHA1_80);
149 rtp_packet_[0] = 0x12;
150 rtcp_packet_[1] = 0x34;
151 EXPECT_FALSE(s2_.UnprotectRtp(rtp_packet_, rtp_len_, &out_len));
Steve Antondb67ba12018-03-19 17:41:42 -0700152 EXPECT_TRUE(metrics_observer->ExpectOnlySingleEnumCount(
153 webrtc::kEnumCounterSrtpUnprotectError, srtp_err_status_bad_param));
zstein4dde3df2017-07-07 14:26:25 -0700154 EXPECT_FALSE(s2_.UnprotectRtcp(rtcp_packet_, rtcp_len_, &out_len));
Steve Antondb67ba12018-03-19 17:41:42 -0700155 EXPECT_TRUE(metrics_observer->ExpectOnlySingleEnumCount(
156 webrtc::kEnumCounterSrtcpUnprotectError, srtp_err_status_auth_fail));
zstein4dde3df2017-07-07 14:26:25 -0700157}
158
159// Test that we fail to unprotect if the payloads are not authenticated.
160TEST_F(SrtpSessionTest, TestUnencryptReject) {
Steve Antondb67ba12018-03-19 17:41:42 -0700161 rtc::scoped_refptr<FakeMetricsObserver> metrics_observer(
162 new rtc::RefCountedObject<FakeMetricsObserver>());
163 s2_.SetMetricsObserver(metrics_observer);
zstein4dde3df2017-07-07 14:26:25 -0700164 int out_len;
Zhi Huangc99b6c72017-11-10 16:44:46 -0800165 EXPECT_TRUE(s1_.SetSend(SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen,
166 kEncryptedHeaderExtensionIds));
167 EXPECT_TRUE(s2_.SetRecv(SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen,
168 kEncryptedHeaderExtensionIds));
zstein4dde3df2017-07-07 14:26:25 -0700169 EXPECT_FALSE(s2_.UnprotectRtp(rtp_packet_, rtp_len_, &out_len));
Steve Antondb67ba12018-03-19 17:41:42 -0700170 EXPECT_TRUE(metrics_observer->ExpectOnlySingleEnumCount(
171 webrtc::kEnumCounterSrtpUnprotectError, srtp_err_status_auth_fail));
zstein4dde3df2017-07-07 14:26:25 -0700172 EXPECT_FALSE(s2_.UnprotectRtcp(rtcp_packet_, rtcp_len_, &out_len));
Steve Antondb67ba12018-03-19 17:41:42 -0700173 EXPECT_TRUE(metrics_observer->ExpectOnlySingleEnumCount(
174 webrtc::kEnumCounterSrtcpUnprotectError, srtp_err_status_cant_check));
zstein4dde3df2017-07-07 14:26:25 -0700175}
176
177// Test that we fail when using buffers that are too small.
178TEST_F(SrtpSessionTest, TestBuffersTooSmall) {
179 int out_len;
Zhi Huangc99b6c72017-11-10 16:44:46 -0800180 EXPECT_TRUE(s1_.SetSend(SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen,
181 kEncryptedHeaderExtensionIds));
zstein4dde3df2017-07-07 14:26:25 -0700182 EXPECT_FALSE(s1_.ProtectRtp(rtp_packet_, rtp_len_, sizeof(rtp_packet_) - 10,
183 &out_len));
184 EXPECT_FALSE(s1_.ProtectRtcp(rtcp_packet_, rtcp_len_,
185 sizeof(rtcp_packet_) - 14, &out_len));
186}
187
188TEST_F(SrtpSessionTest, TestReplay) {
189 static const uint16_t kMaxSeqnum = static_cast<uint16_t>(-1);
190 static const uint16_t seqnum_big = 62275;
191 static const uint16_t seqnum_small = 10;
192 static const uint16_t replay_window = 1024;
193 int out_len;
194
Zhi Huangc99b6c72017-11-10 16:44:46 -0800195 EXPECT_TRUE(s1_.SetSend(SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen,
196 kEncryptedHeaderExtensionIds));
197 EXPECT_TRUE(s2_.SetRecv(SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen,
198 kEncryptedHeaderExtensionIds));
zstein4dde3df2017-07-07 14:26:25 -0700199
200 // Initial sequence number.
201 SetBE16(reinterpret_cast<uint8_t*>(rtp_packet_) + 2, seqnum_big);
202 EXPECT_TRUE(
203 s1_.ProtectRtp(rtp_packet_, rtp_len_, sizeof(rtp_packet_), &out_len));
204
205 // Replay within the 1024 window should succeed.
206 SetBE16(reinterpret_cast<uint8_t*>(rtp_packet_) + 2,
207 seqnum_big - replay_window + 1);
208 EXPECT_TRUE(
209 s1_.ProtectRtp(rtp_packet_, rtp_len_, sizeof(rtp_packet_), &out_len));
210
211 // Replay out side of the 1024 window should fail.
212 SetBE16(reinterpret_cast<uint8_t*>(rtp_packet_) + 2,
213 seqnum_big - replay_window - 1);
214 EXPECT_FALSE(
215 s1_.ProtectRtp(rtp_packet_, rtp_len_, sizeof(rtp_packet_), &out_len));
216
217 // Increment sequence number to a small number.
218 SetBE16(reinterpret_cast<uint8_t*>(rtp_packet_) + 2, seqnum_small);
219 EXPECT_TRUE(
220 s1_.ProtectRtp(rtp_packet_, rtp_len_, sizeof(rtp_packet_), &out_len));
221
222 // Replay around 0 but out side of the 1024 window should fail.
223 SetBE16(reinterpret_cast<uint8_t*>(rtp_packet_) + 2,
224 kMaxSeqnum + seqnum_small - replay_window - 1);
225 EXPECT_FALSE(
226 s1_.ProtectRtp(rtp_packet_, rtp_len_, sizeof(rtp_packet_), &out_len));
227
228 // Replay around 0 but within the 1024 window should succeed.
229 for (uint16_t seqnum = 65000; seqnum < 65003; ++seqnum) {
230 SetBE16(reinterpret_cast<uint8_t*>(rtp_packet_) + 2, seqnum);
231 EXPECT_TRUE(
232 s1_.ProtectRtp(rtp_packet_, rtp_len_, sizeof(rtp_packet_), &out_len));
233 }
234
235 // Go back to normal sequence nubmer.
236 // NOTE: without the fix in libsrtp, this would fail. This is because
237 // without the fix, the loop above would keep incrementing local sequence
238 // number in libsrtp, eventually the new sequence number would go out side
239 // of the window.
240 SetBE16(reinterpret_cast<uint8_t*>(rtp_packet_) + 2, seqnum_small + 1);
241 EXPECT_TRUE(
242 s1_.ProtectRtp(rtp_packet_, rtp_len_, sizeof(rtp_packet_), &out_len));
243}
244
245} // namespace rtc