From 4c900198a25bbe0c71a9d3a09c8c378920f40887 Mon Sep 17 00:00:00 2001 From: Amaury Chamayou Date: Tue, 28 Nov 2023 12:00:12 +0000 Subject: [PATCH] Fix X25519 import/export from PEM Signed-off-by: Amaury Chamayou --- jwcrypto/jwk.py | 8 ++++++-- jwcrypto/tests.py | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/jwcrypto/jwk.py b/jwcrypto/jwk.py index b827578..d25c6b5 100644 --- a/jwcrypto/jwk.py +++ b/jwcrypto/jwk.py @@ -971,9 +971,13 @@ def import_from_pyca(self, key): self._import_pyca_pri_ec(key) elif isinstance(key, ec.EllipticCurvePublicKey): self._import_pyca_pub_ec(key) - elif isinstance(key, (Ed25519PrivateKey, Ed448PrivateKey)): + elif isinstance(key, (Ed25519PrivateKey, + Ed448PrivateKey, + X25519PrivateKey)): self._import_pyca_pri_okp(key) - elif isinstance(key, (Ed25519PublicKey, Ed448PublicKey)): + elif isinstance(key, (Ed25519PublicKey, + Ed448PublicKey, + X25519PublicKey)): self._import_pyca_pub_okp(key) else: raise InvalidJWKValue('Unknown key object %r' % key) diff --git a/jwcrypto/tests.py b/jwcrypto/tests.py index b488ed7..988a33d 100644 --- a/jwcrypto/tests.py +++ b/jwcrypto/tests.py @@ -367,6 +367,16 @@ -----END PUBLIC KEY----- """ +X25519PrivatePEM = b"""-----BEGIN PRIVATE KEY----- +MC4CAQAwBQYDK2VuBCIEIBjAbPTtNY6CUuR5FG1+xb1u5nSRokrNaQYEsgu9O+hP +-----END PRIVATE KEY----- +""" + +X25519PublicPEM = b"""-----BEGIN PUBLIC KEY----- +MCowBQYDK2VuAyEAW+m9ugi1psQFx6dtTl6J/XZ4JFP019S+oq4wyAoWPnQ= +-----END PUBLIC KEY----- +""" + ECPublicPEM = b"""-----BEGIN PUBLIC KEY----- MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEhvGzt82WMJxqTuXCZxnvwrx4enQj 6xc+erlhbTq8gTMAJBzNRPbpuj4NOwTCwjohrtY0TAkthwTuixuojpGKmw== @@ -381,6 +391,13 @@ "y": "ACQczUT26bo-DTsEwsI6Ia7WNEwJLYcE7osbqI6Rips" } +X25519PublicJWK = { + 'crv': 'X25519', + 'kid': '9cgLEZD5VsaV9dUPNehs2pOwxtmH-EWHJY-pC74Wjak', + 'kty': 'OKP', + 'x': 'W-m9ugi1psQFx6dtTl6J_XZ4JFP019S-oq4wyAoWPnQ' +} + class TestJWK(unittest.TestCase): def test_create_pubKeys(self): @@ -570,6 +587,11 @@ def test_import_ec_from_pem(self): self.assertEqual(pub_ec.export_to_pem(), ECPublicPEM) self.assertEqual(json_decode(pub_ec.export()), ECPublicJWK) + def test_import_x25519_from_pem(self): + pub_x25519 = jwk.JWK.from_pem(X25519PublicPEM) + self.assertEqual(pub_x25519.export_to_pem(), X25519PublicPEM) + self.assertEqual(json_decode(pub_x25519.export()), X25519PublicJWK) + def test_export_symmetric(self): key = jwk.JWK(**SymmetricKeys['keys'][0]) self.assertTrue(key.is_symmetric)