Skip to content

Commit

Permalink
Fix X25519 import/export from PEM
Browse files Browse the repository at this point in the history
Signed-off-by: Amaury Chamayou <[email protected]>
  • Loading branch information
achamayou authored and simo5 committed Nov 28, 2023
1 parent 41fb08a commit 4c90019
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
8 changes: 6 additions & 2 deletions jwcrypto/jwk.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 22 additions & 0 deletions jwcrypto/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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==
Expand All @@ -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):
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 4c90019

Please sign in to comment.