Skip to content

Commit 0e011bd

Browse files
committed
Deal with alg specification in JWKS and keys in JWKS that is not supported.
1 parent 5c6c847 commit 0e011bd

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

src/cryptojwt/exception.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,7 @@ class WrongUsage(JWKESTException):
105105

106106
class HTTPException(JWKESTException):
107107
pass
108+
109+
110+
class UnsupportedECurve(Unsupported):
111+
pass

src/cryptojwt/jwk/__init__.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,24 @@ def __init__(self, kty="", alg="", use="", kid="", x5c=None,
4747
if not isinstance(alg, str):
4848
alg = as_unicode(alg)
4949

50-
# The list comes from https://tools.ietf.org/html/rfc7518#page-6
51-
# Should map against SIGNER_ALGS in cryptojwt.jws.jws
52-
if alg not in ["HS256", "HS384", "HS512", "RS256", "RS384",
53-
"RS512", "ES256", "ES384", "ES512", "PS256",
54-
"PS384", "PS512", "none"]:
55-
raise UnsupportedAlgorithm("Unknown algorithm: {}".format(alg))
56-
50+
if use == 'enc':
51+
if alg not in ["RSA1_5", "RSA-OAEP", "RSA-OAEP-256", "A128KW", "A192KW", "A256KW",
52+
"ECDH-ES", "ECDH-ES+A128KW", "ECDH-ES+A192KW", "ECDH-ES+A256KW"]:
53+
raise UnsupportedAlgorithm("Unknown algorithm: {}".format(alg))
54+
elif use == 'sig':
55+
# The list comes from https://tools.ietf.org/html/rfc7518#page-6
56+
# Should map against SIGNER_ALGS in cryptojwt.jws.jws
57+
if alg not in ["HS256", "HS384", "HS512", "RS256", "RS384",
58+
"RS512", "ES256", "ES384", "ES512", "PS256",
59+
"PS384", "PS512", "none"]:
60+
raise UnsupportedAlgorithm("Unknown algorithm: {}".format(alg))
61+
else: # potentially used both for encryption and signing
62+
if alg not in ["HS256", "HS384", "HS512", "RS256", "RS384",
63+
"RS512", "ES256", "ES384", "ES512", "PS256",
64+
"PS384", "PS512", "none", "RSA1_5", "RSA-OAEP", "RSA-OAEP-256",
65+
"A128KW", "A192KW", "A256KW", "ECDH-ES", "ECDH-ES+A128KW",
66+
"ECDH-ES+A192KW", "ECDH-ES+A256KW"]:
67+
raise UnsupportedAlgorithm("Unknown algorithm: {}".format(alg))
5768
self.alg = alg
5869

5970
if isinstance(use, str):

src/cryptojwt/jwk/ec.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from ..exception import DeSerializationNotPossible
66
from ..exception import JWKESTException
7+
from ..exception import UnsupportedECurve
78
from ..utils import as_unicode
89
from ..utils import deser
910
from ..utils import long_to_base64
@@ -38,8 +39,12 @@ def ec_construct_public(num):
3839
:return: A cryptography.hazmat.primitives.asymmetric.ec.EllipticCurvePublicKey
3940
instance.
4041
"""
41-
ecpn = ec.EllipticCurvePublicNumbers(num['x'], num['y'],
42-
NIST2SEC[as_unicode(num['crv'])]())
42+
try:
43+
_sec_crv = NIST2SEC[as_unicode(num['crv'])]
44+
except KeyError:
45+
raise UnsupportedECurve("Unsupported elliptic curve: {}".format(num['crv']))
46+
47+
ecpn = ec.EllipticCurvePublicNumbers(num['x'], num['y'], _sec_crv())
4348
return ecpn.public_key(default_backend())
4449

4550

0 commit comments

Comments
 (0)