python bindings: ctx.decrypt
* lang/python/src/core.py: Fixed methods of detecting whether verify is a boolean variable or a list. * Added methods of catching the missing keys exceptions. * Still retained PEP8 compliance (which might have been where one or two problems crept in). * Though this is essentially the correct behaviour, it still does not quite fit the otiginal test; so that will also require some adjustment. Tested-by: Ben McGinnes <ben@adversary.org> Signed-off-by: Ben McGinnes <ben@adversary.org>
This commit is contained in:
parent
ce045a1ef9
commit
11403a4635
@ -366,6 +366,8 @@ class Context(GpgmeWrapper):
|
|||||||
GPGMEError -- as signaled by the underlying library
|
GPGMEError -- as signaled by the underlying library
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
sink_result = None
|
||||||
|
verify_sigs = None
|
||||||
plaintext = sink if sink else Data()
|
plaintext = sink if sink else Data()
|
||||||
|
|
||||||
if passphrase is not None:
|
if passphrase is not None:
|
||||||
@ -379,13 +381,29 @@ class Context(GpgmeWrapper):
|
|||||||
self.set_passphrase_cb(passphrase_cb)
|
self.set_passphrase_cb(passphrase_cb)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if verify:
|
if verify is not None:
|
||||||
|
if isinstance(verify, bool) is True:
|
||||||
|
if verify is False:
|
||||||
|
verify = True
|
||||||
|
sink_result = True
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
elif isinstance(verify, list) is True:
|
||||||
|
if len(verify) > 0:
|
||||||
|
verify_sigs = True
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
verify = True
|
||||||
self.op_decrypt_verify(ciphertext, plaintext)
|
self.op_decrypt_verify(ciphertext, plaintext)
|
||||||
else:
|
else:
|
||||||
self.op_decrypt(ciphertext, plaintext)
|
self.op_decrypt(ciphertext, plaintext)
|
||||||
except errors.GPGMEError as e:
|
except errors.GPGMEError as e:
|
||||||
result = self.op_decrypt_result()
|
result = self.op_decrypt_result()
|
||||||
verify_result = self.op_verify_result() if verify else None
|
if verify is not None and sink_result is None:
|
||||||
|
verify_result = self.op_verify_result()
|
||||||
|
else:
|
||||||
|
verify_result = None
|
||||||
# Just raise the error, but attach the results first.
|
# Just raise the error, but attach the results first.
|
||||||
e.results = (self.__read__(sink, plaintext), result, verify_result)
|
e.results = (self.__read__(sink, plaintext), result, verify_result)
|
||||||
raise e
|
raise e
|
||||||
@ -396,19 +414,25 @@ class Context(GpgmeWrapper):
|
|||||||
self.set_passphrase_cb(*old_passphrase_cb[1:])
|
self.set_passphrase_cb(*old_passphrase_cb[1:])
|
||||||
|
|
||||||
result = self.op_decrypt_result()
|
result = self.op_decrypt_result()
|
||||||
verify_result = self.op_verify_result() if verify else None
|
|
||||||
|
if verify is not None and sink_result is None:
|
||||||
|
verify_result = self.op_verify_result()
|
||||||
|
else:
|
||||||
|
verify_result = None
|
||||||
|
|
||||||
results = (self.__read__(sink, plaintext), result, verify_result)
|
results = (self.__read__(sink, plaintext), result, verify_result)
|
||||||
|
|
||||||
if result.unsupported_algorithm:
|
if result.unsupported_algorithm:
|
||||||
raise errors.UnsupportedAlgorithm(
|
raise errors.UnsupportedAlgorithm(result.unsupported_algorithm,
|
||||||
result.unsupported_algorithm, results=results)
|
results=results)
|
||||||
|
|
||||||
if verify:
|
if verify:
|
||||||
if any(s.status != errors.NO_ERROR
|
if any(s.status != errors.NO_ERROR
|
||||||
for s in verify_result.signatures):
|
for s in verify_result.signatures):
|
||||||
raise errors.BadSignatures(verify_result, results=results)
|
raise errors.BadSignatures(verify_result, results=results)
|
||||||
|
|
||||||
if not verify: # was: if verify and verify != True:
|
if verify_sigs is not None:
|
||||||
missing = list()
|
missing = []
|
||||||
for key in verify:
|
for key in verify:
|
||||||
ok = False
|
ok = False
|
||||||
for subkey in key.subkeys:
|
for subkey in key.subkeys:
|
||||||
@ -423,8 +447,28 @@ class Context(GpgmeWrapper):
|
|||||||
if not ok:
|
if not ok:
|
||||||
missing.append(key)
|
missing.append(key)
|
||||||
if missing:
|
if missing:
|
||||||
raise errors.MissingSignatures(
|
try:
|
||||||
verify_result, missing, results=results)
|
raise errors.MissingSignatures(verify_result, missing,
|
||||||
|
results=results)
|
||||||
|
except errors.MissingSignatures as miss_e:
|
||||||
|
mse = miss_e
|
||||||
|
mserr = "gpg.errors.MissingSignatures:"
|
||||||
|
print(mserr, miss_e, "\n")
|
||||||
|
# # The full details can then be found in mse.results,
|
||||||
|
# # mse.result, mse.missing if necessary.
|
||||||
|
# mse_list = []
|
||||||
|
# msp = "Missing signatures from: \n".format()
|
||||||
|
# print(msp)
|
||||||
|
# for key in mse.missing:
|
||||||
|
# mse_list.append(key.fpr)
|
||||||
|
# msl = []
|
||||||
|
# msl.append(key.fpr)
|
||||||
|
# for user in key.uids:
|
||||||
|
# msl.append(user.name)
|
||||||
|
# msl.append(user.email)
|
||||||
|
# # msl.append(user.uid)
|
||||||
|
# print(" ".join(msl))
|
||||||
|
return mse
|
||||||
|
|
||||||
return results
|
return results
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user