diff options
| author | diachedelic <diachedelic@gmail.com> | 2022-06-08 12:59:42 +1000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-06-08 08:29:42 +0530 |
| commit | ff5def9ed5725f853f0a2cd33662211ce3418317 (patch) | |
| tree | f241dc2476f33884ff3e8873075a58124b581890 /ext | |
| parent | 753f32024f2dcbdf0b90df95929bfea6b6068556 (diff) | |
feat(ext/crypto): export elliptic keys as "raw" (#14764)
This commit adds support for the "raw" format when exporting public ECDH/ECDSA keys via
the SubtleCrypto.exportKey method.
Diffstat (limited to 'ext')
| -rw-r--r-- | ext/crypto/00_crypto.js | 18 | ||||
| -rw-r--r-- | ext/crypto/export_key.rs | 20 |
2 files changed, 38 insertions, 0 deletions
diff --git a/ext/crypto/00_crypto.js b/ext/crypto/00_crypto.js index c825089e7..2b14a204e 100644 --- a/ext/crypto/00_crypto.js +++ b/ext/crypto/00_crypto.js @@ -3433,6 +3433,24 @@ function exportKeyEC(format, key, innerKey) { switch (format) { + case "raw": { + // 1. + if (key[_type] !== "public") { + throw new DOMException( + "Key is not a public key", + "InvalidAccessError", + ); + } + + // 2. + const data = core.opSync("op_crypto_export_key", { + algorithm: key[_algorithm].name, + namedCurve: key[_algorithm].namedCurve, + format: "raw", + }, innerKey); + + return data.buffer; + } case "pkcs8": { // 1. if (key[_type] !== "private") { diff --git a/ext/crypto/export_key.rs b/ext/crypto/export_key.rs index 64d2d1079..9e124775b 100644 --- a/ext/crypto/export_key.rs +++ b/ext/crypto/export_key.rs @@ -25,6 +25,7 @@ pub struct ExportKeyOptions { #[derive(Deserialize)] #[serde(rename_all = "lowercase")] pub enum ExportKeyFormat { + Raw, Pkcs8, Spki, JwkPublic, @@ -54,6 +55,7 @@ pub enum ExportKeyAlgorithm { #[derive(Serialize)] #[serde(untagged)] pub enum ExportKeyResult { + Raw(ZeroCopyBuf), Pkcs8(ZeroCopyBuf), Spki(ZeroCopyBuf), JwkSecret { @@ -228,6 +230,24 @@ fn export_key_ec( named_curve: EcNamedCurve, ) -> Result<ExportKeyResult, deno_core::anyhow::Error> { match format { + ExportKeyFormat::Raw => { + let subject_public_key = match named_curve { + EcNamedCurve::P256 => { + let point = key_data.as_ec_public_key_p256()?; + + point.as_ref().to_vec() + } + EcNamedCurve::P384 => { + let point = key_data.as_ec_public_key_p384()?; + + point.as_ref().to_vec() + } + EcNamedCurve::P521 => { + return Err(data_error("Unsupported named curve")) + } + }; + Ok(ExportKeyResult::Raw(subject_public_key.into())) + } ExportKeyFormat::Spki => { let subject_public_key = match named_curve { EcNamedCurve::P256 => { |
