2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-01-13 02:51:32 -05:00
|
|
|
|
2021-12-10 09:06:03 -05:00
|
|
|
use std::borrow::Cow;
|
|
|
|
|
|
|
|
use deno_core::error::custom_error;
|
2021-12-13 07:22:03 -05:00
|
|
|
use deno_core::error::type_error;
|
2021-12-10 09:06:03 -05:00
|
|
|
use deno_core::error::AnyError;
|
2023-06-22 17:37:56 -04:00
|
|
|
use deno_core::JsBuffer;
|
|
|
|
use deno_core::ToJsBuffer;
|
2022-02-08 08:18:28 -05:00
|
|
|
use elliptic_curve::sec1::ToEncodedPoint;
|
2023-10-30 11:25:12 -04:00
|
|
|
use p256::pkcs8::DecodePrivateKey;
|
2022-06-20 07:23:57 -04:00
|
|
|
use rsa::pkcs1::DecodeRsaPrivateKey;
|
|
|
|
use rsa::pkcs1::EncodeRsaPublicKey;
|
2021-12-13 07:22:03 -05:00
|
|
|
use rsa::RsaPrivateKey;
|
2021-12-10 09:06:03 -05:00
|
|
|
use serde::Deserialize;
|
|
|
|
use serde::Serialize;
|
|
|
|
|
2022-06-20 07:23:57 -04:00
|
|
|
pub const RSA_ENCRYPTION_OID: const_oid::ObjectIdentifier =
|
|
|
|
const_oid::ObjectIdentifier::new_unwrap("1.2.840.113549.1.1.1");
|
|
|
|
|
|
|
|
pub const ID_SECP256R1_OID: const_oid::ObjectIdentifier =
|
|
|
|
const_oid::ObjectIdentifier::new_unwrap("1.2.840.10045.3.1.7");
|
|
|
|
pub const ID_SECP384R1_OID: const_oid::ObjectIdentifier =
|
|
|
|
const_oid::ObjectIdentifier::new_unwrap("1.3.132.0.34");
|
|
|
|
pub const ID_SECP521R1_OID: const_oid::ObjectIdentifier =
|
|
|
|
const_oid::ObjectIdentifier::new_unwrap("1.3.132.0.35");
|
2021-12-16 11:28:43 -05:00
|
|
|
|
2022-09-19 04:25:03 -04:00
|
|
|
#[derive(Serialize, Deserialize, Copy, Clone, Eq, PartialEq)]
|
2021-12-10 09:06:03 -05:00
|
|
|
pub enum ShaHash {
|
|
|
|
#[serde(rename = "SHA-1")]
|
|
|
|
Sha1,
|
|
|
|
#[serde(rename = "SHA-256")]
|
|
|
|
Sha256,
|
|
|
|
#[serde(rename = "SHA-384")]
|
|
|
|
Sha384,
|
|
|
|
#[serde(rename = "SHA-512")]
|
|
|
|
Sha512,
|
|
|
|
}
|
|
|
|
|
2022-09-19 04:25:03 -04:00
|
|
|
#[derive(Serialize, Deserialize, Copy, Clone, Eq, PartialEq)]
|
2021-12-10 09:06:03 -05:00
|
|
|
pub enum EcNamedCurve {
|
|
|
|
#[serde(rename = "P-256")]
|
|
|
|
P256,
|
|
|
|
#[serde(rename = "P-384")]
|
|
|
|
P384,
|
2021-12-16 11:28:43 -05:00
|
|
|
#[serde(rename = "P-521")]
|
|
|
|
P521,
|
2021-12-10 09:06:03 -05:00
|
|
|
}
|
|
|
|
|
2023-06-22 17:37:56 -04:00
|
|
|
#[derive(Deserialize)]
|
2021-12-10 09:06:03 -05:00
|
|
|
#[serde(rename_all = "lowercase", tag = "type", content = "data")]
|
2023-06-22 17:37:56 -04:00
|
|
|
pub enum V8RawKeyData {
|
|
|
|
Secret(JsBuffer),
|
|
|
|
Private(JsBuffer),
|
|
|
|
Public(JsBuffer),
|
2021-12-10 09:06:03 -05:00
|
|
|
}
|
|
|
|
|
2023-06-22 17:37:56 -04:00
|
|
|
#[derive(Serialize)]
|
|
|
|
#[serde(rename_all = "lowercase", tag = "type", content = "data")]
|
|
|
|
pub enum RustRawKeyData {
|
|
|
|
Secret(ToJsBuffer),
|
|
|
|
Private(ToJsBuffer),
|
|
|
|
Public(ToJsBuffer),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl V8RawKeyData {
|
2021-12-13 07:22:03 -05:00
|
|
|
pub fn as_rsa_public_key(&self) -> Result<Cow<'_, [u8]>, AnyError> {
|
|
|
|
match self {
|
2023-06-22 17:37:56 -04:00
|
|
|
V8RawKeyData::Public(data) => Ok(Cow::Borrowed(data)),
|
|
|
|
V8RawKeyData::Private(data) => {
|
2021-12-13 07:22:03 -05:00
|
|
|
let private_key = RsaPrivateKey::from_pkcs1_der(data)
|
|
|
|
.map_err(|_| type_error("expected valid private key"))?;
|
|
|
|
|
|
|
|
let public_key_doc = private_key
|
|
|
|
.to_public_key()
|
|
|
|
.to_pkcs1_der()
|
|
|
|
.map_err(|_| type_error("expected valid public key"))?;
|
|
|
|
|
2022-06-20 07:23:57 -04:00
|
|
|
Ok(Cow::Owned(public_key_doc.as_bytes().into()))
|
2021-12-13 07:22:03 -05:00
|
|
|
}
|
|
|
|
_ => Err(type_error("expected public key")),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_rsa_private_key(&self) -> Result<&[u8], AnyError> {
|
|
|
|
match self {
|
2023-06-22 17:37:56 -04:00
|
|
|
V8RawKeyData::Private(data) => Ok(data),
|
2021-12-13 07:22:03 -05:00
|
|
|
_ => Err(type_error("expected private key")),
|
|
|
|
}
|
|
|
|
}
|
2021-12-15 16:18:26 -05:00
|
|
|
|
|
|
|
pub fn as_secret_key(&self) -> Result<&[u8], AnyError> {
|
|
|
|
match self {
|
2023-06-22 17:37:56 -04:00
|
|
|
V8RawKeyData::Secret(data) => Ok(data),
|
2021-12-15 16:18:26 -05:00
|
|
|
_ => Err(type_error("expected secret key")),
|
|
|
|
}
|
|
|
|
}
|
2022-01-18 22:38:35 -05:00
|
|
|
|
|
|
|
pub fn as_ec_public_key_p256(&self) -> Result<p256::EncodedPoint, AnyError> {
|
|
|
|
match self {
|
2023-06-22 17:37:56 -04:00
|
|
|
V8RawKeyData::Public(data) => {
|
2022-01-18 22:38:35 -05:00
|
|
|
// public_key is a serialized EncodedPoint
|
2022-11-17 20:59:10 -05:00
|
|
|
p256::EncodedPoint::from_bytes(data)
|
2022-02-08 08:18:28 -05:00
|
|
|
.map_err(|_| type_error("expected valid public EC key"))
|
2022-01-18 22:38:35 -05:00
|
|
|
}
|
2023-06-22 17:37:56 -04:00
|
|
|
V8RawKeyData::Private(data) => {
|
2022-02-08 08:18:28 -05:00
|
|
|
let signing_key = p256::SecretKey::from_pkcs8_der(data)
|
|
|
|
.map_err(|_| type_error("expected valid private EC key"))?;
|
|
|
|
Ok(signing_key.public_key().to_encoded_point(false))
|
|
|
|
}
|
|
|
|
// Should never reach here.
|
2023-06-22 17:37:56 -04:00
|
|
|
V8RawKeyData::Secret(_) => unreachable!(),
|
2022-01-18 22:38:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_ec_public_key_p384(&self) -> Result<p384::EncodedPoint, AnyError> {
|
|
|
|
match self {
|
2023-06-22 17:37:56 -04:00
|
|
|
V8RawKeyData::Public(data) => {
|
2022-01-18 22:38:35 -05:00
|
|
|
// public_key is a serialized EncodedPoint
|
2022-11-17 20:59:10 -05:00
|
|
|
p384::EncodedPoint::from_bytes(data)
|
2022-02-08 08:18:28 -05:00
|
|
|
.map_err(|_| type_error("expected valid public EC key"))
|
2022-01-18 22:38:35 -05:00
|
|
|
}
|
2023-06-22 17:37:56 -04:00
|
|
|
V8RawKeyData::Private(data) => {
|
2022-06-20 07:23:57 -04:00
|
|
|
let signing_key = p384::SecretKey::from_pkcs8_der(data)
|
|
|
|
.map_err(|_| type_error("expected valid private EC key"))?;
|
|
|
|
Ok(signing_key.public_key().to_encoded_point(false))
|
2022-02-08 08:18:28 -05:00
|
|
|
}
|
|
|
|
// Should never reach here.
|
2023-06-22 17:37:56 -04:00
|
|
|
V8RawKeyData::Secret(_) => unreachable!(),
|
2022-01-18 22:38:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_ec_private_key(&self) -> Result<&[u8], AnyError> {
|
|
|
|
match self {
|
2023-06-22 17:37:56 -04:00
|
|
|
V8RawKeyData::Private(data) => Ok(data),
|
2022-01-18 22:38:35 -05:00
|
|
|
_ => Err(type_error("expected private key")),
|
|
|
|
}
|
|
|
|
}
|
2021-12-13 07:22:03 -05:00
|
|
|
}
|
|
|
|
|
2021-12-10 09:06:03 -05:00
|
|
|
pub fn data_error(msg: impl Into<Cow<'static, str>>) -> AnyError {
|
|
|
|
custom_error("DOMExceptionDataError", msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn not_supported_error(msg: impl Into<Cow<'static, str>>) -> AnyError {
|
|
|
|
custom_error("DOMExceptionNotSupportedError", msg)
|
|
|
|
}
|
|
|
|
|
2021-12-13 12:45:08 -05:00
|
|
|
pub fn operation_error(msg: impl Into<Cow<'static, str>>) -> AnyError {
|
|
|
|
custom_error("DOMExceptionOperationError", msg)
|
|
|
|
}
|
|
|
|
|
2021-12-10 09:06:03 -05:00
|
|
|
pub fn unsupported_format() -> AnyError {
|
|
|
|
not_supported_error("unsupported format")
|
|
|
|
}
|