Skip to content

Commit

Permalink
fix(ext/node): fix missing privateKey.x in curve25519 JWK (#27990)
Browse files Browse the repository at this point in the history
Fixes #27972
  • Loading branch information
littledivy authored Feb 6, 2025
1 parent 28faaee commit a401a79
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
13 changes: 11 additions & 2 deletions ext/node/ops/crypto/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1463,19 +1463,28 @@ impl AsymmetricPrivateKey {
AsymmetricPrivateKey::X25519(static_secret) => {
let bytes = static_secret.to_bytes();

let AsymmetricPublicKey::X25519(x) = self.to_public_key() else {
unreachable!();
};

Ok(deno_core::serde_json::json!({
"kty": "OKP",
"crv": "X25519",
"x": bytes_to_b64(x.as_bytes()),
"d": bytes_to_b64(&bytes),
"kty": "OKP",
}))
}
AsymmetricPrivateKey::Ed25519(key) => {
let bytes = key.to_bytes();
let AsymmetricPublicKey::Ed25519(x) = self.to_public_key() else {
unreachable!();
};

Ok(deno_core::serde_json::json!({
"kty": "OKP",
"crv": "Ed25519",
"x": bytes_to_b64(x.as_bytes()),
"d": bytes_to_b64(&bytes),
"kty": "OKP",
}))
}
_ => Err(AsymmetricPrivateKeyJwkError::JwkExportNotImplementedForKeyType),
Expand Down
13 changes: 13 additions & 0 deletions tests/unit_node/crypto/crypto_key_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -755,3 +755,16 @@ Deno.test("X509Certificate checkHost", function () {
assertEquals(cert.checkHost("www.google.com"), undefined);
assertEquals(cert.checkHost("agent1"), "agent1");
});

// https://github.com/denoland/deno/issues/27972
Deno.test("curve25519 generate valid private jwk", function () {
const { publicKey, privateKey } = generateKeyPairSync("ed25519", {
publicKeyEncoding: { format: "jwk" },
privateKeyEncoding: { format: "jwk" },
});

// @ts-ignore @types/node broken
assert(!publicKey.d);
// @ts-ignore @types/node broken
assert(privateKey.d);
});

0 comments on commit a401a79

Please sign in to comment.