Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
cose_public_key = cbor2.loads(key_bytes)
if ALG_KEY not in cose_public_key:
raise COSEKeyException(
'Public key missing required algorithm parameter.')
alg = cose_public_key[ALG_KEY]
if alg == COSE_ALG_ES256:
X_KEY = -2
Y_KEY = -3
required_keys = {ALG_KEY, X_KEY, Y_KEY}
if not set(cose_public_key.keys()).issuperset(required_keys):
raise COSEKeyException('Public key must match COSE_Key spec.')
if len(cose_public_key[X_KEY]) != 32:
raise RegistrationRejectedException('Bad public key.')
x = int(codecs.encode(cose_public_key[X_KEY], 'hex'), 16)
if len(cose_public_key[Y_KEY]) != 32:
raise RegistrationRejectedException('Bad public key.')
y = int(codecs.encode(cose_public_key[Y_KEY], 'hex'), 16)
return alg, EllipticCurvePublicNumbers(
x, y, SECP256R1()).public_key(backend=default_backend())
elif alg in (COSE_ALG_PS256, COSE_ALG_RS256):
E_KEY = -2
N_KEY = -1
required_keys = {ALG_KEY, E_KEY, N_KEY}
def _load_cose_public_key(key_bytes):
ALG_KEY = 3
cose_public_key = cbor2.loads(key_bytes)
if ALG_KEY not in cose_public_key:
raise COSEKeyException(
'Public key missing required algorithm parameter.')
alg = cose_public_key[ALG_KEY]
if alg == COSE_ALG_ES256:
X_KEY = -2
Y_KEY = -3
required_keys = {ALG_KEY, X_KEY, Y_KEY}
if not set(cose_public_key.keys()).issuperset(required_keys):
raise COSEKeyException('Public key must match COSE_Key spec.')
if len(cose_public_key[X_KEY]) != 32:
raise RegistrationRejectedException('Bad public key.')
x = int(codecs.encode(cose_public_key[X_KEY], 'hex'), 16)
x = int(codecs.encode(cose_public_key[X_KEY], 'hex'), 16)
if len(cose_public_key[Y_KEY]) != 32:
raise RegistrationRejectedException('Bad public key.')
y = int(codecs.encode(cose_public_key[Y_KEY], 'hex'), 16)
return alg, EllipticCurvePublicNumbers(
x, y, SECP256R1()).public_key(backend=default_backend())
elif alg in (COSE_ALG_PS256, COSE_ALG_RS256):
E_KEY = -2
N_KEY = -1
required_keys = {ALG_KEY, E_KEY, N_KEY}
if not set(cose_public_key.keys()).issuperset(required_keys):
raise COSEKeyException('Public key must match COSE_Key spec.')
if len(cose_public_key[E_KEY]) != 3 or len(cose_public_key[N_KEY]) != 256:
raise COSEKeyException('Bad public key.')
e = int(codecs.encode(cose_public_key[E_KEY], 'hex'), 16)
n = int(codecs.encode(cose_public_key[N_KEY], 'hex'), 16)
return alg, RSAPublicNumbers(e,
n).public_key(backend=default_backend())
else:
raise COSEKeyException('Unsupported algorithm.')
raise RegistrationRejectedException('Bad public key.')
y = int(codecs.encode(cose_public_key[Y_KEY], 'hex'), 16)
return alg, EllipticCurvePublicNumbers(
x, y, SECP256R1()).public_key(backend=default_backend())
elif alg in (COSE_ALG_PS256, COSE_ALG_RS256):
E_KEY = -2
N_KEY = -1
required_keys = {ALG_KEY, E_KEY, N_KEY}
if not set(cose_public_key.keys()).issuperset(required_keys):
raise COSEKeyException('Public key must match COSE_Key spec.')
if len(cose_public_key[E_KEY]) != 3 or len(cose_public_key[N_KEY]) != 256:
raise COSEKeyException('Bad public key.')
e = int(codecs.encode(cose_public_key[E_KEY], 'hex'), 16)
n = int(codecs.encode(cose_public_key[N_KEY], 'hex'), 16)
return alg, RSAPublicNumbers(e,
n).public_key(backend=default_backend())
else:
raise COSEKeyException('Unsupported algorithm.')
required_keys = {ALG_KEY, E_KEY, N_KEY}
if not set(cose_public_key.keys()).issuperset(required_keys):
raise COSEKeyException('Public key must match COSE_Key spec.')
if len(cose_public_key[E_KEY]) != 3 or len(cose_public_key[N_KEY]) != 256:
raise COSEKeyException('Bad public key.')
e = int(codecs.encode(cose_public_key[E_KEY], 'hex'), 16)
n = int(codecs.encode(cose_public_key[N_KEY], 'hex'), 16)
return alg, RSAPublicNumbers(e,
n).public_key(backend=default_backend())
else:
raise COSEKeyException('Unsupported algorithm.')