Files
unshackle/unshackle/core/cdm/http_cdm.py
T

344 lines
14 KiB
Python

"""The shared part of the CDMs that drive a device behind an HTTP API.
``HttpCdm`` keeps the CDM sessions, the PSSH and required KIDs of each CDM session, the content keys from
the API cache, and the merge of those content keys with the licence keys. An adapter sends its own API's
requests in ``_request_challenge`` and ``_request_keys``. The key vaults are not read or written here:
content-key resolution does that.
"""
from __future__ import annotations
import base64
import secrets
from abc import ABC, abstractmethod
from collections.abc import Sequence
from typing import Any
from uuid import UUID
from pyplayready.system.wrmheader import WRMHeader
from pywidevine.cdm import Cdm as WidevineCdm
from pywidevine.device import DeviceTypes
from pywidevine.key import Key
from requests import RequestException, Session
from unshackle.core.cdm.loader import system_of
def kid_uuid(kid: str | UUID) -> UUID:
"""A KID as a UUID; zeros on the right fill a short hex KID."""
if isinstance(kid, UUID):
return kid
clean = kid.replace("-", "")
return UUID(hex=clean.ljust(32, "0"))
def kid_hex(kid: str | UUID) -> str:
"""A KID as lower-case hex without dashes, the form the required KIDs use."""
return str(kid).replace("-", "").lower()
def b64_or_utf8(text: str) -> bytes:
"""Decode base64 text; text that is not base64 goes out as its UTF-8 bytes."""
try:
return base64.b64decode(text)
except ValueError:
return text.encode("utf-8")
class MockCertificateChain:
"""Mock certificate chain for PlayReady compatibility."""
def __init__(self, name: str) -> None:
self._name = name
def get_name(self) -> str:
return self._name
class HttpCdmExceptions:
"""Exception classes for compatibility with pywidevine CDM."""
class InvalidSession(Exception):
"""Raised when session ID is invalid."""
class TooManySessions(Exception):
"""Raised when the CDM sessions reach the limit."""
class InvalidInitData(Exception):
"""Raised when PSSH/init data is invalid."""
class InvalidLicenseType(Exception):
"""Raised when license type is invalid."""
class InvalidLicenseMessage(Exception):
"""Raised when license message is invalid."""
class InvalidContext(Exception):
"""Raised when the CDM session has no context data."""
class SignatureMismatch(Exception):
"""Raised when signature verification fails."""
class HttpCdm(ABC):
"""A Widevine or PlayReady CDM whose device sits behind an HTTP API.
The DRM classes give each CDM session its PSSH (``set_pssh_b64``) and the KIDs they need
(``set_required_kids``). A challenge is empty when the API cache already holds
every required key, and ``has_cached_keys`` then says so.
"""
service_certificate_challenge = b"\x08\x04"
is_remote_cdm = True
chain_suffix = "Remote"
privacy_cert_devices: tuple[str, ...] = ("L1",)
label: str | None = None
def __init__(
self,
host: str,
device_name: str,
device_type: str | None,
service_name: str | None,
headers: dict[str, str],
) -> None:
self.host = host.rstrip("/")
self.device_name = device_name
self.service_name = service_name or ""
self._is_playready = system_of(device_type, device_name) == "playready"
if device_type:
self.device_type = self.get_device_type_enum(device_type)
self._sessions: dict[bytes, dict[str, Any]] = {}
self._http_session = Session()
self._http_session.headers.update({"Content-Type": "application/json", **headers})
def get_device_type_enum(self, device_type: str) -> DeviceTypes:
"""Convert device type string to enum for compatibility."""
return DeviceTypes.ANDROID if device_type.upper() == "ANDROID" else DeviceTypes.CHROME
@property
def is_playready(self) -> bool:
"""Return True if this CDM is in PlayReady mode."""
return self._is_playready
@property
def certificate_chain(self) -> MockCertificateChain:
"""Mock certificate chain for PlayReady compatibility."""
return MockCertificateChain(f"{self.device_name}_{self.chain_suffix}")
def set_pssh_b64(self, pssh_b64: str, session_id: bytes) -> None:
"""Store the base64 PSSH that a PlayReady CDM session sends as its init data."""
self._session(session_id)["pssh_b64"] = pssh_b64
def set_required_kids(self, kids: Sequence[str | UUID], session_id: bytes) -> None:
"""Store the KIDs a CDM session needs, so that content keys from the API cache can skip the licence."""
self._session(session_id)["required_kids"] = [kid_hex(kid) for kid in kids]
def generate_session_id(self) -> bytes:
"""Make a unique session ID."""
return secrets.token_bytes(16)
def get_init_data_from_pssh(self, pssh: Any, pssh_b64: str | None = None) -> str:
"""Extract init data from various PSSH formats.
PlayReady sends ``pssh_b64`` when it is set. Without it, a WRM header (object or XML text) goes
out as a one-record PlayReady Object, as the remote APIs take a PRO, not a bare header.
"""
if self.is_playready:
if pssh_b64:
return pssh_b64
if isinstance(pssh, str) and pssh.lstrip().startswith("<"):
pssh = WRMHeader(pssh)
if isinstance(pssh, WRMHeader):
from unshackle.core.drm.playready import PlayReady
return PlayReady.pro_b64_for(pssh)
if isinstance(pssh, str):
return pssh
if hasattr(pssh, "dumps"):
dumps_result = pssh.dumps()
if isinstance(dumps_result, str):
try:
base64.b64decode(dumps_result)
return dumps_result
except ValueError:
return base64.b64encode(dumps_result.encode("utf-8")).decode("utf-8")
else:
return base64.b64encode(dumps_result).decode("utf-8")
elif hasattr(pssh, "raw"):
raw_data = pssh.raw
if isinstance(raw_data, str):
raw_data = raw_data.encode("utf-8")
return base64.b64encode(raw_data).decode("utf-8")
else:
raise ValueError(f"Unsupported PSSH type: {type(pssh)}")
def _session(self, session_id: bytes) -> dict[str, Any]:
if session_id not in self._sessions:
raise HttpCdmExceptions.InvalidSession(f"Invalid session ID: {session_id.hex()}")
return self._sessions[session_id]
def _default_certificate(self) -> bytes | None:
"""The common privacy certificate for a Widevine device in ``privacy_cert_devices``, else None."""
if not self._is_playready and self.device_name in self.privacy_cert_devices:
return base64.b64decode(WidevineCdm.common_privacy_cert)
return None
def open(self) -> bytes:
"""Open a new CDM session and return its ID; a device in ``privacy_cert_devices`` starts with the common certificate."""
session_id = self.generate_session_id()
self._sessions[session_id] = {
"service_certificate": self._default_certificate(),
"keys": [],
"init_data": None,
"pssh_b64": None,
"challenge": None,
"remote_session_id": None,
"cached_keys": None,
}
return session_id
def close(self, session_id: bytes) -> None:
"""Close a CDM session; raises InvalidSession for an unknown ID."""
self._session(session_id)
del self._sessions[session_id]
def get_service_certificate(self, session_id: bytes) -> bytes | None:
"""The service certificate of a CDM session, or None."""
return self._session(session_id)["service_certificate"]
def set_service_certificate(self, session_id: bytes, certificate: bytes | str | None) -> str:
"""Set the service certificate (bytes or base64) of a CDM session and return a status message.
With no certificate, a Widevine device in ``privacy_cert_devices`` gets the common privacy certificate.
"""
session = self._session(session_id)
if certificate is None:
session["service_certificate"] = self._default_certificate()
return "Using the default service certificate"
if isinstance(certificate, str):
certificate = base64.b64decode(certificate)
session["service_certificate"] = certificate
return "Successfully set Service Certificate"
def has_cached_keys(self, session_id: bytes) -> bool:
"""True when the CDM session holds keys."""
return bool(self._session(session_id).get("keys"))
def get_license_challenge(
self, session_id: bytes, pssh_or_wrm: Any, license_type: str = "STREAMING", privacy_mode: bool = True
) -> bytes:
"""Make a licence challenge, or return empty bytes when the API cache holds every required key.
Raises InvalidSession for an unknown ID, and ``requests.RequestException`` when the API fails.
"""
session = self._session(session_id)
session["init_data"] = self.get_init_data_from_pssh(pssh_or_wrm, session.get("pssh_b64"))
body: dict[str, Any] = {"scheme": self.device_name, "init_data": session["init_data"]}
if self.service_name:
body["service"] = self.service_name
if session["service_certificate"]:
body["service_certificate"] = base64.b64encode(session["service_certificate"]).decode("utf-8")
return self._request_challenge(session, body, license_type, privacy_mode)
def _take_cached_keys(self, session: dict[str, Any], cached_keys: list[dict[str, Any]]) -> bool:
"""Keep the keys the API cache returned; True when they cover every required KID.
Otherwise the cached keys wait in the CDM session for ``parse_license`` to merge with the licence keys.
"""
required = session.get("required_kids")
have = {kid_hex(k["kid"]) for k in cached_keys}
if required and set(required) - have:
session["cached_keys"] = cached_keys
return False
session["keys"] = cached_keys
return True
def _keep_challenge(self, session: dict[str, Any], challenge: bytes, remote_session_id: Any) -> bytes:
session["challenge"] = challenge
session["remote_session_id"] = remote_session_id
return challenge
def _post(self, path: str, body: dict[str, Any], failure: str, timeout: float = 30) -> dict[str, Any]:
"""POST ``body`` to ``path`` on the host and return the JSON answer; raises on a status other than 200."""
response = self._http_session.post(f"{self.host}{path}", json=body, timeout=timeout)
if response.status_code != 200:
raise RequestException(f"{failure}: {response.status_code} {response.text}")
return response.json()
@abstractmethod
def _request_challenge(
self,
session: dict[str, Any],
body: dict[str, Any],
license_type: str,
privacy_mode: bool,
) -> bytes:
"""Ask the API for a challenge or cached keys; the adapter's wire form.
``body`` holds the scheme, the init data, and the service and service certificate when set.
"""
@abstractmethod
def _request_keys(self, session: dict[str, Any], body: dict[str, Any]) -> list[dict[str, Any]]:
"""Send the licence to the API and return the keys it holds; the adapter's wire form."""
def parse_license(self, session_id: bytes, license_message: bytes | str) -> None:
"""Have the API read a licence, then merge its keys with the cached keys of the CDM session.
A content key from the API cache wins over a licence key with the same KID. A CDM session
that already holds its final keys and has no cached keys left to merge makes this a no-op.
"""
session = self._session(session_id)
if session["keys"] and "cached_keys" not in session:
return
if not session.get("challenge") or not session.get("remote_session_id"):
raise ValueError("No challenge available - call get_license_challenge first")
if isinstance(license_message, str):
if self.is_playready and license_message.lstrip().startswith("<"):
license_message = license_message.encode("utf-8")
else:
license_message = b64_or_utf8(license_message)
license_keys = self._request_keys(
session,
{
"scheme": self.device_name,
"session_id": session["remote_session_id"],
"init_data": session["init_data"],
"license_request": base64.b64encode(session["challenge"]).decode("utf-8"),
"license_response": base64.b64encode(license_message).decode("utf-8"),
},
)
all_keys = list(session.get("cached_keys") or [])
have = {kid_hex(k["kid"]) for k in all_keys}
for key in license_keys:
kid = kid_hex(key["kid"])
if kid not in have:
have.add(kid)
all_keys.append(key)
session["keys"] = all_keys
session.pop("cached_keys", None)
def get_keys(self, session_id: bytes, type_: str | None = None) -> list[Key]:
"""The keys of a CDM session; only the keys of type ``type_`` when it is set."""
keys = [
Key(
k.get("type", "CONTENT"),
kid_uuid(k["kid"]),
bytes.fromhex(k["key"].replace(":", "")) if isinstance(k["key"], str) else k["key"],
k.get("permissions"),
)
for k in self._session(session_id)["keys"]
]
return [key for key in keys if key.type == type_] if type_ else keys