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
|
|
|
|
2023-10-26 12:39:04 -04:00
|
|
|
use base64::Engine;
|
2021-12-10 09:06:03 -05:00
|
|
|
use deno_core::error::AnyError;
|
2023-09-13 11:54:19 -04:00
|
|
|
use deno_core::op2;
|
2023-06-22 17:37:56 -04:00
|
|
|
use deno_core::JsBuffer;
|
|
|
|
use deno_core::ToJsBuffer;
|
2021-12-16 11:28:43 -05:00
|
|
|
use elliptic_curve::pkcs8::PrivateKeyInfo;
|
2022-06-20 07:23:57 -04:00
|
|
|
use p256::pkcs8::EncodePrivateKey;
|
2022-01-19 01:44:35 -05:00
|
|
|
use ring::signature::EcdsaKeyPair;
|
2023-10-30 11:25:12 -04:00
|
|
|
use rsa::pkcs1::UintRef;
|
|
|
|
use rsa::pkcs8::der::Encode;
|
2021-12-10 09:06:03 -05:00
|
|
|
use serde::Deserialize;
|
|
|
|
use serde::Serialize;
|
2022-06-20 07:23:57 -04:00
|
|
|
use spki::der::Decode;
|
2021-12-10 09:06:03 -05:00
|
|
|
|
2023-01-13 02:51:32 -05:00
|
|
|
use crate::key::CryptoNamedCurve;
|
|
|
|
use crate::shared::*;
|
|
|
|
|
2021-12-10 09:06:03 -05:00
|
|
|
#[derive(Deserialize)]
|
2021-12-10 16:23:19 -05:00
|
|
|
#[serde(rename_all = "camelCase")]
|
2021-12-10 09:06:03 -05:00
|
|
|
pub enum KeyData {
|
2023-06-22 17:37:56 -04:00
|
|
|
Spki(JsBuffer),
|
|
|
|
Pkcs8(JsBuffer),
|
|
|
|
Raw(JsBuffer),
|
2021-12-14 06:25:07 -05:00
|
|
|
JwkSecret {
|
|
|
|
k: String,
|
|
|
|
},
|
|
|
|
JwkPublicRsa {
|
|
|
|
n: String,
|
|
|
|
e: String,
|
|
|
|
},
|
|
|
|
JwkPrivateRsa {
|
|
|
|
n: String,
|
|
|
|
e: String,
|
|
|
|
d: String,
|
|
|
|
p: String,
|
|
|
|
q: String,
|
|
|
|
dp: String,
|
|
|
|
dq: String,
|
|
|
|
qi: String,
|
|
|
|
},
|
2021-12-16 11:28:43 -05:00
|
|
|
JwkPublicEc {
|
|
|
|
x: String,
|
|
|
|
y: String,
|
|
|
|
},
|
|
|
|
JwkPrivateEc {
|
|
|
|
x: String,
|
|
|
|
y: String,
|
|
|
|
d: String,
|
|
|
|
},
|
2021-12-10 09:06:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase", tag = "algorithm")]
|
|
|
|
pub enum ImportKeyOptions {
|
|
|
|
#[serde(rename = "RSASSA-PKCS1-v1_5")]
|
2022-10-04 08:07:59 -04:00
|
|
|
RsassaPkcs1v15 {},
|
2021-12-10 09:06:03 -05:00
|
|
|
#[serde(rename = "RSA-PSS")]
|
2022-10-04 08:07:59 -04:00
|
|
|
RsaPss {},
|
2021-12-10 09:06:03 -05:00
|
|
|
#[serde(rename = "RSA-OAEP")]
|
2022-10-04 08:07:59 -04:00
|
|
|
RsaOaep {},
|
2021-12-10 09:06:03 -05:00
|
|
|
#[serde(rename = "ECDSA", rename_all = "camelCase")]
|
|
|
|
Ecdsa { named_curve: EcNamedCurve },
|
|
|
|
#[serde(rename = "ECDH", rename_all = "camelCase")]
|
|
|
|
Ecdh { named_curve: EcNamedCurve },
|
2021-12-10 16:23:19 -05:00
|
|
|
#[serde(rename = "AES", rename_all = "camelCase")]
|
|
|
|
Aes {},
|
|
|
|
#[serde(rename = "HMAC", rename_all = "camelCase")]
|
|
|
|
Hmac {},
|
2021-12-10 09:06:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
#[serde(untagged)]
|
|
|
|
pub enum ImportKeyResult {
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
Rsa {
|
2023-06-22 17:37:56 -04:00
|
|
|
raw_data: RustRawKeyData,
|
2021-12-10 09:06:03 -05:00
|
|
|
modulus_length: usize,
|
2023-06-22 17:37:56 -04:00
|
|
|
public_exponent: ToJsBuffer,
|
2021-12-10 09:06:03 -05:00
|
|
|
},
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2023-06-22 17:37:56 -04:00
|
|
|
Ec { raw_data: RustRawKeyData },
|
2021-12-10 16:23:19 -05:00
|
|
|
#[serde(rename_all = "camelCase")]
|
2022-03-14 13:44:15 -04:00
|
|
|
#[allow(dead_code)]
|
2023-06-22 17:37:56 -04:00
|
|
|
Aes { raw_data: RustRawKeyData },
|
2021-12-10 16:23:19 -05:00
|
|
|
#[serde(rename_all = "camelCase")]
|
2023-06-22 17:37:56 -04:00
|
|
|
Hmac { raw_data: RustRawKeyData },
|
2021-12-10 09:06:03 -05:00
|
|
|
}
|
|
|
|
|
2023-09-13 11:54:19 -04:00
|
|
|
#[op2]
|
|
|
|
#[serde]
|
2021-12-10 09:06:03 -05:00
|
|
|
pub fn op_crypto_import_key(
|
2023-09-13 11:54:19 -04:00
|
|
|
#[serde] opts: ImportKeyOptions,
|
|
|
|
#[serde] key_data: KeyData,
|
2021-12-10 09:06:03 -05:00
|
|
|
) -> Result<ImportKeyResult, AnyError> {
|
|
|
|
match opts {
|
2022-10-04 08:07:59 -04:00
|
|
|
ImportKeyOptions::RsassaPkcs1v15 {} => import_key_rsassa(key_data),
|
|
|
|
ImportKeyOptions::RsaPss {} => import_key_rsapss(key_data),
|
|
|
|
ImportKeyOptions::RsaOaep {} => import_key_rsaoaep(key_data),
|
2021-12-10 09:06:03 -05:00
|
|
|
ImportKeyOptions::Ecdsa { named_curve }
|
|
|
|
| ImportKeyOptions::Ecdh { named_curve } => {
|
|
|
|
import_key_ec(key_data, named_curve)
|
|
|
|
}
|
2021-12-10 16:23:19 -05:00
|
|
|
ImportKeyOptions::Aes {} => import_key_aes(key_data),
|
|
|
|
ImportKeyOptions::Hmac {} => import_key_hmac(key_data),
|
2021-12-10 09:06:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-26 12:39:04 -04:00
|
|
|
const BASE64_URL_SAFE_FORGIVING:
|
|
|
|
base64::engine::general_purpose::GeneralPurpose =
|
|
|
|
base64::engine::general_purpose::GeneralPurpose::new(
|
|
|
|
&base64::alphabet::URL_SAFE,
|
|
|
|
base64::engine::general_purpose::GeneralPurposeConfig::new()
|
|
|
|
.with_decode_allow_trailing_bits(true)
|
|
|
|
.with_decode_padding_mode(base64::engine::DecodePaddingMode::Indifferent),
|
|
|
|
);
|
2022-01-03 07:24:45 -05:00
|
|
|
|
2021-12-14 06:25:07 -05:00
|
|
|
macro_rules! jwt_b64_int_or_err {
|
|
|
|
($name:ident, $b64:expr, $err:expr) => {
|
2023-10-26 12:39:04 -04:00
|
|
|
let bytes = BASE64_URL_SAFE_FORGIVING
|
|
|
|
.decode($b64)
|
2021-12-14 06:25:07 -05:00
|
|
|
.map_err(|_| data_error($err))?;
|
2023-10-30 11:25:12 -04:00
|
|
|
let $name = UintRef::new(&bytes).map_err(|_| data_error($err))?;
|
2021-12-14 06:25:07 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn import_key_rsa_jwk(
|
|
|
|
key_data: KeyData,
|
|
|
|
) -> Result<ImportKeyResult, deno_core::anyhow::Error> {
|
|
|
|
match key_data {
|
|
|
|
KeyData::JwkPublicRsa { n, e } => {
|
|
|
|
jwt_b64_int_or_err!(modulus, &n, "invalid modulus");
|
|
|
|
jwt_b64_int_or_err!(public_exponent, &e, "invalid public exponent");
|
|
|
|
|
|
|
|
let public_key = rsa::pkcs1::RsaPublicKey {
|
|
|
|
modulus,
|
|
|
|
public_exponent,
|
|
|
|
};
|
|
|
|
|
2023-10-30 11:25:12 -04:00
|
|
|
let mut data = Vec::new();
|
|
|
|
public_key
|
|
|
|
.encode_to_vec(&mut data)
|
2021-12-14 06:25:07 -05:00
|
|
|
.map_err(|_| data_error("invalid rsa public key"))?;
|
2023-10-30 11:25:12 -04:00
|
|
|
|
2021-12-14 06:25:07 -05:00
|
|
|
let public_exponent =
|
|
|
|
public_key.public_exponent.as_bytes().to_vec().into();
|
|
|
|
let modulus_length = public_key.modulus.as_bytes().len() * 8;
|
|
|
|
|
|
|
|
Ok(ImportKeyResult::Rsa {
|
2023-06-22 17:37:56 -04:00
|
|
|
raw_data: RustRawKeyData::Public(data.into()),
|
2021-12-14 06:25:07 -05:00
|
|
|
modulus_length,
|
|
|
|
public_exponent,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
KeyData::JwkPrivateRsa {
|
|
|
|
n,
|
|
|
|
e,
|
|
|
|
d,
|
|
|
|
p,
|
|
|
|
q,
|
|
|
|
dp,
|
|
|
|
dq,
|
|
|
|
qi,
|
|
|
|
} => {
|
|
|
|
jwt_b64_int_or_err!(modulus, &n, "invalid modulus");
|
|
|
|
jwt_b64_int_or_err!(public_exponent, &e, "invalid public exponent");
|
|
|
|
jwt_b64_int_or_err!(private_exponent, &d, "invalid private exponent");
|
|
|
|
jwt_b64_int_or_err!(prime1, &p, "invalid first prime factor");
|
|
|
|
jwt_b64_int_or_err!(prime2, &q, "invalid second prime factor");
|
|
|
|
jwt_b64_int_or_err!(exponent1, &dp, "invalid first CRT exponent");
|
|
|
|
jwt_b64_int_or_err!(exponent2, &dq, "invalid second CRT exponent");
|
|
|
|
jwt_b64_int_or_err!(coefficient, &qi, "invalid CRT coefficient");
|
|
|
|
|
|
|
|
let private_key = rsa::pkcs1::RsaPrivateKey {
|
|
|
|
modulus,
|
|
|
|
public_exponent,
|
|
|
|
private_exponent,
|
|
|
|
prime1,
|
|
|
|
prime2,
|
|
|
|
exponent1,
|
|
|
|
exponent2,
|
|
|
|
coefficient,
|
2022-06-20 07:23:57 -04:00
|
|
|
other_prime_infos: None,
|
2021-12-14 06:25:07 -05:00
|
|
|
};
|
|
|
|
|
2023-10-30 11:25:12 -04:00
|
|
|
let mut data = Vec::new();
|
|
|
|
private_key
|
|
|
|
.encode_to_vec(&mut data)
|
2021-12-14 06:25:07 -05:00
|
|
|
.map_err(|_| data_error("invalid rsa private key"))?;
|
|
|
|
|
|
|
|
let public_exponent =
|
|
|
|
private_key.public_exponent.as_bytes().to_vec().into();
|
|
|
|
let modulus_length = private_key.modulus.as_bytes().len() * 8;
|
|
|
|
|
|
|
|
Ok(ImportKeyResult::Rsa {
|
2023-06-22 17:37:56 -04:00
|
|
|
raw_data: RustRawKeyData::Private(data.into()),
|
2021-12-14 06:25:07 -05:00
|
|
|
modulus_length,
|
|
|
|
public_exponent,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-10 09:06:03 -05:00
|
|
|
fn import_key_rsassa(
|
|
|
|
key_data: KeyData,
|
|
|
|
) -> Result<ImportKeyResult, deno_core::anyhow::Error> {
|
|
|
|
match key_data {
|
|
|
|
KeyData::Spki(data) => {
|
|
|
|
// 2-3.
|
2023-11-04 00:29:25 -04:00
|
|
|
let pk_info = spki::SubjectPublicKeyInfoRef::try_from(&*data)
|
2021-12-10 09:06:03 -05:00
|
|
|
.map_err(|e| data_error(e.to_string()))?;
|
|
|
|
|
|
|
|
// 4-5.
|
|
|
|
let alg = pk_info.algorithm.oid;
|
|
|
|
|
2022-10-04 08:07:59 -04:00
|
|
|
// 6-7. (skipped, only support rsaEncryption for interoperability)
|
|
|
|
if alg != RSA_ENCRYPTION_OID {
|
|
|
|
return Err(data_error("unsupported algorithm"));
|
2021-12-10 09:06:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 8-9.
|
2023-11-04 00:29:25 -04:00
|
|
|
let public_key = rsa::pkcs1::RsaPublicKey::from_der(
|
|
|
|
pk_info.subject_public_key.raw_bytes(),
|
|
|
|
)
|
|
|
|
.map_err(|e| data_error(e.to_string()))?;
|
2021-12-10 09:06:03 -05:00
|
|
|
|
|
|
|
let bytes_consumed = public_key
|
|
|
|
.encoded_len()
|
|
|
|
.map_err(|e| data_error(e.to_string()))?;
|
|
|
|
|
|
|
|
if bytes_consumed
|
2023-11-04 00:29:25 -04:00
|
|
|
!= rsa::pkcs1::der::Length::new(
|
|
|
|
pk_info.subject_public_key.raw_bytes().len() as u16,
|
|
|
|
)
|
2021-12-10 09:06:03 -05:00
|
|
|
{
|
|
|
|
return Err(data_error("public key is invalid (too long)"));
|
|
|
|
}
|
|
|
|
|
2023-11-04 08:18:53 -04:00
|
|
|
let data = pk_info.subject_public_key.raw_bytes().to_vec().into();
|
2021-12-10 09:06:03 -05:00
|
|
|
let public_exponent =
|
|
|
|
public_key.public_exponent.as_bytes().to_vec().into();
|
|
|
|
let modulus_length = public_key.modulus.as_bytes().len() * 8;
|
|
|
|
|
|
|
|
Ok(ImportKeyResult::Rsa {
|
2023-06-22 17:37:56 -04:00
|
|
|
raw_data: RustRawKeyData::Public(data),
|
2021-12-10 09:06:03 -05:00
|
|
|
modulus_length,
|
|
|
|
public_exponent,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
KeyData::Pkcs8(data) => {
|
|
|
|
// 2-3.
|
2021-12-16 11:28:43 -05:00
|
|
|
let pk_info = PrivateKeyInfo::from_der(&data)
|
2021-12-10 09:06:03 -05:00
|
|
|
.map_err(|e| data_error(e.to_string()))?;
|
|
|
|
|
|
|
|
// 4-5.
|
|
|
|
let alg = pk_info.algorithm.oid;
|
|
|
|
|
2022-10-04 08:07:59 -04:00
|
|
|
// 6-7. (skipped, only support rsaEncryption for interoperability)
|
|
|
|
if alg != RSA_ENCRYPTION_OID {
|
|
|
|
return Err(data_error("unsupported algorithm"));
|
2021-12-10 09:06:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 8-9.
|
|
|
|
let private_key =
|
|
|
|
rsa::pkcs1::RsaPrivateKey::from_der(pk_info.private_key)
|
|
|
|
.map_err(|e| data_error(e.to_string()))?;
|
|
|
|
|
|
|
|
let bytes_consumed = private_key
|
|
|
|
.encoded_len()
|
|
|
|
.map_err(|e| data_error(e.to_string()))?;
|
|
|
|
|
|
|
|
if bytes_consumed
|
2023-10-30 11:25:12 -04:00
|
|
|
!= rsa::pkcs1::der::Length::new(pk_info.private_key.len() as u16)
|
2021-12-10 09:06:03 -05:00
|
|
|
{
|
|
|
|
return Err(data_error("private key is invalid (too long)"));
|
|
|
|
}
|
|
|
|
|
|
|
|
let data = pk_info.private_key.to_vec().into();
|
|
|
|
let public_exponent =
|
|
|
|
private_key.public_exponent.as_bytes().to_vec().into();
|
|
|
|
let modulus_length = private_key.modulus.as_bytes().len() * 8;
|
|
|
|
|
|
|
|
Ok(ImportKeyResult::Rsa {
|
2023-06-22 17:37:56 -04:00
|
|
|
raw_data: RustRawKeyData::Private(data),
|
2021-12-10 09:06:03 -05:00
|
|
|
modulus_length,
|
|
|
|
public_exponent,
|
|
|
|
})
|
|
|
|
}
|
2021-12-14 06:25:07 -05:00
|
|
|
KeyData::JwkPublicRsa { .. } | KeyData::JwkPrivateRsa { .. } => {
|
|
|
|
import_key_rsa_jwk(key_data)
|
|
|
|
}
|
2021-12-10 09:06:03 -05:00
|
|
|
_ => Err(unsupported_format()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn import_key_rsapss(
|
|
|
|
key_data: KeyData,
|
|
|
|
) -> Result<ImportKeyResult, deno_core::anyhow::Error> {
|
|
|
|
match key_data {
|
|
|
|
KeyData::Spki(data) => {
|
|
|
|
// 2-3.
|
2023-11-04 00:29:25 -04:00
|
|
|
let pk_info = spki::SubjectPublicKeyInfoRef::try_from(&*data)
|
2021-12-10 09:06:03 -05:00
|
|
|
.map_err(|e| data_error(e.to_string()))?;
|
|
|
|
|
|
|
|
// 4-5.
|
|
|
|
let alg = pk_info.algorithm.oid;
|
|
|
|
|
2022-10-04 08:07:59 -04:00
|
|
|
// 6-7. (skipped, only support rsaEncryption for interoperability)
|
|
|
|
if alg != RSA_ENCRYPTION_OID {
|
|
|
|
return Err(data_error("unsupported algorithm"));
|
2021-12-10 09:06:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 8-9.
|
2023-11-04 00:29:25 -04:00
|
|
|
let public_key = rsa::pkcs1::RsaPublicKey::from_der(
|
|
|
|
pk_info.subject_public_key.raw_bytes(),
|
|
|
|
)
|
|
|
|
.map_err(|e| data_error(e.to_string()))?;
|
2021-12-10 09:06:03 -05:00
|
|
|
|
|
|
|
let bytes_consumed = public_key
|
|
|
|
.encoded_len()
|
|
|
|
.map_err(|e| data_error(e.to_string()))?;
|
|
|
|
|
|
|
|
if bytes_consumed
|
2023-11-04 00:29:25 -04:00
|
|
|
!= rsa::pkcs1::der::Length::new(
|
|
|
|
pk_info.subject_public_key.raw_bytes().len() as u16,
|
|
|
|
)
|
2021-12-10 09:06:03 -05:00
|
|
|
{
|
|
|
|
return Err(data_error("public key is invalid (too long)"));
|
|
|
|
}
|
|
|
|
|
2023-11-04 08:18:53 -04:00
|
|
|
let data = pk_info.subject_public_key.raw_bytes().to_vec().into();
|
2021-12-10 09:06:03 -05:00
|
|
|
let public_exponent =
|
|
|
|
public_key.public_exponent.as_bytes().to_vec().into();
|
|
|
|
let modulus_length = public_key.modulus.as_bytes().len() * 8;
|
|
|
|
|
|
|
|
Ok(ImportKeyResult::Rsa {
|
2023-06-22 17:37:56 -04:00
|
|
|
raw_data: RustRawKeyData::Public(data),
|
2021-12-10 09:06:03 -05:00
|
|
|
modulus_length,
|
|
|
|
public_exponent,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
KeyData::Pkcs8(data) => {
|
|
|
|
// 2-3.
|
2021-12-16 11:28:43 -05:00
|
|
|
let pk_info = PrivateKeyInfo::from_der(&data)
|
2021-12-10 09:06:03 -05:00
|
|
|
.map_err(|e| data_error(e.to_string()))?;
|
|
|
|
|
|
|
|
// 4-5.
|
|
|
|
let alg = pk_info.algorithm.oid;
|
|
|
|
|
2022-10-04 08:07:59 -04:00
|
|
|
// 6-7. (skipped, only support rsaEncryption for interoperability)
|
|
|
|
if alg != RSA_ENCRYPTION_OID {
|
|
|
|
return Err(data_error("unsupported algorithm"));
|
2021-12-10 09:06:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 8-9.
|
|
|
|
let private_key =
|
|
|
|
rsa::pkcs1::RsaPrivateKey::from_der(pk_info.private_key)
|
|
|
|
.map_err(|e| data_error(e.to_string()))?;
|
|
|
|
|
|
|
|
let bytes_consumed = private_key
|
|
|
|
.encoded_len()
|
|
|
|
.map_err(|e| data_error(e.to_string()))?;
|
|
|
|
|
|
|
|
if bytes_consumed
|
2023-10-30 11:25:12 -04:00
|
|
|
!= rsa::pkcs1::der::Length::new(pk_info.private_key.len() as u16)
|
2021-12-10 09:06:03 -05:00
|
|
|
{
|
|
|
|
return Err(data_error("private key is invalid (too long)"));
|
|
|
|
}
|
|
|
|
|
|
|
|
let data = pk_info.private_key.to_vec().into();
|
|
|
|
let public_exponent =
|
|
|
|
private_key.public_exponent.as_bytes().to_vec().into();
|
|
|
|
let modulus_length = private_key.modulus.as_bytes().len() * 8;
|
|
|
|
|
|
|
|
Ok(ImportKeyResult::Rsa {
|
2023-06-22 17:37:56 -04:00
|
|
|
raw_data: RustRawKeyData::Private(data),
|
2021-12-10 09:06:03 -05:00
|
|
|
modulus_length,
|
|
|
|
public_exponent,
|
|
|
|
})
|
|
|
|
}
|
2021-12-14 06:25:07 -05:00
|
|
|
KeyData::JwkPublicRsa { .. } | KeyData::JwkPrivateRsa { .. } => {
|
|
|
|
import_key_rsa_jwk(key_data)
|
|
|
|
}
|
2021-12-10 09:06:03 -05:00
|
|
|
_ => Err(unsupported_format()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn import_key_rsaoaep(
|
|
|
|
key_data: KeyData,
|
|
|
|
) -> Result<ImportKeyResult, deno_core::anyhow::Error> {
|
|
|
|
match key_data {
|
|
|
|
KeyData::Spki(data) => {
|
|
|
|
// 2-3.
|
2023-11-04 00:29:25 -04:00
|
|
|
let pk_info = spki::SubjectPublicKeyInfoRef::try_from(&*data)
|
2021-12-10 09:06:03 -05:00
|
|
|
.map_err(|e| data_error(e.to_string()))?;
|
|
|
|
|
|
|
|
// 4-5.
|
|
|
|
let alg = pk_info.algorithm.oid;
|
|
|
|
|
2022-10-04 08:07:59 -04:00
|
|
|
// 6-7. (skipped, only support rsaEncryption for interoperability)
|
|
|
|
if alg != RSA_ENCRYPTION_OID {
|
|
|
|
return Err(data_error("unsupported algorithm"));
|
2021-12-10 09:06:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 8-9.
|
2023-11-04 00:29:25 -04:00
|
|
|
let public_key = rsa::pkcs1::RsaPublicKey::from_der(
|
|
|
|
pk_info.subject_public_key.raw_bytes(),
|
|
|
|
)
|
|
|
|
.map_err(|e| data_error(e.to_string()))?;
|
2021-12-10 09:06:03 -05:00
|
|
|
|
|
|
|
let bytes_consumed = public_key
|
|
|
|
.encoded_len()
|
|
|
|
.map_err(|e| data_error(e.to_string()))?;
|
|
|
|
|
|
|
|
if bytes_consumed
|
2023-11-04 00:29:25 -04:00
|
|
|
!= rsa::pkcs1::der::Length::new(
|
|
|
|
pk_info.subject_public_key.raw_bytes().len() as u16,
|
|
|
|
)
|
2021-12-10 09:06:03 -05:00
|
|
|
{
|
|
|
|
return Err(data_error("public key is invalid (too long)"));
|
|
|
|
}
|
|
|
|
|
2023-11-04 08:18:53 -04:00
|
|
|
let data = pk_info.subject_public_key.raw_bytes().to_vec().into();
|
2021-12-10 09:06:03 -05:00
|
|
|
let public_exponent =
|
|
|
|
public_key.public_exponent.as_bytes().to_vec().into();
|
|
|
|
let modulus_length = public_key.modulus.as_bytes().len() * 8;
|
|
|
|
|
|
|
|
Ok(ImportKeyResult::Rsa {
|
2023-06-22 17:37:56 -04:00
|
|
|
raw_data: RustRawKeyData::Public(data),
|
2021-12-10 09:06:03 -05:00
|
|
|
modulus_length,
|
|
|
|
public_exponent,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
KeyData::Pkcs8(data) => {
|
|
|
|
// 2-3.
|
2021-12-16 11:28:43 -05:00
|
|
|
let pk_info = PrivateKeyInfo::from_der(&data)
|
2021-12-10 09:06:03 -05:00
|
|
|
.map_err(|e| data_error(e.to_string()))?;
|
|
|
|
|
|
|
|
// 4-5.
|
|
|
|
let alg = pk_info.algorithm.oid;
|
|
|
|
|
2022-10-04 08:07:59 -04:00
|
|
|
// 6-7. (skipped, only support rsaEncryption for interoperability)
|
|
|
|
if alg != RSA_ENCRYPTION_OID {
|
|
|
|
return Err(data_error("unsupported algorithm"));
|
2021-12-10 09:06:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// 8-9.
|
|
|
|
let private_key =
|
|
|
|
rsa::pkcs1::RsaPrivateKey::from_der(pk_info.private_key)
|
|
|
|
.map_err(|e| data_error(e.to_string()))?;
|
|
|
|
|
|
|
|
let bytes_consumed = private_key
|
|
|
|
.encoded_len()
|
|
|
|
.map_err(|e| data_error(e.to_string()))?;
|
|
|
|
|
|
|
|
if bytes_consumed
|
2023-10-30 11:25:12 -04:00
|
|
|
!= rsa::pkcs1::der::Length::new(pk_info.private_key.len() as u16)
|
2021-12-10 09:06:03 -05:00
|
|
|
{
|
|
|
|
return Err(data_error("private key is invalid (too long)"));
|
|
|
|
}
|
|
|
|
|
|
|
|
let data = pk_info.private_key.to_vec().into();
|
|
|
|
let public_exponent =
|
|
|
|
private_key.public_exponent.as_bytes().to_vec().into();
|
|
|
|
let modulus_length = private_key.modulus.as_bytes().len() * 8;
|
|
|
|
|
|
|
|
Ok(ImportKeyResult::Rsa {
|
2023-06-22 17:37:56 -04:00
|
|
|
raw_data: RustRawKeyData::Private(data),
|
2021-12-10 09:06:03 -05:00
|
|
|
modulus_length,
|
|
|
|
public_exponent,
|
|
|
|
})
|
|
|
|
}
|
2021-12-14 06:25:07 -05:00
|
|
|
KeyData::JwkPublicRsa { .. } | KeyData::JwkPrivateRsa { .. } => {
|
|
|
|
import_key_rsa_jwk(key_data)
|
|
|
|
}
|
2021-12-10 09:06:03 -05:00
|
|
|
_ => Err(unsupported_format()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-16 11:28:43 -05:00
|
|
|
fn decode_b64url_to_field_bytes<C: elliptic_curve::Curve>(
|
|
|
|
b64: &str,
|
|
|
|
) -> Result<elliptic_curve::FieldBytes<C>, deno_core::anyhow::Error> {
|
|
|
|
jwt_b64_int_or_err!(val, b64, "invalid b64 coordinate");
|
|
|
|
|
|
|
|
let mut bytes = elliptic_curve::FieldBytes::<C>::default();
|
2022-10-03 00:07:25 -04:00
|
|
|
let original_bytes = val.as_bytes();
|
|
|
|
let mut new_bytes: Vec<u8> = vec![];
|
|
|
|
if original_bytes.len() < bytes.len() {
|
|
|
|
new_bytes = vec![0; bytes.len() - original_bytes.len()];
|
|
|
|
}
|
|
|
|
new_bytes.extend_from_slice(original_bytes);
|
|
|
|
|
|
|
|
let val = new_bytes.as_slice();
|
|
|
|
|
2021-12-16 11:28:43 -05:00
|
|
|
if val.len() != bytes.len() {
|
|
|
|
return Err(data_error("invalid b64 coordinate"));
|
|
|
|
}
|
|
|
|
bytes.copy_from_slice(val);
|
|
|
|
|
|
|
|
Ok(bytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn import_key_ec_jwk_to_point(
|
|
|
|
x: String,
|
|
|
|
y: String,
|
|
|
|
named_curve: EcNamedCurve,
|
|
|
|
) -> Result<Vec<u8>, deno_core::anyhow::Error> {
|
|
|
|
let point_bytes = match named_curve {
|
|
|
|
EcNamedCurve::P256 => {
|
|
|
|
let x = decode_b64url_to_field_bytes::<p256::NistP256>(&x)?;
|
|
|
|
let y = decode_b64url_to_field_bytes::<p256::NistP256>(&y)?;
|
|
|
|
|
|
|
|
p256::EncodedPoint::from_affine_coordinates(&x, &y, false).to_bytes()
|
|
|
|
}
|
|
|
|
EcNamedCurve::P384 => {
|
|
|
|
let x = decode_b64url_to_field_bytes::<p384::NistP384>(&x)?;
|
|
|
|
let y = decode_b64url_to_field_bytes::<p384::NistP384>(&y)?;
|
|
|
|
|
|
|
|
p384::EncodedPoint::from_affine_coordinates(&x, &y, false).to_bytes()
|
|
|
|
}
|
2024-01-06 06:18:31 -05:00
|
|
|
EcNamedCurve::P521 => {
|
|
|
|
let x = decode_b64url_to_field_bytes::<p521::NistP521>(&x)?;
|
|
|
|
let y = decode_b64url_to_field_bytes::<p521::NistP521>(&y)?;
|
|
|
|
|
|
|
|
p521::EncodedPoint::from_affine_coordinates(&x, &y, false).to_bytes()
|
|
|
|
}
|
2021-12-16 11:28:43 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok(point_bytes.to_vec())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn import_key_ec_jwk(
|
|
|
|
key_data: KeyData,
|
|
|
|
named_curve: EcNamedCurve,
|
|
|
|
) -> Result<ImportKeyResult, deno_core::anyhow::Error> {
|
|
|
|
match key_data {
|
|
|
|
KeyData::JwkPublicEc { x, y } => {
|
|
|
|
let point_bytes = import_key_ec_jwk_to_point(x, y, named_curve)?;
|
|
|
|
|
|
|
|
Ok(ImportKeyResult::Ec {
|
2023-06-22 17:37:56 -04:00
|
|
|
raw_data: RustRawKeyData::Public(point_bytes.into()),
|
2021-12-16 11:28:43 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
KeyData::JwkPrivateEc { d, x, y } => {
|
2022-01-19 01:44:35 -05:00
|
|
|
jwt_b64_int_or_err!(private_d, &d, "invalid JWK private key");
|
2022-06-20 07:23:57 -04:00
|
|
|
let point_bytes = import_key_ec_jwk_to_point(x, y, named_curve)?;
|
2022-01-19 01:44:35 -05:00
|
|
|
let pkcs8_der = match named_curve {
|
2021-12-16 11:28:43 -05:00
|
|
|
EcNamedCurve::P256 => {
|
|
|
|
let d = decode_b64url_to_field_bytes::<p256::NistP256>(&d)?;
|
2023-11-04 00:29:25 -04:00
|
|
|
let pk = p256::SecretKey::from_bytes(&d)?;
|
2022-01-19 01:44:35 -05:00
|
|
|
|
2023-10-30 11:25:12 -04:00
|
|
|
pk.to_pkcs8_der()
|
|
|
|
.map_err(|_| data_error("invalid JWK private key"))?
|
2021-12-16 11:28:43 -05:00
|
|
|
}
|
2022-01-19 01:44:35 -05:00
|
|
|
EcNamedCurve::P384 => {
|
|
|
|
let d = decode_b64url_to_field_bytes::<p384::NistP384>(&d)?;
|
2023-11-04 00:29:25 -04:00
|
|
|
let pk = p384::SecretKey::from_bytes(&d)?;
|
2022-01-19 01:44:35 -05:00
|
|
|
|
2023-10-30 11:25:12 -04:00
|
|
|
pk.to_pkcs8_der()
|
|
|
|
.map_err(|_| data_error("invalid JWK private key"))?
|
2022-01-19 01:44:35 -05:00
|
|
|
}
|
|
|
|
EcNamedCurve::P521 => {
|
|
|
|
return Err(data_error("Unsupported named curve"))
|
|
|
|
}
|
2021-12-16 11:28:43 -05:00
|
|
|
};
|
|
|
|
|
2022-01-19 01:44:35 -05:00
|
|
|
// Import using ring, to validate key
|
|
|
|
let key_alg = match named_curve {
|
2024-01-01 17:22:48 -05:00
|
|
|
EcNamedCurve::P256 => CryptoNamedCurve::P256.into(),
|
|
|
|
EcNamedCurve::P384 => CryptoNamedCurve::P256.into(),
|
2022-01-19 01:44:35 -05:00
|
|
|
EcNamedCurve::P521 => {
|
|
|
|
return Err(data_error("Unsupported named curve"))
|
|
|
|
}
|
|
|
|
};
|
2021-12-16 11:28:43 -05:00
|
|
|
|
2023-10-27 17:15:09 -04:00
|
|
|
let rng = ring::rand::SystemRandom::new();
|
2022-01-19 01:44:35 -05:00
|
|
|
let _key_pair = EcdsaKeyPair::from_private_key_and_public_key(
|
|
|
|
key_alg,
|
|
|
|
private_d.as_bytes(),
|
|
|
|
point_bytes.as_ref(),
|
2023-10-27 17:15:09 -04:00
|
|
|
&rng,
|
2021-12-16 11:28:43 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
Ok(ImportKeyResult::Ec {
|
2023-06-22 17:37:56 -04:00
|
|
|
raw_data: RustRawKeyData::Private(pkcs8_der.as_bytes().to_vec().into()),
|
2021-12-16 11:28:43 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ECParametersSpki {
|
|
|
|
pub named_curve_alg: spki::der::asn1::ObjectIdentifier,
|
|
|
|
}
|
|
|
|
|
2022-06-20 07:23:57 -04:00
|
|
|
impl<'a> TryFrom<spki::der::asn1::AnyRef<'a>> for ECParametersSpki {
|
2021-12-16 11:28:43 -05:00
|
|
|
type Error = spki::der::Error;
|
|
|
|
|
|
|
|
fn try_from(
|
2022-06-20 07:23:57 -04:00
|
|
|
any: spki::der::asn1::AnyRef<'a>,
|
2021-12-16 11:28:43 -05:00
|
|
|
) -> spki::der::Result<ECParametersSpki> {
|
2023-11-04 00:29:25 -04:00
|
|
|
let x = any.try_into()?;
|
2021-12-16 11:28:43 -05:00
|
|
|
|
|
|
|
Ok(Self { named_curve_alg: x })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-10 09:06:03 -05:00
|
|
|
fn import_key_ec(
|
|
|
|
key_data: KeyData,
|
|
|
|
named_curve: EcNamedCurve,
|
|
|
|
) -> Result<ImportKeyResult, AnyError> {
|
2021-12-16 11:28:43 -05:00
|
|
|
match key_data {
|
2021-12-10 09:06:03 -05:00
|
|
|
KeyData::Raw(data) => {
|
|
|
|
// The point is parsed and validated, ultimately the original data is
|
|
|
|
// returned though.
|
|
|
|
match named_curve {
|
|
|
|
EcNamedCurve::P256 => {
|
|
|
|
// 1-2.
|
|
|
|
let point = p256::EncodedPoint::from_bytes(&data)
|
2022-09-24 02:16:52 -04:00
|
|
|
.map_err(|_| data_error("invalid P-256 elliptic curve point"))?;
|
2021-12-10 09:06:03 -05:00
|
|
|
// 3.
|
|
|
|
if point.is_identity() {
|
2022-09-24 02:16:52 -04:00
|
|
|
return Err(data_error("invalid P-256 elliptic curve point"));
|
2021-12-10 09:06:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
EcNamedCurve::P384 => {
|
|
|
|
// 1-2.
|
|
|
|
let point = p384::EncodedPoint::from_bytes(&data)
|
2022-09-24 02:16:52 -04:00
|
|
|
.map_err(|_| data_error("invalid P-384 elliptic curve point"))?;
|
2021-12-10 09:06:03 -05:00
|
|
|
// 3.
|
|
|
|
if point.is_identity() {
|
2022-09-24 02:16:52 -04:00
|
|
|
return Err(data_error("invalid P-384 elliptic curve point"));
|
2021-12-10 09:06:03 -05:00
|
|
|
}
|
|
|
|
}
|
2024-01-06 06:18:31 -05:00
|
|
|
EcNamedCurve::P521 => {
|
|
|
|
// 1-2.
|
|
|
|
let point = p521::EncodedPoint::from_bytes(&data)
|
|
|
|
.map_err(|_| data_error("invalid P-521 elliptic curve point"))?;
|
|
|
|
// 3.
|
|
|
|
if point.is_identity() {
|
|
|
|
return Err(data_error("invalid P-521 elliptic curve point"));
|
|
|
|
}
|
|
|
|
}
|
2021-12-10 09:06:03 -05:00
|
|
|
};
|
2021-12-16 11:28:43 -05:00
|
|
|
Ok(ImportKeyResult::Ec {
|
2023-06-22 17:37:56 -04:00
|
|
|
raw_data: RustRawKeyData::Public(data.to_vec().into()),
|
2021-12-16 11:28:43 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
KeyData::Pkcs8(data) => {
|
2022-01-19 01:44:35 -05:00
|
|
|
// 2-7
|
|
|
|
// Deserialize PKCS8 - validate structure, extracts named_curve
|
|
|
|
let named_curve_alg = match named_curve {
|
2022-06-20 07:23:57 -04:00
|
|
|
EcNamedCurve::P256 | EcNamedCurve::P384 => {
|
|
|
|
let pk = PrivateKeyInfo::from_der(data.as_ref())
|
|
|
|
.map_err(|_| data_error("expected valid PKCS#8 data"))?;
|
|
|
|
pk.algorithm
|
|
|
|
.parameters
|
|
|
|
.ok_or_else(|| data_error("malformed parameters"))?
|
2023-11-04 00:29:25 -04:00
|
|
|
.try_into()
|
2022-06-20 07:23:57 -04:00
|
|
|
.unwrap()
|
2022-01-19 01:44:35 -05:00
|
|
|
}
|
|
|
|
EcNamedCurve::P521 => {
|
|
|
|
return Err(data_error("Unsupported named curve"))
|
|
|
|
}
|
|
|
|
};
|
2021-12-16 11:28:43 -05:00
|
|
|
|
|
|
|
// 8-9.
|
2022-01-19 01:44:35 -05:00
|
|
|
let pk_named_curve = match named_curve_alg {
|
2021-12-16 11:28:43 -05:00
|
|
|
// id-secp256r1
|
|
|
|
ID_SECP256R1_OID => Some(EcNamedCurve::P256),
|
|
|
|
// id-secp384r1
|
|
|
|
ID_SECP384R1_OID => Some(EcNamedCurve::P384),
|
2022-01-19 01:44:35 -05:00
|
|
|
// id-secp521r1
|
2021-12-16 11:28:43 -05:00
|
|
|
ID_SECP521R1_OID => Some(EcNamedCurve::P521),
|
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
|
|
|
|
// 10.
|
|
|
|
if let Some(pk_named_curve) = pk_named_curve {
|
2022-01-19 01:44:35 -05:00
|
|
|
let signing_alg = match pk_named_curve {
|
2024-01-01 17:22:48 -05:00
|
|
|
EcNamedCurve::P256 => CryptoNamedCurve::P256.into(),
|
|
|
|
EcNamedCurve::P384 => CryptoNamedCurve::P384.into(),
|
2022-01-19 01:44:35 -05:00
|
|
|
EcNamedCurve::P521 => {
|
|
|
|
return Err(data_error("Unsupported named curve"))
|
2021-12-16 11:28:43 -05:00
|
|
|
}
|
2022-01-19 01:44:35 -05:00
|
|
|
};
|
|
|
|
|
2023-10-27 17:15:09 -04:00
|
|
|
let rng = ring::rand::SystemRandom::new();
|
2022-01-19 01:44:35 -05:00
|
|
|
// deserialize pkcs8 using ring crate, to VALIDATE public key
|
2024-08-23 12:36:40 -04:00
|
|
|
let _private_key = EcdsaKeyPair::from_pkcs8(signing_alg, &data, &rng)
|
|
|
|
.map_err(|_| data_error("invalid key"))?;
|
2022-01-19 01:44:35 -05:00
|
|
|
|
2021-12-16 11:28:43 -05:00
|
|
|
// 11.
|
|
|
|
if named_curve != pk_named_curve {
|
|
|
|
return Err(data_error("curve mismatch"));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Err(data_error("Unsupported named curve"));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(ImportKeyResult::Ec {
|
2023-06-22 17:37:56 -04:00
|
|
|
raw_data: RustRawKeyData::Private(data.to_vec().into()),
|
2021-12-16 11:28:43 -05:00
|
|
|
})
|
2021-12-10 09:06:03 -05:00
|
|
|
}
|
2021-12-16 11:28:43 -05:00
|
|
|
KeyData::Spki(data) => {
|
|
|
|
// 2-3.
|
2023-11-04 00:29:25 -04:00
|
|
|
let pk_info = spki::SubjectPublicKeyInfoRef::try_from(&*data)
|
2021-12-16 11:28:43 -05:00
|
|
|
.map_err(|e| data_error(e.to_string()))?;
|
|
|
|
|
|
|
|
// 4.
|
|
|
|
let alg = pk_info.algorithm.oid;
|
|
|
|
// id-ecPublicKey
|
|
|
|
if alg != elliptic_curve::ALGORITHM_OID {
|
|
|
|
return Err(data_error("unsupported algorithm"));
|
|
|
|
}
|
|
|
|
|
|
|
|
// 5-7.
|
|
|
|
let params = ECParametersSpki::try_from(
|
|
|
|
pk_info
|
|
|
|
.algorithm
|
|
|
|
.parameters
|
|
|
|
.ok_or_else(|| data_error("malformed parameters"))?,
|
|
|
|
)
|
|
|
|
.map_err(|_| data_error("malformed parameters"))?;
|
|
|
|
|
|
|
|
// 8-9.
|
|
|
|
let named_curve_alg = params.named_curve_alg;
|
|
|
|
let pk_named_curve = match named_curve_alg {
|
|
|
|
// id-secp256r1
|
|
|
|
ID_SECP256R1_OID => Some(EcNamedCurve::P256),
|
|
|
|
// id-secp384r1
|
|
|
|
ID_SECP384R1_OID => Some(EcNamedCurve::P384),
|
|
|
|
// id-secp521r1
|
|
|
|
ID_SECP521R1_OID => Some(EcNamedCurve::P521),
|
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
|
|
|
|
// 10.
|
|
|
|
let encoded_key;
|
|
|
|
|
|
|
|
if let Some(pk_named_curve) = pk_named_curve {
|
|
|
|
let pk = pk_info.subject_public_key;
|
|
|
|
|
2023-11-04 08:18:53 -04:00
|
|
|
encoded_key = pk.raw_bytes().to_vec();
|
2021-12-16 11:28:43 -05:00
|
|
|
|
|
|
|
let bytes_consumed = match named_curve {
|
|
|
|
EcNamedCurve::P256 => {
|
|
|
|
let point =
|
|
|
|
p256::EncodedPoint::from_bytes(&*encoded_key).map_err(|_| {
|
2022-09-24 02:16:52 -04:00
|
|
|
data_error("invalid P-256 elliptic curve SPKI data")
|
2021-12-16 11:28:43 -05:00
|
|
|
})?;
|
|
|
|
if point.is_identity() {
|
2022-09-24 02:16:52 -04:00
|
|
|
return Err(data_error("invalid P-256 elliptic curve point"));
|
2021-12-16 11:28:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
point.as_bytes().len()
|
|
|
|
}
|
|
|
|
EcNamedCurve::P384 => {
|
|
|
|
let point =
|
|
|
|
p384::EncodedPoint::from_bytes(&*encoded_key).map_err(|_| {
|
2022-09-24 02:16:52 -04:00
|
|
|
data_error("invalid P-384 elliptic curve SPKI data")
|
2021-12-16 11:28:43 -05:00
|
|
|
})?;
|
|
|
|
|
|
|
|
if point.is_identity() {
|
2022-09-24 02:16:52 -04:00
|
|
|
return Err(data_error("invalid P-384 elliptic curve point"));
|
2021-12-16 11:28:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
point.as_bytes().len()
|
|
|
|
}
|
2024-01-06 06:18:31 -05:00
|
|
|
EcNamedCurve::P521 => {
|
|
|
|
let point =
|
|
|
|
p521::EncodedPoint::from_bytes(&*encoded_key).map_err(|_| {
|
|
|
|
data_error("invalid P-521 elliptic curve SPKI data")
|
|
|
|
})?;
|
|
|
|
|
|
|
|
if point.is_identity() {
|
|
|
|
return Err(data_error("invalid P-521 elliptic curve point"));
|
|
|
|
}
|
|
|
|
|
|
|
|
point.as_bytes().len()
|
|
|
|
}
|
2021-12-16 11:28:43 -05:00
|
|
|
};
|
|
|
|
|
2023-11-04 00:29:25 -04:00
|
|
|
if bytes_consumed != pk_info.subject_public_key.raw_bytes().len() {
|
2021-12-16 11:28:43 -05:00
|
|
|
return Err(data_error("public key is invalid (too long)"));
|
|
|
|
}
|
|
|
|
|
|
|
|
// 11.
|
|
|
|
if named_curve != pk_named_curve {
|
|
|
|
return Err(data_error("curve mismatch"));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Err(data_error("Unsupported named curve"));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(ImportKeyResult::Ec {
|
2023-06-22 17:37:56 -04:00
|
|
|
raw_data: RustRawKeyData::Public(encoded_key.into()),
|
2021-12-16 11:28:43 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
KeyData::JwkPublicEc { .. } | KeyData::JwkPrivateEc { .. } => {
|
|
|
|
import_key_ec_jwk(key_data, named_curve)
|
|
|
|
}
|
|
|
|
_ => Err(unsupported_format()),
|
|
|
|
}
|
2021-12-10 09:06:03 -05:00
|
|
|
}
|
2021-12-10 16:23:19 -05:00
|
|
|
|
|
|
|
fn import_key_aes(key_data: KeyData) -> Result<ImportKeyResult, AnyError> {
|
|
|
|
Ok(match key_data {
|
|
|
|
KeyData::JwkSecret { k } => {
|
2023-10-26 12:39:04 -04:00
|
|
|
let data = BASE64_URL_SAFE_FORGIVING
|
|
|
|
.decode(k)
|
2021-12-10 16:23:19 -05:00
|
|
|
.map_err(|_| data_error("invalid key data"))?;
|
|
|
|
ImportKeyResult::Hmac {
|
2023-06-22 17:37:56 -04:00
|
|
|
raw_data: RustRawKeyData::Secret(data.into()),
|
2021-12-10 16:23:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => return Err(unsupported_format()),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn import_key_hmac(key_data: KeyData) -> Result<ImportKeyResult, AnyError> {
|
|
|
|
Ok(match key_data {
|
|
|
|
KeyData::JwkSecret { k } => {
|
2023-10-26 12:39:04 -04:00
|
|
|
let data = BASE64_URL_SAFE_FORGIVING
|
|
|
|
.decode(k)
|
2021-12-10 16:23:19 -05:00
|
|
|
.map_err(|_| data_error("invalid key data"))?;
|
|
|
|
ImportKeyResult::Hmac {
|
2023-06-22 17:37:56 -04:00
|
|
|
raw_data: RustRawKeyData::Secret(data.into()),
|
2021-12-10 16:23:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => return Err(unsupported_format()),
|
|
|
|
})
|
|
|
|
}
|