2023-01-13 02:51:32 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
2023-10-26 12:39:04 -04:00
|
|
|
use base64::prelude::BASE64_URL_SAFE_NO_PAD;
|
|
|
|
use base64::Engine;
|
2023-10-30 11:25:12 -04:00
|
|
|
use deno_core::error::custom_error;
|
2022-09-27 08:13:42 -04: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::ToJsBuffer;
|
2022-09-27 08:13:42 -04:00
|
|
|
use elliptic_curve::pkcs8::PrivateKeyInfo;
|
|
|
|
use rand::rngs::OsRng;
|
|
|
|
use rand::RngCore;
|
|
|
|
use ring::signature::Ed25519KeyPair;
|
|
|
|
use ring::signature::KeyPair;
|
|
|
|
use spki::der::Decode;
|
|
|
|
use spki::der::Encode;
|
|
|
|
|
2023-09-13 11:54:19 -04:00
|
|
|
#[op2(fast)]
|
2023-05-08 17:07:45 -04:00
|
|
|
pub fn op_crypto_generate_ed25519_keypair(
|
2023-09-13 11:54:19 -04:00
|
|
|
#[buffer] pkey: &mut [u8],
|
|
|
|
#[buffer] pubkey: &mut [u8],
|
2023-05-08 17:07:45 -04:00
|
|
|
) -> bool {
|
2022-09-27 08:13:42 -04:00
|
|
|
let mut rng = OsRng;
|
|
|
|
rng.fill_bytes(pkey);
|
|
|
|
|
|
|
|
let pair = match Ed25519KeyPair::from_seed_unchecked(pkey) {
|
|
|
|
Ok(p) => p,
|
|
|
|
Err(_) => return false,
|
|
|
|
};
|
|
|
|
pubkey.copy_from_slice(pair.public_key().as_ref());
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2023-09-13 11:54:19 -04:00
|
|
|
#[op2(fast)]
|
2023-05-08 17:07:45 -04:00
|
|
|
pub fn op_crypto_sign_ed25519(
|
2023-09-13 11:54:19 -04:00
|
|
|
#[buffer] key: &[u8],
|
|
|
|
#[buffer] data: &[u8],
|
|
|
|
#[buffer] signature: &mut [u8],
|
2023-05-08 17:07:45 -04:00
|
|
|
) -> bool {
|
2022-09-27 08:13:42 -04:00
|
|
|
let pair = match Ed25519KeyPair::from_seed_unchecked(key) {
|
|
|
|
Ok(p) => p,
|
|
|
|
Err(_) => return false,
|
|
|
|
};
|
|
|
|
signature.copy_from_slice(pair.sign(data).as_ref());
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2023-09-13 11:54:19 -04:00
|
|
|
#[op2(fast)]
|
2023-05-08 17:07:45 -04:00
|
|
|
pub fn op_crypto_verify_ed25519(
|
2023-09-13 11:54:19 -04:00
|
|
|
#[buffer] pubkey: &[u8],
|
|
|
|
#[buffer] data: &[u8],
|
|
|
|
#[buffer] signature: &[u8],
|
2023-05-08 17:07:45 -04:00
|
|
|
) -> bool {
|
2022-09-27 08:13:42 -04:00
|
|
|
ring::signature::UnparsedPublicKey::new(&ring::signature::ED25519, pubkey)
|
|
|
|
.verify(data, signature)
|
|
|
|
.is_ok()
|
|
|
|
}
|
|
|
|
|
|
|
|
// id-Ed25519 OBJECT IDENTIFIER ::= { 1 3 101 112 }
|
|
|
|
pub const ED25519_OID: const_oid::ObjectIdentifier =
|
|
|
|
const_oid::ObjectIdentifier::new_unwrap("1.3.101.112");
|
|
|
|
|
2023-09-13 11:54:19 -04:00
|
|
|
#[op2(fast)]
|
|
|
|
pub fn op_crypto_import_spki_ed25519(
|
|
|
|
#[buffer] key_data: &[u8],
|
|
|
|
#[buffer] out: &mut [u8],
|
|
|
|
) -> bool {
|
2022-09-27 08:13:42 -04:00
|
|
|
// 2-3.
|
|
|
|
let pk_info = match spki::SubjectPublicKeyInfo::from_der(key_data) {
|
|
|
|
Ok(pk_info) => pk_info,
|
|
|
|
Err(_) => return false,
|
|
|
|
};
|
|
|
|
// 4.
|
|
|
|
let alg = pk_info.algorithm.oid;
|
|
|
|
if alg != ED25519_OID {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// 5.
|
|
|
|
if pk_info.algorithm.parameters.is_some() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
out.copy_from_slice(pk_info.subject_public_key);
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2023-09-13 11:54:19 -04:00
|
|
|
#[op2(fast)]
|
|
|
|
pub fn op_crypto_import_pkcs8_ed25519(
|
|
|
|
#[buffer] key_data: &[u8],
|
|
|
|
#[buffer] out: &mut [u8],
|
|
|
|
) -> bool {
|
2022-09-27 08:13:42 -04:00
|
|
|
// 2-3.
|
2022-10-04 02:06:25 -04:00
|
|
|
// This should probably use OneAsymmetricKey instead
|
2022-09-27 08:13:42 -04:00
|
|
|
let pk_info = match PrivateKeyInfo::from_der(key_data) {
|
|
|
|
Ok(pk_info) => pk_info,
|
|
|
|
Err(_) => return false,
|
|
|
|
};
|
|
|
|
// 4.
|
|
|
|
let alg = pk_info.algorithm.oid;
|
|
|
|
if alg != ED25519_OID {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// 5.
|
|
|
|
if pk_info.algorithm.parameters.is_some() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// 6.
|
|
|
|
// CurvePrivateKey ::= OCTET STRING
|
2022-10-04 02:06:25 -04:00
|
|
|
if pk_info.private_key.len() != 34 {
|
2022-09-27 08:13:42 -04:00
|
|
|
return false;
|
|
|
|
}
|
2022-10-04 02:06:25 -04:00
|
|
|
out.copy_from_slice(&pk_info.private_key[2..]);
|
2022-09-27 08:13:42 -04:00
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2023-09-13 11:54:19 -04:00
|
|
|
#[op2]
|
|
|
|
#[serde]
|
2023-05-08 17:07:45 -04:00
|
|
|
pub fn op_crypto_export_spki_ed25519(
|
2023-09-13 11:54:19 -04:00
|
|
|
#[buffer] pubkey: &[u8],
|
2023-06-22 17:37:56 -04:00
|
|
|
) -> Result<ToJsBuffer, AnyError> {
|
2022-09-27 08:13:42 -04:00
|
|
|
let key_info = spki::SubjectPublicKeyInfo {
|
|
|
|
algorithm: spki::AlgorithmIdentifier {
|
|
|
|
// id-Ed25519
|
|
|
|
oid: ED25519_OID,
|
|
|
|
parameters: None,
|
|
|
|
},
|
|
|
|
subject_public_key: pubkey,
|
|
|
|
};
|
2023-10-30 11:25:12 -04:00
|
|
|
Ok(
|
|
|
|
key_info
|
|
|
|
.to_vec()
|
|
|
|
.map_err(|_| {
|
|
|
|
custom_error("DOMExceptionOperationError", "Failed to export key")
|
|
|
|
})?
|
|
|
|
.into(),
|
|
|
|
)
|
2022-09-27 08:13:42 -04:00
|
|
|
}
|
|
|
|
|
2023-09-13 11:54:19 -04:00
|
|
|
#[op2]
|
|
|
|
#[serde]
|
2023-05-08 17:07:45 -04:00
|
|
|
pub fn op_crypto_export_pkcs8_ed25519(
|
2023-09-13 11:54:19 -04:00
|
|
|
#[buffer] pkey: &[u8],
|
2023-06-22 17:37:56 -04:00
|
|
|
) -> Result<ToJsBuffer, AnyError> {
|
2023-10-30 11:25:12 -04:00
|
|
|
use rsa::pkcs1::der::Encode;
|
|
|
|
|
2022-10-04 02:06:25 -04:00
|
|
|
// This should probably use OneAsymmetricKey instead
|
2022-09-27 08:13:42 -04:00
|
|
|
let pk_info = rsa::pkcs8::PrivateKeyInfo {
|
|
|
|
public_key: None,
|
2023-10-30 11:25:12 -04:00
|
|
|
algorithm: rsa::pkcs8::AlgorithmIdentifierRef {
|
2022-09-27 08:13:42 -04:00
|
|
|
// id-Ed25519
|
|
|
|
oid: ED25519_OID,
|
|
|
|
parameters: None,
|
|
|
|
},
|
|
|
|
private_key: pkey, // OCTET STRING
|
|
|
|
};
|
|
|
|
|
2023-10-30 11:25:12 -04:00
|
|
|
let mut buf = Vec::new();
|
|
|
|
pk_info.encode_to_vec(&mut buf)?;
|
|
|
|
Ok(buf.into())
|
2022-09-27 08:13:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 'x' from Section 2 of RFC 8037
|
|
|
|
// https://www.rfc-editor.org/rfc/rfc8037#section-2
|
2023-09-13 11:54:19 -04:00
|
|
|
#[op2]
|
|
|
|
#[string]
|
|
|
|
pub fn op_crypto_jwk_x_ed25519(
|
|
|
|
#[buffer] pkey: &[u8],
|
|
|
|
) -> Result<String, AnyError> {
|
2022-09-27 08:13:42 -04:00
|
|
|
let pair = Ed25519KeyPair::from_seed_unchecked(pkey)?;
|
2023-10-26 12:39:04 -04:00
|
|
|
Ok(BASE64_URL_SAFE_NO_PAD.encode(pair.public_key().as_ref()))
|
2022-09-27 08:13:42 -04:00
|
|
|
}
|