mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-12-02 05:55:08 +00:00
fix(drm): stop a bridged FairPlay track whose server-CDM answer or licence key does not decrypt it
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
"""The decode check of the content keys that a licence gives a bridged FairPlay-only track."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
from uuid import UUID
|
||||
|
||||
import pytest
|
||||
|
||||
from tests.core.key_resolver import resolver, vaults_of
|
||||
from tests.core.test_vault_bad_keys import BAD, GOOD, KID, FakeDRM, Remote
|
||||
from unshackle.core.drm import key_resolution
|
||||
from unshackle.core.drm.key_resolution import ContentKeyResolver
|
||||
from unshackle.core.vaults import Vaults
|
||||
from unshackle.vaults.SQLite import SQLite
|
||||
|
||||
pytestmark = pytest.mark.unit
|
||||
|
||||
TRACK = SimpleNamespace(id="vid")
|
||||
|
||||
|
||||
class BridgedDRM(FakeDRM):
|
||||
"""A DRM that names KID."""
|
||||
|
||||
def __init__(self, keys: dict[UUID, str], bridged: bool = True) -> None:
|
||||
super().__init__(keys)
|
||||
self.kids = [KID]
|
||||
self.bridged = bridged
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def decodes(monkeypatch: pytest.MonkeyPatch) -> list[Path]:
|
||||
"""Each file the decode check reads; only a file decrypted with GOOD decodes."""
|
||||
checked: list[Path] = []
|
||||
|
||||
def ffmpeg_decodes(path: Path, start: float | None = None, seconds: float = 3, video: bool | None = None) -> bool:
|
||||
checked.append(path)
|
||||
return path.read_bytes() == b"plain:" + GOOD.encode()
|
||||
|
||||
monkeypatch.setattr(key_resolution.binaries, "FFMPEG", "ffmpeg")
|
||||
monkeypatch.setattr(key_resolution, "ffmpeg_decodes", ffmpeg_decodes)
|
||||
return checked
|
||||
|
||||
|
||||
def check(tmp_path: Path, drm: BridgedDRM, vaults: Vaults | None = None, decrypt: bool = True) -> Path:
|
||||
"""Decrypt a file with ``drm`` through ``decrypt_verified``, with a licence that gives GOOD.
|
||||
|
||||
Without ``decrypt`` the file is already plain, as after per-segment decryption.
|
||||
"""
|
||||
path = tmp_path / "track.mp4"
|
||||
path.write_bytes(b"cipher" if decrypt else b"plain:" + drm.content_keys[KID].encode())
|
||||
keys = resolver(vaults, tag="SVC")
|
||||
ContentKeyResolver.LICENSE_KEY_CACHE.update(drm.content_keys)
|
||||
|
||||
def licence(drm: BridgedDRM, track_kid: UUID | None = None) -> None:
|
||||
drm.content_keys[KID] = GOOD
|
||||
|
||||
try:
|
||||
keys.decrypt_verified(drm, path, licence, KID, decrypt, TRACK) # type: ignore[arg-type]
|
||||
finally:
|
||||
keys.wait_vault_writes()
|
||||
return path
|
||||
|
||||
|
||||
@pytest.mark.parametrize("decrypt", [True, False], ids=["decrypt", "segments-decrypted"])
|
||||
def test_a_licence_key_of_a_bridged_track_that_does_not_decode_stops_the_track(
|
||||
tmp_path: Path, decodes: list[Path], decrypt: bool
|
||||
) -> None:
|
||||
local = SQLite("local", tmp_path / "local.db")
|
||||
local.add_key("SVC", KID, BAD)
|
||||
vaults = vaults_of(local, service="SVC")
|
||||
|
||||
with pytest.raises(ValueError, match=f"^The licence content key of KID {KID.hex} does not decrypt track vid$"):
|
||||
check(tmp_path, BridgedDRM({KID: BAD}), vaults, decrypt)
|
||||
|
||||
assert decodes and not (tmp_path / "track.mp4").exists()
|
||||
assert local.is_bad_key(KID, BAD) and local.get_key(KID, "SVC") is None
|
||||
assert KID not in ContentKeyResolver.LICENSE_KEY_CACHE
|
||||
|
||||
|
||||
def test_a_licence_key_of_a_bridged_track_that_decodes_is_kept(tmp_path: Path, decodes: list[Path]) -> None:
|
||||
local = SQLite("local", tmp_path / "local.db")
|
||||
|
||||
path = check(tmp_path, BridgedDRM({KID: GOOD}), vaults_of(local, service="SVC"))
|
||||
|
||||
assert decodes and path.read_bytes() == b"plain:" + GOOD.encode()
|
||||
assert not local.is_bad_key(KID, GOOD)
|
||||
|
||||
|
||||
def test_a_licence_key_of_a_track_that_is_not_bridged_is_not_decoded(tmp_path: Path, decodes: list[Path]) -> None:
|
||||
path = check(tmp_path, BridgedDRM({KID: BAD}, bridged=False))
|
||||
|
||||
assert not decodes and path.read_bytes() == b"plain:" + BAD.encode()
|
||||
|
||||
|
||||
def test_a_bad_vault_key_of_a_bridged_track_gives_way_to_the_licence_key_which_is_checked(
|
||||
tmp_path: Path, decodes: list[Path]
|
||||
) -> None:
|
||||
poisoned = Remote("poisoned", tmp_path / "poisoned.db")
|
||||
poisoned.add_key("SVC", KID, BAD)
|
||||
vaults = vaults_of(SQLite("local", tmp_path / "local.db"), poisoned, service="SVC")
|
||||
drm = BridgedDRM({})
|
||||
drm.content_keys[KID] = vaults.get_key(KID)[0] # type: ignore[assignment]
|
||||
|
||||
path = check(tmp_path, drm, vaults)
|
||||
|
||||
assert drm.decrypted_with == [{KID: BAD}, {KID: GOOD}]
|
||||
assert path.read_bytes() == b"plain:" + GOOD.encode()
|
||||
assert len(decodes) == 2 and vaults.vaults[0].is_bad_key(KID, BAD)
|
||||
Reference in New Issue
Block a user