fix(dash): license a track whose only key system is the W3C common PSSH as ClearKey

This commit is contained in:
imSp4rky
2026-11-28 14:29:36 -07:00
parent b5b34cea02
commit 5f675bd5ff
7 changed files with 222 additions and 60 deletions
+70 -5
View File
@@ -3,6 +3,7 @@
Covers the three seams of the native ClearKey flow:
- ``DASH.get_drm`` emitting a ``ClearKeyCENC`` from a clearkey ContentProtection
element (KID from own attrs or sibling mp4protection, Laurl namespace variants)
- ``DASH.get_common_clearkey`` reading a W3C common PSSH ContentProtection
- ``ClearKeyCENC.get_content_keys`` building the W3C JSON license request and
parsing the JWK Set response (dict/str/bytes, unpadded base64url)
- ``to_dict`` / ``drm_from_dict`` round-trip for the --export/import path
@@ -19,16 +20,21 @@ import pytest
# lxml.etree: XML parser used to build ContentProtection fixtures for DASH.get_drm
from lxml import etree
from pymp4.parser import Box
from pywidevine.cdm import Cdm as WidevineCdm
from unshackle.core.drm.cenc import COMMON_ID
from unshackle.core.drm.clearkey_cenc import ClearKeyCENC
from unshackle.core.manifests.dash import DASH
from unshackle.core.wire import drm_from_dict
KID = UUID("9eb4050d-e44b-4802-932e-27d75083e266")
OTHER_KID = UUID("2c2d0a35-8f2a-4b1d-9c6e-5a7b8c9d0e1f")
KEY = bytes.fromhex("ccd0064c43f7e9fcbaa9b12af3fd1f40")
LAURL = "https://license.example.test/clearkey"
CLEARKEY_URN = "urn:uuid:e2719d58-a985-b3c9-781a-b030af78d30e"
COMMON_URN = "urn:uuid:1077efec-c0b2-4d02-ace3-3c1e52e2fb4b"
CENC_NS = "urn:mpeg:cenc:2013"
@@ -47,6 +53,13 @@ def protection(xml: str) -> etree._Element:
return etree.fromstring(xml.encode("utf8"))
def mp4protection(kid: str) -> etree._Element:
return protection(
f'<ContentProtection xmlns:cenc="{CENC_NS}" '
f'schemeIdUri="urn:mpeg:dash:mp4protection:2011" value="cenc" cenc:default_KID="{kid}"/>'
)
class StubLicence:
"""Callable license stub that records the challenge it was given."""
@@ -122,12 +135,8 @@ def test_get_drm_clearkey_laurl_variants(laurl_xml: str) -> None:
def test_get_drm_clearkey_kid_from_sibling_mp4protection() -> None:
# Canonical DASH-IF shape: default_KID on the mp4protection element only.
clearkey = protection(f'<ContentProtection schemeIdUri="{CLEARKEY_URN}" value="ClearKey1.0"/>')
mp4protection = protection(
f'<ContentProtection xmlns:cenc="{CENC_NS}" '
f'schemeIdUri="urn:mpeg:dash:mp4protection:2011" value="cenc" cenc:default_KID="{KID}"/>'
)
drm = DASH.get_drm([mp4protection, clearkey])
drm = DASH.get_drm([mp4protection(str(KID)), clearkey])
assert len(drm) == 1
assert isinstance(drm[0], ClearKeyCENC)
@@ -140,6 +149,62 @@ def test_get_drm_clearkey_without_any_kid_is_skipped() -> None:
assert DASH.get_drm([elem]) == []
def common_pssh(*kids: UUID, system_id: UUID = COMMON_ID, version: int = 1) -> str:
box = Box.build(
dict(type=b"pssh", version=version, flags=0, system_ID=system_id, key_IDs=list(kids), init_data=b"")
)
return base64.b64encode(box).decode("ascii")
def common_protection(pssh_b64: str, attrs: str = "") -> etree._Element:
return protection(
f'<ContentProtection xmlns:cenc="{CENC_NS}" schemeIdUri="{COMMON_URN}" {attrs}>'
f"<cenc:pssh>{pssh_b64}</cenc:pssh></ContentProtection>"
)
@pytest.mark.parametrize(
("sibling_kid", "pssh_b64"),
[
(str(KID), common_pssh(OTHER_KID)),
(str(KID), common_pssh(version=0)),
(base64.b64encode(KID.bytes).decode("ascii"), common_pssh()),
],
ids=["over-the-box-kid", "v0-box", "base64-kid"],
)
def test_common_clearkey_takes_the_sibling_default_kid(sibling_kid: str, pssh_b64: str) -> None:
clearkey = DASH.get_common_clearkey([mp4protection(sibling_kid), common_protection(pssh_b64)])
assert clearkey is not None
assert clearkey.kids == [KID]
def test_common_clearkey_without_default_kid_takes_the_box_kids() -> None:
clearkey = DASH.get_common_clearkey([common_protection(common_pssh(KID, UUID(int=0), OTHER_KID))])
assert clearkey is not None
assert clearkey.kids == [KID, OTHER_KID]
@pytest.mark.parametrize(
"pssh_b64",
[
common_pssh(version=0),
common_pssh(UUID(int=0)),
common_pssh(KID, system_id=WidevineCdm.uuid),
"not base64!",
"",
],
ids=["v0-box", "placeholder-kid", "other-system", "garbage", "empty"],
)
def test_common_clearkey_with_no_kid_is_none(pssh_b64: str) -> None:
assert DASH.get_common_clearkey([common_protection(pssh_b64)]) is None
def test_get_drm_leaves_a_common_pssh_to_the_caller() -> None:
assert DASH.get_drm([mp4protection(str(KID)), common_protection(common_pssh(KID))]) == []
@pytest.mark.parametrize(
"shape",
["dict", "str", "bytes"],