feat(hls): license every content key of a FairPlay bridge playlist that rotates its key

This commit is contained in:
imSp4rky
2026-12-01 18:32:36 -07:00
parent e0880812d5
commit 41c2b3d86a
12 changed files with 478 additions and 76 deletions
@@ -307,16 +307,49 @@ def test_server_cdm_typeerror_inside_the_service_is_not_retried(drm_type: str, r
assert len(calls) == 1
def _proxy(service: Any, drm_type: str) -> Any:
def _proxy(service: Any, drm_type: str, **relayed: Any) -> Any:
return handlers.handle_proxy_license(
service=service,
title=SimpleNamespace(),
track=SimpleNamespace(),
challenge_b64=base64.b64encode(b"CHAL").decode(),
drm_type=drm_type,
**relayed,
)
WV_PSSH_B64 = wv_pssh_mod.PSSH.new(system_id=wv_pssh_mod.PSSH.SystemId.Widevine, key_ids=[KID]).dumps()
RELAYED_PSSH = {"widevine": WV_PSSH_B64, "playready": PSSH_B64}
@pytest.mark.parametrize("drm_type", ["widevine", "playready"])
def test_proxy_license_relayed_pssh_skips_a_service_that_does_not_declare_it(drm_type: str) -> None:
service = ClassicService()
_proxy(service, drm_type, pssh_b64=RELAYED_PSSH[drm_type])
assert set(service.calls[0]) == {"challenge", "title", "track"}
@pytest.mark.parametrize("drm_type", ["widevine", "playready"])
def test_proxy_license_drops_a_relayed_pssh_that_an_unreadable_signature_rejects(drm_type: str) -> None:
service = ClassicService()
method = getattr(service, f"get_{drm_type}_license")
setattr(service, f"get_{drm_type}_license", lambda *a, **kw: method(*a, **kw))
assert _proxy(service, drm_type, pssh_b64=RELAYED_PSSH[drm_type]).status == 200
assert len(service.calls) == 1
def test_proxy_license_relayed_pssh_reaches_a_var_kwargs_service_as_on_the_local_path() -> None:
widevine, playready = VarKwService(), VarKwService()
_proxy(widevine, "widevine", pssh_b64=WV_PSSH_B64)
_proxy(playready, "playready", pssh_b64=PSSH_B64)
assert widevine.calls[0]["pssh"].dumps() == WV_PSSH_B64
assert playready.calls[0]["pssh_b64"] == PSSH_B64
assert "pssh_b64" not in widevine.calls[0] and "pssh" not in playready.calls[0]
# A PlayReady challenge is SOAP XML, and pyplayready returns it as str on the local path too.
@pytest.mark.parametrize("drm_type, challenge", [("widevine", b"CHAL"), ("playready", "CHAL")])
def test_proxy_license_classic_signature(drm_type: str, challenge: Any) -> None:
@@ -0,0 +1,148 @@
"""The challenge relay gives the server's licence callback the PSSH of the header the client licenses."""
from __future__ import annotations
import json
from collections.abc import Callable
from types import SimpleNamespace
from typing import Any, cast
from uuid import UUID
import pytest
from tests.core.key_resolver import resolver
from tests.core.test_playready_multi_header import _drm, _wrm_header
from tests.remote.key_source import remote_session
from tests.remote.sessions import session_entry
from unshackle.core.api import handlers, server_keys
from unshackle.core.drm import PlayReady
from unshackle.core.manifests.hls import HLS
from unshackle.core.remote_protocol import LicensingMode, Licensor
pytestmark = pytest.mark.unit
K1, K2 = UUID(int=0x11), UUID(int=0x22)
def pr_kids(pssh_b64: str | None) -> list[UUID] | None:
return PlayReady.from_pssh_b64(pssh_b64).kids if pssh_b64 else None
class FakePlayReadyCdm:
"""A CDM that licenses every KID of the header it makes the challenge for."""
def open(self) -> bytes:
return b"s"
def close(self, sid: bytes) -> None:
pass
def get_license_challenge(self, sid: bytes, header: Any) -> str:
self.kids = PlayReady.header_kids(header)
return "<Challenge/>"
def parse_license(self, sid: bytes, licence: str) -> None:
pass
def get_keys(self, sid: bytes) -> list[Any]:
return [SimpleNamespace(key_id=kid, key=bytes(16)) for kid in self.kids]
class PsshService:
"""EXAMPLE: licence callbacks that declare the PSSH argument of the local path."""
def __init__(self) -> None:
self.calls: list[dict[str, Any]] = []
def get_playready_license(self, *, challenge: str, title: Any, track: Any, pssh_b64: str | None = None) -> str:
self.calls.append({"challenge": challenge, "pssh_b64": pssh_b64})
return "<License>x</License>"
def get_widevine_license(self, *, challenge: bytes, title: Any, track: Any, pssh: Any = None) -> bytes:
self.calls.append({"challenge": challenge, "pssh": pssh})
return b"LIC"
def relay(
monkeypatch: pytest.MonkeyPatch, drm: Callable[[], list[Any]], drm_system: str = "playready"
) -> tuple[PsshService, list[Any], Any, Callable[..., Any]]:
"""The server service, the wire PSSH list, the client track and the licence callable dl hands its DRM.
The client is a RemoteService whose HTTP hop calls the real server licence handler, and each track gets ``drm()``.
"""
service = PsshService()
session = session_entry(
service_tag="EXAMPLE",
service_instance=service,
tracks={"vid": SimpleNamespace(id="vid", drm=drm())},
licensing=LicensingMode(Licensor.NO_DEVICE),
)
monkeypatch.setattr(handlers, "ensure_track_drm", lambda *a, **k: None)
monkeypatch.setattr(server_keys, "find_title_for_track", lambda tid, s: SimpleNamespace())
wire: list[Any] = []
def post(route: str, body: Any, **_: Any) -> Any:
wire.append(body.get("pssh"))
return json.loads(cast(bytes, handlers.license_response(body, session, "sess", None).body))
remote = remote_session(None, {})
remote.client = cast(Any, SimpleNamespace(post=post))
client_track = SimpleNamespace(id="vid", drm=drm())
keys = resolver()
keys.service = remote
def licence(**kw: Any) -> Any:
return keys._service_licence(drm_system, title=SimpleNamespace(), track=client_track, **kw)
return service, wire, client_track, licence
def license_all(drms: list[Any], licence: Callable[..., Any]) -> None:
for drm in drms:
drm.get_content_keys(FakePlayReadyCdm(), certificate=None, licence=licence)
@pytest.mark.parametrize(
"kids, drm",
[
([K1], lambda: [HLS.bridge_stub("playready", [K1])]),
([K1, K2], lambda: [HLS.bridge_stub("playready", [K1, K2])]),
([K1, K2], lambda: [HLS.bridge_stub("playready", [K1]), HLS.bridge_stub("playready", [K2])]),
],
ids=["one-header", "two-headers-one-object", "two-headers-two-objects"],
)
def test_playready_relay_gives_each_challenge_the_pssh_of_its_header(
monkeypatch: pytest.MonkeyPatch, kids: list[UUID], drm: Callable[[], list[Any]]
) -> None:
service, wire, client_track, licence = relay(monkeypatch, drm)
license_all(client_track.drm, licence)
assert [pr_kids(c["pssh_b64"]) for c in service.calls] == [[k] for k in kids]
assert [pr_kids(p) for p in wire] == [[k] for k in kids]
def test_widevine_relay_gives_the_callback_the_pssh(monkeypatch: pytest.MonkeyPatch) -> None:
service, wire, client_track, licence = relay(monkeypatch, lambda: [HLS.bridge_stub("widevine", [K1])], "widevine")
pssh = client_track.drm[0].pssh
licence(challenge=b"CHAL", pssh=pssh, session_id=b"s")
assert service.calls[0]["challenge"] == b"CHAL"
assert service.calls[0]["pssh"].dumps() == pssh.dumps() == wire[0]
def test_playready_relay_licenses_a_key_the_server_track_does_not_name(monkeypatch: pytest.MonkeyPatch) -> None:
service, _, _, licence = relay(monkeypatch, lambda: [HLS.bridge_stub("playready", [K1])])
license_all([HLS.bridge_stub("playready", [K2])], licence)
assert [c["pssh_b64"] for c in service.calls] == [None]
def test_playready_relay_licenses_a_header_without_a_kid(monkeypatch: pytest.MonkeyPatch) -> None:
service, _, client_track, licence = relay(monkeypatch, lambda: [_drm(_wrm_header(K1), _wrm_header())])
license_all(client_track.drm, licence)
assert [pr_kids(c["pssh_b64"]) for c in service.calls] == [[K1], None]