101 lines
3.9 KiB
Python
101 lines
3.9 KiB
Python
"""Exercise plaintext pacing followed by real SRTP authentication/decryption."""
|
|
|
|
import asyncio
|
|
|
|
import pylibsrtp
|
|
import pytest
|
|
from aiortc.rtcrtpparameters import RTCRtpHeaderExtensionParameters, RTCRtpSendParameters
|
|
from aiortc.rtp import RtpPacket
|
|
from missioncore_plugin_sdk.link_budget import BudgetProtocolError, budget_peer
|
|
from test_link_budget import Client, Peer
|
|
|
|
|
|
def test_actual_srtp_timestamp_is_after_admission_and_payload_is_unchanged(monkeypatch):
|
|
from missioncore_plugin_sdk import rtp_send_clock
|
|
|
|
async def run():
|
|
now = [100.0]
|
|
monkeypatch.setattr(rtp_send_clock.clock, "current_ntp_time", lambda: int(now[0] * 2**32))
|
|
pc = Peer()
|
|
sender = pc.sender
|
|
mapping = sender._RTCRtpSender__rtp_header_extensions_map
|
|
mapping.configure(RTCRtpSendParameters(headerExtensions=[
|
|
RTCRtpHeaderExtensionParameters(3, "http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time"),
|
|
RTCRtpHeaderExtensionParameters(1, "urn:ietf:params:rtp-hdrext:sdes:mid"),
|
|
]))
|
|
key = bytes(range(30))
|
|
tx = pylibsrtp.Session(pylibsrtp.Policy(
|
|
key=key, ssrc_type=pylibsrtp.Policy.SSRC_ANY_OUTBOUND))
|
|
rx = pylibsrtp.Session(pylibsrtp.Policy(
|
|
key=key, ssrc_type=pylibsrtp.Policy.SSRC_ANY_INBOUND))
|
|
encrypted_at = []
|
|
|
|
async def encrypt(data):
|
|
encrypted_at.append(now[0])
|
|
await pc.ice._send(tx.protect(data))
|
|
|
|
pc.dtls._send_rtp = encrypt
|
|
|
|
class Delayed(Client):
|
|
async def acquire(self, size, *, deadline=None):
|
|
self.sizes.append(size)
|
|
now[0] += .05
|
|
|
|
client = Delayed()
|
|
budget_peer(pc, "video", client=client, clock=lambda: now[0])
|
|
packet = RtpPacket(payload_type=96, marker=1, sequence_number=123, timestamp=45000,
|
|
ssrc=sender._ssrc, payload=b"synthetic H264 fragment")
|
|
packet.extensions.mid = "0"
|
|
packet.extensions.abs_send_time = (int(now[0] * 2**32) >> 14) & 0xFFFFFF
|
|
raw = packet.serialize(mapping)
|
|
try:
|
|
await pc.dtls._send_rtp(raw)
|
|
assert len(pc.sent) == 1 and encrypted_at == [100.05]
|
|
received = RtpPacket.parse(rx.unprotect(pc.sent[0]), mapping)
|
|
assert received.payload == packet.payload
|
|
assert (received.sequence_number, received.timestamp) == (123, 45000)
|
|
assert (received.ssrc, received.marker) == (sender._ssrc, 1)
|
|
assert received.extensions.mid == "0"
|
|
assert received.extensions.abs_send_time == (int(100.05 * 2**32) >> 14) & 0xFFFFFF
|
|
assert client.sizes == [len(raw) + 16]
|
|
metrics = pc._missioncore_egress_metrics
|
|
assert metrics["sent_bytes"] == len(pc.sent[0])
|
|
assert metrics["send_clock_updates"] == 1
|
|
assert 49.9 < metrics["max_send_clock_correction_ms"] < 50.1
|
|
finally:
|
|
await pc.close()
|
|
|
|
asyncio.run(run())
|
|
|
|
|
|
def test_direct_srtp_cannot_bypass_admission():
|
|
async def run():
|
|
pc, client = Peer(), Client()
|
|
budget_peer(pc, "video", client=client)
|
|
try:
|
|
with pytest.raises(BudgetProtocolError):
|
|
await pc.ice._send(b"\x80\x60" + b"x" * 20)
|
|
assert not pc.sent and not client.sizes
|
|
finally:
|
|
await pc.close()
|
|
asyncio.run(run())
|
|
|
|
|
|
def test_expiry_during_admission_prevents_encryption():
|
|
async def run():
|
|
now = [10.0]
|
|
pc = Peer()
|
|
|
|
class Late(Client):
|
|
async def acquire(self, size, *, deadline=None):
|
|
now[0] += .6
|
|
|
|
budget_peer(pc, "video", client=Late(), clock=lambda: now[0])
|
|
try:
|
|
await pc.send(b"\x80\x60\x00\x01" + (1).to_bytes(4, "big") + b"ssrc" + b"x" * 100)
|
|
assert not pc.sent
|
|
assert pc._missioncore_egress_metrics["expired_frames"] == 1
|
|
finally:
|
|
await pc.close()
|
|
asyncio.run(run())
|