use core::fmt;
use crate::profile::{Allowed, KeyRule};
macro_rules! oid_enum {
(
$(#[$meta:meta])*
$name:ident {
$( $(#[$vmeta:meta])* $variant:ident = [$($byte:literal),+] => $label:literal, )+
}
) => {
$(#[$meta])*
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum $name {
$( $(#[$vmeta])* $variant, )+
}
impl $name {
pub const ALL: &'static [Self] = &[$(Self::$variant),+];
pub const fn oid(self) -> &'static [u8] {
match self {
$( Self::$variant => &[$($byte),+], )+
}
}
pub fn from_oid(oid: &[u8]) -> Option<Self> {
Self::ALL.iter().copied().find(|v| v.oid() == oid)
}
}
impl fmt::Display for $name {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
$( Self::$variant => $label, )+
})
}
}
};
}
oid_enum! {
Extension {
AuthorityKeyIdentifier = [0x55, 0x1D, 0x23] => "AuthorityKeyIdentifier",
SubjectKeyIdentifier = [0x55, 0x1D, 0x0E] => "SubjectKeyIdentifier",
KeyUsage = [0x55, 0x1D, 0x0F] => "KeyUsage",
PrivateKeyUsagePeriod = [0x55, 0x1D, 0x10] => "PrivateKeyUsagePeriod",
CertificatePolicies = [0x55, 0x1D, 0x20] => "CertificatePolicies",
SubjectAltName = [0x55, 0x1D, 0x11] => "SubjectAltName",
IssuerAltName = [0x55, 0x1D, 0x12] => "IssuerAltName",
BasicConstraints = [0x55, 0x1D, 0x13] => "BasicConstraints",
ExtendedKeyUsage = [0x55, 0x1D, 0x25] => "ExtendedKeyUsage",
CrlDistributionPoints = [0x55, 0x1D, 0x1F] => "CRLDistributionPoints",
AuthorityInfoAccess = [0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x01, 0x01] => "AuthorityInfoAccess",
}
}
oid_enum! {
ExtendedKeyUsage {
ServerAuth = [0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x01] => "serverAuth",
ClientAuth = [0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02] => "clientAuth",
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum CertSignature {
EcdsaSha256,
EcdsaSha384,
EcdsaSha512,
RsaPkcs1Sha256,
RsaPkcs1Sha384,
RsaPkcs1Sha512,
RsaPssSha256,
RsaPssSha384,
RsaPssSha512,
Ed25519,
}
impl fmt::Display for CertSignature {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::EcdsaSha256 => "ecdsa-with-SHA256",
Self::EcdsaSha384 => "ecdsa-with-SHA384",
Self::EcdsaSha512 => "ecdsa-with-SHA512",
Self::RsaPkcs1Sha256 => "sha256WithRSAEncryption",
Self::RsaPkcs1Sha384 => "sha384WithRSAEncryption",
Self::RsaPkcs1Sha512 => "sha512WithRSAEncryption",
Self::RsaPssSha256 => "RSASSA-PSS with SHA-256",
Self::RsaPssSha384 => "RSASSA-PSS with SHA-384",
Self::RsaPssSha512 => "RSASSA-PSS with SHA-512",
Self::Ed25519 => "Ed25519",
})
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum KeyUsage {
DigitalSignature,
NonRepudiation,
KeyEncipherment,
DataEncipherment,
KeyAgreement,
KeyCertSign,
CrlSign,
EncipherOnly,
DecipherOnly,
}
impl KeyUsage {
pub const fn bit(self) -> u8 {
self as u8
}
}
impl fmt::Display for KeyUsage {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::DigitalSignature => "digitalSignature",
Self::NonRepudiation => "nonRepudiation",
Self::KeyEncipherment => "keyEncipherment",
Self::DataEncipherment => "dataEncipherment",
Self::KeyAgreement => "keyAgreement",
Self::KeyCertSign => "keyCertSign",
Self::CrlSign => "cRLSign",
Self::EncipherOnly => "encipherOnly",
Self::DecipherOnly => "decipherOnly",
})
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct SanRules {
pub wildcards: bool,
pub max_dns_names: Option<u8>,
pub dns_name_for_server_auth: bool,
pub uris: Option<u8>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct SubjectRules {
pub organization: Option<&'static str>,
pub organizational_unit: bool,
pub common_name_contains: Option<&'static str>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct CertRules {
pub keys: Allowed<KeyRule>,
pub signatures: Allowed<CertSignature>,
pub max_validity_days: Option<u32>,
pub extensions: Allowed<Extension>,
pub critical: Option<&'static [Extension]>,
pub key_usage: &'static [KeyUsage],
pub extended_key_usage: Allowed<ExtendedKeyUsage>,
pub end_entity: bool,
pub san: SanRules,
pub subject: SubjectRules,
}
impl CertRules {
pub const ANY: Self = Self {
keys: Allowed::Any,
signatures: Allowed::Any,
max_validity_days: None,
extensions: Allowed::Any,
critical: None,
key_usage: &[],
extended_key_usage: Allowed::Any,
end_entity: false,
san: SanRules { wildcards: true, max_dns_names: None, dns_name_for_server_auth: false, uris: None },
subject: SubjectRules { organization: None, organizational_unit: false, common_name_contains: None },
};
}