2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-04-07 16:26:39 -04:00
|
|
|
|
2023-09-07 12:56:02 -04:00
|
|
|
use deno_core::op2;
|
2023-04-07 13:24:16 -04:00
|
|
|
|
|
|
|
use x509_parser::der_parser::asn1_rs::Any;
|
|
|
|
use x509_parser::der_parser::asn1_rs::Tag;
|
|
|
|
use x509_parser::der_parser::oid::Oid;
|
2024-11-06 19:57:57 -05:00
|
|
|
pub use x509_parser::error::X509Error;
|
2023-04-07 13:24:16 -04:00
|
|
|
use x509_parser::extensions;
|
|
|
|
use x509_parser::pem;
|
|
|
|
use x509_parser::prelude::*;
|
|
|
|
|
2024-08-11 09:28:54 -04:00
|
|
|
use super::KeyObjectHandle;
|
|
|
|
|
|
|
|
use std::ops::Deref;
|
|
|
|
use yoke::Yoke;
|
|
|
|
use yoke::Yokeable;
|
|
|
|
|
2023-04-07 13:24:16 -04:00
|
|
|
use digest::Digest;
|
|
|
|
|
2024-08-11 09:28:54 -04:00
|
|
|
enum CertificateSources {
|
|
|
|
Der(Box<[u8]>),
|
|
|
|
Pem(pem::Pem),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Yokeable)]
|
|
|
|
struct CertificateView<'a> {
|
|
|
|
cert: X509Certificate<'a>,
|
|
|
|
}
|
|
|
|
|
2024-01-27 01:57:41 -05:00
|
|
|
pub(crate) struct Certificate {
|
2024-08-11 09:28:54 -04:00
|
|
|
inner: Yoke<CertificateView<'static>, Box<CertificateSources>>,
|
2023-04-07 13:24:16 -04:00
|
|
|
}
|
|
|
|
|
2024-07-01 18:48:52 -04:00
|
|
|
impl deno_core::GarbageCollected for Certificate {}
|
2024-06-21 01:19:39 -04:00
|
|
|
|
2023-04-07 13:24:16 -04:00
|
|
|
impl Certificate {
|
|
|
|
fn fingerprint<D: Digest>(&self) -> Option<String> {
|
2024-08-11 09:28:54 -04:00
|
|
|
if let CertificateSources::Pem(pem) = self.inner.backing_cart().as_ref() {
|
2023-04-07 13:24:16 -04:00
|
|
|
let mut hasher = D::new();
|
|
|
|
hasher.update(&pem.contents);
|
|
|
|
let bytes = hasher.finalize();
|
|
|
|
// OpenSSL returns colon separated upper case hex values.
|
|
|
|
let mut hex = String::with_capacity(bytes.len() * 2);
|
|
|
|
for byte in bytes {
|
|
|
|
hex.push_str(&format!("{:02X}:", byte));
|
|
|
|
}
|
|
|
|
hex.pop();
|
2024-08-11 09:28:54 -04:00
|
|
|
Some(hex)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2023-04-07 13:24:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-11 09:28:54 -04:00
|
|
|
impl<'a> Deref for CertificateView<'a> {
|
|
|
|
type Target = X509Certificate<'a>;
|
2023-04-07 13:24:16 -04:00
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.cert
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-20 11:28:37 -05:00
|
|
|
#[op2]
|
2024-08-11 09:28:54 -04:00
|
|
|
#[cppgc]
|
|
|
|
pub fn op_node_x509_parse(
|
2023-09-07 12:56:02 -04:00
|
|
|
#[buffer] buf: &[u8],
|
2024-11-06 19:57:57 -05:00
|
|
|
) -> Result<Certificate, X509Error> {
|
2024-08-11 09:28:54 -04:00
|
|
|
let source = match pem::parse_x509_pem(buf) {
|
|
|
|
Ok((_, pem)) => CertificateSources::Pem(pem),
|
|
|
|
Err(_) => CertificateSources::Der(buf.to_vec().into_boxed_slice()),
|
2023-04-07 13:24:16 -04:00
|
|
|
};
|
|
|
|
|
2024-08-11 09:28:54 -04:00
|
|
|
let inner =
|
|
|
|
Yoke::<CertificateView<'static>, Box<CertificateSources>>::try_attach_to_cart(
|
|
|
|
Box::new(source),
|
|
|
|
|source| {
|
|
|
|
let cert = match source {
|
|
|
|
CertificateSources::Pem(pem) => pem.parse_x509()?,
|
|
|
|
CertificateSources::Der(buf) => {
|
|
|
|
X509Certificate::from_der(buf).map(|(_, cert)| cert)?
|
|
|
|
}
|
|
|
|
};
|
2024-11-06 19:57:57 -05:00
|
|
|
Ok::<_, X509Error>(CertificateView { cert })
|
2024-08-11 09:28:54 -04:00
|
|
|
},
|
|
|
|
)?;
|
2024-01-20 11:28:37 -05:00
|
|
|
|
2024-08-11 09:28:54 -04:00
|
|
|
Ok(Certificate { inner })
|
2023-04-07 13:24:16 -04:00
|
|
|
}
|
|
|
|
|
2023-09-07 12:56:02 -04:00
|
|
|
#[op2(fast)]
|
2024-11-06 19:57:57 -05:00
|
|
|
pub fn op_node_x509_ca(#[cppgc] cert: &Certificate) -> bool {
|
2024-08-11 09:28:54 -04:00
|
|
|
let cert = cert.inner.get().deref();
|
2024-11-06 19:57:57 -05:00
|
|
|
cert.is_ca()
|
2023-04-07 13:24:16 -04:00
|
|
|
}
|
|
|
|
|
2023-09-07 12:56:02 -04:00
|
|
|
#[op2(fast)]
|
2023-04-07 13:24:16 -04:00
|
|
|
pub fn op_node_x509_check_email(
|
2024-01-27 01:57:41 -05:00
|
|
|
#[cppgc] cert: &Certificate,
|
2023-09-07 12:56:02 -04:00
|
|
|
#[string] email: &str,
|
2024-11-06 19:57:57 -05:00
|
|
|
) -> bool {
|
2024-08-11 09:28:54 -04:00
|
|
|
let cert = cert.inner.get().deref();
|
2023-04-07 13:24:16 -04:00
|
|
|
let subject = cert.subject();
|
|
|
|
if subject
|
|
|
|
.iter_email()
|
|
|
|
.any(|e| e.as_str().unwrap_or("") == email)
|
|
|
|
{
|
2024-11-06 19:57:57 -05:00
|
|
|
return true;
|
2023-04-07 13:24:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
let subject_alt = cert
|
|
|
|
.extensions()
|
|
|
|
.iter()
|
|
|
|
.find(|e| e.oid == x509_parser::oid_registry::OID_X509_EXT_SUBJECT_ALT_NAME)
|
|
|
|
.and_then(|e| match e.parsed_extension() {
|
|
|
|
extensions::ParsedExtension::SubjectAlternativeName(s) => Some(s),
|
|
|
|
_ => None,
|
|
|
|
});
|
|
|
|
|
|
|
|
if let Some(subject_alt) = subject_alt {
|
|
|
|
for name in &subject_alt.general_names {
|
|
|
|
if let extensions::GeneralName::RFC822Name(n) = name {
|
|
|
|
if *n == email {
|
2024-11-06 19:57:57 -05:00
|
|
|
return true;
|
2023-04-07 13:24:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-06 19:57:57 -05:00
|
|
|
false
|
2023-04-07 13:24:16 -04:00
|
|
|
}
|
|
|
|
|
2023-09-07 12:56:02 -04:00
|
|
|
#[op2]
|
|
|
|
#[string]
|
2024-11-06 19:57:57 -05:00
|
|
|
pub fn op_node_x509_fingerprint(#[cppgc] cert: &Certificate) -> Option<String> {
|
|
|
|
cert.fingerprint::<sha1::Sha1>()
|
2023-04-07 13:24:16 -04:00
|
|
|
}
|
|
|
|
|
2023-09-07 12:56:02 -04:00
|
|
|
#[op2]
|
|
|
|
#[string]
|
2023-04-07 13:24:16 -04:00
|
|
|
pub fn op_node_x509_fingerprint256(
|
2024-01-27 01:57:41 -05:00
|
|
|
#[cppgc] cert: &Certificate,
|
2024-11-06 19:57:57 -05:00
|
|
|
) -> Option<String> {
|
|
|
|
cert.fingerprint::<sha2::Sha256>()
|
2023-04-07 13:24:16 -04:00
|
|
|
}
|
|
|
|
|
2023-09-07 12:56:02 -04:00
|
|
|
#[op2]
|
|
|
|
#[string]
|
2023-04-07 13:24:16 -04:00
|
|
|
pub fn op_node_x509_fingerprint512(
|
2024-01-27 01:57:41 -05:00
|
|
|
#[cppgc] cert: &Certificate,
|
2024-11-06 19:57:57 -05:00
|
|
|
) -> Option<String> {
|
|
|
|
cert.fingerprint::<sha2::Sha512>()
|
2023-04-07 13:24:16 -04:00
|
|
|
}
|
|
|
|
|
2023-09-07 12:56:02 -04:00
|
|
|
#[op2]
|
|
|
|
#[string]
|
2023-04-07 13:24:16 -04:00
|
|
|
pub fn op_node_x509_get_issuer(
|
2024-01-27 01:57:41 -05:00
|
|
|
#[cppgc] cert: &Certificate,
|
2024-11-06 19:57:57 -05:00
|
|
|
) -> Result<String, X509Error> {
|
2024-08-11 09:28:54 -04:00
|
|
|
let cert = cert.inner.get().deref();
|
2024-11-06 19:57:57 -05:00
|
|
|
x509name_to_string(cert.issuer(), oid_registry())
|
2023-04-07 13:24:16 -04:00
|
|
|
}
|
|
|
|
|
2023-09-07 12:56:02 -04:00
|
|
|
#[op2]
|
|
|
|
#[string]
|
2023-04-07 13:24:16 -04:00
|
|
|
pub fn op_node_x509_get_subject(
|
2024-01-27 01:57:41 -05:00
|
|
|
#[cppgc] cert: &Certificate,
|
2024-11-06 19:57:57 -05:00
|
|
|
) -> Result<String, X509Error> {
|
2024-08-11 09:28:54 -04:00
|
|
|
let cert = cert.inner.get().deref();
|
2024-11-06 19:57:57 -05:00
|
|
|
x509name_to_string(cert.subject(), oid_registry())
|
2023-04-07 13:24:16 -04:00
|
|
|
}
|
|
|
|
|
2024-08-11 09:28:54 -04:00
|
|
|
#[op2]
|
|
|
|
#[cppgc]
|
|
|
|
pub fn op_node_x509_public_key(
|
|
|
|
#[cppgc] cert: &Certificate,
|
2024-11-06 19:57:57 -05:00
|
|
|
) -> Result<KeyObjectHandle, super::keys::X509PublicKeyError> {
|
2024-08-11 09:28:54 -04:00
|
|
|
let cert = cert.inner.get().deref();
|
|
|
|
let public_key = &cert.tbs_certificate.subject_pki;
|
|
|
|
|
|
|
|
KeyObjectHandle::new_x509_public_key(public_key)
|
|
|
|
}
|
|
|
|
|
2023-04-07 13:24:16 -04:00
|
|
|
// Attempt to convert attribute to string. If type is not a string, return value is the hex
|
|
|
|
// encoding of the attribute value
|
|
|
|
fn attribute_value_to_string(
|
|
|
|
attr: &Any,
|
|
|
|
_attr_type: &Oid,
|
|
|
|
) -> Result<String, X509Error> {
|
|
|
|
// TODO: replace this with helper function, when it is added to asn1-rs
|
|
|
|
match attr.tag() {
|
|
|
|
Tag::NumericString
|
|
|
|
| Tag::BmpString
|
|
|
|
| Tag::VisibleString
|
|
|
|
| Tag::PrintableString
|
|
|
|
| Tag::GeneralString
|
|
|
|
| Tag::ObjectDescriptor
|
|
|
|
| Tag::GraphicString
|
|
|
|
| Tag::T61String
|
|
|
|
| Tag::VideotexString
|
|
|
|
| Tag::Utf8String
|
|
|
|
| Tag::Ia5String => {
|
|
|
|
let s = core::str::from_utf8(attr.data)
|
|
|
|
.map_err(|_| X509Error::InvalidAttributes)?;
|
|
|
|
Ok(s.to_owned())
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
// type is not a string, get slice and convert it to base64
|
|
|
|
Ok(data_encoding::HEXUPPER.encode(attr.as_bytes()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn x509name_to_string(
|
|
|
|
name: &X509Name,
|
|
|
|
oid_registry: &oid_registry::OidRegistry,
|
|
|
|
) -> Result<String, x509_parser::error::X509Error> {
|
2023-04-21 16:38:10 -04:00
|
|
|
// Lifted from https://github.com/rusticata/x509-parser/blob/4d618c2ed6b1fc102df16797545895f7c67ee0fe/src/x509.rs#L543-L566
|
|
|
|
// since it's a private function (Copyright 2017 Pierre Chifflier)
|
2023-08-27 00:04:12 -04:00
|
|
|
name.iter_rdn().try_fold(String::new(), |acc, rdn| {
|
|
|
|
rdn
|
|
|
|
.iter()
|
|
|
|
.try_fold(String::new(), |acc2, attr| {
|
|
|
|
let val_str =
|
|
|
|
attribute_value_to_string(attr.attr_value(), attr.attr_type())?;
|
|
|
|
// look ABBREV, and if not found, use shortname
|
|
|
|
let abbrev = match oid2abbrev(attr.attr_type(), oid_registry) {
|
|
|
|
Ok(s) => String::from(s),
|
|
|
|
_ => format!("{:?}", attr.attr_type()),
|
|
|
|
};
|
|
|
|
let rdn = format!("{}={}", abbrev, val_str);
|
|
|
|
match acc2.len() {
|
|
|
|
0 => Ok(rdn),
|
|
|
|
_ => Ok(acc2 + " + " + rdn.as_str()),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.map(|v| match acc.len() {
|
|
|
|
0 => v,
|
|
|
|
_ => acc + "\n" + v.as_str(),
|
|
|
|
})
|
2023-04-07 13:24:16 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-09-07 12:56:02 -04:00
|
|
|
#[op2]
|
|
|
|
#[string]
|
2024-11-06 19:57:57 -05:00
|
|
|
pub fn op_node_x509_get_valid_from(#[cppgc] cert: &Certificate) -> String {
|
2024-08-11 09:28:54 -04:00
|
|
|
let cert = cert.inner.get().deref();
|
2024-11-06 19:57:57 -05:00
|
|
|
cert.validity().not_before.to_string()
|
2023-04-07 13:24:16 -04:00
|
|
|
}
|
|
|
|
|
2023-09-07 12:56:02 -04:00
|
|
|
#[op2]
|
|
|
|
#[string]
|
2024-11-06 19:57:57 -05:00
|
|
|
pub fn op_node_x509_get_valid_to(#[cppgc] cert: &Certificate) -> String {
|
2024-08-11 09:28:54 -04:00
|
|
|
let cert = cert.inner.get().deref();
|
2024-11-06 19:57:57 -05:00
|
|
|
cert.validity().not_after.to_string()
|
2023-04-07 13:24:16 -04:00
|
|
|
}
|
|
|
|
|
2023-09-07 12:56:02 -04:00
|
|
|
#[op2]
|
|
|
|
#[string]
|
2024-11-06 19:57:57 -05:00
|
|
|
pub fn op_node_x509_get_serial_number(#[cppgc] cert: &Certificate) -> String {
|
2024-08-11 09:28:54 -04:00
|
|
|
let cert = cert.inner.get().deref();
|
2023-04-07 13:24:16 -04:00
|
|
|
let mut s = cert.serial.to_str_radix(16);
|
|
|
|
s.make_ascii_uppercase();
|
2024-11-06 19:57:57 -05:00
|
|
|
s
|
2023-04-07 13:24:16 -04:00
|
|
|
}
|
|
|
|
|
2023-09-07 12:56:02 -04:00
|
|
|
#[op2(fast)]
|
2024-11-06 19:57:57 -05:00
|
|
|
pub fn op_node_x509_key_usage(#[cppgc] cert: &Certificate) -> u16 {
|
2024-08-11 09:28:54 -04:00
|
|
|
let cert = cert.inner.get().deref();
|
2023-04-07 13:24:16 -04:00
|
|
|
let key_usage = cert
|
|
|
|
.extensions()
|
|
|
|
.iter()
|
|
|
|
.find(|e| e.oid == x509_parser::oid_registry::OID_X509_EXT_KEY_USAGE)
|
|
|
|
.and_then(|e| match e.parsed_extension() {
|
|
|
|
extensions::ParsedExtension::KeyUsage(k) => Some(k),
|
|
|
|
_ => None,
|
|
|
|
});
|
|
|
|
|
2024-11-06 19:57:57 -05:00
|
|
|
key_usage.map(|k| k.flags).unwrap_or(0)
|
2023-04-07 13:24:16 -04:00
|
|
|
}
|