Files
unshackle/unshackle/core/drm/playready.py
T
imSp4rky 31328dc109 fix(drm): retry a licence call only for an argument the service rejects
A TypeError raised inside the service no longer sends the challenge again.
2026-11-02 09:01:42 -07:00

726 lines
28 KiB
Python

from __future__ import annotations
import base64
import re
import shutil
import subprocess
import textwrap
import time
from pathlib import Path
from threading import Lock
from typing import Any, Callable, Optional, Union
from uuid import UUID
import m3u8
from construct import Container
from pymp4.parser import Box
from pyplayready.cdm import Cdm as PlayReadyCdm
from pyplayready.misc.exceptions import InvalidPssh
from pyplayready.system.pssh import PSSH
from pyplayready.system.wrmheader import WRMHeader
from rich.text import Text
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.session import AnySession, utf8_text
from unshackle.core.utilities import call_licence, get_boxes, log_event
from unshackle.core.utils import run_proxy
from unshackle.core.utils.subprocess import ffprobe
class PlayReady:
"""PlayReady DRM System."""
_ABSORB_LOCK = Lock()
def __init__(
self,
pssh: PSSH,
kid: Union[UUID, str, bytes, None] = None,
pssh_b64: Optional[str] = None,
**kwargs: Any,
) -> None:
if not pssh:
raise ValueError("Provided PSSH is empty.")
if not isinstance(pssh, PSSH):
raise TypeError(f"Expected pssh to be a {PSSH}, not {pssh!r}")
kids: list[UUID] = []
for header in pssh.wrm_headers:
for header_kid in self.header_kids(header):
if header_kid not in kids:
kids.append(header_kid)
if pssh_b64:
for extra_kid in self.extract_kids_from_pssh_b64(pssh_b64):
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)
elif isinstance(kid, bytes):
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)
self._pssh = pssh
self._kids = kids
self._extra_headers: list[WRMHeader] = []
self._refused_headers: set[str] = set()
if not self.kids:
raise PlayReady.Exceptions.KIDNotFound("No Key ID was found within PSSH and none were provided.")
self.content_keys: dict[UUID, str] = {}
self.data: dict = kwargs or {}
if pssh_b64:
self.data.setdefault("pssh_b64", pssh_b64)
def extract_kids_from_pssh_b64(self, pssh_b64: str) -> list[UUID]:
"""Extract all KIDs from base64-encoded PSSH data."""
try:
# PSSH XML comes from third-party manifests; defusedxml guards against entity expansion
import defusedxml.ElementTree as ET
# Decode the PSSH
pssh_bytes = base64.b64decode(pssh_b64)
# Try to find XML in the PSSH data
pssh_str = pssh_bytes.decode("utf-16le", errors="ignore")
# Find WRMHEADER
xml_start = pssh_str.find("<WRMHEADER")
if xml_start == -1:
# Try UTF-8
pssh_str = pssh_bytes.decode("utf-8", errors="ignore")
xml_start = pssh_str.find("<WRMHEADER")
if xml_start != -1:
clean_xml = pssh_str[xml_start:]
xml_end = clean_xml.find("</WRMHEADER>") + len("</WRMHEADER>")
clean_xml = clean_xml[:xml_end]
root = ET.fromstring(clean_xml)
ns = {"pr": "http://schemas.microsoft.com/DRM/2007/03/PlayReadyHeader"}
kids = []
kid_elements = root.findall(".//pr:CUSTOMATTRIBUTES/pr:KIDS/pr:KID", ns)
for kid_elem in kid_elements:
value = kid_elem.get("VALUE")
if value:
try:
kid_bytes = base64.b64decode(value + "==")
kid_uuid = UUID(bytes_le=kid_bytes)
kids.append(kid_uuid)
except ValueError:
pass
# v4.2/v4.3: DATA/PROTECTINFO/KIDS/KID
protectinfo_kids = root.findall(".//pr:DATA/pr:PROTECTINFO/pr:KIDS/pr:KID", ns)
for kid_elem in protectinfo_kids:
value = kid_elem.get("VALUE")
if value:
try:
kid_bytes = base64.b64decode(value + "==")
kid_uuid = UUID(bytes_le=kid_bytes)
if kid_uuid not in kids:
kids.append(kid_uuid)
except ValueError:
pass
# v4.1: DATA/PROTECTINFO/KID
protectinfo_kid = root.findall(".//pr:DATA/pr:PROTECTINFO/pr:KID", ns)
for kid_elem in protectinfo_kid:
value = kid_elem.get("VALUE")
if value:
try:
kid_bytes = base64.b64decode(value + "==")
kid_uuid = UUID(bytes_le=kid_bytes)
if kid_uuid not in kids:
kids.append(kid_uuid)
except ValueError:
pass
# v4.0: DATA/KID
individual_kids = root.findall(".//pr:DATA/pr:KID", ns)
for kid_elem in individual_kids:
if kid_elem.text:
try:
kid_bytes = base64.b64decode(kid_elem.text.strip() + "==")
kid_uuid = UUID(bytes_le=kid_bytes)
if kid_uuid not in kids:
kids.append(kid_uuid)
except ValueError:
pass
return kids
# covers bad base64 (binascii.Error), defusedxml rejections (both ValueError) and XML
# ParseError (SyntaxError) from arbitrary third-party PSSH data
except (ValueError, SyntaxError):
pass
return []
@classmethod
def from_pssh_b64(cls, pssh_b64: str, kid: Optional[UUID] = None) -> PlayReady:
"""Load a base64 PSSH box or PlayReady Object, or a bare UTF-16LE WRMHEADER that some packagers emit.
It wraps a bare WRMHEADER in a PlayReady Object and keeps that as `pssh_b64`, so remote CDMs and
the REST API can parse it again.
"""
try:
pssh = PSSH(pssh_b64)
except InvalidPssh:
try:
raw = base64.b64decode(pssh_b64)
except ValueError:
raw = b""
if not raw.startswith(b"<\x00"):
raise
pssh_b64 = cls.pro_b64_for(WRMHeader(raw))
pssh = PSSH(pssh_b64)
return cls(pssh=pssh, kid=kid, pssh_b64=pssh_b64)
@classmethod
def from_track(cls, track: AnyTrack, session: Optional[AnySession] = None) -> PlayReady:
if not session:
session = run_proxy.fallback_session()
session.headers.update(config.headers)
kid: Optional[UUID] = None
pssh_boxes: list[Container] = []
tenc_boxes: list[Container] = []
if track.descriptor == track.Descriptor.HLS:
m3u_url = track.url
assert isinstance(m3u_url, str)
master = m3u8.loads(utf8_text(session.get(m3u_url)), uri=m3u_url)
pssh_boxes.extend(
Box.parse(base64.b64decode(x.uri.split(",")[-1]))
for x in (master.session_keys or master.keys)
if x
and x.keyformat
and x.keyformat.lower() in {f"urn:uuid:{PSSH.SYSTEM_ID}", "com.microsoft.playready"}
)
init_data = track.get_init_segment(session=session)
if init_data:
probe = ffprobe(init_data)
if probe:
for stream in probe.get("streams") or []:
enc_key_id = stream.get("tags", {}).get("enc_key_id")
if enc_key_id:
kid = UUID(bytes=base64.b64decode(enc_key_id))
pssh_boxes.extend(list(get_boxes(init_data, b"pssh")))
tenc_boxes.extend(list(get_boxes(init_data, b"tenc")))
pssh = next((b for b in pssh_boxes if b.system_ID == PSSH.SYSTEM_ID), None)
if not pssh:
raise PlayReady.Exceptions.PSSHNotFound("PSSH was not found in track data.")
tenc = next(iter(tenc_boxes), None)
if not kid and tenc and tenc.key_ID.int != 0:
kid = tenc.key_ID
pssh_bytes = Box.build(pssh)
return cls(pssh=PSSH(pssh_bytes), kid=kid, pssh_b64=base64.b64encode(pssh_bytes).decode())
@classmethod
def from_init_data(cls, init_data: bytes) -> PlayReady:
if not init_data:
raise ValueError("Init data should be provided.")
if not isinstance(init_data, bytes):
raise TypeError(f"Expected init data to be bytes, not {init_data!r}")
kid: Optional[UUID] = None
pssh_boxes: list[Container] = list(get_boxes(init_data, b"pssh"))
tenc_boxes: list[Container] = list(get_boxes(init_data, b"tenc"))
probe = ffprobe(init_data)
if probe:
for stream in probe.get("streams") or []:
enc_key_id = stream.get("tags", {}).get("enc_key_id")
if enc_key_id:
kid = UUID(bytes=base64.b64decode(enc_key_id))
pssh = next((b for b in pssh_boxes if b.system_ID == PSSH.SYSTEM_ID), None)
if not pssh:
raise PlayReady.Exceptions.PSSHNotFound("PSSH was not found in track data.")
tenc = next(iter(tenc_boxes), None)
if not kid and tenc and tenc.key_ID.int != 0:
kid = tenc.key_ID
pssh_bytes = Box.build(pssh)
return cls(pssh=PSSH(pssh_bytes), kid=kid, pssh_b64=base64.b64encode(pssh_bytes).decode())
@property
def pssh(self) -> PSSH:
return self._pssh
@property
def pssh_b64(self) -> Optional[str]:
return self.data.get("pssh_b64")
@property
def kid(self) -> Optional[UUID]:
return next(iter(self.kids), None)
def to_dict(self) -> dict[str, Any]:
"""Serialise this DRM instance for export/import (PSSH + KIDs).
unshackle stores the content keys once at the export's track level, and does not
duplicate them here.
"""
return {
"system": "PlayReady",
"pssh_b64": self.pssh_b64,
"kids": [kid.hex for kid in self.kids],
}
@property
def kids(self) -> list[UUID]:
return self._kids
@staticmethod
def header_kids(header: WRMHeader) -> list[UUID]:
"""Return the KIDs one WRM header names, in header order."""
kids: list[UUID] = []
for signed_key_id in getattr(header, "key_ids", []):
try:
if isinstance(signed_key_id.value, UUID):
kids.append(signed_key_id.value)
else:
kids.append(UUID(bytes_le=base64.b64decode(signed_key_id.value)))
except (ValueError, TypeError):
continue
return kids
def absorb(self, *others: PlayReady) -> None:
"""Fold sibling PlayReady objects into this one so their headers and KIDs get licensed too.
A manifest can attach one PlayReady object per WRM header to the same track
(ISM shares its Protection list across tracks; HLS emits one per EXT-X-KEY). The
caller keeps this object's identity, which the decrypt path and drm_lock depend on.
"""
with PlayReady._ABSORB_LOCK:
known = {header.dumps() for header in [*self.pssh.wrm_headers, *self._extra_headers]}
for other in others:
if other is self:
continue
for header in other.distinct_headers():
if header.dumps() not in known:
known.add(header.dumps())
self._extra_headers.append(header)
for kid in other.kids:
if kid not in self._kids:
self._kids.append(kid)
for kid, key in dict(other.content_keys).items():
self.content_keys.setdefault(kid, key)
def distinct_headers(self) -> list[WRMHeader]:
"""Return the WRM headers to license, deduplicated by the KID set each one names.
Two headers naming the same KIDs (a v4.0 header beside its v4.3 twin, or one PRH per
rendition for one content key) cost one challenge. A header with no readable KID is kept: it
may still be the only route to a content key nothing else names.
"""
headers: list[WRMHeader] = []
seen: set[frozenset[UUID]] = set()
for header in [*self.pssh.wrm_headers, *self._extra_headers]:
kid_set = frozenset(self.header_kids(header))
if kid_set and kid_set in seen:
continue
seen.add(kid_set)
headers.append(header)
return headers
@staticmethod
def pro_b64_for(header: WRMHeader) -> str:
"""Build a base64 one-record PlayReady Object holding only this header.
Remote CDMs send the value given to set_pssh_b64 rather than the header passed
to get_license_challenge, so a per-header challenge needs a per-header PRO.
"""
record = header.dumps().encode("utf-16-le")
pro = PSSH.PlayreadyHeader.build(
{"length": 10 + len(record), "records": [{"type": 1, "length": len(record), "data": record}]}
)
return base64.b64encode(pro).decode()
def extract_keys_from_cdm(self, cdm: PlayReadyCdm, session_id: bytes) -> dict:
"""Extract keys from CDM session with cross-library compatibility.
Args:
cdm: CDM instance
session_id: Session identifier
Returns:
Dictionary mapping KID UUIDs to hex keys
"""
keys = {}
for key in cdm.get_keys(session_id):
if hasattr(key, "key_id"):
kid = key.key_id
elif hasattr(key, "kid"):
kid = key.kid
else:
continue
if hasattr(key, "key") and hasattr(key.key, "hex"):
key_hex = key.key.hex()
elif hasattr(key, "key") and isinstance(key.key, bytes):
key_hex = key.key.hex()
elif hasattr(key, "key") and isinstance(key.key, str):
key_hex = key.key
else:
continue
keys[kid] = key_hex
return keys
def get_content_keys(self, cdm: PlayReadyCdm, certificate: Callable, licence: Callable) -> None:
"""License every distinct WRM header whose KIDs still lack a content key.
A v4.2+ header may list several KIDs and one response may stack several licences,
so one challenge often covers everything. Content that ships several headers (ISM
with one ProtectionHeader per track type, HLS with one PRH per playlist) gets one
challenge per header that is not yet covered. Headers are sent as-is: Microsoft lets
the server ignore the header, so a merged header could ask for keys it never grants.
A header that a round refused, or that yielded nothing, is not sent again by this
object: every track of a title runs this method, and a repeat would only repeat the
refusal.
"""
headers = self.distinct_headers()
wanted = set(self.kids)
for index, header in enumerate(headers):
header_kids = self.header_kids(header)
if header_kids and set(header_kids) <= self.content_keys.keys():
continue
if len(headers) > 1 and header.dumps() in self._refused_headers:
continue
if len(headers) == 1:
pssh_b64, required = self.pssh_b64, self.kids
else:
pssh_b64, required = self.pro_b64_for(header), header_kids or self.kids
try:
keys = self._license_header(cdm, header, pssh_b64, required, licence)
except PlayReady.Exceptions.DeviceRevoked:
raise
except Exception as e:
self._refused_headers.add(header.dumps())
if not self.content_keys and index == len(headers) - 1:
raise
log_event(
"drm_license_error",
level="WARNING",
message=f"PlayReady header {index + 1}/{len(headers)} was refused: {e}",
drm_type="PlayReady",
header_kids=[kid.hex for kid in header_kids],
)
continue
self.content_keys.update(keys)
if keys:
log_event(
"drm_content_keys",
level="INFO",
message=f"Recovered {len(keys)} PlayReady content key(s) from CDM",
drm_type="PlayReady",
key_count=len(keys),
keys=[{"kid": k.hex if hasattr(k, "hex") else str(k), "key": v} for k, v in keys.items()],
)
else:
self._refused_headers.add(header.dumps())
if not self.content_keys:
raise PlayReady.Exceptions.EmptyLicense("No Content Keys were within the License")
missing = wanted - self.content_keys.keys()
if missing:
log_event(
"drm_missing_keys",
level="WARNING",
message=f"No PlayReady key for KID(s) {', '.join(sorted(kid.hex for kid in missing))}",
drm_type="PlayReady",
missing_kids=[kid.hex for kid in missing],
)
def _license_header(
self,
cdm: PlayReadyCdm,
header: WRMHeader,
pssh_b64: Optional[str],
required_kids: list[UUID],
licence: Callable,
) -> dict[UUID, str]:
"""Run one challenge/response round for a single header and return the keys it yielded."""
session_id = cdm.open()
try:
if hasattr(cdm, "set_pssh_b64") and pssh_b64:
try:
cdm.set_pssh_b64(pssh_b64, session_id=session_id)
except TypeError: # CDM predating the per-session signature
cdm.set_pssh_b64(pssh_b64)
if hasattr(cdm, "set_required_kids"):
try:
cdm.set_required_kids(required_kids, session_id=session_id)
except TypeError: # CDM predating the per-session signature
cdm.set_required_kids(required_kids)
challenge = cdm.get_license_challenge(session_id, header)
if challenge:
license_str = ""
try:
log_event(
"drm_license_request",
level="DEBUG",
message="Requesting PlayReady license",
drm_type="PlayReady",
challenge_size=len(challenge),
kid_count=len(required_kids),
)
license_res = call_licence(licence, challenge, pssh_b64=pssh_b64, session_id=session_id)
if isinstance(license_res, bytes):
license_str = license_res.decode(errors="ignore")
else:
license_str = str(license_res)
if "<License>" not in license_str:
try:
license_str = base64.b64decode(license_str + "===").decode()
except ValueError:
pass
cdm.parse_license(session_id, license_str)
log_event(
"drm_license_response",
level="DEBUG",
message="Parsed PlayReady license",
drm_type="PlayReady",
response_size=len(license_str),
)
except PlayReady.Exceptions.DeviceRevoked:
raise
except Exception as e:
revoked = self.detect_revocation(license_str) or self.detect_revocation(str(e))
if revoked:
raise PlayReady.Exceptions.DeviceRevoked(revoked) from e
raise
return self.extract_keys_from_cdm(cdm, session_id)
finally:
cdm.close(session_id)
@staticmethod
def detect_revocation(text: str) -> Optional[str]:
"""Return the decoded error string if a revocation HRESULT is present, else None.
Reads the code from the raw SOAP body (<StatusCode>0x8004C065</StatusCode>)
or from an exception message that carries it, so this method covers any service.
"""
from unshackle.core.drm.playready_errors import describe, is_revocation
if not text:
return None
for token in re.findall(r"0x[0-9A-Fa-f]{8}|-?\d{7,}", text):
code = int(token, 16) if token.lower().startswith("0x") else int(token)
if is_revocation(code):
return describe(code) or f"0x{code & 0xFFFFFFFF:08X}"
return None
def decrypt(self, path: Path) -> None:
"""
Decrypt a Track with PlayReady DRM.
Args:
path: Path to the encrypted file to decrypt
Raises:
EnvironmentError if the required decryption executable could not be found.
ValueError if unshackle has not yet downloaded the track.
SubprocessError if the decryption process returned a non-zero exit code.
"""
if not self.content_keys:
raise ValueError("Cannot decrypt a Track without any Content Keys...")
if not path or not path.exists():
raise ValueError("Tried to decrypt a file that does not exist.")
decrypter = str(getattr(config, "decryption", "")).lower()
tool = "mp4decrypt" if decrypter == "mp4decrypt" else "shaka-packager"
log_event(
"drm_decrypt",
level="DEBUG",
message=f"Decrypting {path.name} with {tool}",
drm_type="PlayReady",
tool=tool,
file=path.name,
key_count=len(self.content_keys),
)
decrypt_start = time.monotonic()
if decrypter == "mp4decrypt":
self.decrypt_with_mp4decrypt(path)
else:
self.decrypt_with_shaka_packager(path)
log_event(
"drm_decrypt_complete",
level="DEBUG",
message=f"Decrypted {path.name} with {tool}",
drm_type="PlayReady",
tool=tool,
file=path.name,
duration_ms=round((time.monotonic() - decrypt_start) * 1000, 1),
output_size=path.stat().st_size if path.exists() else 0,
)
def own_kids(self) -> list[UUID]:
"""Return the KIDs that identify this track's own content key.
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"""
if not binaries.Mp4decrypt:
raise EnvironmentError("mp4decrypt executable not found but is required.")
output_path = path.with_stem(f"{path.stem}_decrypted")
key_args = self.mp4decrypt_key_args()
cmd = [
str(binaries.Mp4decrypt),
"--show-progress",
*key_args,
str(path),
str(output_path),
]
try:
subprocess.run(
cmd,
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
encoding="utf-8",
errors="replace",
)
except subprocess.CalledProcessError as e:
error_msg = e.stderr if e.stderr else f"mp4decrypt failed with exit code {e.returncode}"
raise subprocess.CalledProcessError(e.returncode, cmd, output=e.stdout, stderr=error_msg)
if not output_path.exists():
raise RuntimeError(f"mp4decrypt failed: output file {output_path} was not created")
if output_path.stat().st_size == 0:
raise RuntimeError(f"mp4decrypt failed: output file {output_path} is empty")
path.unlink()
shutil.move(output_path, path)
def decrypt_with_shaka_packager(self, path: Path) -> None:
"""Decrypt with shaka-packager (original method)"""
if not binaries.ShakaPackager:
raise EnvironmentError("Shaka Packager executable not found but is required.")
output_path = path.with_stem(f"{path.stem}_decrypted")
config.directories.temp.mkdir(parents=True, exist_ok=True)
try:
arguments: list[str | Path] = [
f"input={path},stream=0,output={output_path},output_format=MP4",
"--enable_raw_key_decryption",
"--keys",
drm_key_args.shaka_keys(self.content_keys, self.own_kids()),
"--temp_dir",
config.directories.temp,
]
p = subprocess.Popen(
[binaries.ShakaPackager, *arguments],
stdout=subprocess.DEVNULL,
stderr=subprocess.PIPE,
text=True,
encoding="utf-8",
errors="replace",
)
stream_skipped = False
had_error = False
shaka_log_buffer = ""
assert p.stderr is not None
for line in iter(p.stderr.readline, ""):
line = line.strip()
if not line:
continue
if "Skip stream" in line:
stream_skipped = True
if ":INFO:" in line:
continue
if "I0" in line or "W0" in line:
continue
if ":ERROR:" in line:
had_error = True
if "Insufficient bits in bitstream for given AVC profile" in line:
continue
shaka_log_buffer += f"{line.strip()}\n"
if shaka_log_buffer:
shaka_log_buffer = "\n ".join(
textwrap.wrap(shaka_log_buffer.rstrip(), width=console.width - 22, initial_indent="")
)
console.log(Text.from_ansi("\n[PlayReady]: " + shaka_log_buffer))
p.wait()
if p.returncode != 0 or had_error:
raise subprocess.CalledProcessError(p.returncode, [binaries.ShakaPackager, *arguments])
path.unlink()
if not stream_skipped:
shutil.move(output_path, path)
except subprocess.CalledProcessError as e:
if e.returncode == 0xC000013A:
raise KeyboardInterrupt()
raise
class Exceptions:
class PSSHNotFound(Exception):
pass
class KIDNotFound(Exception):
pass
class CEKNotFound(Exception):
pass
class EmptyLicense(Exception):
pass
class DeviceRevoked(Exception):
pass
__all__ = ("PlayReady",)