1use core::time::Duration;
4
5use crate::cert::CertRules;
6use crate::ids::{CipherSuite, NamedGroup, SignatureScheme, TlsVersion};
7
8#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
11pub enum Level {
12 Must,
14 Should,
16 May,
18 ShouldNot,
20 MustNot,
22}
23
24#[derive(Clone, Copy, Debug, PartialEq, Eq)]
26pub enum Allowed<T: 'static> {
27 Any,
29 Only {
32 required: &'static [T],
34 optional: &'static [T],
36 },
37}
38
39impl<T: PartialEq + Copy> Allowed<T> {
40 pub fn allows(&self, item: &T) -> bool {
42 match self {
43 Self::Any => true,
44 Self::Only { required, optional } => required.contains(item) || optional.contains(item),
45 }
46 }
47
48 pub const fn required(&self) -> &'static [T] {
50 match self {
51 Self::Any => &[],
52 Self::Only { required, .. } => required,
53 }
54 }
55
56 pub const fn optional(&self) -> &'static [T] {
58 match self {
59 Self::Any => &[],
60 Self::Only { optional, .. } => optional,
61 }
62 }
63
64 pub fn iter(&self) -> impl Iterator<Item = T> + '_ {
66 let (required, optional): (&[T], &[T]) = match self {
67 Self::Any => (&[], &[]),
68 Self::Only { required, optional } => (required, optional),
69 };
70 required.iter().chain(optional).copied()
71 }
72}
73
74#[derive(Clone, Copy, Debug, PartialEq, Eq)]
76pub struct VersionRules {
77 pub support: Level,
80 pub cipher_suites: Allowed<CipherSuite>,
82 pub groups: Allowed<NamedGroup>,
84 pub signature_schemes: Allowed<SignatureScheme>,
86 pub cert_signature_schemes: Allowed<SignatureScheme>,
88}
89
90#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
92pub enum Curve {
93 P256,
95 P384,
97 P521,
99 BrainpoolP256r1,
101 BrainpoolP384r1,
103 BrainpoolP512r1,
105}
106
107impl Curve {
108 pub fn from_oid(oid: &[u8]) -> Option<Self> {
111 Some(match oid {
112 [0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07] => Self::P256,
113 [0x2B, 0x81, 0x04, 0x00, 0x22] => Self::P384,
114 [0x2B, 0x81, 0x04, 0x00, 0x23] => Self::P521,
115 [0x2B, 0x24, 0x03, 0x03, 0x02, 0x08, 0x01, 0x01, 0x07] => Self::BrainpoolP256r1,
116 [0x2B, 0x24, 0x03, 0x03, 0x02, 0x08, 0x01, 0x01, 0x0B] => Self::BrainpoolP384r1,
117 [0x2B, 0x24, 0x03, 0x03, 0x02, 0x08, 0x01, 0x01, 0x0D] => Self::BrainpoolP512r1,
118 _ => return None,
119 })
120 }
121}
122
123impl core::fmt::Display for Curve {
124 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
125 f.write_str(match self {
126 Self::P256 => "P-256",
127 Self::P384 => "P-384",
128 Self::P521 => "P-521",
129 Self::BrainpoolP256r1 => "brainpoolP256r1",
130 Self::BrainpoolP384r1 => "brainpoolP384r1",
131 Self::BrainpoolP512r1 => "brainpoolP512r1",
132 })
133 }
134}
135
136#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
138pub enum PublicKey {
139 Rsa(u16),
141 Ec(Curve),
143 Ed25519,
145}
146
147impl core::fmt::Display for PublicKey {
148 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
149 match self {
150 Self::Rsa(bits) => write!(f, "RSA-{bits}"),
151 Self::Ec(curve) => write!(f, "ECDSA {curve}"),
152 Self::Ed25519 => f.write_str("Ed25519"),
153 }
154 }
155}
156
157#[derive(Clone, Copy, Debug, PartialEq, Eq)]
159pub enum KeyRule {
160 RsaMin(u16),
162 Ec(Curve),
164 Ed25519,
166}
167
168impl KeyRule {
169 pub const fn accepts(self, key: PublicKey) -> bool {
171 match (self, key) {
172 (Self::RsaMin(min), PublicKey::Rsa(bits)) => bits >= min,
173 (Self::Ec(want), PublicKey::Ec(have)) => want as u8 == have as u8,
174 (Self::Ed25519, PublicKey::Ed25519) => true,
175 _ => false,
176 }
177 }
178}
179
180#[derive(Clone, Copy, Debug, PartialEq, Eq)]
182pub enum Revocation {
183 Unspecified,
185 CrlRequired,
187 Required,
189}
190
191#[derive(Clone, Copy, Debug, PartialEq, Eq)]
196pub struct Profile {
197 pub id: &'static str,
199 pub name: &'static str,
201 pub sources: &'static [&'static str],
203 pub tls12: Option<VersionRules>,
205 pub tls13: Option<VersionRules>,
207 pub cert: CertRules,
209 pub mutual_auth: bool,
211 pub revocation: Revocation,
213 pub max_session_lifetime: Option<Duration>,
215 pub encrypt_then_mac: Level,
217 pub extended_master_secret: Level,
219 pub renegotiation: Level,
221 pub early_data: Level,
223 pub sni: Level,
225 pub notes: &'static [&'static str],
228}
229
230impl Profile {
231 pub const fn rules(&self, version: TlsVersion) -> Option<&VersionRules> {
233 match version {
234 TlsVersion::TLS12 => self.tls12.as_ref(),
235 TlsVersion::TLS13 => self.tls13.as_ref(),
236 _ => None,
237 }
238 }
239
240 pub fn versions(&self) -> impl Iterator<Item = TlsVersion> {
242 let v13 = self.tls13.map(|_| TlsVersion::TLS13);
243 let v12 = self.tls12.map(|_| TlsVersion::TLS12);
244 v13.into_iter().chain(v12)
245 }
246}