diff options
author | Divy Srivastava <dj.srivastava23@gmail.com> | 2024-08-28 19:54:49 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-08-28 19:54:49 +0530 |
commit | 553bd7dec328884785da805d8ef4f9c4510e1366 (patch) | |
tree | f44d1dac915305bfae9d1b9cdbfcf310f6103c8a /ext/node/ops/crypto | |
parent | 14a34a0cd76b1d5e4c19b583a3b6aad7db8a6187 (diff) |
fix(ext/node): import EC JWK keys (#25266)
Diffstat (limited to 'ext/node/ops/crypto')
-rw-r--r-- | ext/node/ops/crypto/keys.rs | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/ext/node/ops/crypto/keys.rs b/ext/node/ops/crypto/keys.rs index 7d7ec140e..eccd08564 100644 --- a/ext/node/ops/crypto/keys.rs +++ b/ext/node/ops/crypto/keys.rs @@ -13,6 +13,7 @@ use deno_core::unsync::spawn_blocking; use deno_core::GarbageCollected; use deno_core::ToJsBuffer; use ed25519_dalek::pkcs8::BitStringRef; +use elliptic_curve::JwkEcKey; use num_bigint::BigInt; use num_traits::FromPrimitive as _; use pkcs8::DecodePrivateKey as _; @@ -571,6 +572,36 @@ impl KeyObjectHandle { Ok(KeyObjectHandle::AsymmetricPublic(key)) } + pub fn new_ec_jwk( + jwk: &JwkEcKey, + is_public: bool, + ) -> Result<KeyObjectHandle, AnyError> { + // https://datatracker.ietf.org/doc/html/rfc7518#section-6.2.1.1 + let handle = match jwk.crv() { + "P-256" if is_public => { + KeyObjectHandle::AsymmetricPublic(AsymmetricPublicKey::Ec( + EcPublicKey::P256(p256::PublicKey::from_jwk(jwk)?), + )) + } + "P-256" => KeyObjectHandle::AsymmetricPrivate(AsymmetricPrivateKey::Ec( + EcPrivateKey::P256(p256::SecretKey::from_jwk(jwk)?), + )), + "P-384" if is_public => { + KeyObjectHandle::AsymmetricPublic(AsymmetricPublicKey::Ec( + EcPublicKey::P384(p384::PublicKey::from_jwk(jwk)?), + )) + } + "P-384" => KeyObjectHandle::AsymmetricPrivate(AsymmetricPrivateKey::Ec( + EcPrivateKey::P384(p384::SecretKey::from_jwk(jwk)?), + )), + _ => { + return Err(type_error(format!("unsupported curve: {}", jwk.crv()))); + } + }; + + Ok(handle) + } + pub fn new_ed_raw( curve: &str, data: &[u8], @@ -1083,6 +1114,15 @@ pub fn op_node_create_ed_raw( #[op2] #[cppgc] +pub fn op_node_create_ec_jwk( + #[serde] jwk: elliptic_curve::JwkEcKey, + is_public: bool, +) -> Result<KeyObjectHandle, AnyError> { + KeyObjectHandle::new_ec_jwk(&jwk, is_public) +} + +#[op2] +#[cppgc] pub fn op_node_create_public_key( #[buffer] key: &[u8], #[string] format: &str, |