1use core::fmt;
5
6use crate::profile::{Allowed, KeyRule};
7
8macro_rules! oid_enum {
9 (
10 $(#[$meta:meta])*
11 $name:ident {
12 $( $(#[$vmeta:meta])* $variant:ident = [$($byte:literal),+] => $label:literal, )+
13 }
14 ) => {
15 $(#[$meta])*
16 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
17 #[non_exhaustive]
18 pub enum $name {
19 $( $(#[$vmeta])* $variant, )+
20 }
21
22 impl $name {
23 pub const ALL: &'static [Self] = &[$(Self::$variant),+];
25
26 pub const fn oid(self) -> &'static [u8] {
28 match self {
29 $( Self::$variant => &[$($byte),+], )+
30 }
31 }
32
33 pub fn from_oid(oid: &[u8]) -> Option<Self> {
35 Self::ALL.iter().copied().find(|v| v.oid() == oid)
36 }
37 }
38
39 impl fmt::Display for $name {
40 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41 f.write_str(match self {
42 $( Self::$variant => $label, )+
43 })
44 }
45 }
46 };
47}
48
49oid_enum! {
50 Extension {
52 AuthorityKeyIdentifier = [0x55, 0x1D, 0x23] => "AuthorityKeyIdentifier",
54 SubjectKeyIdentifier = [0x55, 0x1D, 0x0E] => "SubjectKeyIdentifier",
56 KeyUsage = [0x55, 0x1D, 0x0F] => "KeyUsage",
58 PrivateKeyUsagePeriod = [0x55, 0x1D, 0x10] => "PrivateKeyUsagePeriod",
60 CertificatePolicies = [0x55, 0x1D, 0x20] => "CertificatePolicies",
62 SubjectAltName = [0x55, 0x1D, 0x11] => "SubjectAltName",
64 IssuerAltName = [0x55, 0x1D, 0x12] => "IssuerAltName",
66 BasicConstraints = [0x55, 0x1D, 0x13] => "BasicConstraints",
68 ExtendedKeyUsage = [0x55, 0x1D, 0x25] => "ExtendedKeyUsage",
70 CrlDistributionPoints = [0x55, 0x1D, 0x1F] => "CRLDistributionPoints",
72 AuthorityInfoAccess = [0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x01, 0x01] => "AuthorityInfoAccess",
74 }
75}
76
77oid_enum! {
78 ExtendedKeyUsage {
80 ServerAuth = [0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x01] => "serverAuth",
82 ClientAuth = [0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02] => "clientAuth",
84 }
85}
86
87#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
89#[non_exhaustive]
90pub enum CertSignature {
91 EcdsaSha256,
93 EcdsaSha384,
95 EcdsaSha512,
97 RsaPkcs1Sha256,
99 RsaPkcs1Sha384,
101 RsaPkcs1Sha512,
103 RsaPssSha256,
105 RsaPssSha384,
107 RsaPssSha512,
109 Ed25519,
111}
112
113impl fmt::Display for CertSignature {
114 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
115 f.write_str(match self {
116 Self::EcdsaSha256 => "ecdsa-with-SHA256",
117 Self::EcdsaSha384 => "ecdsa-with-SHA384",
118 Self::EcdsaSha512 => "ecdsa-with-SHA512",
119 Self::RsaPkcs1Sha256 => "sha256WithRSAEncryption",
120 Self::RsaPkcs1Sha384 => "sha384WithRSAEncryption",
121 Self::RsaPkcs1Sha512 => "sha512WithRSAEncryption",
122 Self::RsaPssSha256 => "RSASSA-PSS with SHA-256",
123 Self::RsaPssSha384 => "RSASSA-PSS with SHA-384",
124 Self::RsaPssSha512 => "RSASSA-PSS with SHA-512",
125 Self::Ed25519 => "Ed25519",
126 })
127 }
128}
129
130#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
132#[non_exhaustive]
133pub enum KeyUsage {
134 DigitalSignature,
136 NonRepudiation,
138 KeyEncipherment,
140 DataEncipherment,
142 KeyAgreement,
144 KeyCertSign,
146 CrlSign,
148 EncipherOnly,
150 DecipherOnly,
152}
153
154impl KeyUsage {
155 pub const fn bit(self) -> u8 {
157 self as u8
158 }
159}
160
161impl fmt::Display for KeyUsage {
162 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
163 f.write_str(match self {
164 Self::DigitalSignature => "digitalSignature",
165 Self::NonRepudiation => "nonRepudiation",
166 Self::KeyEncipherment => "keyEncipherment",
167 Self::DataEncipherment => "dataEncipherment",
168 Self::KeyAgreement => "keyAgreement",
169 Self::KeyCertSign => "keyCertSign",
170 Self::CrlSign => "cRLSign",
171 Self::EncipherOnly => "encipherOnly",
172 Self::DecipherOnly => "decipherOnly",
173 })
174 }
175}
176
177#[derive(Clone, Copy, Debug, PartialEq, Eq)]
179pub struct SanRules {
180 pub wildcards: bool,
182 pub max_dns_names: Option<u8>,
184 pub dns_name_for_server_auth: bool,
186 pub uris: Option<u8>,
188}
189
190#[derive(Clone, Copy, Debug, PartialEq, Eq)]
192pub struct SubjectRules {
193 pub organization: Option<&'static str>,
195 pub organizational_unit: bool,
197 pub common_name_contains: Option<&'static str>,
200}
201
202#[derive(Clone, Copy, Debug, PartialEq, Eq)]
204pub struct CertRules {
205 pub keys: Allowed<KeyRule>,
207 pub signatures: Allowed<CertSignature>,
209 pub max_validity_days: Option<u32>,
213 pub extensions: Allowed<Extension>,
215 pub critical: Option<&'static [Extension]>,
218 pub key_usage: &'static [KeyUsage],
220 pub extended_key_usage: Allowed<ExtendedKeyUsage>,
222 pub end_entity: bool,
224 pub san: SanRules,
226 pub subject: SubjectRules,
228}
229
230impl CertRules {
231 pub const ANY: Self = Self {
233 keys: Allowed::Any,
234 signatures: Allowed::Any,
235 max_validity_days: None,
236 extensions: Allowed::Any,
237 critical: None,
238 key_usage: &[],
239 extended_key_usage: Allowed::Any,
240 end_entity: false,
241 san: SanRules { wildcards: true, max_dns_names: None, dns_name_for_server_auth: false, uris: None },
242 subject: SubjectRules { organization: None, organizational_unit: false, common_name_contains: None },
243 };
244}