Skip to content

Commit bf47439

Browse files
committed
Allow all the supported curves.
1 parent c85b638 commit bf47439

2 files changed

Lines changed: 16 additions & 9 deletions

File tree

src/cryptojwt/jwk/jwk.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from ..utils import base64url_to_long, b64d, as_bytes
1414

1515
from .ec import ECKey
16+
from .ec import NIST2SEC
1617
from .rsa import RSAKey
1718
from .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:

tests/test_02_jwk.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,9 +575,18 @@ def test_load_pem_file_ec():
575575
assert key.has_private_key()
576576

577577

578-
def test_key_from_jwk_dict():
578+
def test_key_from_jwk_dict_rsa():
579579
rsa_key = new_rsa_key()
580580
jwk = rsa_key.serialize(private=True)
581581
_key = key_from_jwk_dict(jwk)
582582
assert isinstance(_key, RSAKey)
583583
assert _key.has_private_key()
584+
585+
586+
def test_key_from_jwk_dict_ec():
587+
key = ECKey().load(full_path('570-ec-sect571r1-keypair.pem'))
588+
assert key.has_private_key()
589+
jwk = key.serialize(private=True)
590+
_key = key_from_jwk_dict(jwk)
591+
assert isinstance(_key, ECKey)
592+
assert _key.has_private_key()

0 commit comments

Comments
 (0)