blob: 16a840a307646dd4d375bfaab11af337c783c356 [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
Steve Anton10542f22019-01-11 09:11:00 -080011#include "pc/srtp_session.h"
zstein4dde3df2017-07-07 14:26:25 -070012
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <string.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020014
zstein4dde3df2017-07-07 14:26:25 -070015#include <string>
16
Steve Anton10542f22019-01-11 09:11:00 -080017#include "media/base/fake_rtp.h"
18#include "pc/test/srtp_test_util.h"
19#include "rtc_base/byte_order.h"
20#include "rtc_base/ssl_stream_adapter.h" // For rtc::SRTP_*
Mirko Bonadei17f48782018-09-28 08:51:10 +020021#include "system_wrappers/include/metrics.h"
Steve Antonb443dfe2019-03-05 14:09:49 -080022#include "test/gmock.h"
Yves Gerey3e707812018-11-28 16:47:49 +010023#include "test/gtest.h"
Jonas Orelanded99dae2022-03-09 09:28:10 +010024#include "test/scoped_key_value_config.h"
Steve Antondb67ba12018-03-19 17:41:42 -070025#include "third_party/libsrtp/include/srtp.h"
zstein4dde3df2017-07-07 14:26:25 -070026
Steve Antonb443dfe2019-03-05 14:09:49 -080027using ::testing::ElementsAre;
28using ::testing::Pair;
29
zstein4dde3df2017-07-07 14:26:25 -070030namespace rtc {
31
Zhi Huangc99b6c72017-11-10 16:44:46 -080032std::vector<int> kEncryptedHeaderExtensionIds;
33
Mirko Bonadei6a489f22019-04-09 15:11:12 +020034class SrtpSessionTest : public ::testing::Test {
Qingsi Wang7fc821d2018-07-12 12:54:53 -070035 public:
Jonas Orelanded99dae2022-03-09 09:28:10 +010036 SrtpSessionTest() : s1_(field_trials_), s2_(field_trials_) {
37 webrtc::metrics::Reset();
38 }
Qingsi Wang7fc821d2018-07-12 12:54:53 -070039
zstein4dde3df2017-07-07 14:26:25 -070040 protected:
41 virtual void SetUp() {
42 rtp_len_ = sizeof(kPcmuFrame);
43 rtcp_len_ = sizeof(kRtcpReport);
44 memcpy(rtp_packet_, kPcmuFrame, rtp_len_);
45 memcpy(rtcp_packet_, kRtcpReport, rtcp_len_);
46 }
47 void TestProtectRtp(const std::string& cs) {
48 int out_len = 0;
49 EXPECT_TRUE(
50 s1_.ProtectRtp(rtp_packet_, rtp_len_, sizeof(rtp_packet_), &out_len));
51 EXPECT_EQ(out_len, rtp_len_ + rtp_auth_tag_len(cs));
52 EXPECT_NE(0, memcmp(rtp_packet_, kPcmuFrame, rtp_len_));
53 rtp_len_ = out_len;
54 }
55 void TestProtectRtcp(const std::string& cs) {
56 int out_len = 0;
57 EXPECT_TRUE(s1_.ProtectRtcp(rtcp_packet_, rtcp_len_, sizeof(rtcp_packet_),
58 &out_len));
59 EXPECT_EQ(out_len, rtcp_len_ + 4 + rtcp_auth_tag_len(cs)); // NOLINT
60 EXPECT_NE(0, memcmp(rtcp_packet_, kRtcpReport, rtcp_len_));
61 rtcp_len_ = out_len;
62 }
63 void TestUnprotectRtp(const std::string& cs) {
64 int out_len = 0, expected_len = sizeof(kPcmuFrame);
65 EXPECT_TRUE(s2_.UnprotectRtp(rtp_packet_, rtp_len_, &out_len));
66 EXPECT_EQ(expected_len, out_len);
67 EXPECT_EQ(0, memcmp(rtp_packet_, kPcmuFrame, out_len));
68 }
69 void TestUnprotectRtcp(const std::string& cs) {
70 int out_len = 0, expected_len = sizeof(kRtcpReport);
71 EXPECT_TRUE(s2_.UnprotectRtcp(rtcp_packet_, rtcp_len_, &out_len));
72 EXPECT_EQ(expected_len, out_len);
73 EXPECT_EQ(0, memcmp(rtcp_packet_, kRtcpReport, out_len));
74 }
Jonas Orelanded99dae2022-03-09 09:28:10 +010075 webrtc::test::ScopedKeyValueConfig field_trials_;
zstein4dde3df2017-07-07 14:26:25 -070076 cricket::SrtpSession s1_;
77 cricket::SrtpSession s2_;
78 char rtp_packet_[sizeof(kPcmuFrame) + 10];
79 char rtcp_packet_[sizeof(kRtcpReport) + 4 + 10];
80 int rtp_len_;
81 int rtcp_len_;
82};
83
84// Test that we can set up the session and keys properly.
85TEST_F(SrtpSessionTest, TestGoodSetup) {
Mirko Bonadei7750d802021-07-26 17:27:42 +020086 EXPECT_TRUE(s1_.SetSend(kSrtpAes128CmSha1_80, kTestKey1, kTestKeyLen,
Zhi Huangc99b6c72017-11-10 16:44:46 -080087 kEncryptedHeaderExtensionIds));
Mirko Bonadei7750d802021-07-26 17:27:42 +020088 EXPECT_TRUE(s2_.SetRecv(kSrtpAes128CmSha1_80, kTestKey1, kTestKeyLen,
Zhi Huangc99b6c72017-11-10 16:44:46 -080089 kEncryptedHeaderExtensionIds));
zstein4dde3df2017-07-07 14:26:25 -070090}
91
92// Test that we can't change the keys once set.
93TEST_F(SrtpSessionTest, TestBadSetup) {
Mirko Bonadei7750d802021-07-26 17:27:42 +020094 EXPECT_TRUE(s1_.SetSend(kSrtpAes128CmSha1_80, kTestKey1, kTestKeyLen,
Zhi Huangc99b6c72017-11-10 16:44:46 -080095 kEncryptedHeaderExtensionIds));
Mirko Bonadei7750d802021-07-26 17:27:42 +020096 EXPECT_TRUE(s2_.SetRecv(kSrtpAes128CmSha1_80, kTestKey1, kTestKeyLen,
Zhi Huangc99b6c72017-11-10 16:44:46 -080097 kEncryptedHeaderExtensionIds));
Mirko Bonadei7750d802021-07-26 17:27:42 +020098 EXPECT_FALSE(s1_.SetSend(kSrtpAes128CmSha1_80, kTestKey2, kTestKeyLen,
Zhi Huangc99b6c72017-11-10 16:44:46 -080099 kEncryptedHeaderExtensionIds));
Mirko Bonadei7750d802021-07-26 17:27:42 +0200100 EXPECT_FALSE(s2_.SetRecv(kSrtpAes128CmSha1_80, kTestKey2, kTestKeyLen,
Zhi Huangc99b6c72017-11-10 16:44:46 -0800101 kEncryptedHeaderExtensionIds));
zstein4dde3df2017-07-07 14:26:25 -0700102}
103
104// Test that we fail keys of the wrong length.
105TEST_F(SrtpSessionTest, TestKeysTooShort) {
Mirko Bonadei7750d802021-07-26 17:27:42 +0200106 EXPECT_FALSE(s1_.SetSend(kSrtpAes128CmSha1_80, kTestKey1, 1,
Zhi Huangc99b6c72017-11-10 16:44:46 -0800107 kEncryptedHeaderExtensionIds));
Mirko Bonadei7750d802021-07-26 17:27:42 +0200108 EXPECT_FALSE(s2_.SetRecv(kSrtpAes128CmSha1_80, kTestKey1, 1,
Zhi Huangc99b6c72017-11-10 16:44:46 -0800109 kEncryptedHeaderExtensionIds));
zstein4dde3df2017-07-07 14:26:25 -0700110}
111
112// Test that we can encrypt and decrypt RTP/RTCP using AES_CM_128_HMAC_SHA1_80.
113TEST_F(SrtpSessionTest, TestProtect_AES_CM_128_HMAC_SHA1_80) {
Mirko Bonadei7750d802021-07-26 17:27:42 +0200114 EXPECT_TRUE(s1_.SetSend(kSrtpAes128CmSha1_80, kTestKey1, kTestKeyLen,
Zhi Huangc99b6c72017-11-10 16:44:46 -0800115 kEncryptedHeaderExtensionIds));
Mirko Bonadei7750d802021-07-26 17:27:42 +0200116 EXPECT_TRUE(s2_.SetRecv(kSrtpAes128CmSha1_80, kTestKey1, kTestKeyLen,
Zhi Huangc99b6c72017-11-10 16:44:46 -0800117 kEncryptedHeaderExtensionIds));
Mirko Bonadei7750d802021-07-26 17:27:42 +0200118 TestProtectRtp(kCsAesCm128HmacSha1_80);
119 TestProtectRtcp(kCsAesCm128HmacSha1_80);
120 TestUnprotectRtp(kCsAesCm128HmacSha1_80);
121 TestUnprotectRtcp(kCsAesCm128HmacSha1_80);
zstein4dde3df2017-07-07 14:26:25 -0700122}
123
124// Test that we can encrypt and decrypt RTP/RTCP using AES_CM_128_HMAC_SHA1_32.
125TEST_F(SrtpSessionTest, TestProtect_AES_CM_128_HMAC_SHA1_32) {
Mirko Bonadei7750d802021-07-26 17:27:42 +0200126 EXPECT_TRUE(s1_.SetSend(kSrtpAes128CmSha1_32, kTestKey1, kTestKeyLen,
Zhi Huangc99b6c72017-11-10 16:44:46 -0800127 kEncryptedHeaderExtensionIds));
Mirko Bonadei7750d802021-07-26 17:27:42 +0200128 EXPECT_TRUE(s2_.SetRecv(kSrtpAes128CmSha1_32, kTestKey1, kTestKeyLen,
Zhi Huangc99b6c72017-11-10 16:44:46 -0800129 kEncryptedHeaderExtensionIds));
Mirko Bonadei7750d802021-07-26 17:27:42 +0200130 TestProtectRtp(kCsAesCm128HmacSha1_32);
131 TestProtectRtcp(kCsAesCm128HmacSha1_32);
132 TestUnprotectRtp(kCsAesCm128HmacSha1_32);
133 TestUnprotectRtcp(kCsAesCm128HmacSha1_32);
zstein4dde3df2017-07-07 14:26:25 -0700134}
135
136TEST_F(SrtpSessionTest, TestGetSendStreamPacketIndex) {
Mirko Bonadei7750d802021-07-26 17:27:42 +0200137 EXPECT_TRUE(s1_.SetSend(kSrtpAes128CmSha1_32, kTestKey1, kTestKeyLen,
Zhi Huangc99b6c72017-11-10 16:44:46 -0800138 kEncryptedHeaderExtensionIds));
zstein4dde3df2017-07-07 14:26:25 -0700139 int64_t index;
140 int out_len = 0;
141 EXPECT_TRUE(s1_.ProtectRtp(rtp_packet_, rtp_len_, sizeof(rtp_packet_),
142 &out_len, &index));
Artem Titov880fa812021-07-30 22:30:23 +0200143 // `index` will be shifted by 16.
zstein4dde3df2017-07-07 14:26:25 -0700144 int64_t be64_index = static_cast<int64_t>(NetworkToHost64(1 << 16));
145 EXPECT_EQ(be64_index, index);
146}
147
148// Test that we fail to unprotect if someone tampers with the RTP/RTCP paylaods.
149TEST_F(SrtpSessionTest, TestTamperReject) {
150 int out_len;
Mirko Bonadei7750d802021-07-26 17:27:42 +0200151 EXPECT_TRUE(s1_.SetSend(kSrtpAes128CmSha1_80, kTestKey1, kTestKeyLen,
Zhi Huangc99b6c72017-11-10 16:44:46 -0800152 kEncryptedHeaderExtensionIds));
Mirko Bonadei7750d802021-07-26 17:27:42 +0200153 EXPECT_TRUE(s2_.SetRecv(kSrtpAes128CmSha1_80, kTestKey1, kTestKeyLen,
Zhi Huangc99b6c72017-11-10 16:44:46 -0800154 kEncryptedHeaderExtensionIds));
Mirko Bonadei7750d802021-07-26 17:27:42 +0200155 TestProtectRtp(kCsAesCm128HmacSha1_80);
156 TestProtectRtcp(kCsAesCm128HmacSha1_80);
zstein4dde3df2017-07-07 14:26:25 -0700157 rtp_packet_[0] = 0x12;
158 rtcp_packet_[1] = 0x34;
159 EXPECT_FALSE(s2_.UnprotectRtp(rtp_packet_, rtp_len_, &out_len));
Ying Wangef3998f2019-12-09 13:06:53 +0100160 EXPECT_METRIC_THAT(
Steve Antonb443dfe2019-03-05 14:09:49 -0800161 webrtc::metrics::Samples("WebRTC.PeerConnection.SrtpUnprotectError"),
162 ElementsAre(Pair(srtp_err_status_bad_param, 1)));
zstein4dde3df2017-07-07 14:26:25 -0700163 EXPECT_FALSE(s2_.UnprotectRtcp(rtcp_packet_, rtcp_len_, &out_len));
Ying Wangef3998f2019-12-09 13:06:53 +0100164 EXPECT_METRIC_THAT(
Steve Antonb443dfe2019-03-05 14:09:49 -0800165 webrtc::metrics::Samples("WebRTC.PeerConnection.SrtcpUnprotectError"),
166 ElementsAre(Pair(srtp_err_status_auth_fail, 1)));
zstein4dde3df2017-07-07 14:26:25 -0700167}
168
169// Test that we fail to unprotect if the payloads are not authenticated.
170TEST_F(SrtpSessionTest, TestUnencryptReject) {
171 int out_len;
Mirko Bonadei7750d802021-07-26 17:27:42 +0200172 EXPECT_TRUE(s1_.SetSend(kSrtpAes128CmSha1_80, kTestKey1, kTestKeyLen,
Zhi Huangc99b6c72017-11-10 16:44:46 -0800173 kEncryptedHeaderExtensionIds));
Mirko Bonadei7750d802021-07-26 17:27:42 +0200174 EXPECT_TRUE(s2_.SetRecv(kSrtpAes128CmSha1_80, kTestKey1, kTestKeyLen,
Zhi Huangc99b6c72017-11-10 16:44:46 -0800175 kEncryptedHeaderExtensionIds));
zstein4dde3df2017-07-07 14:26:25 -0700176 EXPECT_FALSE(s2_.UnprotectRtp(rtp_packet_, rtp_len_, &out_len));
Ying Wangef3998f2019-12-09 13:06:53 +0100177 EXPECT_METRIC_THAT(
Steve Antonb443dfe2019-03-05 14:09:49 -0800178 webrtc::metrics::Samples("WebRTC.PeerConnection.SrtpUnprotectError"),
179 ElementsAre(Pair(srtp_err_status_auth_fail, 1)));
zstein4dde3df2017-07-07 14:26:25 -0700180 EXPECT_FALSE(s2_.UnprotectRtcp(rtcp_packet_, rtcp_len_, &out_len));
Ying Wangef3998f2019-12-09 13:06:53 +0100181 EXPECT_METRIC_THAT(
Steve Antonb443dfe2019-03-05 14:09:49 -0800182 webrtc::metrics::Samples("WebRTC.PeerConnection.SrtcpUnprotectError"),
183 ElementsAre(Pair(srtp_err_status_cant_check, 1)));
zstein4dde3df2017-07-07 14:26:25 -0700184}
185
186// Test that we fail when using buffers that are too small.
187TEST_F(SrtpSessionTest, TestBuffersTooSmall) {
188 int out_len;
Mirko Bonadei7750d802021-07-26 17:27:42 +0200189 EXPECT_TRUE(s1_.SetSend(kSrtpAes128CmSha1_80, kTestKey1, kTestKeyLen,
Zhi Huangc99b6c72017-11-10 16:44:46 -0800190 kEncryptedHeaderExtensionIds));
zstein4dde3df2017-07-07 14:26:25 -0700191 EXPECT_FALSE(s1_.ProtectRtp(rtp_packet_, rtp_len_, sizeof(rtp_packet_) - 10,
192 &out_len));
193 EXPECT_FALSE(s1_.ProtectRtcp(rtcp_packet_, rtcp_len_,
194 sizeof(rtcp_packet_) - 14, &out_len));
195}
196
197TEST_F(SrtpSessionTest, TestReplay) {
198 static const uint16_t kMaxSeqnum = static_cast<uint16_t>(-1);
199 static const uint16_t seqnum_big = 62275;
200 static const uint16_t seqnum_small = 10;
201 static const uint16_t replay_window = 1024;
202 int out_len;
203
Mirko Bonadei7750d802021-07-26 17:27:42 +0200204 EXPECT_TRUE(s1_.SetSend(kSrtpAes128CmSha1_80, kTestKey1, kTestKeyLen,
Zhi Huangc99b6c72017-11-10 16:44:46 -0800205 kEncryptedHeaderExtensionIds));
Mirko Bonadei7750d802021-07-26 17:27:42 +0200206 EXPECT_TRUE(s2_.SetRecv(kSrtpAes128CmSha1_80, kTestKey1, kTestKeyLen,
Zhi Huangc99b6c72017-11-10 16:44:46 -0800207 kEncryptedHeaderExtensionIds));
zstein4dde3df2017-07-07 14:26:25 -0700208
209 // Initial sequence number.
210 SetBE16(reinterpret_cast<uint8_t*>(rtp_packet_) + 2, seqnum_big);
211 EXPECT_TRUE(
212 s1_.ProtectRtp(rtp_packet_, rtp_len_, sizeof(rtp_packet_), &out_len));
213
214 // Replay within the 1024 window should succeed.
215 SetBE16(reinterpret_cast<uint8_t*>(rtp_packet_) + 2,
216 seqnum_big - replay_window + 1);
217 EXPECT_TRUE(
218 s1_.ProtectRtp(rtp_packet_, rtp_len_, sizeof(rtp_packet_), &out_len));
219
220 // Replay out side of the 1024 window should fail.
221 SetBE16(reinterpret_cast<uint8_t*>(rtp_packet_) + 2,
222 seqnum_big - replay_window - 1);
223 EXPECT_FALSE(
224 s1_.ProtectRtp(rtp_packet_, rtp_len_, sizeof(rtp_packet_), &out_len));
225
226 // Increment sequence number to a small number.
227 SetBE16(reinterpret_cast<uint8_t*>(rtp_packet_) + 2, seqnum_small);
228 EXPECT_TRUE(
229 s1_.ProtectRtp(rtp_packet_, rtp_len_, sizeof(rtp_packet_), &out_len));
230
231 // Replay around 0 but out side of the 1024 window should fail.
232 SetBE16(reinterpret_cast<uint8_t*>(rtp_packet_) + 2,
233 kMaxSeqnum + seqnum_small - replay_window - 1);
234 EXPECT_FALSE(
235 s1_.ProtectRtp(rtp_packet_, rtp_len_, sizeof(rtp_packet_), &out_len));
236
237 // Replay around 0 but within the 1024 window should succeed.
238 for (uint16_t seqnum = 65000; seqnum < 65003; ++seqnum) {
239 SetBE16(reinterpret_cast<uint8_t*>(rtp_packet_) + 2, seqnum);
240 EXPECT_TRUE(
241 s1_.ProtectRtp(rtp_packet_, rtp_len_, sizeof(rtp_packet_), &out_len));
242 }
243
244 // Go back to normal sequence nubmer.
245 // NOTE: without the fix in libsrtp, this would fail. This is because
246 // without the fix, the loop above would keep incrementing local sequence
247 // number in libsrtp, eventually the new sequence number would go out side
248 // of the window.
249 SetBE16(reinterpret_cast<uint8_t*>(rtp_packet_) + 2, seqnum_small + 1);
250 EXPECT_TRUE(
251 s1_.ProtectRtp(rtp_packet_, rtp_len_, sizeof(rtp_packet_), &out_len));
252}
253
254} // namespace rtc