diff options
| author | J08nY | 2017-07-17 19:03:08 +0200 |
|---|---|---|
| committer | J08nY | 2017-07-17 19:03:08 +0200 |
| commit | 8fe73c6364c873e1bd52286353c3f79a4486127a (patch) | |
| tree | b6b51769fb2f45150a83be7d7231f60ed9b6b88a /src/mailman_pgp/pgp | |
| parent | 8de6bac71e3966d89523dbdb4449efe86af3586f (diff) | |
| download | mailman-pgp-8fe73c6364c873e1bd52286353c3f79a4486127a.tar.gz mailman-pgp-8fe73c6364c873e1bd52286353c3f79a4486127a.tar.zst mailman-pgp-8fe73c6364c873e1bd52286353c3f79a4486127a.zip | |
Diffstat (limited to 'src/mailman_pgp/pgp')
| -rw-r--r-- | src/mailman_pgp/pgp/inline.py | 12 | ||||
| -rw-r--r-- | src/mailman_pgp/pgp/mime.py | 16 | ||||
| -rw-r--r-- | src/mailman_pgp/pgp/wrapper.py | 111 |
3 files changed, 122 insertions, 17 deletions
diff --git a/src/mailman_pgp/pgp/inline.py b/src/mailman_pgp/pgp/inline.py index ccc8176..372204a 100644 --- a/src/mailman_pgp/pgp/inline.py +++ b/src/mailman_pgp/pgp/inline.py @@ -258,12 +258,18 @@ class InlineWrapper: def sign_encrypt(self, key, *keys, hash=None, cipher=SymmetricKeyAlgorithm.AES256): """ + Sign and encrypt the message, in one go. - :param key: - :param keys: + :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: + :type hash: pgpy.constants.HashAlgorithm :param cipher: - :return: + :type cipher: pgpy.constants.SymmetricKeyAlgorithm + :return: The signed + encrypted message. + :rtype: mailman.email.message.Message """ if len(keys) == 0: raise ValueError('At least one key necessary.') diff --git a/src/mailman_pgp/pgp/mime.py b/src/mailman_pgp/pgp/mime.py index a7e31b8..611c1f2 100644 --- a/src/mailman_pgp/pgp/mime.py +++ b/src/mailman_pgp/pgp/mime.py @@ -116,6 +116,12 @@ class MIMEWrapper: return self.is_encrypted() def is_keys(self): + """ + Whether the message has only keys as per RFC3156 section 7. + + :return: If the message is keys. + :rtype: bool + """ for part in walk(self.msg): if (not part.is_multipart() # noqa and part.get_content_type() != MIMEWrapper._keys_type): @@ -149,10 +155,12 @@ class MIMEWrapper: def attach_key(self, key): """ + Attach a key to this message, as per RFC3156 section 7. - :param key: + :param key: A key to attach. :type key: pgpy.PGPKey - :return: + :return: The message with the key attached. + :rtype: mailman.email.message.Message """ filename = '0x' + key.fingerprint.keyid + '.asc' key_part = MIMEApplication(_data=str(key), @@ -220,7 +228,7 @@ class MIMEWrapper: :param key: The key to sign with. :type key: pgpy.PGPKey :param hash: - :type hash: HashAlgorithm + :type hash: pgpy.constants.HashAlgorithm :return: The signed message. :rtype: mailman.email.message.Message """ @@ -293,7 +301,7 @@ class MIMEWrapper: :param keys: The key/s to encrypt with. :type keys: pgpy.PGPKey :param cipher: The symmetric cipher to use. - :type cipher: SymmetricKeyAlgorithm + :type cipher: pgpy.constants.SymmetricKeyAlgorithm :return: The encrypted message. :rtype: mailman.email.message.Message """ diff --git a/src/mailman_pgp/pgp/wrapper.py b/src/mailman_pgp/pgp/wrapper.py index 6e8a8f9..f5cc8e1 100644 --- a/src/mailman_pgp/pgp/wrapper.py +++ b/src/mailman_pgp/pgp/wrapper.py @@ -51,28 +51,56 @@ class PGPWrapper(): return self.default.get_payload() def is_signed(self): + """ + Whether this message is signed. + + :return: If the message is signed. + :rtype: bool + """ return self.mime.is_signed() or self.inline.is_signed() def has_signature(self): + """ + Whether some parts of the message are signed. + + :return: If some parts of the message are signed. + :rtype: bool + """ return self.mime.has_signature() or self.inline.has_signature() def get_signed(self): + """ + Get the signed content of the message. + + :return: The signed contents of the message. + :rtype: Generator[str] + """ if self.mime.is_signed(): yield from self.mime.get_signed() elif self.inline.is_signed(): yield from self.inline.get_signed() def sign(self, key, **kwargs): + """ + 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 + """ return self.default.sign(key, **kwargs) def verify(self, key): """ - Verify the signature of this message with key. + Verify the signatures of this message with key. :param key: The key to verify with. :type key: pgpy.PGPKey - :return: The verified signature. - :rtype: generator of pgpy.types.SignatureVerification + :return: The verified signatures. + :rtype: Generator[pgpy.types.SignatureVerification] """ if self.mime.is_signed(): yield from self.mime.verify(key) @@ -86,12 +114,34 @@ class PGPWrapper(): verification in self.verify(key)) def is_encrypted(self): + """ + Whether the message is encrypted. + + :return: If the message is encrypted. + :rtype: bool + """ return self.mime.is_encrypted() or self.inline.is_encrypted() def has_encryption(self): + """ + Whether some parts of the message are encrypted. + + :return: If some parts of the message are encrypted. + :rtype: bool + """ return self.mime.has_encryption() or self.inline.has_encryption() def encrypt(self, *keys, **kwargs): + """ + 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 + """ return self.default.encrypt(*keys, **kwargs) def decrypt(self, key): @@ -101,17 +151,63 @@ class PGPWrapper(): :param key: The key to decrypt with. :type key: pgpy.PGPKey :return: The decrypted message. - :rtype: PGPMessage + :rtype: mailman.email.message.Message """ if self.mime.is_encrypted(): return self.mime.decrypt(key) elif self.inline.is_encrypted(): return self.inline.decrypt(key) + def sign_encrypt(self, key, *keys, **kwargs): + """ + Sign and encrypt the 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: + :type hash: pgpy.constants.HashAlgorithm + :param cipher: + :type cipher: pgpy.constants.SymmetricKeyAlgorithm + :return: The signed + encrypted message. + :rtype: mailman.email.message.Message + """ + return self.default.sign_encrypt(key, *keys, **kwargs) + + def sign_then_encrypt(self, key, *keys, **kwargs): + """ + Sign then encrypt the message. + + :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: + :type hash: pgpy.constants.HashAlgorithm + :param cipher: + :type cipher: pgpy.constants.SymmetricKeyAlgorithm + :return: The signed + encrypted message. + :rtype: mailman.email.message.Message + """ + return self.default.sign_then_encrypt(key, *keys, **kwargs) + def is_keys(self): + """ + Whether the message is all keys (all parts). + + :return: If the message is keys. + :rtype: bool + """ return self.mime.is_keys() or self.inline.is_keys() def has_keys(self): + """ + Whether the message contains public or private keys. + + :return: If the message contains keys. + :rtype: bool + """ return self.mime.has_keys() or self.inline.has_keys() def keys(self): @@ -119,14 +215,9 @@ class PGPWrapper(): Get the collection of keys in this message. :return: A collection of keys. + :rtype: Generator[pgpy.PGPKey] """ if self.mime.has_keys(): yield from self.mime.keys() elif self.inline.has_keys(): yield from self.inline.keys() - - def sign_encrypt(self, key, *keys, **kwargs): - return self.default.sign_encrypt(key, *keys, **kwargs) - - def sign_then_encrypt(self, key, *keys, **kwargs): - return self.default.sign_then_encrypt(key, *keys, **kwargs) |
