From 26d81a7befa0b78d5160d5c325ac38c8f08139a6 Mon Sep 17 00:00:00 2001 From: rlaphoenix Date: Fri, 5 Aug 2022 08:31:14 +0100 Subject: [PATCH] PSSH: Allow crafting v0 boxes with just Key IDs This is actually possible and in some cases necessary. While v0 boxes do not use key_IDs field of the PSSH Box, we can store the provided key_ids in the init data. E.g., Apple Music. --- pywidevine/pssh.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/pywidevine/pssh.py b/pywidevine/pssh.py index 25c1720..779bf32 100644 --- a/pywidevine/pssh.py +++ b/pywidevine/pssh.py @@ -126,11 +126,9 @@ class PSSH: if flags < 0: raise ValueError(f"Invalid flags, cannot be less than 0.") - if version == 0: - if key_ids is not None: - raise ValueError("Version 0 PSSH boxes must use init_data only, not key_ids.") - if init_data is None: - raise ValueError("Version 0 PSSH boxes must use init_data but it wasn't provided.") + if version == 0 and key_ids is not None and init_data is not None: + # v0 boxes use only init_data in the pssh field, but we can use the key_ids within the init_data + raise ValueError("Version 0 PSSH boxes must use only init_data, not init_data and key_ids.") elif version == 1: # TODO: I cannot tell if they need either init_data or key_ids exclusively, or both is fine # So for now I will just make sure at least one is supplied @@ -177,6 +175,9 @@ class PSSH: init_data=[init_data, b""][init_data is None] ))) + if key_ids and version == 0: + PSSH.overwrite_key_ids(box, [UUID(bytes=x) for x in key_ids]) + return cls(box) @classmethod