Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ext/node): fix missing privateKey.x in curve25519 JWK #27990

Merged
merged 1 commit into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});