From 77e58fe7f9fc20dabf77424efbd25ce332f8f59c Mon Sep 17 00:00:00 2001 From: Sean Michael Wykes <8363933+SeanWykes@users.noreply.github.com> Date: Wed, 19 Jan 2022 00:38:35 -0300 Subject: feat(ext/crypto): implement pkcs8/spki/jwk exportKey for ECDSA and ECDH (#13104) --- ext/crypto/shared.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'ext/crypto/shared.rs') diff --git a/ext/crypto/shared.rs b/ext/crypto/shared.rs index 3b32bb2a2..de287efb0 100644 --- a/ext/crypto/shared.rs +++ b/ext/crypto/shared.rs @@ -106,6 +106,35 @@ impl RawKeyData { _ => Err(type_error("expected secret key")), } } + + pub fn as_ec_public_key_p256(&self) -> Result { + match self { + RawKeyData::Public(data) => { + // public_key is a serialized EncodedPoint + p256::EncodedPoint::from_bytes(&data) + .map_err(|_| type_error("expected valid private EC key")) + } + _ => Err(type_error("expected private key")), + } + } + + pub fn as_ec_public_key_p384(&self) -> Result { + match self { + RawKeyData::Public(data) => { + // public_key is a serialized EncodedPoint + p384::EncodedPoint::from_bytes(&data) + .map_err(|_| type_error("expected valid private EC key")) + } + _ => Err(type_error("expected private key")), + } + } + + pub fn as_ec_private_key(&self) -> Result<&[u8], AnyError> { + match self { + RawKeyData::Private(data) => Ok(data), + _ => Err(type_error("expected private key")), + } + } } pub fn data_error(msg: impl Into>) -> AnyError { -- cgit v1.2.3