Skip to content

Commit f0d8fbb

Browse files
author
Jussi Kukkonen
committed
imports: Fix external imports (crypto, nacl)
Modify the way nacl and cryptography imports are made to make them compatible with vendoring tool.
1 parent 2e7ed4f commit f0d8fbb

File tree

8 files changed

+38
-37
lines changed

8 files changed

+38
-37
lines changed

securesystemslib/ecdsa_keys.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
from cryptography.hazmat.primitives.serialization import load_pem_public_key
5252
from cryptography.hazmat.primitives.serialization import load_pem_private_key
5353

54-
import cryptography.exceptions
54+
from cryptography.exceptions import (InvalidSignature, UnsupportedAlgorithm)
5555

5656
_SCHEME_HASHER = {
5757
'ecdsa-sha2-nistp256': ec.ECDSA(hashes.SHA256()),
@@ -335,7 +335,7 @@ def verify_signature(public_key, scheme, signature, data):
335335
ecdsa_key.verify(signature, data, _SCHEME_HASHER[scheme])
336336
return True
337337

338-
except (TypeError, cryptography.exceptions.InvalidSignature):
338+
except (TypeError, InvalidSignature):
339339
return False
340340

341341

@@ -418,7 +418,7 @@ def create_ecdsa_public_and_private_from_pem(pem, password=None):
418418
private = load_pem_private_key(pem.encode('utf-8'), password=password,
419419
backend=default_backend())
420420

421-
except (ValueError, cryptography.exceptions.UnsupportedAlgorithm) as e:
421+
except (ValueError, UnsupportedAlgorithm) as e:
422422
raise exceptions.CryptoError('Could not import private'
423423
' PEM.\n' + str(e))
424424

securesystemslib/ed25519_keys.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,10 @@
7272
NACL = True
7373
NO_NACL_MSG = "ed25519 key support requires the nacl library"
7474
try:
75-
import nacl.signing
76-
import nacl.encoding
75+
from nacl.encoding import RawEncoder
76+
from nacl.signing import (SigningKey, VerifyKey)
77+
# avoid conflicts with own exceptions of same name
78+
from nacl import exceptions as nacl_exceptions
7779
except ImportError:
7880
NACL = False
7981

@@ -142,8 +144,8 @@ def generate_public_and_private():
142144

143145
# Generate the public key. PyNaCl (i.e., 'nacl' module) performs the actual
144146
# key generation.
145-
nacl_key = nacl.signing.SigningKey(seed)
146-
public = nacl_key.verify_key.encode(encoder=nacl.encoding.RawEncoder())
147+
nacl_key = SigningKey(seed)
148+
public = nacl_key.verify_key.encode(encoder=RawEncoder())
147149

148150
return public, seed
149151

@@ -233,11 +235,11 @@ def create_signature(public_key, private_key, data, scheme):
233235
# statement to accommodate schemes that might be added in the future.
234236
if scheme == 'ed25519':
235237
try:
236-
nacl_key = nacl.signing.SigningKey(private_key)
238+
nacl_key = SigningKey(private_key)
237239
nacl_sig = nacl_key.sign(data)
238240
signature = nacl_sig.signature
239241

240-
except (ValueError, TypeError, nacl.exceptions.CryptoError) as e:
242+
except (ValueError, TypeError, nacl_exceptions.CryptoError) as e:
241243
raise exceptions.CryptoError('An "ed25519" signature'
242244
' could not be created with PyNaCl.' + str(e))
243245

@@ -325,11 +327,11 @@ def verify_signature(public_key, scheme, signature, data):
325327
if scheme in _SUPPORTED_ED25519_SIGNING_SCHEMES:
326328
if NACL:
327329
try:
328-
nacl_verify_key = nacl.signing.VerifyKey(public)
330+
nacl_verify_key = VerifyKey(public)
329331
nacl_verify_key.verify(data, signature)
330332
valid_signature = True
331333

332-
except nacl.exceptions.BadSignatureError:
334+
except nacl_exceptions.BadSignatureError:
333335
pass
334336

335337
# Verify 'ed25519' signature with the pure Python implementation.

securesystemslib/gpg/dsa.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
CRYPTO = True
2020
NO_CRYPTO_MSG = 'DSA key support for GPG requires the cryptography library'
2121
try:
22-
import cryptography.hazmat.primitives.asymmetric.dsa as dsa
23-
import cryptography.hazmat.backends as backends
24-
import cryptography.hazmat.primitives.asymmetric.utils as dsautils
25-
import cryptography.exceptions
22+
from cryptography.exceptions import InvalidSignature
23+
from cryptography.hazmat import backends
24+
from cryptography.hazmat.primitives.asymmetric import dsa
25+
from cryptography.hazmat.primitives.asymmetric import utils as dsautils
2626
except ImportError:
2727
CRYPTO = False
2828

@@ -248,5 +248,5 @@ def verify_signature(signature_object, pubkey_info, content,
248248
dsautils.Prehashed(hasher())
249249
)
250250
return True
251-
except cryptography.exceptions.InvalidSignature:
251+
except InvalidSignature:
252252
return False

securesystemslib/gpg/eddsa.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
CRYPTO = True
2727
NO_CRYPTO_MSG = 'EdDSA key support for GPG requires the cryptography library'
2828
try:
29-
import cryptography.hazmat.primitives.asymmetric.ed25519 as pyca_ed25519
30-
import cryptography.exceptions
29+
from cryptography.hazmat.primitives.asymmetric import ed25519 as pyca_ed25519
30+
from cryptography.exceptions import InvalidSignature
3131
except ImportError:
3232
CRYPTO = False
3333

@@ -242,5 +242,5 @@ def verify_signature(signature_object, pubkey_info, content,
242242
)
243243
return True
244244

245-
except cryptography.exceptions.InvalidSignature:
245+
except InvalidSignature:
246246
return False

securesystemslib/gpg/rsa.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
CRYPTO = True
2020
NO_CRYPTO_MSG = 'RSA key support for GPG requires the cryptography library'
2121
try:
22-
import cryptography.hazmat.primitives.asymmetric.rsa as rsa
23-
import cryptography.hazmat.backends as backends
24-
import cryptography.hazmat.primitives.asymmetric.padding as padding
25-
import cryptography.hazmat.primitives.asymmetric.utils as utils
26-
import cryptography.exceptions
22+
from cryptography.hazmat.primitives.asymmetric import rsa
23+
from cryptography.hazmat import backends
24+
from cryptography.hazmat.primitives.asymmetric import padding
25+
from cryptography.hazmat.primitives.asymmetric import utils
26+
from cryptography.exceptions import InvalidSignature
2727
except ImportError:
2828
CRYPTO = False
2929

@@ -221,5 +221,5 @@ def verify_signature(signature_object, pubkey_info, content,
221221
utils.Prehashed(hasher())
222222
)
223223
return True
224-
except cryptography.exceptions.InvalidSignature:
224+
except InvalidSignature:
225225
return False

securesystemslib/gpg/util.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
CRYPTO = True
2525
NO_CRYPTO_MSG = 'gpg.utils requires the cryptography library'
2626
try:
27-
import cryptography.hazmat.backends as backends
28-
import cryptography.hazmat.primitives.hashes as hashing
27+
from cryptography.hazmat import backends
28+
from cryptography.hazmat.primitives import hashes as hashing
2929
except ImportError:
3030
CRYPTO = False
3131

securesystemslib/hash.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,8 @@
4747

4848
# If `pyca_crypto` is installed, add it to supported libraries
4949
try:
50-
import cryptography.exceptions
51-
import cryptography.hazmat.backends
52-
import cryptography.hazmat.primitives.hashes as _pyca_hashes
50+
from cryptography.hazmat.backends import default_backend
51+
from cryptography.hazmat.primitives import hashes as _pyca_hashes
5352
import binascii
5453

5554
# Dictionary of `pyca/cryptography` supported hash algorithms.
@@ -204,8 +203,7 @@ def digest(algorithm=DEFAULT_HASH_ALGORITHM, hash_library=DEFAULT_HASH_LIBRARY):
204203
try:
205204
hash_algorithm = PYCA_DIGEST_OBJECTS_CACHE[algorithm]()
206205
return PycaDiggestWrapper(
207-
cryptography.hazmat.primitives.hashes.Hash(hash_algorithm,
208-
cryptography.hazmat.backends.default_backend()))
206+
_pyca_hashes.Hash(hash_algorithm, default_backend()))
209207

