diff options
| -rw-r--r-- | src/mailman_pgp/pgp/mime.py | 31 |
1 files changed, 26 insertions, 5 deletions
diff --git a/src/mailman_pgp/pgp/mime.py b/src/mailman_pgp/pgp/mime.py index 7a632bf..8b87abe 100644 --- a/src/mailman_pgp/pgp/mime.py +++ b/src/mailman_pgp/pgp/mime.py @@ -19,8 +19,13 @@ from email.message import Message from email.utils import collapse_rfc2231_value +from pgpy import PGPKey, PGPSignature + class PGPMIMEWrapper: + _signed_subtype = 'application/pgp-signature' + _encrypted_subtype = 'application/pgp-encrypted' + def __init__(self, msg: Message): self.msg = msg @@ -41,9 +46,9 @@ class PGPMIMEWrapper: protocol_param = collapse_rfc2231_value(self.msg.get_param('protocol')) content_subtype = self.msg.get_content_subtype() - return second_type == 'application/pgp-signature' and \ - content_subtype == 'signed' and \ - protocol_param == 'application/pgp-signature' + return second_type == PGPMIMEWrapper._signed_subtype and \ + content_subtype == 'signed' and \ + protocol_param == PGPMIMEWrapper._signed_subtype def is_mime_encrypted(self): """ @@ -59,7 +64,23 @@ class PGPMIMEWrapper: protocol_param = collapse_rfc2231_value(self.msg.get_param('protocol')) return 'Version: 1' in first_part and \ - first_type == 'application/pgp-encrypted' and \ + first_type == PGPMIMEWrapper._encrypted_subtype and \ second_type == 'application/octet-stream' and \ content_subtype == 'encrypted' and \ - protocol_param == 'application/pgp-encrypted' + protocol_param == PGPMIMEWrapper._encrypted_subtype + + def verify(self, key: PGPKey): + """ + + :param key: + :return: + """ + clear_text = str(self.msg.get_payload(0)) + sig_text = self.msg.get_payload(1).get_payload() + signature = PGPSignature.from_blob(sig_text) + verification = key.verify(clear_text, + signature) + return signature in [v.signature for v in verification.good_signatures] + + def decrypt(self, key: PGPKey): + pass |
