diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/mailman_pgp/commands/eml_key.py | 2 | ||||
| -rw-r--r-- | src/mailman_pgp/commands/tests/test_key.py | 24 | ||||
| -rw-r--r-- | src/mailman_pgp/model/sighash.py | 2 | ||||
| -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 | ||||
| -rw-r--r-- | src/mailman_pgp/rules/signature.py | 2 | ||||
| -rw-r--r-- | src/mailman_pgp/workflows/subscription.py | 2 | ||||
| -rw-r--r-- | src/mailman_pgp/workflows/tests/test_base.py | 4 |
9 files changed, 139 insertions, 36 deletions
diff --git a/src/mailman_pgp/commands/eml_key.py b/src/mailman_pgp/commands/eml_key.py index bb107cf..9ea0384 100644 --- a/src/mailman_pgp/commands/eml_key.py +++ b/src/mailman_pgp/commands/eml_key.py @@ -33,9 +33,9 @@ from mailman_pgp.model.address import PGPAddress from mailman_pgp.model.list import PGPMailingList from mailman_pgp.pgp.mime import MIMEWrapper from mailman_pgp.pgp.wrapper import PGPWrapper -from mailman_pgp.workflows.pubkey import CONFIRM_REQUEST from mailman_pgp.workflows.key_change import (CHANGE_CONFIRM_REQUEST, KeyChangeWorkflow) +from mailman_pgp.workflows.pubkey import CONFIRM_REQUEST def _get_email(msg): diff --git a/src/mailman_pgp/commands/tests/test_key.py b/src/mailman_pgp/commands/tests/test_key.py index 83bf49e..4642bb5 100644 --- a/src/mailman_pgp/commands/tests/test_key.py +++ b/src/mailman_pgp/commands/tests/test_key.py @@ -143,8 +143,8 @@ class TestPreSubscription(unittest.TestCase): self.assertFalse(pgp_address.key_confirmed) items = get_queue_messages('virgin', expected_count=2) - if items[0].msg[ - 'Subject'] == 'The results of your email commands': # pragma: no cover + if (items[0].msg['Subject'] == + 'The results of your email commands'): # pragma: no cover results = items[0].msg confirm_request = items[1].msg else: @@ -187,8 +187,8 @@ class TestPreSubscription(unittest.TestCase): self.assertFalse(pgp_address.key_confirmed) items = get_queue_messages('virgin', expected_count=2) - if items[0].msg[ - 'Subject'] == 'The results of your email commands': # pragma: no cover + if (items[0].msg['Subject'] == + 'The results of your email commands'): # pragma: no cover results = items[0].msg confirm_request = items[1].msg else: @@ -576,8 +576,8 @@ class TestAfterSubscription(unittest.TestCase): make_testable_runner(CommandRunner, 'command').run() items = get_queue_messages('virgin', expected_count=2) - if items[0].msg[ - 'Subject'] == 'The results of your email commands': # pragma: no cover + if (items[0].msg['Subject'] == + 'The results of your email commands'): # pragma: no cover results = items[0].msg confirm_request = items[1].msg else: @@ -613,8 +613,8 @@ class TestAfterSubscription(unittest.TestCase): make_testable_runner(CommandRunner, 'command').run() items = get_queue_messages('virgin', expected_count=2) - if items[0].msg[ - 'Subject'] == 'The results of your email commands': # pragma: no cover + if (items[0].msg['Subject'] == + 'The results of your email commands'): # pragma: no cover results = items[0].msg confirm_request = items[1].msg else: @@ -648,8 +648,8 @@ class TestAfterSubscription(unittest.TestCase): make_testable_runner(CommandRunner, 'command').run() items = get_queue_messages('virgin', expected_count=2) - if items[0].msg[ - 'Subject'] == 'The results of your email commands': # pragma: no cover + if (items[0].msg['Subject'] == + 'The results of your email commands'): # pragma: no cover confirm_request = items[1].msg else: confirm_request = items[0].msg @@ -765,8 +765,8 @@ class TestGeneral(unittest.TestCase): listid='test.example.com') make_testable_runner(CommandRunner, 'command').run() items = get_queue_messages('virgin', expected_count=2) - if items[0].msg[ - 'Subject'] == 'The results of your email commands': # pragma: no cover + if (items[0].msg['Subject'] == + 'The results of your email commands'): # pragma: no cover pubkey_message = items[1].msg else: pubkey_message = items[0].msg diff --git a/src/mailman_pgp/model/sighash.py b/src/mailman_pgp/model/sighash.py index 0c61e38..2dddc02 100644 --- a/src/mailman_pgp/model/sighash.py +++ b/src/mailman_pgp/model/sighash.py @@ -16,7 +16,7 @@ # this program. If not, see <http://www.gnu.org/licenses/>. """""" -from sqlalchemy import LargeBinary, Column, String, DateTime +from sqlalchemy import Column, DateTime, LargeBinary, String from mailman_pgp.model.base import Base 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) diff --git a/src/mailman_pgp/rules/signature.py b/src/mailman_pgp/rules/signature.py index c8ef62d..7d0d348 100644 --- a/src/mailman_pgp/rules/signature.py +++ b/src/mailman_pgp/rules/signature.py @@ -27,10 +27,8 @@ from public import public from zope.component import getUtility from zope.interface import implementer -from mailman_pgp.database import query from mailman_pgp.model.address import PGPAddress from mailman_pgp.model.list import PGPMailingList -from mailman_pgp.model.sighash import PGPSigHash from mailman_pgp.pgp.wrapper import PGPWrapper log = logging.getLogger('mailman.plugin.pgp') diff --git a/src/mailman_pgp/workflows/subscription.py b/src/mailman_pgp/workflows/subscription.py index ad9a09d..809b7cb 100644 --- a/src/mailman_pgp/workflows/subscription.py +++ b/src/mailman_pgp/workflows/subscription.py @@ -25,7 +25,7 @@ from public import public from zope.interface import implementer from mailman_pgp.workflows.base import PGPMixin -from mailman_pgp.workflows.pubkey import SetPubkeyMixin, ConfirmPubkeyMixin +from mailman_pgp.workflows.pubkey import ConfirmPubkeyMixin, SetPubkeyMixin @public diff --git a/src/mailman_pgp/workflows/tests/test_base.py b/src/mailman_pgp/workflows/tests/test_base.py index 4b3ad26..15ed833 100644 --- a/src/mailman_pgp/workflows/tests/test_base.py +++ b/src/mailman_pgp/workflows/tests/test_base.py @@ -38,8 +38,8 @@ from mailman_pgp.pgp.tests.base import load_key from mailman_pgp.pgp.wrapper import PGPWrapper from mailman_pgp.testing.layers import PGPConfigLayer from mailman_pgp.workflows.base import (PGPMixin) -from mailman_pgp.workflows.pubkey import (KEY_REQUEST, SetPubkeyMixin, - ConfirmPubkeyMixin) +from mailman_pgp.workflows.pubkey import (ConfirmPubkeyMixin, KEY_REQUEST, + SetPubkeyMixin) class PubkeyMixinTestSetup(): |
