aboutsummaryrefslogtreecommitdiff
path: root/src/mailman_pgp/pgp
diff options
context:
space:
mode:
authorJ08nY2017-06-19 19:35:44 +0200
committerJ08nY2017-06-19 19:35:44 +0200
commit01d1c164eb5b1734ffe03ff642e46707ffdce3df (patch)
tree935d52249e41a56d06780bb47b81dc9275f8e6ff /src/mailman_pgp/pgp
parentea3c9c3778e79b8fcef648e59aee7affe724791b (diff)
downloadmailman-pgp-01d1c164eb5b1734ffe03ff642e46707ffdce3df.tar.gz
mailman-pgp-01d1c164eb5b1734ffe03ff642e46707ffdce3df.tar.zst
mailman-pgp-01d1c164eb5b1734ffe03ff642e46707ffdce3df.zip
Add PGPMimeWrapper.
- PGPMimeWrapper is an adapter that provides some RFC3156 functions on an email.message.Message object.
Diffstat (limited to 'src/mailman_pgp/pgp')
-rw-r--r--src/mailman_pgp/pgp/mime.py36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/mailman_pgp/pgp/mime.py b/src/mailman_pgp/pgp/mime.py
index f3b0d32..08bd111 100644
--- a/src/mailman_pgp/pgp/mime.py
+++ b/src/mailman_pgp/pgp/mime.py
@@ -1 +1,37 @@
""""""
+from email.message import Message
+from email.utils import collapse_rfc2231_value
+
+
+class PGPMIMEWrapper:
+ def __init__(self, msg: Message):
+ self.msg = msg
+
+ def _is_mime(self):
+ is_multipart = self.msg.is_multipart()
+ payloads = len(self.msg.get_payload())
+ first_part = self.msg.get_payload(0)
+ return is_multipart and payloads == 2 and 'Version: 1' in first_part
+
+ def is_mime_signed(self):
+ """
+ Whether the whole message is MIME signed as per RFC3156 section 5.
+ :return:
+ """
+ protocol_param = collapse_rfc2231_value(self.msg.get_param('protocol'))
+ content_subtype = self.msg.get_content_subtype()
+ return self._is_mime() and \
+ content_subtype == 'signed' and \
+ protocol_param == 'application/pgp-signature'
+
+ def is_mime_encrypted(self):
+ """
+ Whether the whole message is MIME encrypted as per RFC3156 section 4.
+ :return:
+ """
+ content_subtype = self.msg.get_content_subtype()
+ protocol_param = collapse_rfc2231_value(self.msg.get_param('protocol'))
+
+ return self._is_mime() and \
+ content_subtype == 'encrypted' and \
+ protocol_param == 'application/pgp-encrypted'