fix(drm): give the zero-KID fallback only the track's own key

mp4decrypt and shaka use the last key for a repeated KID.
This commit is contained in:
imSp4rky
2026-09-23 14:27:30 -06:00
parent f470eed703
commit 47f30668ee
5 changed files with 228 additions and 58 deletions
+5 -1
View File
@@ -34,7 +34,11 @@ def drm_from_dict(data: dict[str, Any]) -> Union[Widevine, PlayReady, ClearKeyCE
elif system == "Widevine":
from pywidevine.pssh import PSSH as WidevinePSSH
drm = Widevine(pssh=WidevinePSSH(pssh_b64), kid=kids[0] if kids else None)
wv_pssh = WidevinePSSH(pssh_b64)
# kids repeats the PSSH KIDs, so kids[0] is not the track's own KID; only a PSSH
# without KIDs needs it
drm = Widevine(pssh=wv_pssh, kid=kids[0] if kids and not wv_pssh.key_ids else None)
else:
raise ValueError(f"Unsupported DRM system for reconstruction: {system!r}")
+62
View File
@@ -0,0 +1,62 @@
"""Decrypter key arguments shared by the Widevine and PlayReady DRM systems."""
from __future__ import annotations
from typing import Any, Iterable, Optional
ZERO_KID = "00" * 16
def _hex(value: Any) -> str:
"""Return a KID or content key as lowercase hex without dashes."""
if isinstance(value, bytes):
return value.hex()
if hasattr(value, "hex") and not isinstance(value, str):
return str(value.hex)
return str(value).replace("-", "").lower()
def zero_kid_key(content_keys: dict[Any, Any], own_kids: Iterable[Any]) -> Optional[str]:
"""Return the content key to give the all-zero KID, or None when no content key is safe.
Some tracks carry an all-zero default KID in the init segment and signal the real KID
out of band. The decrypter then needs the track's content key under the zero KID as well.
A decrypter applies the last content key given for a repeated KID. Thus only one content
key can go under the zero KID, and it must be the content key of the track:
- The DRM holds exactly one content key: that content key.
- The DRM holds several content keys: the content key of the one own KID that has one.
- Otherwise: None. A missing content key makes the decrypt fail loudly, but an incorrect
content key gives output that does not decode, and the verify step then flags good
content keys as bad.
"""
keys = {_hex(kid): _hex(key) for kid, key in content_keys.items()}
if ZERO_KID in keys or not keys:
return None
if len(keys) == 1:
return next(iter(keys.values()))
matches = {_hex(kid) for kid in own_kids} & keys.keys()
if len(matches) == 1:
return keys[matches.pop()]
return None
def mp4decrypt_key_args(content_keys: dict[Any, Any], own_kids: Iterable[Any]) -> list[str]:
"""Build the mp4decrypt --key arguments: every content key, then the zero-KID fallback."""
key_args: list[str] = []
for kid, key in content_keys.items():
key_args.extend(["--key", f"{_hex(kid)}:{_hex(key)}"])
fallback = zero_kid_key(content_keys, own_kids)
if fallback:
key_args.extend(["--key", f"{ZERO_KID}:{fallback}"])
return key_args
def shaka_keys(content_keys: dict[Any, Any], own_kids: Iterable[Any]) -> str:
"""Build the shaka-packager --keys value: every content key, then the zero-KID fallback."""
entries = [f"label={i}:key_id={_hex(kid)}:key={_hex(key)}" for i, (kid, key) in enumerate(content_keys.items())]
fallback = zero_kid_key(content_keys, own_kids)
if fallback:
entries.append(f"label={len(entries)}:key_id={ZERO_KID}:key={fallback}")
return ",".join(entries)
+16 -28
View File
@@ -24,6 +24,7 @@ from unshackle.core import binaries
from unshackle.core.config import config
from unshackle.core.console import console
from unshackle.core.constants import AnyTrack
from unshackle.core.drm import key_args as drm_key_args
from unshackle.core.utilities import get_boxes, log_event
from unshackle.core.utils.subprocess import ffprobe
@@ -55,6 +56,7 @@ class PlayReady:
if extra_kid not in kids:
kids.append(extra_kid)
self._kid: Optional[UUID] = None
if kid:
if isinstance(kid, str):
kid = UUID(hex=kid)
@@ -62,6 +64,7 @@ class PlayReady:
kid = UUID(bytes=kid)
if not isinstance(kid, UUID):
raise ValueError(f"Expected kid to be a {UUID}, str, or bytes, not {kid!r}")
self._kid = kid
if kid not in kids:
kids.append(kid)
@@ -563,23 +566,19 @@ class PlayReady:
output_size=path.stat().st_size if path.exists() else 0,
)
def mp4decrypt_key_args(self) -> list[str]:
"""Build the mp4decrypt --key arguments for every content key."""
key_args = []
for kid, key in self.content_keys.items():
kid_hex = kid.hex if hasattr(kid, "hex") else str(kid).replace("-", "")
key_hex = key if isinstance(key, str) else key.hex()
key_args.extend(["--key", f"{kid_hex}:{key_hex}"])
def own_kids(self) -> list[UUID]:
"""Return the KIDs that identify this track's own content key.
# Fallback for tracks whose tenc default_KID is all-zero and whose real
# KID is signalled out-of-band: emit a zero-KID entry per content key.
zero_kid = "00" * 16
existing_kids = {kid.hex if hasattr(kid, "hex") else str(kid).replace("-", "") for kid in self.content_keys}
if zero_kid not in existing_kids:
for key in self.content_keys.values():
key_hex = key if isinstance(key, str) else key.hex()
key_args.extend(["--key", f"{zero_kid}:{key_hex}"])
return key_args
The KID given to the constructor (from the manifest or the init segment) names the
track exactly. A PSSH can name the KIDs of other renditions too.
"""
if self._kid and self._kid in self.content_keys:
return [self._kid]
return self.kids
def mp4decrypt_key_args(self) -> list[str]:
"""Build the mp4decrypt --key arguments for every content key and the zero-KID fallback."""
return drm_key_args.mp4decrypt_key_args(self.content_keys, self.own_kids())
def decrypt_with_mp4decrypt(self, path: Path) -> None:
"""Decrypt using mp4decrypt"""
@@ -633,18 +632,7 @@ class PlayReady:
f"input={path},stream=0,output={output_path},output_format=MP4",
"--enable_raw_key_decryption",
"--keys",
",".join(
[
*[
f"label={i}:key_id={kid.hex}:key={key.lower()}"
for i, (kid, key) in enumerate(self.content_keys.items())
],
*[
f"label={i}:key_id={'00' * 16}:key={key.lower()}"
for i, (kid, key) in enumerate(self.content_keys.items(), len(self.content_keys))
],
]
),
drm_key_args.shaka_keys(self.content_keys, self.own_kids()),
"--temp_dir",
config.directories.temp,
]
+14 -29
View File
@@ -21,6 +21,7 @@ from unshackle.core import binaries
from unshackle.core.config import config
from unshackle.core.console import console
from unshackle.core.constants import AnyTrack
from unshackle.core.drm import key_args as drm_key_args
from unshackle.core.utilities import get_boxes, log_event
from unshackle.core.utils.subprocess import ffprobe
@@ -315,23 +316,19 @@ class Widevine:
output_size=path.stat().st_size if path.exists() else 0,
)
def mp4decrypt_key_args(self) -> list[str]:
"""Build the mp4decrypt --key arguments for every content key."""
key_args = []
for kid, key in self.content_keys.items():
kid_hex = kid.hex if hasattr(kid, "hex") else str(kid).replace("-", "")
key_hex = key if isinstance(key, str) else key.hex()
key_args.extend(["--key", f"{kid_hex}:{key_hex}"])
def own_kids(self) -> list[UUID]:
"""Return the KIDs that identify this track's own content key.
# Fallback for tracks whose tenc default_KID is all-zero and whose real
# KID is signalled out-of-band: emit a zero-KID entry per content key.
zero_kid = "00" * 16
existing_kids = {kid.hex if hasattr(kid, "hex") else str(kid).replace("-", "") for kid in self.content_keys}
if zero_kid not in existing_kids:
for key in self.content_keys.values():
key_hex = key if isinstance(key, str) else key.hex()
key_args.extend(["--key", f"{zero_kid}:{key_hex}"])
return key_args
The KID given to the constructor (from the manifest or the init segment) names the
track exactly. A PSSH can name the KIDs of other renditions too.
"""
if self._kid and self._kid in self.content_keys:
return [self._kid]
return self.kids
def mp4decrypt_key_args(self) -> list[str]:
"""Build the mp4decrypt --key arguments for every content key and the zero-KID fallback."""
return drm_key_args.mp4decrypt_key_args(self.content_keys, self.own_kids())
def decrypt_with_mp4decrypt(self, path: Path) -> None:
"""Decrypt using mp4decrypt"""
@@ -385,19 +382,7 @@ class Widevine:
f"input={path},stream=0,output={output_path},output_format=MP4",
"--enable_raw_key_decryption",
"--keys",
",".join(
[
*[
"label={}:key_id={}:key={}".format(i, kid.hex, key.lower())
for i, (kid, key) in enumerate(self.content_keys.items())
],
*[
# some services use a blank KID on the file, but real KID for license server
"label={}:key_id={}:key={}".format(i, "00" * 16, key.lower())
for i, (kid, key) in enumerate(self.content_keys.items(), len(self.content_keys))
],
]
),
drm_key_args.shaka_keys(self.content_keys, self.own_kids()),
"--temp_dir",
config.directories.temp,
]