summaryrefslogtreecommitdiff
path: root/alot/crypto.py
diff options
context:
space:
mode:
Diffstat (limited to 'alot/crypto.py')
-rw-r--r--alot/crypto.py21
1 files changed, 19 insertions, 2 deletions
diff --git a/alot/crypto.py b/alot/crypto.py
index e8595d90..34bdccb5 100644
--- a/alot/crypto.py
+++ b/alot/crypto.py
@@ -157,7 +157,7 @@ def detached_signature_for(plaintext_str, keys):
return sign_result.signatures, sigblob
-def encrypt(plaintext_str, keys=None):
+def encrypt(plaintext_str, keys):
"""Encrypt data and return the encrypted form.
:param str plaintext_str: the mail to encrypt
@@ -166,12 +166,29 @@ def encrypt(plaintext_str, keys=None):
:returns: encrypted mail
:rtype: str
"""
+ assert keys, 'Must provide at least one key to encrypt with'
ctx = gpg.core.Context(armor=True)
out = ctx.encrypt(plaintext_str, recipients=keys, sign=False,
always_trust=True)[0]
return out
+NO_ERROR = None
+
+
+def bad_signatures_to_str(error):
+ """
+ Convert a bad signature exception to a text message.
+ This is a workaround for gpg not handling non-ascii data correctly.
+
+ :param BadSignatures error: BadSignatures exception
+ """
+ return ", ".join("{}: {}".format(s.fpr,
+ "Bad signature for key(s)")
+ for s in error.result.signatures
+ if s.status != NO_ERROR)
+
+
def verify_detached(message, signature):
"""Verifies whether the message is authentic by checking the signature.
@@ -186,7 +203,7 @@ def verify_detached(message, signature):
verify_results = ctx.verify(message, signature)[1]
return verify_results.signatures
except gpg.errors.BadSignatures as e:
- raise GPGProblem(str(e), code=GPGCode.BAD_SIGNATURE)
+ raise GPGProblem(bad_signatures_to_str(e), code=GPGCode.BAD_SIGNATURE)
except gpg.errors.GPGMEError as e:
raise GPGProblem(str(e), code=e.getcode())