~SrtpTransportTest() override {
if (srtp_transport1_) {
srtp_transport1_->UnregisterRtpDemuxerSink(&rtp_sink1_);
}
if (srtp_transport2_) {
srtp_transport2_->UnregisterRtpDemuxerSink(&rtp_sink2_);
}
}
void TestSendRecvRtpPacket(int crypto_suite) {
size_t rtp_len = sizeof(kPcmuFrame);
size_t packet_size = rtp_len + rtp_auth_tag_len(crypto_suite);
Buffer rtp_packet_buffer = Buffer::CreateUninitializedWithSize(packet_size);
char* rtp_packet_data = rtp_packet_buffer.data<char>();
memcpy(rtp_packet_data, kPcmuFrame, rtp_len); // In order to be able to run this test function multiple times we can not // use the same sequence number twice. Increase the sequence number by one.
SetBE16(std::span<uint8_t>(rtp_packet_buffer).subspan(2),
++sequence_number_);
CopyOnWriteBuffer rtp_packet1to2(rtp_packet_data, rtp_len, packet_size);
CopyOnWriteBuffer rtp_packet2to1(rtp_packet_data, rtp_len, packet_size);
AsyncSocketPacketOptions options; // Send a packet from `srtp_transport1_` to `srtp_transport2_` and verify // that the packet can be successfully received and decrypted.
ASSERT_TRUE(srtp_transport1_->SendRtpPacket(&rtp_packet1to2, options,
PF_SRTP_BYPASS));
ASSERT_TRUE(rtp_sink2_.last_recv_rtp_packet().data());
EXPECT_EQ(0, memcmp(rtp_sink2_.last_recv_rtp_packet().data(),
original_rtp_data, rtp_len)); // Get the encrypted packet from underneath packet transport and verify // the data is actually encrypted. auto fake_rtp_packet_transport = static_cast<FakePacketTransport*>(
srtp_transport1_->rtp_packet_transport());
EXPECT_NE(0, memcmp(fake_rtp_packet_transport->last_sent_packet()->data(),
original_rtp_data, rtp_len));
// Do the same thing in the opposite direction;
ASSERT_TRUE(srtp_transport2_->SendRtpPacket(&rtp_packet2to1, options,
PF_SRTP_BYPASS));
ASSERT_TRUE(rtp_sink1_.last_recv_rtp_packet().data());
EXPECT_EQ(0, memcmp(rtp_sink1_.last_recv_rtp_packet().data(),
original_rtp_data, rtp_len));
fake_rtp_packet_transport = static_cast<FakePacketTransport*>(
srtp_transport2_->rtp_packet_transport());
EXPECT_NE(0, memcmp(fake_rtp_packet_transport->last_sent_packet()->data(),
original_rtp_data, rtp_len));
}
AsyncSocketPacketOptions options; // Send a packet from `srtp_transport1_` to `srtp_transport2_` and verify // that the packet can be successfully received and decrypted.
ASSERT_TRUE(srtp_transport1_->SendRtcpPacket(&rtcp_packet1to2, options,
PF_SRTP_BYPASS));
ASSERT_TRUE(rtp_sink2_.last_recv_rtcp_packet().data());
EXPECT_EQ(0, memcmp(rtp_sink2_.last_recv_rtcp_packet().data(),
rtcp_packet_data, rtcp_len)); // Get the encrypted packet from underneath packet transport and verify the // data is actually encrypted. auto fake_rtp_packet_transport = static_cast<FakePacketTransport*>(
srtp_transport1_->rtp_packet_transport());
EXPECT_NE(0, memcmp(fake_rtp_packet_transport->last_sent_packet()->data(),
rtcp_packet_data, rtcp_len));
// Do the same thing in the opposite direction;
ASSERT_TRUE(srtp_transport2_->SendRtcpPacket(&rtcp_packet2to1, options,
PF_SRTP_BYPASS));
ASSERT_TRUE(rtp_sink1_.last_recv_rtcp_packet().data());
EXPECT_EQ(0, memcmp(rtp_sink1_.last_recv_rtcp_packet().data(),
rtcp_packet_data, rtcp_len));
fake_rtp_packet_transport = static_cast<FakePacketTransport*>(
srtp_transport2_->rtp_packet_transport());
EXPECT_NE(0, memcmp(fake_rtp_packet_transport->last_sent_packet()->data(),
rtcp_packet_data, rtcp_len));
}
void TestSendRecvPacketWithEncryptedHeaderExtension(
int crypto_suite, const std::vector<int>& encrypted_header_ids) {
size_t rtp_len = sizeof(kPcmuFrameWithExtensions);
size_t packet_size = rtp_len + rtp_auth_tag_len(crypto_suite);
Buffer rtp_packet_buffer = Buffer::CreateUninitializedWithSize(packet_size);
char* rtp_packet_data = rtp_packet_buffer.data<char>();
memcpy(rtp_packet_data, kPcmuFrameWithExtensions, rtp_len); // In order to be able to run this test function multiple times we can not // use the same sequence number twice. Increase the sequence number by one.
SetBE16(std::span<uint8_t>(rtp_packet_buffer).subspan(2),
++sequence_number_);
CopyOnWriteBuffer rtp_packet1to2(rtp_packet_data, rtp_len, packet_size);
CopyOnWriteBuffer rtp_packet2to1(rtp_packet_data, rtp_len, packet_size);
AsyncSocketPacketOptions options; // Send a packet from `srtp_transport1_` to `srtp_transport2_` and verify // that the packet can be successfully received and decrypted.
ASSERT_TRUE(srtp_transport1_->SendRtpPacket(&rtp_packet1to2, options,
PF_SRTP_BYPASS));
ASSERT_TRUE(rtp_sink2_.last_recv_rtp_packet().data());
EXPECT_EQ(0, memcmp(rtp_sink2_.last_recv_rtp_packet().data(),
original_rtp_data, rtp_len)); // Get the encrypted packet from underneath packet transport and verify the // data and header extension are actually encrypted. auto fake_rtp_packet_transport = static_cast<FakePacketTransport*>(
srtp_transport1_->rtp_packet_transport());
EXPECT_NE(0, memcmp(fake_rtp_packet_transport->last_sent_packet()->data(),
original_rtp_data, rtp_len));
CompareHeaderExtensions( reinterpret_cast<const char*>(
fake_rtp_packet_transport->last_sent_packet()->data()),
fake_rtp_packet_transport->last_sent_packet()->size(),
original_rtp_data, rtp_len, encrypted_header_ids, false);
// Do the same thing in the opposite direction;
ASSERT_TRUE(srtp_transport2_->SendRtpPacket(&rtp_packet2to1, options,
PF_SRTP_BYPASS));
ASSERT_TRUE(rtp_sink1_.last_recv_rtp_packet().data());
EXPECT_EQ(0, memcmp(rtp_sink1_.last_recv_rtp_packet().data(),
original_rtp_data, rtp_len));
fake_rtp_packet_transport = static_cast<FakePacketTransport*>(
srtp_transport2_->rtp_packet_transport());
EXPECT_NE(0, memcmp(fake_rtp_packet_transport->last_sent_packet()->data(),
original_rtp_data, rtp_len));
CompareHeaderExtensions( reinterpret_cast<const char*>(
fake_rtp_packet_transport->last_sent_packet()->data()),
fake_rtp_packet_transport->last_sent_packet()->size(),
original_rtp_data, rtp_len, encrypted_header_ids, false);
}
// Create a packet and try to send it three times.
size_t rtp_len = sizeof(kPcmuFrame);
size_t packet_size = rtp_len + rtp_auth_tag_len(kSrtpAeadAes128Gcm);
Buffer rtp_packet_buffer = Buffer::CreateUninitializedWithSize(packet_size);
char* rtp_packet_data = rtp_packet_buffer.data<char>();
memcpy(rtp_packet_data, kPcmuFrame, rtp_len);
// First attempt will succeed.
CopyOnWriteBuffer first_try(rtp_packet_data, rtp_len, packet_size);
EXPECT_TRUE(srtp_transport->SendRtpPacket(
&first_try, AsyncSocketPacketOptions(), PF_SRTP_BYPASS));
EXPECT_EQ(rtp_sink.rtp_count(), 1);
// Second attempt will be rejected by libSRTP as a replay attack // (srtp_err_status_replay_fail) since the sequence number was already seen. // Hence the packet never reaches the sink.
CopyOnWriteBuffer second_try(rtp_packet_data, rtp_len, packet_size);
EXPECT_TRUE(srtp_transport->SendRtpPacket(
&second_try, AsyncSocketPacketOptions(), PF_SRTP_BYPASS));
EXPECT_EQ(rtp_sink.rtp_count(), 1);
// Reset the sink.
EXPECT_TRUE(srtp_transport->UnregisterRtpDemuxerSink(&rtp_sink));
EXPECT_TRUE(
srtp_transport->RegisterRtpDemuxerSink(demuxer_criteria, &rtp_sink));
// Third attempt will succeed again since libSRTP does not remember seeing // the sequence number after the reset.
CopyOnWriteBuffer third_try(rtp_packet_data, rtp_len, packet_size);
EXPECT_TRUE(srtp_transport->SendRtpPacket(
&third_try, AsyncSocketPacketOptions(), PF_SRTP_BYPASS));
EXPECT_EQ(rtp_sink.rtp_count(), 2); // Clear the sink to clean up.
srtp_transport->UnregisterRtpDemuxerSink(&rtp_sink);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.