67
67
CRYPTO = False
68
68
69
69
# Perform object format-checking and add ability to handle/raise exceptions.
70
- import securesystemslib . formats
71
- import securesystemslib . exceptions
70
+ from . import formats
71
+ from . import exceptions
72
72
73
73
_SUPPORTED_ECDSA_SCHEMES = ['ecdsa-sha2-nistp256' ]
74
74
@@ -136,14 +136,14 @@ def generate_public_and_private(scheme='ecdsa-sha2-nistp256'):
136
136
"""
137
137
138
138
if not CRYPTO : # pragma: no cover
139
- raise securesystemslib . exceptions .UnsupportedLibraryError (NO_CRYPTO_MSG )
139
+ raise exceptions .UnsupportedLibraryError (NO_CRYPTO_MSG )
140
140
141
141
# Does 'scheme' have the correct format?
142
142
# Verify that 'scheme' is of the correct type, and that it's one of the
143
143
# supported ECDSA . It must conform to
144
144
# 'securesystemslib.formats.ECDSA_SCHEME_SCHEMA'. Raise
145
145
# 'securesystemslib.exceptions.FormatError' if the check fails.
146
- securesystemslib . formats .ECDSA_SCHEME_SCHEMA .check_match (scheme )
146
+ formats .ECDSA_SCHEME_SCHEMA .check_match (scheme )
147
147
148
148
public_key = None
149
149
private_key = None
@@ -158,7 +158,7 @@ def generate_public_and_private(scheme='ecdsa-sha2-nistp256'):
158
158
# The ECDSA_SCHEME_SCHEMA.check_match() above should have detected any
159
159
# invalid 'scheme'. This is a defensive check.
160
160
else : #pragma: no cover
161
- raise securesystemslib . exceptions .UnsupportedAlgorithmError ('An unsupported'
161
+ raise exceptions .UnsupportedAlgorithmError ('An unsupported'
162
162
' scheme specified: ' + repr (scheme ) + '.\n Supported'
163
163
' algorithms: ' + repr (_SUPPORTED_ECDSA_SCHEMES ))
164
164
@@ -225,19 +225,19 @@ def create_signature(public_key, private_key, data, scheme='ecdsa-sha2-nistp256'
225
225
"""
226
226
227
227
if not CRYPTO : # pragma: no cover
228
- raise securesystemslib . exceptions .UnsupportedLibraryError (NO_CRYPTO_MSG )
228
+ raise exceptions .UnsupportedLibraryError (NO_CRYPTO_MSG )
229
229
230
230
# Do 'public_key' and 'private_key' have the correct format?
231
231
# This check will ensure that the arguments conform to
232
232
# 'securesystemslib.formats.PEMECDSA_SCHEMA'. Raise
233
233
# 'securesystemslib.exceptions.FormatError' if the check fails.
234
- securesystemslib . formats .PEMECDSA_SCHEMA .check_match (public_key )
234
+ formats .PEMECDSA_SCHEMA .check_match (public_key )
235
235
236
236
# Is 'private_key' properly formatted?
237
- securesystemslib . formats .PEMECDSA_SCHEMA .check_match (private_key )
237
+ formats .PEMECDSA_SCHEMA .check_match (private_key )
238
238
239
239
# Is 'scheme' properly formatted?
240
- securesystemslib . formats .ECDSA_SCHEME_SCHEMA .check_match (scheme )
240
+ formats .ECDSA_SCHEME_SCHEMA .check_match (scheme )
241
241
242
242
# 'ecdsa-sha2-nistp256' is the only currently supported ECDSA scheme, so this
243
243
# if-clause isn't strictly needed. Nevertheless, the conditional statement
@@ -251,13 +251,13 @@ def create_signature(public_key, private_key, data, scheme='ecdsa-sha2-nistp256'
251
251
signature = private_key .sign (data , ec .ECDSA (hashes .SHA256 ()))
252
252
253
253
except TypeError as e :
254
- raise securesystemslib . exceptions .CryptoError ('Could not create'
254
+ raise exceptions .CryptoError ('Could not create'
255
255
' signature: ' + str (e ))
256
256
257
257
# A defensive check for an invalid 'scheme'. The
258
258
# ECDSA_SCHEME_SCHEMA.check_match() above should have already validated it.
259
259
else : #pragma: no cover
260
- raise securesystemslib . exceptions .UnsupportedAlgorithmError ('Unsupported'
260
+ raise exceptions .UnsupportedAlgorithmError ('Unsupported'
261
261
' signature scheme is specified: ' + repr (scheme ))
262
262
263
263
return signature , scheme
@@ -316,19 +316,19 @@ def verify_signature(public_key, scheme, signature, data):
316
316
"""
317
317
318
318
if not CRYPTO : # pragma: no cover
319
- raise securesystemslib . exceptions .UnsupportedLibraryError (NO_CRYPTO_MSG )
319
+ raise exceptions .UnsupportedLibraryError (NO_CRYPTO_MSG )
320
320
321
321
# Are the arguments properly formatted?
322
- # If not, raise 'securesystemslib. exceptions.FormatError'.
323
- securesystemslib . formats .PEMECDSA_SCHEMA .check_match (public_key )
324
- securesystemslib . formats .ECDSA_SCHEME_SCHEMA .check_match (scheme )
325
- securesystemslib . formats .ECDSASIGNATURE_SCHEMA .check_match (signature )
322
+ # If not, raise 'exceptions.FormatError'.
323
+ formats .PEMECDSA_SCHEMA .check_match (public_key )
324
+ formats .ECDSA_SCHEME_SCHEMA .check_match (scheme )
325
+ formats .ECDSASIGNATURE_SCHEMA .check_match (signature )
326
326
327
327
ecdsa_key = load_pem_public_key (public_key .encode ('utf-8' ),
328
328
backend = default_backend ())
329
329
330
330
if not isinstance (ecdsa_key , ec .EllipticCurvePublicKey ):
331
- raise securesystemslib . exceptions .FormatError ('Invalid ECDSA public'
331
+ raise exceptions .FormatError ('Invalid ECDSA public'
332
332
' key: ' + repr (public_key ))
333
333
334
334
else :
@@ -399,15 +399,15 @@ def create_ecdsa_public_and_private_from_pem(pem, password=None):
399
399
"""
400
400
401
401
if not CRYPTO : # pragma: no cover
402
- raise securesystemslib . exceptions .UnsupportedLibraryError (NO_CRYPTO_MSG )
402
+ raise exceptions .UnsupportedLibraryError (NO_CRYPTO_MSG )
403
403
404
404
# Does 'pem' have the correct format?
405
405
# This check will ensure 'pem' conforms to
406
406
# 'securesystemslib.formats.ECDSARSA_SCHEMA'.
407
- securesystemslib . formats .PEMECDSA_SCHEMA .check_match (pem )
407
+ formats .PEMECDSA_SCHEMA .check_match (pem )
408
408
409
409
if password is not None :
410
- securesystemslib . formats .PASSWORD_SCHEMA .check_match (password )
410
+ formats .PASSWORD_SCHEMA .check_match (password )
411
411
password = password .encode ('utf-8' )
412
412
413
413
else :
@@ -424,7 +424,7 @@ def create_ecdsa_public_and_private_from_pem(pem, password=None):
424
424
backend = default_backend ())
425
425
426
426
except (ValueError , cryptography .exceptions .UnsupportedAlgorithm ) as e :
427
- raise securesystemslib . exceptions .CryptoError ('Could not import private'
427
+ raise exceptions .CryptoError ('Could not import private'
428
428
' PEM.\n ' + str (e ))
429
429
430
430
public = private .public_key ()
@@ -486,14 +486,14 @@ def create_ecdsa_encrypted_pem(private_pem, passphrase):
486
486
"""
487
487
488
488
if not CRYPTO : # pragma: no cover
489
- raise securesystemslib . exceptions .UnsupportedLibraryError (NO_CRYPTO_MSG )
489
+ raise exceptions .UnsupportedLibraryError (NO_CRYPTO_MSG )
490
490
491
491
# Does 'private_key' have the correct format?
492
492
# Raise 'securesystemslib.exceptions.FormatError' if the check fails.
493
- securesystemslib . formats .PEMRSA_SCHEMA .check_match (private_pem )
493
+ formats .PEMRSA_SCHEMA .check_match (private_pem )
494
494
495
495
# Does 'passphrase' have the correct format?
496
- securesystemslib . formats .PASSWORD_SCHEMA .check_match (passphrase )
496
+ formats .PASSWORD_SCHEMA .check_match (passphrase )
497
497
498
498
encrypted_pem = None
499
499
0 commit comments