diff --git a/test/transport/SrtpTest.cpp b/test/transport/SrtpTest.cpp index d42ca7b7..95cc9491 100644 --- a/test/transport/SrtpTest.cpp +++ b/test/transport/SrtpTest.cpp @@ -555,6 +555,31 @@ TEST_F(SrtpTest, muteUntilRoc1) } } +// sequence number starts close to 65535 and all packets with roc = 0 are lost. +// First packet seen is on ROC 1 already +TEST_F(SrtpTest, unprotect1st) +{ + setupDtls(); + connect(); + + uint32_t roc = 0; + + auto packet = memory::makeUniquePacket(_allocator, _audioPacket); + auto header = rtp::RtpHeader::fromPacket(*packet); + header->ssrc = 1; + header->sequenceNumber = 670; + header->timestamp = 160; + const auto audioPacketLength = packet->getLength(); + const auto packetCopy = memory::makeUniquePacket(_allocator, *packet); + + ASSERT_TRUE(_srtp1->protect(*packet)); + + ASSERT_TRUE(_srtp2->unprotectFirstRtp(*packet, roc)); + EXPECT_EQ(roc, 0); + ASSERT_TRUE(packet->getLength() == audioPacketLength); + EXPECT_EQ(0, std::memcmp(packet->get(), packetCopy->get(), packetCopy->getLength())); +} + // sequence number starts close to 65535 and all packets with roc = 0 are lost. // First packet seen is on ROC 1 already TEST_F(SrtpTest, losePacketsBeforeRoc1) @@ -573,6 +598,7 @@ TEST_F(SrtpTest, losePacketsBeforeRoc1) header->ssrc = 1; header->sequenceNumber = i & 0xFFFFu; header->timestamp = i * 160; + const auto audioPacketLength = packet->getLength(); ASSERT_TRUE(_srtp1->protect(*packet)); const uint32_t continuationPoint = 65550; @@ -594,6 +620,7 @@ TEST_F(SrtpTest, losePacketsBeforeRoc1) EXPECT_EQ(roc, 1); unprotectedExtSeqNo = i; ++unprotectCount; + ASSERT_TRUE(packet->getLength() == audioPacketLength); } } EXPECT_EQ(unprotectedExtSeqNo, 65600); diff --git a/transport/dtls/SrtpClient.cpp b/transport/dtls/SrtpClient.cpp index 0c5bfa8a..587cd62d 100644 --- a/transport/dtls/SrtpClient.cpp +++ b/transport/dtls/SrtpClient.cpp @@ -927,6 +927,7 @@ bool SrtpClient::unprotectFirstRtp(memory::Packet& packet, uint32_t& rolloverCou const auto result = srtp_unprotect(_remoteSrtp, packet.get(), &bufferLength); if (result == srtp_err_status_ok) { + packet.setLength(utils::checkedCast(bufferLength)); return true; } @@ -936,8 +937,8 @@ bool SrtpClient::unprotectFirstRtp(memory::Packet& packet, uint32_t& rolloverCou assert(false); return false; } - const uint32_t ssrc = header->ssrc; + const uint32_t ssrc = header->ssrc; { // use remote srtp context to fake a packet with ROC=0 alignas(sizeof(uint32_t)) uint8_t fakePacketRoc0[40]{0}; @@ -972,6 +973,7 @@ bool SrtpClient::unprotectFirstRtp(memory::Packet& packet, uint32_t& rolloverCou if (srtp_err_status_ok == srtp_unprotect(_remoteSrtp, packet.get(), &bufferLength)) { + packet.setLength(utils::checkedCast(bufferLength)); return true; } }