use std::time::SystemTime;
use const_oid::db::rfc5912::{
ECDSA_WITH_SHA_256, ECDSA_WITH_SHA_384, ECDSA_WITH_SHA_512, ID_KP_OCSP_SIGNING,
ID_KP_TIME_STAMPING, SHA_256_WITH_RSA_ENCRYPTION, SHA_384_WITH_RSA_ENCRYPTION,
SHA_512_WITH_RSA_ENCRYPTION,
};
use const_oid::db::rfc8410::ID_ED_25519;
use der::{Decode, Encode};
use rsa::pkcs1v15::{Signature, VerifyingKey};
use rsa::RsaPublicKey;
use sha2::{Sha256, Sha384, Sha512};
use signature::hazmat::PrehashVerifier;
use signature::Verifier;
use spki::DecodePublicKey;
use std::collections::BTreeSet;
use const_oid::db::rfc5280::ANY_POLICY;
use const_oid::db::rfc5912::{
ID_SHA_1, ID_SHA_256, ID_SHA_384, ID_SHA_512,
};
use const_oid::ObjectIdentifier;
use sha1::{Digest as _, Sha1};
use x509_cert::crl::CertificateList;
use x509_cert::ext::pkix::name::GeneralName;
use x509_cert::ext::pkix::{
BasicConstraints, ExtendedKeyUsage, KeyUsage, NameConstraints, SubjectAltName,
};
use x509_cert::name::{Name, RelativeDistinguishedName};
use x509_cert::Certificate;
use x509_ocsp::{BasicOcspResponse, CertId, CertStatus, ResponderId};
use crate::error::Error;
use crate::policy::{process_policies, PolicyInput};
use crate::Result;
const MAX_DEPTH: usize = 10;
const MAX_CANDIDATE_EVALUATIONS: usize = 256;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum LeafPurpose {
Any,
DocumentSigning,
TimeStamping,
}
const RECOGNIZED_EXTENSIONS: &[ObjectIdentifier] = &[
const_oid::db::rfc5280::ID_CE_BASIC_CONSTRAINTS,
const_oid::db::rfc5280::ID_CE_KEY_USAGE,
const_oid::db::rfc5280::ID_CE_EXT_KEY_USAGE,
const_oid::db::rfc5280::ID_CE_NAME_CONSTRAINTS,
const_oid::db::rfc5280::ID_CE_CERTIFICATE_POLICIES,
const_oid::db::rfc5280::ID_CE_POLICY_MAPPINGS,
const_oid::db::rfc5280::ID_CE_POLICY_CONSTRAINTS,
const_oid::db::rfc5280::ID_CE_INHIBIT_ANY_POLICY,
const_oid::db::rfc5280::ID_CE_SUBJECT_ALT_NAME,
const_oid::db::rfc5280::ID_CE_ISSUER_ALT_NAME,
const_oid::db::rfc5280::ID_CE_SUBJECT_KEY_IDENTIFIER,
const_oid::db::rfc5280::ID_CE_AUTHORITY_KEY_IDENTIFIER,
const_oid::db::rfc5280::ID_CE_CRL_DISTRIBUTION_POINTS,
const_oid::db::rfc5280::ID_CE_FRESHEST_CRL,
const_oid::db::rfc5280::ID_CE_SUBJECT_DIRECTORY_ATTRIBUTES,
const_oid::db::rfc5280::ID_PE_AUTHORITY_INFO_ACCESS,
const_oid::db::rfc5280::ID_PE_SUBJECT_INFO_ACCESS,
const_oid::db::rfc6960::ID_PKIX_OCSP_NOCHECK,
];
fn ext<'a, T: Decode<'a> + const_oid::AssociatedOid>(
cert: &'a Certificate,
) -> std::result::Result<Option<T>, String> {
match cert.tbs_certificate.get::<T>() {
Ok(Some((_, v))) => Ok(Some(v)),
Ok(None) => Ok(None),
Err(e) => Err(format!("malformed or duplicated certificate extension: {e}")),
}
}
fn check_extensions(cert: &Certificate) -> std::result::Result<(), String> {
if let Some(exts) = &cert.tbs_certificate.extensions {
for e in exts {
if e.critical && !RECOGNIZED_EXTENSIONS.contains(&e.extn_id) {
return Err(format!(
"unrecognized critical certificate extension {}",
e.extn_id
));
}
}
}
ext::<BasicConstraints>(cert)?;
ext::<KeyUsage>(cert)?;
ext::<ExtendedKeyUsage>(cert)?;
ext::<NameConstraints>(cert)?;
ext::<SubjectAltName>(cert)?;
ext::<x509_cert::ext::pkix::CertificatePolicies>(cert)?;
ext::<x509_cert::ext::pkix::PolicyMappings>(cert)?;
ext::<x509_cert::ext::pkix::PolicyConstraints>(cert)?;
ext::<x509_cert::ext::pkix::InhibitAnyPolicy>(cert)?;
Ok(())
}
fn check_leaf_purpose(leaf: &Certificate, purpose: LeafPurpose) -> std::result::Result<(), String> {
if purpose == LeafPurpose::Any {
return Ok(());
}
if let Some(ku) = ext::<KeyUsage>(leaf)? {
if !(ku.digital_signature() || ku.non_repudiation()) {
return Err(
"signer certificate keyUsage permits neither digitalSignature nor nonRepudiation"
.into(),
);
}
}
if purpose == LeafPurpose::TimeStamping {
match ext::<ExtendedKeyUsage>(leaf)? {
Some(eku) if eku.0.contains(&ID_KP_TIME_STAMPING) => {}
_ => {
return Err(
"TSA certificate lacks the id-kp-timeStamping extended key usage (RFC 3161 §2.3)"
.into(),
)
}
}
}
Ok(())
}
#[derive(Clone, Default)]
pub struct TrustStore {
roots: Vec<Certificate>,
required_policy: Option<ObjectIdentifier>,
}
impl TrustStore {
pub fn new() -> Self {
Self::default()
}
pub fn from_pem(pem: &[u8]) -> Result<Self> {
let roots = Certificate::load_pem_chain(pem).map_err(|e| Error::Crypto(e.to_string()))?;
Ok(Self {
roots,
required_policy: None,
})
}
pub fn from_ders<I: IntoIterator<Item = Vec<u8>>>(ders: I) -> Result<Self> {
let mut roots = Vec::new();
for der in ders {
roots.push(Certificate::from_der(&der).map_err(|e| Error::Crypto(e.to_string()))?);
}
Ok(Self {
roots,
required_policy: None,
})
}
pub fn require_policy(mut self, oid: &str) -> Result<Self> {
self.required_policy =
Some(ObjectIdentifier::new(oid).map_err(|e| Error::Crypto(e.to_string()))?);
Ok(self)
}
pub fn is_empty(&self) -> bool {
self.roots.is_empty()
}
pub fn len(&self) -> usize {
self.roots.len()
}
}
#[derive(Debug, Clone)]
pub(crate) struct ChainResult {
pub trusted: bool,
pub detail: String,
}
pub(crate) fn verify_chain(
leaf: &Certificate,
pool: &[Certificate],
store: &TrustStore,
crls: &[CertificateList],
ocsps: &[BasicOcspResponse],
at: SystemTime,
purpose: LeafPurpose,
) -> ChainResult {
let at = at
.duration_since(SystemTime::UNIX_EPOCH)
.map(|d| d.as_secs() as i64)
.unwrap_or(0);
if !store.roots.iter().any(|r| same_cert(r, leaf)) {
if let Err(detail) = check_extensions(leaf) {
return fail(&detail);
}
}
if let Err(detail) = check_leaf_purpose(leaf, purpose) {
return fail(&detail);
}
let mut seen: BTreeSet<Vec<u8>> = BTreeSet::new();
let pool: Vec<&Certificate> = pool
.iter()
.filter(|c| c.to_der().map(|d| seen.insert(d)).unwrap_or(false))
.collect();
let mut ctx = SearchCtx {
store,
pool: &pool,
crls,
ocsps,
at,
budget: MAX_CANDIDATE_EVALUATIONS,
};
let mut path: Vec<Certificate> = vec![leaf.clone()];
extend_path(&mut path, &mut ctx)
}
struct SearchCtx<'a> {
store: &'a TrustStore,
pool: &'a [&'a Certificate],
crls: &'a [CertificateList],
ocsps: &'a [BasicOcspResponse],
at: i64,
budget: usize,
}
impl SearchCtx<'_> {
fn spend(&mut self) -> bool {
if self.budget == 0 {
return false;
}
self.budget -= 1;
true
}
}
fn extend_path(path: &mut Vec<Certificate>, ctx: &mut SearchCtx<'_>) -> ChainResult {
let current = path.last().unwrap().clone();
let at = ctx.at;
if !valid_at(¤t, at) {
return fail("a certificate in the path is expired or not yet valid");
}
if ctx.store.roots.iter().any(|r| same_cert(r, ¤t)) {
return finalize(path, ctx.store);
}
let mut pending: Option<ChainResult> = None;
for root in ctx.store.roots.iter() {
if !ctx.spend() {
return pending.unwrap_or_else(|| fail("certificate path search budget exhausted"));
}
if !issued_by(¤t, root) {
continue;
}
if !valid_at(root, at) || revoked(¤t, root, ctx.crls, ctx.ocsps, at) {
continue;
}
path.push(root.clone());
let result = finalize(path, ctx.store);
if result.trusted {
return result;
}
pending.get_or_insert(result);
path.pop();
}
let intermediates = path.len() - 1;
if intermediates >= MAX_DEPTH {
return pending.unwrap_or_else(|| fail("certificate path too long"));
}
let counted_below = path[1..]
.iter()
.filter(|c| !crate::policy::is_self_issued(c))
.count();
for &next in ctx.pool.iter() {
if same_cert(next, ¤t) || path.iter().any(|c| same_cert(c, next)) {
continue;
}
if !ctx.spend() {
return pending.unwrap_or_else(|| fail("certificate path search budget exhausted"));
}
if !issued_by(¤t, next) {
continue;
}
if let Err(detail) = check_extensions(next) {
pending.get_or_insert(fail(&detail));
continue;
}
match ca_constraints(next) {
Some((true, path_len)) if !path_len.is_some_and(|n| (n as usize) < counted_below) => {}
_ => continue,
}
if !permits_cert_sign(next) || revoked(¤t, next, ctx.crls, ctx.ocsps, at) {
continue;
}
path.push(next.clone());
let result = extend_path(path, ctx);
if result.trusted {
return result;
}
pending.get_or_insert(result);
path.pop();
}
pending.unwrap_or_else(|| fail("could not build a path to a trusted root"))
}
fn finalize(path: &[Certificate], store: &TrustStore) -> ChainResult {
if let Err(detail) = check_name_constraints(path) {
return fail(&detail);
}
let certs: Vec<&Certificate> = path[..path.len().saturating_sub(1)].iter().rev().collect();
let input = PolicyInput {
initial_policy_set: match store.required_policy {
Some(p) => BTreeSet::from([p]),
None => BTreeSet::from([ANY_POLICY]),
},
initial_explicit_policy: store.required_policy.is_some(),
};
if let Err(detail) = process_policies(&certs, &input) {
return fail(&detail);
}
let anchor = path.last().expect("non-empty path");
if path.len() == 1 {
ok("certificate is a trusted root")
} else {
ok(&format!("chains to trusted root ({})", dn(anchor)))
}
}
fn check_name_constraints(path: &[Certificate]) -> std::result::Result<(), String> {
let mut collected: Vec<NameConstraints> = Vec::new();
for (i, cert) in path.iter().enumerate().rev() {
let is_anchor = i == path.len() - 1;
let exempt_self_issued = i != 0 && crate::policy::is_self_issued(cert);
if !is_anchor && !exempt_self_issued {
for name in cert_names(cert) {
for nc in &collected {
if name_excluded(&name, nc) {
return Err("subject name excluded by a CA name constraint".into());
}
if !name_permitted(&name, nc) {
return Err("subject name outside a CA's permitted name constraints".into());
}
}
}
}
if let Ok(Some((_, nc))) = cert.tbs_certificate.get::<NameConstraints>() {
collected.push(nc);
}
}
Ok(())
}
const EMAIL_ADDRESS_OID: ObjectIdentifier = ObjectIdentifier::new_unwrap("1.2.840.113549.1.9.1");
fn cert_names(cert: &Certificate) -> Vec<GeneralName> {
let mut names = Vec::new();
if !cert.tbs_certificate.subject.0.is_empty() {
names.push(GeneralName::DirectoryName(cert.tbs_certificate.subject.clone()));
}
for rdn in cert.tbs_certificate.subject.0.iter() {
for atv in rdn.0.iter() {
if atv.oid == EMAIL_ADDRESS_OID {
if let Ok(email) = der::asn1::Ia5String::new(&value_string(atv)) {
names.push(GeneralName::Rfc822Name(email));
}
}
}
}
if let Ok(Some((_, san))) = cert.tbs_certificate.get::<SubjectAltName>() {
names.extend(san.0.iter().cloned());
}
names
}
fn value_string(atv: &x509_cert::attr::AttributeTypeAndValue) -> String {
let bytes = atv.value.value();
String::from_utf8_lossy(bytes).into_owned()
}
fn name_excluded(name: &GeneralName, nc: &NameConstraints) -> bool {
nc.excluded_subtrees
.as_ref()
.is_some_and(|subs| subs.iter().any(|s| within_subtree(name, &s.base) == Some(true)))
}
fn name_permitted(name: &GeneralName, nc: &NameConstraints) -> bool {
let Some(subs) = &nc.permitted_subtrees else {
return true; };
let same_type: Vec<_> = subs
.iter()
.filter(|s| within_subtree(name, &s.base).is_some())
.collect();
if same_type.is_empty() {
return true; }
same_type
.iter()
.any(|s| within_subtree(name, &s.base) == Some(true))
}
fn within_subtree(name: &GeneralName, base: &GeneralName) -> Option<bool> {
match (name, base) {
(GeneralName::DirectoryName(n), GeneralName::DirectoryName(b)) => Some(dn_within(n, b)),
(GeneralName::DnsName(n), GeneralName::DnsName(b)) => {
Some(dns_within(n.as_str(), b.as_str()))
}
(GeneralName::Rfc822Name(n), GeneralName::Rfc822Name(b)) => {
Some(email_within(n.as_str(), b.as_str()))
}
(GeneralName::IpAddress(n), GeneralName::IpAddress(b)) => {
Some(ip_within(n.as_bytes(), b.as_bytes()))
}
(GeneralName::UniformResourceIdentifier(n), GeneralName::UniformResourceIdentifier(b)) => {
Some(host_within(uri_host(n.as_str()), b.as_str()))
}
_ => None,
}
}
fn host_within(host: &str, base: &str) -> bool {
let h = host.to_ascii_lowercase();
let b = base.to_ascii_lowercase();
match b.strip_prefix('.') {
Some(domain) => h.ends_with(&format!(".{domain}")),
None => h == b,
}
}
fn dn_within(name: &Name, base: &Name) -> bool {
if base.0.len() > name.0.len() {
return false;
}
base.0
.iter()
.zip(name.0.iter())
.all(|(b, n)| rdn_key(b) == rdn_key(n))
}
fn rdn_key(rdn: &RelativeDistinguishedName) -> BTreeSet<(ObjectIdentifier, String)> {
rdn.0
.iter()
.map(|atv| {
let folded = String::from_utf8_lossy(atv.value.value())
.split_whitespace()
.collect::<Vec<_>>()
.join(" ")
.to_lowercase();
(atv.oid, folded)
})
.collect()
}
fn dns_within(name: &str, base: &str) -> bool {
let n = name.to_ascii_lowercase();
let b = base.trim_start_matches('.').to_ascii_lowercase();
if b.is_empty() {
return true;
}
n == b || n.ends_with(&format!(".{b}"))
}
fn email_within(name: &str, base: &str) -> bool {
let n = name.to_ascii_lowercase();
let b = base.to_ascii_lowercase();
if b.contains('@') {
return n == b; }
match n.split_once('@') {
Some((_, host)) => host_within(host, &b),
None => false,
}
}
fn ip_within(name: &[u8], base: &[u8]) -> bool {
if base.len() != name.len() * 2 {
return false;
}
let (net, mask) = base.split_at(name.len());
name.iter()
.zip(net)
.zip(mask)
.all(|((nb, ab), mb)| (nb & mb) == (ab & mb))
}
fn uri_host(uri: &str) -> &str {
let after_scheme = uri.split("://").nth(1).unwrap_or(uri);
let authority = after_scheme.split(['/', '?', '#']).next().unwrap_or("");
let host = authority.rsplit('@').next().unwrap_or(authority);
host.split(':').next().unwrap_or(host)
}
fn ocsp_revoked(
cert: &Certificate,
issuer: &Certificate,
ocsps: &[BasicOcspResponse],
at: i64,
) -> bool {
for basic in ocsps {
if !ocsp_authentic(basic, issuer, at) {
continue;
}
for single in basic.tbs_response_data.responses.iter() {
let CertStatus::Revoked(info) = &single.cert_status else {
continue;
};
let Some(want) = cert_id_for(&single.cert_id.hash_algorithm.oid, issuer, cert) else {
continue;
};
if !cert_id_eq(&single.cert_id, &want) {
continue;
}
let revoked_at = info.revocation_time.0.to_unix_duration().as_secs() as i64;
if revoked_at <= at {
return true;
}
}
}
false
}
fn cert_id_for(oid: &ObjectIdentifier, issuer: &Certificate, cert: &Certificate) -> Option<CertId> {
let serial = cert.tbs_certificate.serial_number.clone();
if *oid == ID_SHA_1 {
CertId::from_issuer::<Sha1>(issuer, serial).ok()
} else if *oid == ID_SHA_256 {
CertId::from_issuer::<Sha256>(issuer, serial).ok()
} else if *oid == ID_SHA_384 {
CertId::from_issuer::<Sha384>(issuer, serial).ok()
} else if *oid == ID_SHA_512 {
CertId::from_issuer::<Sha512>(issuer, serial).ok()
} else {
None
}
}
fn ocsp_authentic(basic: &BasicOcspResponse, issuer: &Certificate, at: i64) -> bool {
let Ok(tbs) = basic.tbs_response_data.to_der() else {
return false;
};
let Some(sig) = basic.signature.as_bytes() else {
return false;
};
let oid = basic.signature_algorithm.oid;
let rid = &basic.tbs_response_data.responder_id;
if responder_is(rid, issuer) && verify_with_cert(issuer, &tbs, oid, sig) {
return true;
}
if let Some(certs) = &basic.certs {
for c in certs {
if responder_is(rid, c)
&& issued_by(c, issuer)
&& has_ocsp_signing_eku(c)
&& valid_at(c, at)
&& check_extensions(c).is_ok()
&& verify_with_cert(c, &tbs, oid, sig)
{
return true;
}
}
}
false
}
fn verify_with_cert(cert: &Certificate, tbs: &[u8], oid: ObjectIdentifier, sig: &[u8]) -> bool {
match cert.tbs_certificate.subject_public_key_info.to_der() {
Ok(spki) => verify_signature(tbs, oid, sig, &spki),
Err(_) => false,
}
}
fn responder_is(rid: &ResponderId, cert: &Certificate) -> bool {
match rid {
ResponderId::ByName(name) => {
name.to_der().ok() == cert.tbs_certificate.subject.to_der().ok()
}
ResponderId::ByKey(key_hash) => {
match cert
.tbs_certificate
.subject_public_key_info
.subject_public_key
.as_bytes()
{
Some(pk) => Sha1::digest(pk).as_slice() == key_hash.as_bytes(),
None => false,
}
}
}
}
fn has_ocsp_signing_eku(cert: &Certificate) -> bool {
matches!(
cert.tbs_certificate.get::<ExtendedKeyUsage>(),
Ok(Some((_, eku))) if eku.0.contains(&ID_KP_OCSP_SIGNING)
)
}
fn cert_id_eq(a: &CertId, b: &CertId) -> bool {
a.issuer_name_hash.as_bytes() == b.issuer_name_hash.as_bytes()
&& a.issuer_key_hash.as_bytes() == b.issuer_key_hash.as_bytes()
&& a.serial_number.to_der().ok() == b.serial_number.to_der().ok()
}
fn ok(detail: &str) -> ChainResult {
ChainResult {
trusted: true,
detail: detail.to_string(),
}
}
fn fail(detail: &str) -> ChainResult {
ChainResult {
trusted: false,
detail: detail.to_string(),
}
}
fn ca_constraints(cert: &Certificate) -> Option<(bool, Option<u8>)> {
match cert.tbs_certificate.get::<BasicConstraints>() {
Ok(Some((_, bc))) => Some((bc.ca, bc.path_len_constraint)),
_ => None,
}
}
fn permits_cert_sign(cert: &Certificate) -> bool {
match cert.tbs_certificate.get::<KeyUsage>() {
Ok(Some((_, ku))) => ku.key_cert_sign(),
_ => true, }
}
fn revoked(
cert: &Certificate,
issuer: &Certificate,
crls: &[CertificateList],
ocsps: &[BasicOcspResponse],
at: i64,
) -> bool {
crl_revoked(cert, issuer, crls, at) || ocsp_revoked(cert, issuer, ocsps, at)
}
fn crl_revoked(cert: &Certificate, issuer: &Certificate, crls: &[CertificateList], at: i64) -> bool {
let serial = cert.tbs_certificate.serial_number.to_der().ok();
let ca_subject = issuer.tbs_certificate.subject.to_der().ok();
for crl in crls {
if crl.tbs_cert_list.issuer.to_der().ok() != ca_subject {
continue;
}
if !verify_crl_signature(crl, issuer) {
continue;
}
if let Some(revoked) = &crl.tbs_cert_list.revoked_certificates {
if revoked.iter().any(|entry| {
entry.serial_number.to_der().ok() == serial
&& time_secs(&entry.revocation_date) <= at
}) {
return true;
}
}
}
false
}
fn verify_crl_signature(crl: &CertificateList, issuer: &Certificate) -> bool {
let Ok(tbs) = crl.tbs_cert_list.to_der() else {
return false;
};
let Some(sig) = crl.signature.as_bytes() else {
return false;
};
let Ok(spki) = issuer.tbs_certificate.subject_public_key_info.to_der() else {
return false;
};
if crl.signature_algorithm != crl.tbs_cert_list.signature {
return false;
}
verify_signature(&tbs, crl.signature_algorithm.oid, sig, &spki)
}
fn issued_by(child: &Certificate, issuer: &Certificate) -> bool {
let child_issuer = child.tbs_certificate.issuer.to_der().ok();
let issuer_subject = issuer.tbs_certificate.subject.to_der().ok();
if child_issuer.is_none() || child_issuer != issuer_subject {
return false;
}
verify_cert_signature(child, issuer)
}
fn verify_cert_signature(child: &Certificate, issuer: &Certificate) -> bool {
let Ok(tbs) = child.tbs_certificate.to_der() else {
return false;
};
let Some(sig) = child.signature.as_bytes() else {
return false;
};
let Ok(spki) = issuer.tbs_certificate.subject_public_key_info.to_der() else {
return false;
};
if child.signature_algorithm != child.tbs_certificate.signature {
return false;
}
verify_signature(&tbs, child.signature_algorithm.oid, sig, &spki)
}
fn verify_signature(tbs: &[u8], oid: ObjectIdentifier, sig: &[u8], signer_spki_der: &[u8]) -> bool {
if oid == SHA_256_WITH_RSA_ENCRYPTION
|| oid == SHA_384_WITH_RSA_ENCRYPTION
|| oid == SHA_512_WITH_RSA_ENCRYPTION
{
let (Ok(pubkey), Ok(signature)) = (
RsaPublicKey::from_public_key_der(signer_spki_der),
Signature::try_from(sig),
) else {
return false;
};
if oid == SHA_256_WITH_RSA_ENCRYPTION {
VerifyingKey::<Sha256>::new(pubkey).verify(tbs, &signature).is_ok()
} else if oid == SHA_384_WITH_RSA_ENCRYPTION {
VerifyingKey::<Sha384>::new(pubkey).verify(tbs, &signature).is_ok()
} else {
VerifyingKey::<Sha512>::new(pubkey).verify(tbs, &signature).is_ok()
}
} else if oid == ECDSA_WITH_SHA_256 {
verify_ecdsa(signer_spki_der, &Sha256::digest(tbs), sig)
} else if oid == ECDSA_WITH_SHA_384 {
verify_ecdsa(signer_spki_der, &Sha384::digest(tbs), sig)
} else if oid == ECDSA_WITH_SHA_512 {
verify_ecdsa(signer_spki_der, &Sha512::digest(tbs), sig)
} else if oid == ID_ED_25519 {
verify_ed25519(signer_spki_der, tbs, sig)
} else {
false }
}
pub(crate) fn verify_ed25519(spki_der: &[u8], tbs: &[u8], sig: &[u8]) -> bool {
use signature::Verifier as _;
use spki::DecodePublicKey as _;
if let (Ok(vk), Ok(s)) = (
ed25519_dalek::VerifyingKey::from_public_key_der(spki_der),
ed25519::Signature::from_slice(sig),
) {
return vk.verify(tbs, &s).is_ok();
}
false
}
pub(crate) fn verify_ecdsa(spki_der: &[u8], prehash: &[u8], sig: &[u8]) -> bool {
if let Ok(vk) = p256::ecdsa::VerifyingKey::from_public_key_der(spki_der) {
return p256::ecdsa::Signature::from_der(sig)
.map(|s| vk.verify_prehash(prehash, &s).is_ok())
.unwrap_or(false);
}
if let Ok(vk) = p384::ecdsa::VerifyingKey::from_public_key_der(spki_der) {
return p384::ecdsa::Signature::from_der(sig)
.map(|s| vk.verify_prehash(prehash, &s).is_ok())
.unwrap_or(false);
}
false
}
fn valid_at(cert: &Certificate, at: i64) -> bool {
let nb = time_secs(&cert.tbs_certificate.validity.not_before);
let na = time_secs(&cert.tbs_certificate.validity.not_after);
at >= nb && at <= na
}
fn time_secs(t: &x509_cert::time::Time) -> i64 {
t.to_unix_duration().as_secs() as i64
}
fn same_cert(a: &Certificate, b: &Certificate) -> bool {
match (a.to_der(), b.to_der()) {
(Ok(x), Ok(y)) => x == y,
_ => false,
}
}
fn dn(cert: &Certificate) -> String {
cert.tbs_certificate.subject.to_string()
}