2023-01-13 07:51:32 +00:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
2022-06-20 11:23:57 +00:00
|
|
|
use const_oid::AssociatedOid;
|
|
|
|
use const_oid::ObjectIdentifier;
|
2021-12-14 16:02:14 +00:00
|
|
|
use deno_core::error::custom_error;
|
2021-12-13 12:22:03 +00:00
|
|
|
use deno_core::error::AnyError;
|
2022-03-14 17:44:15 +00:00
|
|
|
use deno_core::op;
|
2021-12-13 12:22:03 +00:00
|
|
|
use deno_core::ZeroCopyBuf;
|
2022-06-20 11:23:57 +00:00
|
|
|
use elliptic_curve::sec1::ToEncodedPoint;
|
|
|
|
use p256::pkcs8::DecodePrivateKey;
|
|
|
|
use rsa::pkcs1::UIntRef;
|
2021-12-13 12:22:03 +00:00
|
|
|
use serde::Deserialize;
|
|
|
|
use serde::Serialize;
|
|
|
|
use spki::der::asn1;
|
2022-06-20 11:23:57 +00:00
|
|
|
use spki::der::Decode;
|
|
|
|
use spki::der::Encode;
|
2022-01-19 03:38:35 +00:00
|
|
|
use spki::AlgorithmIdentifier;
|
2021-12-13 12:22:03 +00:00
|
|
|
|
2023-01-13 07:51:32 +00:00
|
|
|
use crate::shared::*;
|
|
|
|
|
2021-12-13 12:22:03 +00:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct ExportKeyOptions {
|
|
|
|
format: ExportKeyFormat,
|
|
|
|
#[serde(flatten)]
|
|
|
|
algorithm: ExportKeyAlgorithm,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "lowercase")]
|
|
|
|
pub enum ExportKeyFormat {
|
2022-06-08 02:59:42 +00:00
|
|
|
Raw,
|
2021-12-13 12:22:03 +00:00
|
|
|
Pkcs8,
|
|
|
|
Spki,
|
2021-12-14 16:02:14 +00:00
|
|
|
JwkPublic,
|
|
|
|
JwkPrivate,
|
2022-01-05 00:00:37 +00:00
|
|
|
JwkSecret,
|
2021-12-13 12:22:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase", tag = "algorithm")]
|
|
|
|
pub enum ExportKeyAlgorithm {
|
|
|
|
#[serde(rename = "RSASSA-PKCS1-v1_5")]
|
|
|
|
RsassaPkcs1v15 {},
|
|
|
|
#[serde(rename = "RSA-PSS")]
|
|
|
|
RsaPss {},
|
|
|
|
#[serde(rename = "RSA-OAEP")]
|
|
|
|
RsaOaep {},
|
2022-01-19 03:38:35 +00:00
|
|
|
#[serde(rename = "ECDSA", rename_all = "camelCase")]
|
|
|
|
Ecdsa { named_curve: EcNamedCurve },
|
|
|
|
#[serde(rename = "ECDH", rename_all = "camelCase")]
|
|
|
|
Ecdh { named_curve: EcNamedCurve },
|
2022-01-05 00:00:37 +00:00
|
|
|
#[serde(rename = "AES")]
|
|
|
|
Aes {},
|
|
|
|
#[serde(rename = "HMAC")]
|
|
|
|
Hmac {},
|
2021-12-13 12:22:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
#[serde(untagged)]
|
|
|
|
pub enum ExportKeyResult {
|
2022-06-08 02:59:42 +00:00
|
|
|
Raw(ZeroCopyBuf),
|
2021-12-13 12:22:03 +00:00
|
|
|
Pkcs8(ZeroCopyBuf),
|
|
|
|
Spki(ZeroCopyBuf),
|
2022-01-05 00:00:37 +00:00
|
|
|
JwkSecret {
|
|
|
|
k: String,
|
|
|
|
},
|
2021-12-14 16:02:14 +00:00
|
|
|
JwkPublicRsa {
|
|
|
|
n: String,
|
|
|
|
e: String,
|
|
|
|
},
|
|
|
|
JwkPrivateRsa {
|
|
|
|
n: String,
|
|
|
|
e: String,
|
|
|
|
d: String,
|
|
|
|
p: String,
|
|
|
|
q: String,
|
|
|
|
dp: String,
|
|
|
|
dq: String,
|
|
|
|
qi: String,
|
|
|
|
},
|
2022-01-19 03:38:35 +00:00
|
|
|
JwkPublicEc {
|
|
|
|
x: String,
|
|
|
|
y: String,
|
|
|
|
},
|
|
|
|
JwkPrivateEc {
|
|
|
|
x: String,
|
|
|
|
y: String,
|
|
|
|
d: String,
|
|
|
|
},
|
2021-12-13 12:22:03 +00:00
|
|
|
}
|
|
|
|
|
2022-03-14 17:44:15 +00:00
|
|
|
#[op]
|
2021-12-13 12:22:03 +00:00
|
|
|
pub fn op_crypto_export_key(
|
|
|
|
opts: ExportKeyOptions,
|
|
|
|
key_data: RawKeyData,
|
|
|
|
) -> Result<ExportKeyResult, AnyError> {
|
|
|
|
match opts.algorithm {
|
|
|
|
ExportKeyAlgorithm::RsassaPkcs1v15 {}
|
|
|
|
| ExportKeyAlgorithm::RsaPss {}
|
|
|
|
| ExportKeyAlgorithm::RsaOaep {} => export_key_rsa(opts.format, key_data),
|
2022-01-19 03:38:35 +00:00
|
|
|
ExportKeyAlgorithm::Ecdh { named_curve }
|
|
|
|
| ExportKeyAlgorithm::Ecdsa { named_curve } => {
|
|
|
|
export_key_ec(opts.format, key_data, opts.algorithm, named_curve)
|
|
|
|
}
|
2022-01-05 00:00:37 +00:00
|
|
|
ExportKeyAlgorithm::Aes {} | ExportKeyAlgorithm::Hmac {} => {
|
|
|
|
export_key_symmetric(opts.format, key_data)
|
|
|
|
}
|
2021-12-13 12:22:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-20 11:23:57 +00:00
|
|
|
fn uint_to_b64(bytes: UIntRef) -> String {
|
2021-12-14 16:02:14 +00:00
|
|
|
base64::encode_config(bytes.as_bytes(), base64::URL_SAFE_NO_PAD)
|
|
|
|
}
|
|
|
|
|
2022-01-05 00:00:37 +00:00
|
|
|
fn bytes_to_b64(bytes: &[u8]) -> String {
|
|
|
|
base64::encode_config(bytes, base64::URL_SAFE_NO_PAD)
|
|
|
|
}
|
|
|
|
|
2021-12-13 12:22:03 +00:00
|
|
|
fn export_key_rsa(
|
|
|
|
format: ExportKeyFormat,
|
|
|
|
key_data: RawKeyData,
|
|
|
|
) -> Result<ExportKeyResult, deno_core::anyhow::Error> {
|
|
|
|
match format {
|
|
|
|
ExportKeyFormat::Spki => {
|
|
|
|
let subject_public_key = &key_data.as_rsa_public_key()?;
|
|
|
|
|
|
|
|
// the SPKI structure
|
|
|
|
let key_info = spki::SubjectPublicKeyInfo {
|
|
|
|
algorithm: spki::AlgorithmIdentifier {
|
|
|
|
// rsaEncryption(1)
|
2022-06-20 11:23:57 +00:00
|
|
|
oid: const_oid::ObjectIdentifier::new_unwrap("1.2.840.113549.1.1.1"),
|
2021-12-13 12:22:03 +00:00
|
|
|
// parameters field should not be ommited (None).
|
|
|
|
// It MUST have ASN.1 type NULL.
|
2022-06-20 11:23:57 +00:00
|
|
|
parameters: Some(asn1::AnyRef::from(asn1::Null)),
|
2021-12-13 12:22:03 +00:00
|
|
|
},
|
|
|
|
subject_public_key,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Infallible because we know the public key is valid.
|
|
|
|
let spki_der = key_info.to_vec().unwrap();
|
|
|
|
Ok(ExportKeyResult::Spki(spki_der.into()))
|
|
|
|
}
|
|
|
|
ExportKeyFormat::Pkcs8 => {
|
|
|
|
let private_key = key_data.as_rsa_private_key()?;
|
|
|
|
|
|
|
|
// the PKCS#8 v1 structure
|
|
|
|
// PrivateKeyInfo ::= SEQUENCE {
|
|
|
|
// version Version,
|
|
|
|
// privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
|
|
|
|
// privateKey PrivateKey,
|
|
|
|
// attributes [0] IMPLICIT Attributes OPTIONAL }
|
|
|
|
|
|
|
|
// version is 0 when publickey is None
|
|
|
|
|
|
|
|
let pk_info = rsa::pkcs8::PrivateKeyInfo {
|
|
|
|
public_key: None,
|
|
|
|
algorithm: rsa::pkcs8::AlgorithmIdentifier {
|
|
|
|
// rsaEncryption(1)
|
2022-06-20 11:23:57 +00:00
|
|
|
oid: rsa::pkcs8::ObjectIdentifier::new_unwrap("1.2.840.113549.1.1.1"),
|
2021-12-13 12:22:03 +00:00
|
|
|
// parameters field should not be ommited (None).
|
|
|
|
// It MUST have ASN.1 type NULL as per defined in RFC 3279 Section 2.3.1
|
2022-06-20 11:23:57 +00:00
|
|
|
parameters: Some(asn1::AnyRef::from(asn1::Null)),
|
2021-12-13 12:22:03 +00:00
|
|
|
},
|
|
|
|
private_key,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Infallible because we know the private key is valid.
|
|
|
|
let pkcs8_der = pk_info.to_vec().unwrap();
|
|
|
|
|
|
|
|
Ok(ExportKeyResult::Pkcs8(pkcs8_der.into()))
|
|
|
|
}
|
2021-12-14 16:02:14 +00:00
|
|
|
ExportKeyFormat::JwkPublic => {
|
|
|
|
let public_key = key_data.as_rsa_public_key()?;
|
|
|
|
let public_key = rsa::pkcs1::RsaPublicKey::from_der(&public_key)
|
|
|
|
.map_err(|_| {
|
|
|
|
custom_error(
|
|
|
|
"DOMExceptionOperationError",
|
|
|
|
"failed to decode public key",
|
|
|
|
)
|
|
|
|
})?;
|
|
|
|
|
|
|
|
Ok(ExportKeyResult::JwkPublicRsa {
|
|
|
|
n: uint_to_b64(public_key.modulus),
|
|
|
|
e: uint_to_b64(public_key.public_exponent),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
ExportKeyFormat::JwkPrivate => {
|
|
|
|
let private_key = key_data.as_rsa_private_key()?;
|
|
|
|
let private_key = rsa::pkcs1::RsaPrivateKey::from_der(private_key)
|
|
|
|
.map_err(|_| {
|
|
|
|
custom_error(
|
|
|
|
"DOMExceptionOperationError",
|
|
|
|
"failed to decode private key",
|
|
|
|
)
|
|
|
|
})?;
|
|
|
|
|
|
|
|
Ok(ExportKeyResult::JwkPrivateRsa {
|
|
|
|
n: uint_to_b64(private_key.modulus),
|
|
|
|
e: uint_to_b64(private_key.public_exponent),
|
|
|
|
d: uint_to_b64(private_key.private_exponent),
|
|
|
|
p: uint_to_b64(private_key.prime1),
|
|
|
|
q: uint_to_b64(private_key.prime2),
|
|
|
|
dp: uint_to_b64(private_key.exponent1),
|
|
|
|
dq: uint_to_b64(private_key.exponent2),
|
|
|
|
qi: uint_to_b64(private_key.coefficient),
|
|
|
|
})
|
|
|
|
}
|
2022-01-05 00:00:37 +00:00
|
|
|
_ => Err(unsupported_format()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn export_key_symmetric(
|
|
|
|
format: ExportKeyFormat,
|
|
|
|
key_data: RawKeyData,
|
|
|
|
) -> Result<ExportKeyResult, deno_core::anyhow::Error> {
|
|
|
|
match format {
|
|
|
|
ExportKeyFormat::JwkSecret => {
|
|
|
|
let bytes = key_data.as_secret_key()?;
|
|
|
|
|
|
|
|
Ok(ExportKeyResult::JwkSecret {
|
|
|
|
k: bytes_to_b64(bytes),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
_ => Err(unsupported_format()),
|
2021-12-13 12:22:03 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-19 03:38:35 +00:00
|
|
|
|
|
|
|
fn export_key_ec(
|
|
|
|
format: ExportKeyFormat,
|
|
|
|
key_data: RawKeyData,
|
|
|
|
algorithm: ExportKeyAlgorithm,
|
|
|
|
named_curve: EcNamedCurve,
|
|
|
|
) -> Result<ExportKeyResult, deno_core::anyhow::Error> {
|
|
|
|
match format {
|
2022-06-08 02:59:42 +00:00
|
|
|
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()))
|
|
|
|
}
|
2022-01-19 03:38:35 +00:00
|
|
|
ExportKeyFormat::Spki => {
|
|
|
|
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"))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let alg_id = match named_curve {
|
2022-06-20 11:23:57 +00:00
|
|
|
EcNamedCurve::P256 => AlgorithmIdentifier {
|
|
|
|
oid: elliptic_curve::ALGORITHM_OID,
|
|
|
|
parameters: Some((&p256::NistP256::OID).into()),
|
|
|
|
},
|
|
|
|
EcNamedCurve::P384 => AlgorithmIdentifier {
|
|
|
|
oid: elliptic_curve::ALGORITHM_OID,
|
|
|
|
parameters: Some((&p384::NistP384::OID).into()),
|
|
|
|
},
|
|
|
|
EcNamedCurve::P521 => {
|
|
|
|
return Err(data_error("Unsupported named curve"))
|
|
|
|
}
|
2022-01-19 03:38:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let alg_id = match algorithm {
|
|
|
|
ExportKeyAlgorithm::Ecdh { .. } => AlgorithmIdentifier {
|
2022-10-04 11:39:41 +00:00
|
|
|
oid: ObjectIdentifier::new_unwrap("1.2.840.10045.2.1"),
|
2022-01-19 03:38:35 +00:00
|
|
|
parameters: alg_id.parameters,
|
|
|
|
},
|
|
|
|
_ => alg_id,
|
|
|
|
};
|
|
|
|
|
|
|
|
// the SPKI structure
|
|
|
|
let key_info = spki::SubjectPublicKeyInfo {
|
|
|
|
algorithm: alg_id,
|
|
|
|
subject_public_key: &subject_public_key,
|
|
|
|
};
|
|
|
|
|
|
|
|
let spki_der = key_info.to_vec().unwrap();
|
|
|
|
|
|
|
|
Ok(ExportKeyResult::Spki(spki_der.into()))
|
|
|
|
}
|
|
|
|
ExportKeyFormat::Pkcs8 => {
|
|
|
|
// private_key is a PKCS#8 DER-encoded private key
|
|
|
|
let private_key = key_data.as_ec_private_key()?;
|
|
|
|
|
|
|
|
Ok(ExportKeyResult::Pkcs8(private_key.to_vec().into()))
|
|
|
|
}
|
|
|
|
ExportKeyFormat::JwkPublic => match named_curve {
|
|
|
|
EcNamedCurve::P256 => {
|
|
|
|
let point = key_data.as_ec_public_key_p256()?;
|
|
|
|
let coords = point.coordinates();
|
|
|
|
|
|
|
|
if let p256::elliptic_curve::sec1::Coordinates::Uncompressed { x, y } =
|
|
|
|
coords
|
|
|
|
{
|
|
|
|
Ok(ExportKeyResult::JwkPublicEc {
|
|
|
|
x: bytes_to_b64(x),
|
|
|
|
y: bytes_to_b64(y),
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
Err(custom_error(
|
|
|
|
"DOMExceptionOperationError",
|
|
|
|
"failed to decode public key",
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EcNamedCurve::P384 => {
|
|
|
|
let point = key_data.as_ec_public_key_p384()?;
|
|
|
|
let coords = point.coordinates();
|
|
|
|
|
|
|
|
if let p384::elliptic_curve::sec1::Coordinates::Uncompressed { x, y } =
|
|
|
|
coords
|
|
|
|
{
|
|
|
|
Ok(ExportKeyResult::JwkPublicEc {
|
|
|
|
x: bytes_to_b64(x),
|
|
|
|
y: bytes_to_b64(y),
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
Err(custom_error(
|
|
|
|
"DOMExceptionOperationError",
|
|
|
|
"failed to decode public key",
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EcNamedCurve::P521 => Err(data_error("Unsupported named curve")),
|
|
|
|
},
|
|
|
|
ExportKeyFormat::JwkPrivate => {
|
|
|
|
let private_key = key_data.as_ec_private_key()?;
|
|
|
|
|
|
|
|
match named_curve {
|
|
|
|
EcNamedCurve::P256 => {
|
2022-06-20 11:23:57 +00:00
|
|
|
let ec_key =
|
|
|
|
p256::SecretKey::from_pkcs8_der(private_key).map_err(|_| {
|
2022-01-19 03:38:35 +00:00
|
|
|
custom_error(
|
|
|
|
"DOMExceptionOperationError",
|
|
|
|
"failed to decode private key",
|
|
|
|
)
|
|
|
|
})?;
|
|
|
|
|
2022-06-20 11:23:57 +00:00
|
|
|
let point = ec_key.public_key().to_encoded_point(false);
|
2022-01-19 03:38:35 +00:00
|
|
|
if let elliptic_curve::sec1::Coordinates::Uncompressed { x, y } =
|
|
|
|
point.coordinates()
|
|
|
|
{
|
|
|
|
Ok(ExportKeyResult::JwkPrivateEc {
|
|
|
|
x: bytes_to_b64(x),
|
|
|
|
y: bytes_to_b64(y),
|
2022-06-20 11:23:57 +00:00
|
|
|
d: bytes_to_b64(&ec_key.to_be_bytes()),
|
2022-01-19 03:38:35 +00:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
Err(data_error("expected valid public EC key"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
EcNamedCurve::P384 => {
|
2022-06-20 11:23:57 +00:00
|
|
|
let ec_key =
|
|
|
|
p384::SecretKey::from_pkcs8_der(private_key).map_err(|_| {
|
2022-01-19 03:38:35 +00:00
|
|
|
custom_error(
|
|
|
|
"DOMExceptionOperationError",
|
|
|
|
"failed to decode private key",
|
|
|
|
)
|
|
|
|
})?;
|
|
|
|
|
2022-06-20 11:23:57 +00:00
|
|
|
let point = ec_key.public_key().to_encoded_point(false);
|
2022-01-19 03:38:35 +00:00
|
|
|
if let elliptic_curve::sec1::Coordinates::Uncompressed { x, y } =
|
|
|
|
point.coordinates()
|
|
|
|
{
|
|
|
|
Ok(ExportKeyResult::JwkPrivateEc {
|
|
|
|
x: bytes_to_b64(x),
|
|
|
|
y: bytes_to_b64(y),
|
2022-06-20 11:23:57 +00:00
|
|
|
d: bytes_to_b64(&ec_key.to_be_bytes()),
|
2022-01-19 03:38:35 +00:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
Err(data_error("expected valid public EC key"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => Err(not_supported_error("Unsupported namedCurve")),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ExportKeyFormat::JwkSecret => Err(unsupported_format()),
|
|
|
|
}
|
|
|
|
}
|