aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJ08nY2017-06-27 00:11:39 +0200
committerJ08nY2017-06-27 00:11:39 +0200
commit016740a6ca2aa789713abdf9caadf9d102c8b866 (patch)
tree2476d0fc0d44d1d4fb7ef05f03e6825349327317
parent1e7fcefa467e32ab54967e78ffc8485a46d3eaeb (diff)
downloadmailman-pgp-016740a6ca2aa789713abdf9caadf9d102c8b866.tar.gz
mailman-pgp-016740a6ca2aa789713abdf9caadf9d102c8b866.tar.zst
mailman-pgp-016740a6ca2aa789713abdf9caadf9d102c8b866.zip
-rw-r--r--src/mailman_pgp/pgp/mime.py65
1 files changed, 48 insertions, 17 deletions
diff --git a/src/mailman_pgp/pgp/mime.py b/src/mailman_pgp/pgp/mime.py
index c36cc37..31dcdf3 100644
--- a/src/mailman_pgp/pgp/mime.py
+++ b/src/mailman_pgp/pgp/mime.py
@@ -171,10 +171,7 @@ class MIMEWrapper:
:rtype: mailman.email.message.Message
"""
payload = self.msg.as_string()
- # Wrap payload in PGPMessage with cleartext=True to get a signature
- # of a canonical document.
- pmsg = PGPMessage.new(payload, cleartext=True)
- signature = key.sign(pmsg, hash=hash)
+ signature = key.sign(payload, hash=hash)
micalg = self._micalg(signature.hash_algorithm)
out = MultipartDigestMessage('signed', micalg=micalg,
@@ -212,18 +209,7 @@ class MIMEWrapper:
copy_headers(self.msg, out)
return out
- def encrypt(self, *keys, cipher=SymmetricKeyAlgorithm.AES256):
- """
- Encrypt the message with key/s, using cipher.
-
- :param keys: The key/s to encrypt with.
- :type keys: pgpy.PGPKey
- :param cipher: The symmetric cipher to use.
- :type cipher: SymmetricKeyAlgorithm
- :return: mailman.email.message.Message
- """
- payload = self.msg.as_string()
- pmsg = PGPMessage.new(payload)
+ def _encrypt(self, pmsg, *keys, cipher):
if len(keys) == 1:
pmsg = keys[0].encrypt(pmsg, cipher=cipher)
else:
@@ -232,7 +218,9 @@ class MIMEWrapper:
pmsg = key.encrypt(pmsg, cipher=cipher,
session_key=session_key)
del session_key
+ return pmsg
+ def _wrap_encrypted(self, payload):
out = MultipartDigestMessage('encrypted',
protocol=MIMEWrapper._encrypted_type)
out.preamble = MIMEWrapper._encryption_preamble
@@ -243,7 +231,7 @@ class MIMEWrapper:
first_part.add_header('content-description',
'PGP/MIME version identification')
- second_part = MIMEApplication(_data=str(pmsg),
+ second_part = MIMEApplication(_data=str(payload),
_subtype='octet-stream',
_encoder=encode_7or8bit,
name='encrypted.asc')
@@ -255,3 +243,46 @@ class MIMEWrapper:
out.attach(second_part)
copy_headers(self.msg, out)
return out
+
+ def encrypt(self, *keys, cipher=SymmetricKeyAlgorithm.AES256):
+ """
+ Encrypt the message with key/s, using cipher.
+
+ :param keys: The key/s to encrypt with.
+ :type keys: pgpy.PGPKey
+ :param cipher: The symmetric cipher to use.
+ :type cipher: SymmetricKeyAlgorithm
+ :return: The encrypted message.
+ :rtype: mailman.email.message.Message
+ """
+ payload = self.msg.as_string()
+ pmsg = PGPMessage.new(payload)
+ pmsg = self._encrypt(pmsg, *keys, cipher=cipher)
+ return self._wrap_encrypted(pmsg)
+
+ def sign_encrypt(self, key, *keys, hash=None,
+ cipher=SymmetricKeyAlgorithm.AES256):
+ """
+ Sign and encrypt te message, in one go.
+
+ :param key: The key to sign with.
+ :type key: pgpy.PGPKey
+ :param keys: The key/s to encrypt with.
+ :type keys: pgpy.PGPKey
+ :param hash:
+ :param cipher:
+ :return: The signed + encrypted message.
+ :rtype: mailman.email.message.Message
+ """
+ payload = self.msg.as_string()
+ pmsg = PGPMessage.new(payload)
+ pmsg |= key.sign(pmsg, hash=hash)
+ pmsg = self._encrypt(pmsg, *keys, cipher=cipher)
+ return self._wrap_encrypted(pmsg)
+
+ def sign_then_encrypt(self, key, *keys, hash=None,
+ cipher=SymmetricKeyAlgorithm.AES256):
+ out = self.sign(key, hash)
+ out_wrapped = MIMEWrapper(out)
+ out = out_wrapped.encrypt(*keys, cipher=cipher)
+ return out