210208
except KeyError:
211209
raise exceptions.UnsupportedAlgorithmError(algorithm)

securesystemslib/rsa_keys.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@
7272
from cryptography.hazmat.backends import default_backend
7373

7474
# Import Exception classes need to catch pyca/cryptography exceptions.
75-
import cryptography.exceptions
75+
from cryptography.exceptions import (
76+
InvalidSignature, UnsupportedAlgorithm)
7677

7778
# 'cryptography.hazmat.primitives.asymmetric' (i.e., pyca/cryptography's
7879
# public-key cryptography modules) supports algorithms like the Digital
@@ -369,7 +370,7 @@ def create_rsa_signature(private_key, data, scheme='rsassa-pss-sha256'):
369370
# serialized key is of a type that is not supported by the backend, or if
370371
# the key is encrypted with a symmetric cipher that is not supported by
371372
# the backend.
372-
except cryptography.exceptions.UnsupportedAlgorithm: # pragma: no cover
373+
except UnsupportedAlgorithm: # pragma: no cover
373374
raise exceptions.CryptoError('The private key is'
374375
' encrypted with an unsupported algorithm.')
375376

@@ -481,11 +482,11 @@ def verify_rsa_signature(signature, signature_scheme, public_key, data):
481482

482483
return True
483484

484-
except cryptography.exceptions.InvalidSignature:
485+
except InvalidSignature:
485486
return False
486487

487488
# Raised by load_pem_public_key().
488-
except (ValueError, cryptography.exceptions.UnsupportedAlgorithm) as e:
489+
except (ValueError, UnsupportedAlgorithm) as e:
489490
raise exceptions.CryptoError('The PEM could not be'
490491
' decoded successfully, or contained an unsupported key type: ' + str(e))
491492

@@ -670,7 +671,7 @@ def create_rsa_public_and_private_from_pem(pem, passphrase=None):
670671
# Or if the key was encrypted but no password was supplied.
671672
# UnsupportedAlgorithm: If the private key (or if the key is encrypted with
672673
# an unsupported symmetric cipher) is not supported by the backend.
673-
except (ValueError, TypeError, cryptography.exceptions.UnsupportedAlgorithm) as e:
674+
except (ValueError, TypeError, UnsupportedAlgorithm) as e:
674675
# Raise 'securesystemslib.exceptions.CryptoError' and pyca/cryptography's
675676
# exception message. Avoid propogating pyca/cryptography's exception trace
676677
# to avoid revealing sensitive error.

0 commit comments

Comments
 (0)