summaryrefslogtreecommitdiff
path: root/alot/crypto.py
diff options
context:
space:
mode:
authorStanislav Ochotnicky <sochotnicky@redhat.com>2013-09-02 14:51:21 +0200
committerPatrick Totzke <patricktotzke@gmail.com>2013-10-30 20:39:59 +0000
commitb968e3f31b8a6cc39e419d5e3e1a06b1e2f720ce (patch)
tree90eb3d26ecbdd7886777202fba0ff974d413b973 /alot/crypto.py
parent9bbdc29d98976fe0eeddf49cea41e6cad431fba1 (diff)
crypto: eliminate invalid keys for gpgme.ERR_AMBIGUOUS_NAME in get_key
Previously when Context.get_key raised gpgme.ERR_AMBIGUOUS_NAME crypto.get_key would not verify the ambiguous keys are actually valid for signing/encryption. Now when gpgme.get_key raises ERR_AMBIGUOUS_NAME we elimitate those invalid keys. If there is only one valid key left we return it, otherwise re-raise
Diffstat (limited to 'alot/crypto.py')
-rw-r--r--alot/crypto.py29
1 files changed, 25 insertions, 4 deletions
diff --git a/alot/crypto.py b/alot/crypto.py
index 8346b4ec..e75c9b36 100644
--- a/alot/crypto.py
+++ b/alot/crypto.py
@@ -124,10 +124,31 @@ def get_key(keyid, validate=False, encrypt=False, sign=False):
validate_key(key, encrypt=encrypt, sign=sign)
except gpgme.GpgmeError as e:
if e.code == gpgme.ERR_AMBIGUOUS_NAME:
- raise GPGProblem(("More than one key found matching this filter." +
- " Please be more specific (use a key ID like " +
- "4AC8EE1D)."),
- code=GPGCode.AMBIGUOUS_NAME)
+ keys = list_keys(hint=keyid)
+ valid_key = None
+ for k in keys:
+ try:
+ validate_key(k, encrypt=encrypt, sign=sign)
+ except GPGProblem:
+ # if the key is invalid for given action skip it
+ continue
+
+ if valid_key:
+ # we have already found one valid key and now we find
+ # another?
+ raise GPGProblem(("More than one key found matching " +
+ "this filter. Please be more " +
+ "specific (use a key ID like " +
+ "4AC8EE1D)."),
+ code=GPGCode.AMBIGUOUS_NAME)
+ valid_key = k
+
+ if not valid_key:
+ # there were multiple keys found but none of them are valid for
+ # given action
+ raise GPGProblem("Can not find usable key for \'" + keyid + "\'.",
+ code=GPGCode.NOT_FOUND)
+ return valid_key
elif e.code == gpgme.ERR_INV_VALUE or e.code == gpgme.ERR_EOF:
raise GPGProblem("Can not find key for \'" + keyid + "\'.",
code=GPGCode.NOT_FOUND)