diff options
author | Ram Rachum <ram@rachum.com> | 2020-06-19 23:39:00 +0300 |
---|---|---|
committer | Sybren A. Stüvel <sybren@stuvel.eu> | 2021-02-24 13:19:55 +0100 |
commit | 1a5b2d166fc95e5f3f07fdfec075acdf4d0eda92 (patch) | |
tree | 94973309e96f59a1367819c51240e2059aaca288 /rsa | |
parent | b8ac79fdbc28d3ad5e15c50930d223e98df10487 (diff) | |
download | rsa-git-1a5b2d166fc95e5f3f07fdfec075acdf4d0eda92.tar.gz |
Fix exception causes all over the codebase
The mistake is this: In some parts of the code, an exception is being
caught and replaced with a more user-friendly error. In these cases the
syntax `raise new_error from old_error` needs to be used.
Python's exception chaining means it shows not only the traceback of the
current exception, but that of the original exception (and possibly
more.) This is regardless of `raise from`. The usage of `raise from`
tells Python to put a more accurate message between the tracebacks.
Instead of this:
During handling of the above exception, another exception occurred:
You'll get this:
The above exception was the direct cause of the following exception:
The first is inaccurate, because it signifies a bug in the
exception-handling code itself, which is a separate situation than
wrapping an exception.
Diffstat (limited to 'rsa')
-rw-r--r-- | rsa/cli.py | 8 | ||||
-rw-r--r-- | rsa/key.py | 6 | ||||
-rw-r--r-- | rsa/pkcs1_v2.py | 4 |
3 files changed, 9 insertions, 9 deletions
@@ -58,10 +58,10 @@ def keygen() -> None: try: keysize = int(cli_args[0]) - except ValueError: + except ValueError as ex: parser.print_help() print('Not a valid number: %s' % cli_args[0], file=sys.stderr) - raise SystemExit(1) + raise SystemExit(1) from ex print('Generating %i-bit key' % keysize, file=sys.stderr) (pub_key, priv_key) = rsa.newkeys(keysize) @@ -280,8 +280,8 @@ class VerifyOperation(CryptoOperation): try: rsa.verify(indata, signature, pub_key) - except rsa.VerificationError: - raise SystemExit('Verification failed.') + except rsa.VerificationError as ex: + raise SystemExit('Verification failed.') from ex print('Verification OK', file=sys.stderr) @@ -131,10 +131,10 @@ class AbstractKey: try: return methods[file_format] - except KeyError: + except KeyError as ex: formats = ', '.join(sorted(methods.keys())) raise ValueError('Unsupported format: %r, try one of %s' % (file_format, - formats)) + formats)) from ex def save_pkcs1(self, format: str = 'PEM') -> bytes: """Saves the key in PKCS#1 DER or PEM format. @@ -703,7 +703,7 @@ def calculate_keys_custom_exponent(p: int, q: int, exponent: int) -> typing.Tupl raise rsa.common.NotRelativePrimeError( exponent, phi_n, ex.d, msg="e (%d) and phi_n (%d) are not relatively prime (divider=%i)" % - (exponent, phi_n, ex.d)) + (exponent, phi_n, ex.d)) from ex if (exponent * d) % phi_n != 1: raise ValueError("e (%d) and d (%d) are not mult. inv. modulo " diff --git a/rsa/pkcs1_v2.py b/rsa/pkcs1_v2.py index f780aff..ed616f5 100644 --- a/rsa/pkcs1_v2.py +++ b/rsa/pkcs1_v2.py @@ -49,12 +49,12 @@ def mgf1(seed: bytes, length: int, hasher: str = 'SHA-1') -> bytes: try: hash_length = pkcs1.HASH_METHODS[hasher]().digest_size - except KeyError: + except KeyError as ex: raise ValueError( 'Invalid `hasher` specified. Please select one of: {hash_list}'.format( hash_list=', '.join(sorted(pkcs1.HASH_METHODS.keys())) ) - ) + ) from ex # If l > 2^32(hLen), output "mask too long" and stop. if length > (2**32 * hash_length): |