zstein | 4dde3df | 2017-07-07 14:26:25 -0700 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "pc/srtpsession.h" |
zstein | 4dde3df | 2017-07-07 14:26:25 -0700 | [diff] [blame] | 12 | |
| 13 | #include <string> |
| 14 | |
Steve Anton | db67ba1 | 2018-03-19 17:41:42 -0700 | [diff] [blame] | 15 | #include "api/fakemetricsobserver.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "media/base/fakertp.h" |
| 17 | #include "pc/srtptestutil.h" |
| 18 | #include "rtc_base/gunit.h" |
Steve Anton | db67ba1 | 2018-03-19 17:41:42 -0700 | [diff] [blame] | 19 | #include "rtc_base/ptr_util.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "rtc_base/sslstreamadapter.h" // For rtc::SRTP_* |
Steve Anton | db67ba1 | 2018-03-19 17:41:42 -0700 | [diff] [blame] | 21 | #include "third_party/libsrtp/include/srtp.h" |
zstein | 4dde3df | 2017-07-07 14:26:25 -0700 | [diff] [blame] | 22 | |
| 23 | namespace rtc { |
| 24 | |
Steve Anton | db67ba1 | 2018-03-19 17:41:42 -0700 | [diff] [blame] | 25 | using webrtc::FakeMetricsObserver; |
| 26 | |
Zhi Huang | c99b6c7 | 2017-11-10 16:44:46 -0800 | [diff] [blame] | 27 | std::vector<int> kEncryptedHeaderExtensionIds; |
| 28 | |
zstein | 4dde3df | 2017-07-07 14:26:25 -0700 | [diff] [blame] | 29 | class 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. |
| 74 | TEST_F(SrtpSessionTest, TestGoodSetup) { |
Zhi Huang | c99b6c7 | 2017-11-10 16:44:46 -0800 | [diff] [blame] | 75 | 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)); |
zstein | 4dde3df | 2017-07-07 14:26:25 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | // Test that we can't change the keys once set. |
| 82 | TEST_F(SrtpSessionTest, TestBadSetup) { |
Zhi Huang | c99b6c7 | 2017-11-10 16:44:46 -0800 | [diff] [blame] | 83 | 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)); |
zstein | 4dde3df | 2017-07-07 14:26:25 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | // Test that we fail keys of the wrong length. |
| 94 | TEST_F(SrtpSessionTest, TestKeysTooShort) { |
Zhi Huang | c99b6c7 | 2017-11-10 16:44:46 -0800 | [diff] [blame] | 95 | 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)); |
zstein | 4dde3df | 2017-07-07 14:26:25 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | // Test that we can encrypt and decrypt RTP/RTCP using AES_CM_128_HMAC_SHA1_80. |
| 102 | TEST_F(SrtpSessionTest, TestProtect_AES_CM_128_HMAC_SHA1_80) { |
Zhi Huang | c99b6c7 | 2017-11-10 16:44:46 -0800 | [diff] [blame] | 103 | 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)); |
zstein | 4dde3df | 2017-07-07 14:26:25 -0700 | [diff] [blame] | 107 | 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. |
| 114 | TEST_F(SrtpSessionTest, TestProtect_AES_CM_128_HMAC_SHA1_32) { |
Zhi Huang | c99b6c7 | 2017-11-10 16:44:46 -0800 | [diff] [blame] | 115 | 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)); |
zstein | 4dde3df | 2017-07-07 14:26:25 -0700 | [diff] [blame] | 119 | 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 | |
| 125 | TEST_F(SrtpSessionTest, TestGetSendStreamPacketIndex) { |
Zhi Huang | c99b6c7 | 2017-11-10 16:44:46 -0800 | [diff] [blame] | 126 | EXPECT_TRUE(s1_.SetSend(SRTP_AES128_CM_SHA1_32, kTestKey1, kTestKeyLen, |
| 127 | kEncryptedHeaderExtensionIds)); |
zstein | 4dde3df | 2017-07-07 14:26:25 -0700 | [diff] [blame] | 128 | 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. |
| 138 | TEST_F(SrtpSessionTest, TestTamperReject) { |
Steve Anton | db67ba1 | 2018-03-19 17:41:42 -0700 | [diff] [blame] | 139 | rtc::scoped_refptr<FakeMetricsObserver> metrics_observer( |
| 140 | new rtc::RefCountedObject<FakeMetricsObserver>()); |
| 141 | s2_.SetMetricsObserver(metrics_observer); |
zstein | 4dde3df | 2017-07-07 14:26:25 -0700 | [diff] [blame] | 142 | int out_len; |
Zhi Huang | c99b6c7 | 2017-11-10 16:44:46 -0800 | [diff] [blame] | 143 | 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)); |
zstein | 4dde3df | 2017-07-07 14:26:25 -0700 | [diff] [blame] | 147 | 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 Anton | db67ba1 | 2018-03-19 17:41:42 -0700 | [diff] [blame] | 152 | EXPECT_TRUE(metrics_observer->ExpectOnlySingleEnumCount( |
| 153 | webrtc::kEnumCounterSrtpUnprotectError, srtp_err_status_bad_param)); |
zstein | 4dde3df | 2017-07-07 14:26:25 -0700 | [diff] [blame] | 154 | EXPECT_FALSE(s2_.UnprotectRtcp(rtcp_packet_, rtcp_len_, &out_len)); |
Steve Anton | db67ba1 | 2018-03-19 17:41:42 -0700 | [diff] [blame] | 155 | EXPECT_TRUE(metrics_observer->ExpectOnlySingleEnumCount( |
| 156 | webrtc::kEnumCounterSrtcpUnprotectError, srtp_err_status_auth_fail)); |
zstein | 4dde3df | 2017-07-07 14:26:25 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | // Test that we fail to unprotect if the payloads are not authenticated. |
| 160 | TEST_F(SrtpSessionTest, TestUnencryptReject) { |
Steve Anton | db67ba1 | 2018-03-19 17:41:42 -0700 | [diff] [blame] | 161 | rtc::scoped_refptr<FakeMetricsObserver> metrics_observer( |
| 162 | new rtc::RefCountedObject<FakeMetricsObserver>()); |
| 163 | s2_.SetMetricsObserver(metrics_observer); |
zstein | 4dde3df | 2017-07-07 14:26:25 -0700 | [diff] [blame] | 164 | int out_len; |
Zhi Huang | c99b6c7 | 2017-11-10 16:44:46 -0800 | [diff] [blame] | 165 | 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)); |
zstein | 4dde3df | 2017-07-07 14:26:25 -0700 | [diff] [blame] | 169 | EXPECT_FALSE(s2_.UnprotectRtp(rtp_packet_, rtp_len_, &out_len)); |
Steve Anton | db67ba1 | 2018-03-19 17:41:42 -0700 | [diff] [blame] | 170 | EXPECT_TRUE(metrics_observer->ExpectOnlySingleEnumCount( |
| 171 | webrtc::kEnumCounterSrtpUnprotectError, srtp_err_status_auth_fail)); |
zstein | 4dde3df | 2017-07-07 14:26:25 -0700 | [diff] [blame] | 172 | EXPECT_FALSE(s2_.UnprotectRtcp(rtcp_packet_, rtcp_len_, &out_len)); |
Steve Anton | db67ba1 | 2018-03-19 17:41:42 -0700 | [diff] [blame] | 173 | EXPECT_TRUE(metrics_observer->ExpectOnlySingleEnumCount( |
| 174 | webrtc::kEnumCounterSrtcpUnprotectError, srtp_err_status_cant_check)); |
zstein | 4dde3df | 2017-07-07 14:26:25 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | // Test that we fail when using buffers that are too small. |
| 178 | TEST_F(SrtpSessionTest, TestBuffersTooSmall) { |
| 179 | int out_len; |
Zhi Huang | c99b6c7 | 2017-11-10 16:44:46 -0800 | [diff] [blame] | 180 | EXPECT_TRUE(s1_.SetSend(SRTP_AES128_CM_SHA1_80, kTestKey1, kTestKeyLen, |
| 181 | kEncryptedHeaderExtensionIds)); |
zstein | 4dde3df | 2017-07-07 14:26:25 -0700 | [diff] [blame] | 182 | 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 | |
| 188 | TEST_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 Huang | c99b6c7 | 2017-11-10 16:44:46 -0800 | [diff] [blame] | 195 | 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)); |
zstein | 4dde3df | 2017-07-07 14:26:25 -0700 | [diff] [blame] | 199 | |
| 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 |