Skip to content

Commit 4c90019

Browse files
achamayousimo5
authored andcommittedNov 28, 2023
Fix X25519 import/export from PEM
Signed-off-by: Amaury Chamayou <[email protected]>
1 parent 41fb08a commit 4c90019

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed
 

Diff for: ‎jwcrypto/jwk.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -971,9 +971,13 @@ def import_from_pyca(self, key):
971971
self._import_pyca_pri_ec(key)
972972
elif isinstance(key, ec.EllipticCurvePublicKey):
973973
self._import_pyca_pub_ec(key)
974-
elif isinstance(key, (Ed25519PrivateKey, Ed448PrivateKey)):
974+
elif isinstance(key, (Ed25519PrivateKey,
975+
Ed448PrivateKey,
976+
X25519PrivateKey)):
975977
self._import_pyca_pri_okp(key)
976-
elif isinstance(key, (Ed25519PublicKey, Ed448PublicKey)):
978+
elif isinstance(key, (Ed25519PublicKey,
979+
Ed448PublicKey,
980+
X25519PublicKey)):
977981
self._import_pyca_pub_okp(key)
978982
else:
979983
raise InvalidJWKValue('Unknown key object %r' % key)

Diff for: ‎jwcrypto/tests.py

+22
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,16 @@
367367
-----END PUBLIC KEY-----
368368
"""
369369

370+
X25519PrivatePEM = b"""-----BEGIN PRIVATE KEY-----
371+
MC4CAQAwBQYDK2VuBCIEIBjAbPTtNY6CUuR5FG1+xb1u5nSRokrNaQYEsgu9O+hP
372+
-----END PRIVATE KEY-----
373+
"""
374+
375+
X25519PublicPEM = b"""-----BEGIN PUBLIC KEY-----
376+
MCowBQYDK2VuAyEAW+m9ugi1psQFx6dtTl6J/XZ4JFP019S+oq4wyAoWPnQ=
377+
-----END PUBLIC KEY-----
378+
"""
379+
370380
ECPublicPEM = b"""-----BEGIN PUBLIC KEY-----
371381
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEhvGzt82WMJxqTuXCZxnvwrx4enQj
372382
6xc+erlhbTq8gTMAJBzNRPbpuj4NOwTCwjohrtY0TAkthwTuixuojpGKmw==
@@ -381,6 +391,13 @@
381391
"y": "ACQczUT26bo-DTsEwsI6Ia7WNEwJLYcE7osbqI6Rips"
382392
}
383393

394+
X25519PublicJWK = {
395+
'crv': 'X25519',
396+
'kid': '9cgLEZD5VsaV9dUPNehs2pOwxtmH-EWHJY-pC74Wjak',
397+
'kty': 'OKP',
398+
'x': 'W-m9ugi1psQFx6dtTl6J_XZ4JFP019S-oq4wyAoWPnQ'
399+
}
400+
384401

385402
class TestJWK(unittest.TestCase):
386403
def test_create_pubKeys(self):
@@ -570,6 +587,11 @@ def test_import_ec_from_pem(self):
570587
self.assertEqual(pub_ec.export_to_pem(), ECPublicPEM)
571588
self.assertEqual(json_decode(pub_ec.export()), ECPublicJWK)
572589

590+
def test_import_x25519_from_pem(self):
591+
pub_x25519 = jwk.JWK.from_pem(X25519PublicPEM)
592+
self.assertEqual(pub_x25519.export_to_pem(), X25519PublicPEM)
593+
self.assertEqual(json_decode(pub_x25519.export()), X25519PublicJWK)
594+
573595
def test_export_symmetric(self):
574596
key = jwk.JWK(**SymmetricKeys['keys'][0])
575597
self.assertTrue(key.is_symmetric)

0 commit comments

Comments
 (0)
Please sign in to comment.