From 8b9826322916cab2c795f5325fa2922c8cd0f020 Mon Sep 17 00:00:00 2001 From: Daniel Kahn Gillmor Date: Mon, 25 Nov 2019 16:45:45 -0500 Subject: email-print-mime-structure: decrypt PGP/MIME parts as bytes Fully decode the encrypted part before passing it to any decryption mechanism. Signed-off-by: Daniel Kahn Gillmor Acked-by: Sean Whitton --- email-print-mime-structure | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/email-print-mime-structure b/email-print-mime-structure index 27fb532..cdbe2ee 100755 --- a/email-print-mime-structure +++ b/email-print-mime-structure @@ -87,8 +87,8 @@ class MimePrinter(object): (parent.get_content_type().lower() == 'multipart/encrypted') and \ (str(parent.get_param('protocol')).lower() == 'application/pgp-encrypted') and \ (num == 2): - ciphertext = z.get_payload() - if not isinstance(ciphertext, str): + ciphertext = z.get_payload(decode=True) + if not isinstance(ciphertext, bytes): logging.warning('encrypted part was not a leaf mime part somehow') return if self.args.pgpkey: @@ -104,7 +104,7 @@ class MimePrinter(object): print(f'{newprefix}↧ (decrypts to)') self.print_tree(cryptopayload, newprefix + '└', z, 0) - def pgpy_decrypt(self, keys:List[str], ciphertext:str) -> Optional[Message]: + def pgpy_decrypt(self, keys:List[str], ciphertext:bytes) -> Optional[Message]: if pgpy is None: logging.warning(f'Python module pgpy is not available, not decrypting (try "apt install python3-pgpy")') return None @@ -121,11 +121,11 @@ class MimePrinter(object): pass return None - def gpg_decrypt(self, ciphertext:str) -> Optional[Message]: + def gpg_decrypt(self, ciphertext:bytes) -> Optional[Message]: inp:int outp:int inp, outp = os.pipe() - with open(outp, 'w') as outf: + with open(outp, 'wb') as outf: outf.write(ciphertext) out:subprocess.CompletedProcess[bytes] = subprocess.run(['gpg', '--batch', '--decrypt'], stdin=inp, -- cgit v1.2.3