use core::time::Duration;
use crate::cert::CertRules;
use crate::ids::{CipherSuite, NamedGroup, SignatureScheme, TlsVersion};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Level {
Must,
Should,
May,
ShouldNot,
MustNot,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Allowed<T: 'static> {
Any,
Only {
required: &'static [T],
optional: &'static [T],
},
}
impl<T: PartialEq + Copy> Allowed<T> {
pub fn allows(&self, item: &T) -> bool {
match self {
Self::Any => true,
Self::Only { required, optional } => required.contains(item) || optional.contains(item),
}
}
pub const fn required(&self) -> &'static [T] {
match self {
Self::Any => &[],
Self::Only { required, .. } => required,
}
}
pub const fn optional(&self) -> &'static [T] {
match self {
Self::Any => &[],
Self::Only { optional, .. } => optional,
}
}
pub fn iter(&self) -> impl Iterator<Item = T> + '_ {
let (required, optional): (&[T], &[T]) = match self {
Self::Any => (&[], &[]),
Self::Only { required, optional } => (required, optional),
};
required.iter().chain(optional).copied()
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct VersionRules {
pub support: Level,
pub cipher_suites: Allowed<CipherSuite>,
pub groups: Allowed<NamedGroup>,
pub signature_schemes: Allowed<SignatureScheme>,
pub cert_signature_schemes: Allowed<SignatureScheme>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Curve {
P256,
P384,
P521,
BrainpoolP256r1,
BrainpoolP384r1,
BrainpoolP512r1,
}
impl Curve {
pub fn from_oid(oid: &[u8]) -> Option<Self> {
Some(match oid {
[0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07] => Self::P256,
[0x2B, 0x81, 0x04, 0x00, 0x22] => Self::P384,
[0x2B, 0x81, 0x04, 0x00, 0x23] => Self::P521,
[0x2B, 0x24, 0x03, 0x03, 0x02, 0x08, 0x01, 0x01, 0x07] => Self::BrainpoolP256r1,
[0x2B, 0x24, 0x03, 0x03, 0x02, 0x08, 0x01, 0x01, 0x0B] => Self::BrainpoolP384r1,
[0x2B, 0x24, 0x03, 0x03, 0x02, 0x08, 0x01, 0x01, 0x0D] => Self::BrainpoolP512r1,
_ => return None,
})
}
}
impl core::fmt::Display for Curve {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str(match self {
Self::P256 => "P-256",
Self::P384 => "P-384",
Self::P521 => "P-521",
Self::BrainpoolP256r1 => "brainpoolP256r1",
Self::BrainpoolP384r1 => "brainpoolP384r1",
Self::BrainpoolP512r1 => "brainpoolP512r1",
})
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum PublicKey {
Rsa(u16),
Ec(Curve),
Ed25519,
}
impl core::fmt::Display for PublicKey {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::Rsa(bits) => write!(f, "RSA-{bits}"),
Self::Ec(curve) => write!(f, "ECDSA {curve}"),
Self::Ed25519 => f.write_str("Ed25519"),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum KeyRule {
RsaMin(u16),
Ec(Curve),
Ed25519,
}
impl KeyRule {
pub const fn accepts(self, key: PublicKey) -> bool {
match (self, key) {
(Self::RsaMin(min), PublicKey::Rsa(bits)) => bits >= min,
(Self::Ec(want), PublicKey::Ec(have)) => want as u8 == have as u8,
(Self::Ed25519, PublicKey::Ed25519) => true,
_ => false,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Revocation {
Unspecified,
CrlRequired,
Required,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Profile {
pub id: &'static str,
pub name: &'static str,
pub sources: &'static [&'static str],
pub tls12: Option<VersionRules>,
pub tls13: Option<VersionRules>,
pub cert: CertRules,
pub mutual_auth: bool,
pub revocation: Revocation,
pub max_session_lifetime: Option<Duration>,
pub encrypt_then_mac: Level,
pub extended_master_secret: Level,
pub renegotiation: Level,
pub early_data: Level,
pub sni: Level,
pub notes: &'static [&'static str],
}
impl Profile {
pub const fn rules(&self, version: TlsVersion) -> Option<&VersionRules> {
match version {
TlsVersion::TLS12 => self.tls12.as_ref(),
TlsVersion::TLS13 => self.tls13.as_ref(),
_ => None,
}
}
pub fn versions(&self) -> impl Iterator<Item = TlsVersion> {
let v13 = self.tls13.map(|_| TlsVersion::TLS13);
let v12 = self.tls12.map(|_| TlsVersion::TLS12);
v13.into_iter().chain(v12)
}
}