fix(drm): end a licence with no usable key in one line for every DRM system

This commit is contained in:
imSp4rky
2026-11-24 17:13:08 -07:00
parent d954f61100
commit 4692e6bb96
6 changed files with 69 additions and 19 deletions
+23
View File
@@ -4,7 +4,9 @@ import base64
from typing import Any from typing import Any
from uuid import UUID from uuid import UUID
import click
import pytest import pytest
from pyplayready.misc.exceptions import InvalidLicense, InvalidXmrLicense
from pyplayready.system.pssh import PSSH from pyplayready.system.pssh import PSSH
from pyplayready.system.wrmheader import WRMHeader from pyplayready.system.wrmheader import WRMHeader
@@ -414,3 +416,24 @@ def test_a_single_refused_header_still_raises_on_every_call() -> None:
for _ in range(2): for _ in range(2):
with pytest.raises(RuntimeError, match="licence refused"): with pytest.raises(RuntimeError, match="licence refused"):
drm.get_content_keys(cdm=cdm, certificate=_cert, licence=_licence) drm.get_content_keys(cdm=cdm, certificate=_cert, licence=_licence)
@pytest.mark.parametrize(
"error, expected",
[
(InvalidXmrLicense("Cannot parse an empty licence message"), click.ClickException),
(InvalidLicense("bad"), click.ClickException),
(InvalidLicense("server fault 0x8004C065"), PlayReady.Exceptions.DeviceRevoked),
],
)
def test_a_licence_the_cdm_cannot_parse_is_one_line_unless_the_device_is_revoked(
error: Exception, expected: type[Exception]
) -> None:
cdm = FakeCdm({KID_A: KEY_A})
def parse_license(session_id: str, response: str) -> None:
raise error
cdm.parse_license = parse_license # type: ignore[method-assign]
with pytest.raises(expected):
_drm(_wrm_header(KID_A)).get_content_keys(cdm=cdm, certificate=_cert, licence=_licence)
+23 -6
View File
@@ -3,10 +3,15 @@ from __future__ import annotations
from typing import Any from typing import Any
from uuid import UUID from uuid import UUID
import click
import pytest import pytest
import requests
from pywidevine.exceptions import InvalidLicenseMessage
from pywidevine.pssh import PSSH from pywidevine.pssh import PSSH
from unshackle.core.drm import Widevine from unshackle.core.cdm.custom_remote_cdm import CustomRemoteCDMExceptions
from unshackle.core.cdm.decrypt_labs_remote_cdm import DecryptLabsRemoteCDMExceptions
from unshackle.core.drm import ClearKeyCENC, PlayReady, Widevine
from unshackle.core.utilities import declared_kwargs from unshackle.core.utilities import declared_kwargs
KID = UUID("00112233-4455-6677-8899-aabbccddeeff") KID = UUID("00112233-4455-6677-8899-aabbccddeeff")
@@ -95,13 +100,25 @@ def test_an_empty_challenge_raises_instead_of_licensing() -> None:
) )
def test_a_licence_the_cdm_cannot_parse_is_one_line() -> None: @pytest.mark.parametrize(
import click "error",
from pywidevine.exceptions import InvalidLicenseMessage [
InvalidLicenseMessage("Could not parse license_message as a SignedMessage"),
CustomRemoteCDMExceptions.InvalidLicenseMessage("bad"),
DecryptLabsRemoteCDMExceptions.InvalidLicenseMessage("bad"),
requests.RequestException("License decrypt failed: 400"),
],
)
def test_a_licence_the_cdm_cannot_parse_is_one_line(error: Exception) -> None:
class JunkCdm(FakeCdm): class JunkCdm(FakeCdm):
def parse_license(self, session_id: str, response: Any) -> None: def parse_license(self, session_id: str, response: Any) -> None:
raise InvalidLicenseMessage("Could not parse license_message as a SignedMessage") raise error
with pytest.raises(click.ClickException, match=KID.hex): with pytest.raises(click.ClickException, match=KID.hex):
_drm().get_content_keys(JunkCdm(), certificate=lambda **_: None, licence=lambda **_: b"junk licence") _drm().get_content_keys(JunkCdm(), certificate=lambda **_: None, licence=lambda **_: b"junk licence")
@pytest.mark.parametrize("drm", [Widevine, PlayReady, ClearKeyCENC])
def test_a_licence_with_no_usable_key_is_one_line(drm: Any) -> None:
assert issubclass(drm.Exceptions.EmptyLicense, click.ClickException)
assert issubclass(drm.Exceptions.CEKNotFound, click.ClickException)
@@ -10,6 +10,7 @@ import base64
from types import SimpleNamespace from types import SimpleNamespace
from uuid import UUID from uuid import UUID
import click
import pytest import pytest
from pyplayready.system.pssh import PSSH as PRPSSH from pyplayready.system.pssh import PSSH as PRPSSH
from pywidevine.pssh import PSSH as WvPSSH from pywidevine.pssh import PSSH as WvPSSH
@@ -113,5 +114,5 @@ def test_failed_licence_without_injected_keys_still_raises(server_env):
get_widevine_license=lambda **_: b"garbage", get_widevine_license=lambda **_: b"garbage",
) )
with pytest.raises(ValueError, match="not a licence"): with pytest.raises(click.ClickException, match="not a licence"):
handlers.handle_single_server_cdm(service, SimpleNamespace(), track, pssh.dumps(), "widevine", None) handlers.handle_single_server_cdm(service, SimpleNamespace(), track, pssh.dumps(), "widevine", None)
+3 -2
View File
@@ -6,6 +6,7 @@ from pathlib import Path
from typing import Any, Callable, Iterable, Optional, Union from typing import Any, Callable, Iterable, Optional, Union
from uuid import UUID from uuid import UUID
import click
from requests import Session from requests import Session
from unshackle.core.config import config from unshackle.core.config import config
@@ -192,10 +193,10 @@ class ClearKeyCENC:
class KIDNotFound(Exception): class KIDNotFound(Exception):
"""KID (Encryption Key ID) was not found.""" """KID (Encryption Key ID) was not found."""
class CEKNotFound(Exception): class CEKNotFound(click.ClickException):
"""CEK (Content Encryption Key) for KID was not found in License.""" """CEK (Content Encryption Key) for KID was not found in License."""
class EmptyLicense(Exception): class EmptyLicense(click.ClickException):
"""License returned no Content Encryption Keys.""" """License returned no Content Encryption Keys."""
+12 -3
View File
@@ -7,6 +7,7 @@ from threading import Lock
from typing import Any, Callable, Optional, Union from typing import Any, Callable, Optional, Union
from uuid import UUID from uuid import UUID
import click
import m3u8 import m3u8
from construct import Container from construct import Container
from pymp4.parser import Box from pymp4.parser import Box
@@ -491,7 +492,15 @@ class PlayReady:
except ValueError: except ValueError:
pass pass
cdm.parse_license(session_id, license_str) 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( log_event(
"drm_license_response", "drm_license_response",
level="DEBUG", level="DEBUG",
@@ -561,10 +570,10 @@ class PlayReady:
class KIDNotFound(Exception): class KIDNotFound(Exception):
pass pass
class CEKNotFound(Exception): class CEKNotFound(click.ClickException):
pass pass
class EmptyLicense(Exception): class EmptyLicense(click.ClickException):
pass pass
class DeviceRevoked(Exception): class DeviceRevoked(Exception):
+6 -7
View File
@@ -10,7 +10,6 @@ import m3u8
from construct import Container from construct import Container
from pymp4.parser import Box from pymp4.parser import Box
from pywidevine.cdm import Cdm as WidevineCdm from pywidevine.cdm import Cdm as WidevineCdm
from pywidevine.exceptions import InvalidLicenseMessage
from pywidevine.pssh import PSSH from pywidevine.pssh import PSSH
from unshackle.core.config import config from unshackle.core.config import config
@@ -251,10 +250,10 @@ class Widevine:
license_res = call_licence(licence, challenge, pssh=self.pssh, session_id=session_id) license_res = call_licence(licence, challenge, pssh=self.pssh, session_id=session_id)
try: try:
cdm.parse_license(session_id, license_res) cdm.parse_license(session_id, license_res)
except InvalidLicenseMessage: except Exception as e:
raise click.ClickException( raise Widevine.Exceptions.EmptyLicense(
f"The licence server sent no usable Widevine licence for KID {kid.hex}" f"No usable Widevine licence for KID {kid.hex}: {e}"
) from None ) from e
log_event( log_event(
"drm_license_response", "drm_license_response",
level="DEBUG", level="DEBUG",
@@ -318,10 +317,10 @@ class Widevine:
class KIDNotFound(Exception): class KIDNotFound(Exception):
"""KID (Encryption Key ID) was not found.""" """KID (Encryption Key ID) was not found."""
class CEKNotFound(Exception): class CEKNotFound(click.ClickException):
"""CEK (Content Encryption Key) for KID was not found in License.""" """CEK (Content Encryption Key) for KID was not found in License."""
class EmptyLicense(Exception): class EmptyLicense(click.ClickException):
"""License returned no Content Encryption Keys.""" """License returned no Content Encryption Keys."""