Files
unshackle/unshackle/core/drm/playready.py
T

346 lines
14 KiB
Python

from __future__ import annotations
import base64
import re
from collections.abc import Callable
from threading import Lock
from typing import Any
from uuid import UUID
import click
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 unshackle.core.cdm.http_cdm import HttpCdm
from unshackle.core.drm.cenc import PLAYREADY_ID, CencDrm, as_kid
from unshackle.core.remote_protocol import APIErrorCode
from unshackle.core.utilities import call_licence, log_event
class PlayReady(CencDrm):
"""PlayReady DRM System."""
SYSTEM_ID = PLAYREADY_ID
NAME = "PlayReady"
_ABSORB_LOCK = Lock()
def __init__(
self,
pssh: PSSH,
kid: UUID | str | bytes | None = None,
pssh_b64: str | None = 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}")
self._kid = as_kid(kid)
kids = [k for header in pssh.wrm_headers for k in self.header_kids(header)]
kids += [k for header in pssh.wrm_headers for k in self.custom_attribute_kids(header)]
if self._kid:
kids.append(self._kid)
self._pssh = pssh
self._kids = list(dict.fromkeys(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)
@classmethod
def from_pssh_b64(cls, pssh_b64: str, kid: UUID | None = 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_pssh_box(cls, box: Container, kid: UUID | None) -> PlayReady:
"""Make the DRM from a parsed PlayReady pssh box and the track's own KID."""
pssh_bytes = Box.build(box)
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) -> str | None:
return self.data.get("pssh_b64")
@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."""
return [signed_key_id.value for signed_key_id in header.key_ids]
@staticmethod
def custom_attribute_kids(header: WRMHeader) -> list[UUID]:
"""Return the KIDs a WRM header lists under CUSTOMATTRIBUTES/KIDS, skipping a value that is not a KID."""
if header.custom_attributes is None:
return []
kids: list[UUID] = []
for element in header.custom_attributes.findall("KIDS/KID"):
try:
kids.append(UUID(bytes_le=base64.b64decode(element.get("VALUE", "") + "==")))
except ValueError:
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[UUID, str]:
"""Map each licence key to its hex content key: pyplayready names the KID key_id, pywidevine (HttpCdm) kid."""
return {(key.key_id if hasattr(key, "key_id") else key.kid): key.key.hex() for key in cdm.get_keys(session_id)}
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: str | None,
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 isinstance(cdm, HttpCdm):
if pssh_b64:
cdm.set_pssh_b64(pssh_b64, session_id=session_id)
cdm.set_required_kids(required_kids, session_id=session_id)
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
try:
cdm.parse_license(session_id, license_str)
except Exception as e:
if self.detect_revocation(license_str) or self.detect_revocation(str(e)):
raise
kids = ", ".join(kid.hex for kid in required_kids)
raise PlayReady.Exceptions.EmptyLicense(
f"No usable PlayReady licence for KID {kids}: {e}"
) from e
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) -> str | None:
"""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)
return None
class Exceptions:
class PSSHNotFound(Exception):
error_code = APIErrorCode.DRM_ERROR
class KIDNotFound(Exception):
error_code = APIErrorCode.DRM_ERROR
class CEKNotFound(click.ClickException):
error_code = APIErrorCode.DRM_ERROR
class EmptyLicense(click.ClickException):
error_code = APIErrorCode.DRM_ERROR
class DeviceRevoked(Exception):
error_code = APIErrorCode.DRM_ERROR
__all__ = ("PlayReady",)