diff options
author | Luca Casonato <hello@lcas.dev> | 2024-08-09 15:39:46 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-08-09 13:39:46 +0000 |
commit | 4dc8fe20200b6bbdc93c30e49ebbeb161dbda85e (patch) | |
tree | 9f43dc905168c86edda4f9d5f0ffc6b359138e69 /ext/node | |
parent | 854c7ba1980e949642c7240f38318cdd31f0113c (diff) |
fix(ext/node): use pem private keys in createPublicKey (#24969)
Diffstat (limited to 'ext/node')
-rw-r--r-- | ext/node/ops/crypto/keys.rs | 35 |
1 files changed, 17 insertions, 18 deletions
diff --git a/ext/node/ops/crypto/keys.rs b/ext/node/ops/crypto/keys.rs index 87e0e5439..cebafd584 100644 --- a/ext/node/ops/crypto/keys.rs +++ b/ext/node/ops/crypto/keys.rs @@ -522,7 +522,7 @@ impl KeyObjectHandle { key: &[u8], format: &str, typ: &str, - _passphrase: Option<&[u8]>, + passphrase: Option<&[u8]>, ) -> Result<KeyObjectHandle, AnyError> { let document = match format { "pem" => { @@ -542,23 +542,22 @@ impl KeyObjectHandle { Document::from_pkcs1_der(document.as_bytes()) .map_err(|_| type_error("invalid PKCS#1 public key"))? } - EncryptedPrivateKeyInfo::PEM_LABEL => { - // FIXME - return Err(type_error( - "deriving public key from encrypted private key", - )); - } - PrivateKeyInfo::PEM_LABEL => { - // FIXME - return Err(type_error("public key cannot be a private key")); - } - sec1::EcPrivateKey::PEM_LABEL => { - // FIXME - return Err(type_error("deriving public key from ec private key")); - } - rsa::pkcs1::RsaPrivateKey::PEM_LABEL => { - // FIXME - return Err(type_error("deriving public key from rsa private key")); + EncryptedPrivateKeyInfo::PEM_LABEL + | PrivateKeyInfo::PEM_LABEL + | sec1::EcPrivateKey::PEM_LABEL + | rsa::pkcs1::RsaPrivateKey::PEM_LABEL => { + let handle = KeyObjectHandle::new_asymmetric_private_key_from_js( + key, format, typ, passphrase, + )?; + match handle { + KeyObjectHandle::AsymmetricPrivate(private) => { + return Ok(KeyObjectHandle::AsymmetricPublic( + private.to_public_key(), + )) + } + KeyObjectHandle::AsymmetricPublic(_) + | KeyObjectHandle::Secret(_) => unreachable!(), + } } // TODO: handle x509 certificates as public keys _ => { |