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 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #include "pc/srtp_session.h" |
zstein | 4dde3df | 2017-07-07 14:26:25 -0700 | [diff] [blame] | 12 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 13 | #include <string.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 14 | |
zstein | 4dde3df | 2017-07-07 14:26:25 -0700 | [diff] [blame] | 15 | #include <string> |
| 16 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 17 | #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 Bonadei | 17f4878 | 2018-09-28 08:51:10 +0200 | [diff] [blame] | 21 | #include "system_wrappers/include/metrics.h" |
Steve Anton | b443dfe | 2019-03-05 14:09:49 -0800 | [diff] [blame] | 22 | #include "test/gmock.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 23 | #include "test/gtest.h" |
Jonas Oreland | ed99dae | 2022-03-09 09:28:10 +0100 | [diff] [blame] | 24 | #include "test/scoped_key_value_config.h" |
Steve Anton | db67ba1 | 2018-03-19 17:41:42 -0700 | [diff] [blame] | 25 | #include "third_party/libsrtp/include/srtp.h" |
zstein | 4dde3df | 2017-07-07 14:26:25 -0700 | [diff] [blame] | 26 | |
Steve Anton | b443dfe | 2019-03-05 14:09:49 -0800 | [diff] [blame] | 27 | using ::testing::ElementsAre; |
| 28 | using ::testing::Pair; |
| 29 | |
zstein | 4dde3df | 2017-07-07 14:26:25 -0700 | [diff] [blame] | 30 | namespace rtc { |
| 31 | |
Zhi Huang | c99b6c7 | 2017-11-10 16:44:46 -0800 | [diff] [blame] | 32 | std::vector<int> kEncryptedHeaderExtensionIds; |
| 33 | |
Mirko Bonadei | 6a489f2 | 2019-04-09 15:11:12 +0200 | [diff] [blame] | 34 | class SrtpSessionTest : public ::testing::Test { |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 35 | public: |
Jonas Oreland | ed99dae | 2022-03-09 09:28:10 +0100 | [diff] [blame] | 36 | SrtpSessionTest() : s1_(field_trials_), s2_(field_trials_) { |
| 37 | webrtc::metrics::Reset(); |
| 38 | } |
Qingsi Wang | 7fc821d | 2018-07-12 12:54:53 -0700 | [diff] [blame] | 39 | |
zstein | 4dde3df | 2017-07-07 14:26:25 -0700 | [diff] [blame] | 40 | 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 Oreland | ed99dae | 2022-03-09 09:28:10 +0100 | [diff] [blame] | 75 | webrtc::test::ScopedKeyValueConfig field_trials_; |
zstein | 4dde3df | 2017-07-07 14:26:25 -0700 | [diff] [blame] | 76 | 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. |
| 85 | TEST_F(SrtpSessionTest, TestGoodSetup) { |
Mirko Bonadei | 7750d80 | 2021-07-26 17:27:42 +0200 | [diff] [blame] | 86 | EXPECT_TRUE(s1_.SetSend(kSrtpAes128CmSha1_80, kTestKey1, kTestKeyLen, |
Zhi Huang | c99b6c7 | 2017-11-10 16:44:46 -0800 | [diff] [blame] | 87 | kEncryptedHeaderExtensionIds)); |
Mirko Bonadei | 7750d80 | 2021-07-26 17:27:42 +0200 | [diff] [blame] | 88 | EXPECT_TRUE(s2_.SetRecv(kSrtpAes128CmSha1_80, kTestKey1, kTestKeyLen, |
Zhi Huang | c99b6c7 | 2017-11-10 16:44:46 -0800 | [diff] [blame] | 89 | kEncryptedHeaderExtensionIds)); |
zstein | 4dde3df | 2017-07-07 14:26:25 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | // Test that we can't change the keys once set. |
| 93 | TEST_F(SrtpSessionTest, TestBadSetup) { |
Mirko Bonadei | 7750d80 | 2021-07-26 17:27:42 +0200 | [diff] [blame] | 94 | EXPECT_TRUE(s1_.SetSend(kSrtpAes128CmSha1_80, kTestKey1, kTestKeyLen, |
Zhi Huang | c99b6c7 | 2017-11-10 16:44:46 -0800 | [diff] [blame] | 95 | kEncryptedHeaderExtensionIds)); |
Mirko Bonadei | 7750d80 | 2021-07-26 17:27:42 +0200 | [diff] [blame] | 96 | EXPECT_TRUE(s2_.SetRecv(kSrtpAes128CmSha1_80, kTestKey1, kTestKeyLen, |
Zhi Huang | c99b6c7 | 2017-11-10 16:44:46 -0800 | [diff] [blame] | 97 | kEncryptedHeaderExtensionIds)); |
Mirko Bonadei | 7750d80 | 2021-07-26 17:27:42 +0200 | [diff] [blame] | 98 | EXPECT_FALSE(s1_.SetSend(kSrtpAes128CmSha1_80, kTestKey2, kTestKeyLen, |
Zhi Huang | c99b6c7 | 2017-11-10 16:44:46 -0800 | [diff] [blame] | 99 | kEncryptedHeaderExtensionIds)); |
Mirko Bonadei | 7750d80 | 2021-07-26 17:27:42 +0200 | [diff] [blame] | 100 | EXPECT_FALSE(s2_.SetRecv(kSrtpAes128CmSha1_80, kTestKey2, kTestKeyLen, |
Zhi Huang | c99b6c7 | 2017-11-10 16:44:46 -0800 | [diff] [blame] | 101 | kEncryptedHeaderExtensionIds)); |
zstein | 4dde3df | 2017-07-07 14:26:25 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | // Test that we fail keys of the wrong length. |
| 105 | TEST_F(SrtpSessionTest, TestKeysTooShort) { |
Mirko Bonadei | 7750d80 | 2021-07-26 17:27:42 +0200 | [diff] [blame] | 106 | EXPECT_FALSE(s1_.SetSend(kSrtpAes128CmSha1_80, kTestKey1, 1, |
Zhi Huang | c99b6c7 | 2017-11-10 16:44:46 -0800 | [diff] [blame] | 107 | kEncryptedHeaderExtensionIds)); |
Mirko Bonadei | 7750d80 | 2021-07-26 17:27:42 +0200 | [diff] [blame] | 108 | EXPECT_FALSE(s2_.SetRecv(kSrtpAes128CmSha1_80, kTestKey1, 1, |
Zhi Huang | c99b6c7 | 2017-11-10 16:44:46 -0800 | [diff] [blame] | 109 | kEncryptedHeaderExtensionIds)); |
zstein | 4dde3df | 2017-07-07 14:26:25 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | // Test that we can encrypt and decrypt RTP/RTCP using AES_CM_128_HMAC_SHA1_80. |
| 113 | TEST_F(SrtpSessionTest, TestProtect_AES_CM_128_HMAC_SHA1_80) { |
Mirko Bonadei | 7750d80 | 2021-07-26 17:27:42 +0200 | [diff] [blame] | 114 | EXPECT_TRUE(s1_.SetSend(kSrtpAes128CmSha1_80, kTestKey1, kTestKeyLen, |
Zhi Huang | c99b6c7 | 2017-11-10 16:44:46 -0800 | [diff] [blame] | 115 | kEncryptedHeaderExtensionIds)); |
Mirko Bonadei | 7750d80 | 2021-07-26 17:27:42 +0200 | [diff] [blame] | 116 | EXPECT_TRUE(s2_.SetRecv(kSrtpAes128CmSha1_80, kTestKey1, kTestKeyLen, |
Zhi Huang | c99b6c7 | 2017-11-10 16:44:46 -0800 | [diff] [blame] | 117 | kEncryptedHeaderExtensionIds)); |
Mirko Bonadei | 7750d80 | 2021-07-26 17:27:42 +0200 | [diff] [blame] | 118 | TestProtectRtp(kCsAesCm128HmacSha1_80); |
| 119 | TestProtectRtcp(kCsAesCm128HmacSha1_80); |
| 120 | TestUnprotectRtp(kCsAesCm128HmacSha1_80); |
| 121 | TestUnprotectRtcp(kCsAesCm128HmacSha1_80); |
zstein | 4dde3df | 2017-07-07 14:26:25 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | // Test that we can encrypt and decrypt RTP/RTCP using AES_CM_128_HMAC_SHA1_32. |
| 125 | TEST_F(SrtpSessionTest, TestProtect_AES_CM_128_HMAC_SHA1_32) { |
Mirko Bonadei | 7750d80 | 2021-07-26 17:27:42 +0200 | [diff] [blame] | 126 | EXPECT_TRUE(s1_.SetSend(kSrtpAes128CmSha1_32, kTestKey1, kTestKeyLen, |
Zhi Huang | c99b6c7 | 2017-11-10 16:44:46 -0800 | [diff] [blame] | 127 | kEncryptedHeaderExtensionIds)); |
Mirko Bonadei | 7750d80 | 2021-07-26 17:27:42 +0200 | [diff] [blame] | 128 | EXPECT_TRUE(s2_.SetRecv(kSrtpAes128CmSha1_32, kTestKey1, kTestKeyLen, |
Zhi Huang | c99b6c7 | 2017-11-10 16:44:46 -0800 | [diff] [blame] | 129 | kEncryptedHeaderExtensionIds)); |
Mirko Bonadei | 7750d80 | 2021-07-26 17:27:42 +0200 | [diff] [blame] | 130 | TestProtectRtp(kCsAesCm128HmacSha1_32); |
| 131 | TestProtectRtcp(kCsAesCm128HmacSha1_32); |
| 132 | TestUnprotectRtp(kCsAesCm128HmacSha1_32); |
| 133 | TestUnprotectRtcp(kCsAesCm128HmacSha1_32); |
zstein | 4dde3df | 2017-07-07 14:26:25 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | TEST_F(SrtpSessionTest, TestGetSendStreamPacketIndex) { |
Mirko Bonadei | 7750d80 | 2021-07-26 17:27:42 +0200 | [diff] [blame] | 137 | EXPECT_TRUE(s1_.SetSend(kSrtpAes128CmSha1_32, kTestKey1, kTestKeyLen, |
Zhi Huang | c99b6c7 | 2017-11-10 16:44:46 -0800 | [diff] [blame] | 138 | kEncryptedHeaderExtensionIds)); |
zstein | 4dde3df | 2017-07-07 14:26:25 -0700 | [diff] [blame] | 139 | 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 Titov | 880fa81 | 2021-07-30 22:30:23 +0200 | [diff] [blame] | 143 | // `index` will be shifted by 16. |
zstein | 4dde3df | 2017-07-07 14:26:25 -0700 | [diff] [blame] | 144 | 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. |
| 149 | TEST_F(SrtpSessionTest, TestTamperReject) { |
| 150 | int out_len; |
Mirko Bonadei | 7750d80 | 2021-07-26 17:27:42 +0200 | [diff] [blame] | 151 | EXPECT_TRUE(s1_.SetSend(kSrtpAes128CmSha1_80, kTestKey1, kTestKeyLen, |
Zhi Huang | c99b6c7 | 2017-11-10 16:44:46 -0800 | [diff] [blame] | 152 | kEncryptedHeaderExtensionIds)); |
Mirko Bonadei | 7750d80 | 2021-07-26 17:27:42 +0200 | [diff] [blame] | 153 | EXPECT_TRUE(s2_.SetRecv(kSrtpAes128CmSha1_80, kTestKey1, kTestKeyLen, |
Zhi Huang | c99b6c7 | 2017-11-10 16:44:46 -0800 | [diff] [blame] | 154 | kEncryptedHeaderExtensionIds)); |
Mirko Bonadei | 7750d80 | 2021-07-26 17:27:42 +0200 | [diff] [blame] | 155 | TestProtectRtp(kCsAesCm128HmacSha1_80); |
| 156 | TestProtectRtcp(kCsAesCm128HmacSha1_80); |
zstein | 4dde3df | 2017-07-07 14:26:25 -0700 | [diff] [blame] | 157 | rtp_packet_[0] = 0x12; |
| 158 | rtcp_packet_[1] = 0x34; |
| 159 | EXPECT_FALSE(s2_.UnprotectRtp(rtp_packet_, rtp_len_, &out_len)); |
Ying Wang | ef3998f | 2019-12-09 13:06:53 +0100 | [diff] [blame] | 160 | EXPECT_METRIC_THAT( |
Steve Anton | b443dfe | 2019-03-05 14:09:49 -0800 | [diff] [blame] | 161 | webrtc::metrics::Samples("WebRTC.PeerConnection.SrtpUnprotectError"), |
| 162 | ElementsAre(Pair(srtp_err_status_bad_param, 1))); |
zstein | 4dde3df | 2017-07-07 14:26:25 -0700 | [diff] [blame] | 163 | EXPECT_FALSE(s2_.UnprotectRtcp(rtcp_packet_, rtcp_len_, &out_len)); |
Ying Wang | ef3998f | 2019-12-09 13:06:53 +0100 | [diff] [blame] | 164 | EXPECT_METRIC_THAT( |
Steve Anton | b443dfe | 2019-03-05 14:09:49 -0800 | [diff] [blame] | 165 | webrtc::metrics::Samples("WebRTC.PeerConnection.SrtcpUnprotectError"), |
| 166 | ElementsAre(Pair(srtp_err_status_auth_fail, 1))); |
zstein | 4dde3df | 2017-07-07 14:26:25 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | // Test that we fail to unprotect if the payloads are not authenticated. |
| 170 | TEST_F(SrtpSessionTest, TestUnencryptReject) { |
| 171 | int out_len; |
Mirko Bonadei | 7750d80 | 2021-07-26 17:27:42 +0200 | [diff] [blame] | 172 | EXPECT_TRUE(s1_.SetSend(kSrtpAes128CmSha1_80, kTestKey1, kTestKeyLen, |
Zhi Huang | c99b6c7 | 2017-11-10 16:44:46 -0800 | [diff] [blame] | 173 | kEncryptedHeaderExtensionIds)); |
Mirko Bonadei | 7750d80 | 2021-07-26 17:27:42 +0200 | [diff] [blame] | 174 | EXPECT_TRUE(s2_.SetRecv(kSrtpAes128CmSha1_80, kTestKey1, kTestKeyLen, |
Zhi Huang | c99b6c7 | 2017-11-10 16:44:46 -0800 | [diff] [blame] | 175 | kEncryptedHeaderExtensionIds)); |
zstein | 4dde3df | 2017-07-07 14:26:25 -0700 | [diff] [blame] | 176 | EXPECT_FALSE(s2_.UnprotectRtp(rtp_packet_, rtp_len_, &out_len)); |
Ying Wang | ef3998f | 2019-12-09 13:06:53 +0100 | [diff] [blame] | 177 | EXPECT_METRIC_THAT( |
Steve Anton | b443dfe | 2019-03-05 14:09:49 -0800 | [diff] [blame] | 178 | webrtc::metrics::Samples("WebRTC.PeerConnection.SrtpUnprotectError"), |
| 179 | ElementsAre(Pair(srtp_err_status_auth_fail, 1))); |
zstein | 4dde3df | 2017-07-07 14:26:25 -0700 | [diff] [blame] | 180 | EXPECT_FALSE(s2_.UnprotectRtcp(rtcp_packet_, rtcp_len_, &out_len)); |
Ying Wang | ef3998f | 2019-12-09 13:06:53 +0100 | [diff] [blame] | 181 | EXPECT_METRIC_THAT( |
Steve Anton | b443dfe | 2019-03-05 14:09:49 -0800 | [diff] [blame] | 182 | webrtc::metrics::Samples("WebRTC.PeerConnection.SrtcpUnprotectError"), |
| 183 | ElementsAre(Pair(srtp_err_status_cant_check, 1))); |
zstein | 4dde3df | 2017-07-07 14:26:25 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | // Test that we fail when using buffers that are too small. |
| 187 | TEST_F(SrtpSessionTest, TestBuffersTooSmall) { |
| 188 | int out_len; |
Mirko Bonadei | 7750d80 | 2021-07-26 17:27:42 +0200 | [diff] [blame] | 189 | EXPECT_TRUE(s1_.SetSend(kSrtpAes128CmSha1_80, kTestKey1, kTestKeyLen, |
Zhi Huang | c99b6c7 | 2017-11-10 16:44:46 -0800 | [diff] [blame] | 190 | kEncryptedHeaderExtensionIds)); |
zstein | 4dde3df | 2017-07-07 14:26:25 -0700 | [diff] [blame] | 191 | 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 | |
| 197 | TEST_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 Bonadei | 7750d80 | 2021-07-26 17:27:42 +0200 | [diff] [blame] | 204 | EXPECT_TRUE(s1_.SetSend(kSrtpAes128CmSha1_80, kTestKey1, kTestKeyLen, |
Zhi Huang | c99b6c7 | 2017-11-10 16:44:46 -0800 | [diff] [blame] | 205 | kEncryptedHeaderExtensionIds)); |
Mirko Bonadei | 7750d80 | 2021-07-26 17:27:42 +0200 | [diff] [blame] | 206 | EXPECT_TRUE(s2_.SetRecv(kSrtpAes128CmSha1_80, kTestKey1, kTestKeyLen, |
Zhi Huang | c99b6c7 | 2017-11-10 16:44:46 -0800 | [diff] [blame] | 207 | kEncryptedHeaderExtensionIds)); |
zstein | 4dde3df | 2017-07-07 14:26:25 -0700 | [diff] [blame] | 208 | |
| 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 |