fix(drm): keep the input when a cbcs, ClearKey, stripe or segment decrypt fails

This commit is contained in:
imSp4rky
2026-11-28 18:27:40 -07:00
parent 79cc16d290
commit 5a2680d63d
8 changed files with 214 additions and 36 deletions
+15 -7
View File
@@ -1,5 +1,6 @@
from __future__ import annotations
import contextlib
from pathlib import Path
from Cryptodome.Cipher import Blowfish
@@ -24,7 +25,10 @@ class BlowfishStripe:
return Blowfish.new(self.key, Blowfish.MODE_CBC, iv=self.IV).decrypt(chunk) # nosec B304
def decrypt(self, path: Path) -> None:
"""Decrypt the file to a new file that then replaces it. A short trailing chunk stays clear."""
"""Decrypt the file to a new file that then replaces it. A short trailing chunk stays clear.
A failure keeps ``path`` as it is and removes the new file.
"""
if not path or not path.exists():
raise ValueError("Tried to decrypt a file that does not exist.")
log_event(
@@ -35,12 +39,16 @@ class BlowfishStripe:
file=path.name,
)
decrypted_path = path.with_suffix(f".decrypted{path.suffix}")
with path.open("rb") as src, decrypted_path.open("wb") as dst:
while stripe := src.read(self.CHUNK * 3):
if len(stripe) >= self.CHUNK:
stripe = self.decrypt_chunk(stripe[: self.CHUNK]) + stripe[self.CHUNK :]
dst.write(stripe)
decrypted_path.replace(path)
try:
with path.open("rb") as src, decrypted_path.open("wb") as dst:
while stripe := src.read(self.CHUNK * 3):
if len(stripe) >= self.CHUNK:
stripe = self.decrypt_chunk(stripe[: self.CHUNK]) + stripe[self.CHUNK :]
dst.write(stripe)
decrypted_path.replace(path)
finally:
with contextlib.suppress(OSError):
decrypted_path.unlink(missing_ok=True)
__all__ = ("BlowfishStripe",)