Skip to content

Commit

Permalink
Fix key frame (#394)
Browse files Browse the repository at this point in the history
* fix 1st pkt decryption bug
  • Loading branch information
olofkallander authored Dec 13, 2024
1 parent 8ac44a3 commit b6af4db
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
27 changes: 27 additions & 0 deletions test/transport/SrtpTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
Expand All @@ -594,6 +620,7 @@ TEST_F(SrtpTest, losePacketsBeforeRoc1)
EXPECT_EQ(roc, 1);
unprotectedExtSeqNo = i;
++unprotectCount;
ASSERT_TRUE(packet->getLength() == audioPacketLength);
}
}
EXPECT_EQ(unprotectedExtSeqNo, 65600);
Expand Down
4 changes: 3 additions & 1 deletion transport/dtls/SrtpClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<size_t>(bufferLength));
return true;
}

Expand All @@ -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};
Expand Down Expand Up @@ -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<size_t>(bufferLength));
return true;
}
}
Expand Down

0 comments on commit b6af4db

Please sign in to comment.