Skip to content

Commit 0ac207d

Browse files
Raise specialized WolfCryptApiError for failed API calls.
This exception takes the return value and converts that to a human-readable string (if error string support is compiled in.) The specialized exception is derived from WolfCryptError meaning that existing users will not notice the difference except for the nicer description.
1 parent ec8ce54 commit 0ac207d

8 files changed

Lines changed: 176 additions & 167 deletions

File tree

wolfcrypt/__init__.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,14 @@
4646
if top_level_py not in ["setup.py", "build_ffi.py"]:
4747
from wolfcrypt._ffi import ffi as _ffi
4848
from wolfcrypt._ffi import lib as _lib
49-
from wolfcrypt.exceptions import WolfCryptError
49+
from wolfcrypt.exceptions import WolfCryptApiError
5050

5151
if hasattr(_lib, 'WC_RNG_SEED_CB_ENABLED'):
5252
if _lib.WC_RNG_SEED_CB_ENABLED:
5353
ret = _lib.wc_SetSeed_Cb(_ffi.addressof(_lib, "wc_GenerateSeed"))
5454
if ret < 0:
55-
raise WolfCryptError("wc_SetSeed_Cb failed (%d)" % ret)
55+
raise WolfCryptApiError("wc_SetSeed_Cb failed", ret)
5656
if _lib.FIPS_ENABLED and _lib.FIPS_VERSION >= 5:
5757
ret = _lib.wolfCrypt_SetPrivateKeyReadEnable_fips(1, _lib.WC_KEYTYPE_ALL)
5858
if ret < 0:
59-
raise WolfCryptError("wolfCrypt_SetPrivateKeyReadEnable_fips failed"
60-
" (%d)" % ret)
59+
raise WolfCryptApiError("wolfCrypt_SetPrivateKeyReadEnable_fips failed", ret)

wolfcrypt/asn.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
from wolfcrypt._ffi import ffi as _ffi
2626
from wolfcrypt._ffi import lib as _lib
27-
from wolfcrypt.exceptions import WolfCryptError
27+
from wolfcrypt.exceptions import WolfCryptError, WolfCryptApiError
2828

2929
if _lib.SHA_ENABLED:
3030
from wolfcrypt.hashes import Sha
@@ -41,8 +41,7 @@ def pem_to_der(pem, pem_type):
4141
ret = _lib.wc_PemToDer(pem, len(pem), pem_type, der, _ffi.NULL,
4242
_ffi.NULL, _ffi.NULL)
4343
if ret != 0:
44-
err = "Error converting from PEM to DER. ({})".format(ret)
45-
raise WolfCryptError(err)
44+
raise WolfCryptApiError("Error converting from PEM to DER.", ret)
4645

4746
try:
4847
result = _ffi.buffer(der[0][0].buffer, der[0][0].length)[:]
@@ -54,15 +53,13 @@ def der_to_pem(der, pem_type):
5453
pem_length = _lib.wc_DerToPemEx(der, len(der), _ffi.NULL, 0, _ffi.NULL,
5554
pem_type)
5655
if pem_length <= 0:
57-
err = "Error getting required PEM buffer length. ({})".format(pem_length)
58-
raise WolfCryptError(err)
56+
raise WolfCryptApiError("Error getting required PEM buffer length.", pem_length)
5957

6058
pem = _ffi.new("byte[%d]" % pem_length)
6159
pem_length = _lib.wc_DerToPemEx(der, len(der), pem, pem_length,
6260
_ffi.NULL, pem_type)
6361
if pem_length <= 0:
64-
err = "Error converting from DER to PEM. ({})".format(pem_length)
65-
raise WolfCryptError(err)
62+
raise WolfCryptApiError("Error converting from DER to PEM.", pem_length)
6663

6764
return _ffi.buffer(pem, pem_length)[:]
6865

@@ -76,8 +73,7 @@ def hash_oid_from_class(hash_cls):
7673
elif _lib.SHA512_ENABLED and hash_cls == Sha512:
7774
return _lib.SHA512h
7875
else:
79-
err = "Unknown hash class {}.".format(hash_cls.__name__)
80-
raise WolfCryptError(err)
76+
raise WolfCryptError(f"Unknown hash class {hash_cls.__name__}")
8177

8278
def make_signature(data, hash_cls, key=None):
8379
hash_obj = hash_cls()
@@ -89,8 +85,7 @@ def make_signature(data, hash_cls, key=None):
8985
plaintext_len = _lib.wc_EncodeSignature(plaintext_sig, digest,
9086
len(digest), hash_oid)
9187
if plaintext_len == 0:
92-
err = "Error calling wc_EncodeSignature. ({})".format(plaintext_len)
93-
raise WolfCryptError(err)
88+
raise WolfCryptError(f"Error calling wc_EncodeSignature. ({plaintext_len})")
9489

9590
plaintext_sig = _ffi.buffer(plaintext_sig, plaintext_len)[:]
9691
if key:

0 commit comments

Comments
 (0)