diff options
author | Divy Srivastava <dj.srivastava23@gmail.com> | 2024-08-23 09:36:28 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-08-23 22:06:28 +0530 |
commit | d9a7b30d1fa93dc742c9a3ee0fe8666be7ce1c0f (patch) | |
tree | f84a25cb776c870fd1e33a88e6df38615e717007 /ext/node/ops/crypto | |
parent | d54d29662f30c0fa5e1f048fdce4835e51248682 (diff) |
fix(ext/node): import JWK octet key pairs (#25180)
Ref https://github.com/denoland/deno/issues/24129
`kty: "okp"` is defined in
[rfc8037](https://www.rfc-editor.org/rfc/rfc8037.html)
Diffstat (limited to 'ext/node/ops/crypto')
-rw-r--r-- | ext/node/ops/crypto/keys.rs | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/ext/node/ops/crypto/keys.rs b/ext/node/ops/crypto/keys.rs index 45849cbd9..7d7ec140e 100644 --- a/ext/node/ops/crypto/keys.rs +++ b/ext/node/ops/crypto/keys.rs @@ -571,6 +571,50 @@ impl KeyObjectHandle { Ok(KeyObjectHandle::AsymmetricPublic(key)) } + pub fn new_ed_raw( + curve: &str, + data: &[u8], + is_public: bool, + ) -> Result<KeyObjectHandle, AnyError> { + match curve { + "Ed25519" => { + let data = data + .try_into() + .map_err(|_| type_error("invalid Ed25519 key"))?; + if !is_public { + Ok(KeyObjectHandle::AsymmetricPrivate( + AsymmetricPrivateKey::Ed25519( + ed25519_dalek::SigningKey::from_bytes(data), + ), + )) + } else { + Ok(KeyObjectHandle::AsymmetricPublic( + AsymmetricPublicKey::Ed25519( + ed25519_dalek::VerifyingKey::from_bytes(data)?, + ), + )) + } + } + "X25519" => { + let data: [u8; 32] = data + .try_into() + .map_err(|_| type_error("invalid x25519 key"))?; + if !is_public { + Ok(KeyObjectHandle::AsymmetricPrivate( + AsymmetricPrivateKey::X25519(x25519_dalek::StaticSecret::from( + data, + )), + )) + } else { + Ok(KeyObjectHandle::AsymmetricPublic( + AsymmetricPublicKey::X25519(x25519_dalek::PublicKey::from(data)), + )) + } + } + _ => Err(type_error("unsupported curve")), + } + } + pub fn new_asymmetric_public_key_from_js( key: &[u8], format: &str, @@ -1029,6 +1073,16 @@ pub fn op_node_create_private_key( #[op2] #[cppgc] +pub fn op_node_create_ed_raw( + #[string] curve: &str, + #[buffer] key: &[u8], + is_public: bool, +) -> Result<KeyObjectHandle, AnyError> { + KeyObjectHandle::new_ed_raw(curve, key, is_public) +} + +#[op2] +#[cppgc] pub fn op_node_create_public_key( #[buffer] key: &[u8], #[string] format: &str, |