mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-12-04 06:55:09 +00:00
feat(drm)!: load DRM systems beyond the built-ins from plugins in directories.plugins
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
"""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 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,
|
||||
device_suffix=".exd",
|
||||
load_device=ExampleDevice,
|
||||
)
|
||||
OTHER = DrmPlugin(
|
||||
id="other",
|
||||
label="OTHER",
|
||||
licensing="held",
|
||||
media="own",
|
||||
mode="track",
|
||||
decrypt=decrypt,
|
||||
wire_data=("key",),
|
||||
)
|
||||
Reference in New Issue
Block a user