diff options
Diffstat (limited to 'src/mailman_pgp/pgp')
| -rw-r--r-- | src/mailman_pgp/pgp/mime.py | 47 | ||||
| -rw-r--r-- | src/mailman_pgp/pgp/tests/data/mime_encrypted_then_signed.eml (renamed from src/mailman_pgp/pgp/tests/data/mime_encrypted_signed.eml) | 0 | ||||
| -rw-r--r-- | src/mailman_pgp/pgp/tests/test_mime.py | 2 | ||||
| -rw-r--r-- | src/mailman_pgp/pgp/tests/test_wrapper.py | 2 |
4 files changed, 29 insertions, 22 deletions
diff --git a/src/mailman_pgp/pgp/mime.py b/src/mailman_pgp/pgp/mime.py index 47ed192..81208bd 100644 --- a/src/mailman_pgp/pgp/mime.py +++ b/src/mailman_pgp/pgp/mime.py @@ -165,20 +165,7 @@ class MIMEWrapper: } return 'pgp-' + algs[hash_algo] - def sign(self, key, hash=None): - """ - Sign a message with key. - - :param key: The key to sign with. - :type key: pgpy.PGPKey - :param hash: - :type hash: HashAlgorithm - :return: The signed message. - :rtype: mailman.email.message.Message - """ - payload = self.msg.as_string() - signature = key.sign(payload, hash=hash) - + def _wrap_signed(self, msg, signature): micalg = self._micalg(signature.hash_algorithm) out = MultipartDigestMessage('signed', micalg=micalg, protocol=MIMEWrapper._signed_type) @@ -193,11 +180,26 @@ class MIMEWrapper: second_part.add_header('content-disposition', 'attachment', filename='signature.asc') - out.attach(copy.deepcopy(self.msg)) + out.attach(copy.deepcopy(msg)) out.attach(second_part) - copy_headers(self.msg, out) + copy_headers(msg, out) return out + def sign(self, key, hash=None): + """ + Sign a message with key. + + :param key: The key to sign with. + :type key: pgpy.PGPKey + :param hash: + :type hash: HashAlgorithm + :return: The signed message. + :rtype: mailman.email.message.Message + """ + payload = self.msg.as_string() + signature = key.sign(payload, hash=hash) + return self._wrap_signed(self.msg, signature) + def decrypt(self, key): """ Decrypt this message with key. @@ -214,9 +216,11 @@ class MIMEWrapper: dmsg = decrypted.message if isinstance(dmsg, bytearray): - dmsg = dmsg.decode('utf-8') + dmsg = dmsg.decode(decrypted.charset or 'utf-8') out = message_from_string(dmsg, _class=Message) + if decrypted.is_signed: + out = self._wrap_signed(out, decrypted.signatures.pop()) copy_headers(self.msg, out) return out @@ -252,7 +256,6 @@ class MIMEWrapper: filename='encrypted.asc') out.attach(first_part) out.attach(second_part) - copy_headers(self.msg, out) return out def encrypt(self, *keys, cipher=SymmetricKeyAlgorithm.AES256): @@ -272,7 +275,9 @@ class MIMEWrapper: payload = self.msg.as_string() pmsg = PGPMessage.new(payload) pmsg = self._encrypt(pmsg, *keys, cipher=cipher) - return self._wrap_encrypted(pmsg) + out = self._wrap_encrypted(pmsg) + copy_headers(self.msg, out) + return out def sign_encrypt(self, key, *keys, hash=None, cipher=SymmetricKeyAlgorithm.AES256): @@ -299,7 +304,9 @@ class MIMEWrapper: pmsg = PGPMessage.new(payload) pmsg |= key.sign(pmsg, hash=hash) pmsg = self._encrypt(pmsg, *keys, cipher=cipher) - return self._wrap_encrypted(pmsg) + out = self._wrap_encrypted(pmsg) + copy_headers(self.msg, out) + return out def sign_then_encrypt(self, key, *keys, hash=None, cipher=SymmetricKeyAlgorithm.AES256): diff --git a/src/mailman_pgp/pgp/tests/data/mime_encrypted_signed.eml b/src/mailman_pgp/pgp/tests/data/mime_encrypted_then_signed.eml index 2a350ab..2a350ab 100644 --- a/src/mailman_pgp/pgp/tests/data/mime_encrypted_signed.eml +++ b/src/mailman_pgp/pgp/tests/data/mime_encrypted_then_signed.eml diff --git a/src/mailman_pgp/pgp/tests/test_mime.py b/src/mailman_pgp/pgp/tests/test_mime.py index 34b6fad..a3549a4 100644 --- a/src/mailman_pgp/pgp/tests/test_mime.py +++ b/src/mailman_pgp/pgp/tests/test_mime.py @@ -75,7 +75,7 @@ class TestEncryption(MIMEWrapperTestCase): @parameterized.expand([ (load_message('data/mime_encrypted.eml'), True), - (load_message('data/mime_encrypted_signed.eml'), + (load_message('data/mime_encrypted_then_signed.eml'), True) ]) def test_is_encrypted(self, message, encrypted): diff --git a/src/mailman_pgp/pgp/tests/test_wrapper.py b/src/mailman_pgp/pgp/tests/test_wrapper.py index 66fc9a9..1c55ed2 100644 --- a/src/mailman_pgp/pgp/tests/test_wrapper.py +++ b/src/mailman_pgp/pgp/tests/test_wrapper.py @@ -79,7 +79,7 @@ class TestEncryption(PGPWrapperTestCase): @parameterized.expand([ (load_message('data/inline_encrypted.eml'), True), - (load_message('data/mime_encrypted_signed.eml'), + (load_message('data/mime_encrypted_then_signed.eml'), True), (load_message('data/inline_cleartext_signed.eml'), False), |
