1313from ..utils import base64url_to_long , b64d , as_bytes
1414
1515from .ec import ECKey
16+ from .ec import NIST2SEC
1617from .rsa import RSAKey
1718from .hmac import SYMKey
1819
@@ -27,15 +28,12 @@ def key_from_jwk_dict(jwk_dict):
2728 _jwk_dict = copy .copy (jwk_dict )
2829
2930 if _jwk_dict ['kty' ] == 'EC' :
30- if _jwk_dict ["crv" ] == "P-256" :
31- curve = ec .SECP256R1 ()
32- elif _jwk_dict ["crv" ] == "P-384" :
33- curve = ec .SECP384R1 ()
34- elif _jwk_dict ["crv" ] == "P-521" :
35- curve = ec .SECP521R1 ()
31+ if _jwk_dict ["crv" ] in NIST2SEC :
32+ curve = NIST2SEC [_jwk_dict ["crv" ]]()
3633 else :
3734 raise UnsupportedAlgorithm (
3835 "Unknown curve: %s" % (_jwk_dict ["crv" ]))
36+
3937 if _jwk_dict .get ("d" , None ) is not None :
4038 # Ecdsa private key.
4139 _jwk_dict ['priv_key' ] = ec .derive_private_key (
@@ -55,11 +53,11 @@ def key_from_jwk_dict(jwk_dict):
5553 base64url_to_long (_jwk_dict ["e" ]),
5654 base64url_to_long (_jwk_dict ["n" ]))
5755 if _jwk_dict .get ("p" , None ) is not None :
58- # Rsa private key.
56+ # Rsa private key. These MUST be present
5957 p_long = base64url_to_long (_jwk_dict ["p" ])
6058 q_long = base64url_to_long (_jwk_dict ["q" ])
6159 d_long = base64url_to_long (_jwk_dict ["d" ])
62-
60+ # If not present these can be calculated from the others
6361 if 'dp' not in _jwk_dict :
6462 dp_long = rsa_crt_dmp1 (d_long , p_long )
6563 else :
0 commit comments