mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-12-04 06:55:09 +00:00
85 lines
2.1 KiB
Python
85 lines
2.1 KiB
Python
"""Test DRM plugins: EXAMPLE licenses locally and decrypts per segment, OTHER holds its key and decrypts a whole file.
|
|
|
|
Both use a repeating XOR of the content key, so a test can build ciphertext with ``xor``.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
from typing import Any
|
|
from uuid import UUID
|
|
|
|
from unshackle.core.drm.custom import CustomDRM
|
|
from unshackle.core.plugins import DrmPlugin
|
|
|
|
FINISHED: list[Path] = []
|
|
|
|
|
|
def xor(data: bytes, key: bytes) -> bytes:
|
|
return bytes(b ^ key[i % len(key)] for i, b in enumerate(data))
|
|
|
|
|
|
def drm_key(drm: CustomDRM) -> bytes:
|
|
key = drm.key or drm.data.get("key")
|
|
if not key:
|
|
raise ValueError(f"No {drm.label} key")
|
|
return bytes.fromhex(key)
|
|
|
|
|
|
def decrypt(drm: CustomDRM, src: Path, dst: Path) -> None:
|
|
dst.write_bytes(xor(src.read_bytes(), drm_key(drm)))
|
|
|
|
|
|
def licence(drm: CustomDRM, cdm: ExampleDevice) -> None:
|
|
"""The EXAMPLE device turns the ticket into one KID and its key."""
|
|
kid = UUID(hex=drm.data["ticket"])
|
|
drm.kids = [kid]
|
|
drm.content_keys[kid] = cdm.path.read_bytes().hex()
|
|
|
|
|
|
def kids(drm: CustomDRM) -> list[UUID]:
|
|
"""The EXAMPLE ticket is the hex of its KID."""
|
|
return [UUID(hex=drm.data["ticket"])] if "ticket" in drm.data else []
|
|
|
|
|
|
def finish(drm: CustomDRM, path: Path, track: Any) -> bool:
|
|
FINISHED.append(path)
|
|
return True
|
|
|
|
|
|
class ExampleDevice:
|
|
"""A plugin CDM: the plugin id as its system, the device file it was built from and its name."""
|
|
|
|
drm_system = "example"
|
|
|
|
def __init__(self, path: Path) -> None:
|
|
self.path = path
|
|
self.device_name = path.stem
|
|
|
|
|
|
EXAMPLE = DrmPlugin(
|
|
id="example",
|
|
label="EXAMPLE",
|
|
licensing="local",
|
|
media="own",
|
|
mode="segment",
|
|
decrypt=decrypt,
|
|
finish=finish,
|
|
licence=licence,
|
|
kids=kids,
|
|
wire_data=("ticket",),
|
|
device_suffix=".exd",
|
|
load_device=ExampleDevice,
|
|
version="1.0",
|
|
device_facts=("device_name",),
|
|
)
|
|
OTHER = DrmPlugin(
|
|
id="other",
|
|
label="OTHER",
|
|
licensing="held",
|
|
media="own",
|
|
mode="track",
|
|
decrypt=decrypt,
|
|
wire_data=("key",),
|
|
)
|