Files
unshackle/unshackle/commands/prd.py
T

107 lines
3.6 KiB
Python

from pathlib import Path
import click
from pyplayready import main as upstream
from unshackle.core.config import config
from unshackle.core.constants import context_settings
CONTENT_KEY_TYPES = {"aesctr": "AES128BitCTR", "aescbc": "AES128BitCBC"}
EXISTING_FILE = click.Path(exists=True, dir_okay=False, path_type=Path)
@click.group(
short_help="Manage creation of PRD (Playready Device) files.",
context_settings=context_settings,
)
def prd() -> None:
"""Manage creation of PRD (PlayReady Device) files."""
@prd.command()
@click.argument("paths", type=click.Path(exists=True, path_type=Path), nargs=-1)
@click.option("-e", "--encryption_key", type=EXISTING_FILE, help="Optional Device ECC private encryption key")
@click.option("-s", "--signing_key", type=EXISTING_FILE, help="Optional Device ECC private signing key")
@click.option("-o", "--output", type=Path, default=None, help="Output Path or Directory")
@click.pass_context
def new(
ctx: click.Context,
paths: tuple[Path, ...],
encryption_key: Path | None,
signing_key: Path | None,
output: Path | None,
) -> None:
"""Make a new .PRD device file.
Accepts either paths to a group key and certificate or a single directory
containing ``zgpriv.dat`` and ``bgroupcert.dat``.
"""
if len(paths) == 1 and paths[0].is_dir():
device_dir: Path | None = paths[0]
group_key, group_certificate = paths[0] / "zgpriv.dat", paths[0] / "bgroupcert.dat"
if not group_key.is_file() or not group_certificate.is_file():
raise click.UsageError("Folder must contain zgpriv.dat and bgroupcert.dat", ctx)
elif len(paths) == 2:
group_key, group_certificate = paths
device_dir = None
else:
raise click.UsageError("Provide either a folder path or paths to group_key and group_certificate", ctx)
ctx.invoke(
upstream.create_device,
group_key=group_key,
group_certificate=group_certificate,
encryption_key=encryption_key,
signing_key=signing_key,
output=output or device_dir or config.directories.prds,
)
@prd.command(name="reprovision")
@click.argument("prd_path", type=EXISTING_FILE)
@click.option("-e", "--encryption_key", type=EXISTING_FILE, help="Optional Device ECC private encryption key")
@click.option("-s", "--signing_key", type=EXISTING_FILE, help="Optional Device ECC private signing key")
@click.option("-o", "--output", type=Path, default=None, help="Output Path or Directory")
@click.pass_context
def reprovision(
ctx: click.Context,
prd_path: Path,
encryption_key: Path | None,
signing_key: Path | None,
output: Path | None,
) -> None:
"""Reprovision a PlayReady Device (.prd) file."""
ctx.invoke(
upstream.reprovision_device,
prd_path=prd_path,
encryption_key=encryption_key,
signing_key=signing_key,
output=output,
)
@prd.command()
@click.argument("device", type=EXISTING_FILE)
@click.option(
"-c",
"--ckt",
type=click.Choice(list(CONTENT_KEY_TYPES), case_sensitive=False),
default="aesctr",
help="Content Key Encryption Type",
)
@click.option(
"-sl",
"--security-level",
type=click.Choice(["150", "2000", "3000"], case_sensitive=False),
default="2000",
help="Minimum Security Level",
)
@click.pass_context
def test(ctx: click.Context, device: Path, ckt: str, security_level: str) -> None:
"""Do a test of a PlayReady device on the Microsoft demo server."""
ctx.invoke(upstream.test, device=device, ckt=CONTENT_KEY_TYPES[ckt], security_level=security_level)
prd.add_command(upstream.export_device, name="export")
prd.add_command(upstream.inspect)