uselesskey_core_x509_chain_negative/
lib.rs1#![forbid(unsafe_code)]
10#![warn(missing_docs)]
11#![cfg_attr(not(feature = "std"), no_std)]
12
13extern crate alloc;
14
15use alloc::string::{String, ToString};
16use uselesskey_core_x509_spec::{ChainSpec, KeyUsage, NotBeforeOffset};
17
18#[derive(Clone, Debug, Eq, PartialEq, Hash)]
20pub enum ChainNegative {
21 HostnameMismatch {
23 wrong_hostname: String,
25 },
26 UnknownCa,
30 ExpiredLeaf,
32 NotYetValidLeaf,
34 ExpiredIntermediate,
36 NotYetValidIntermediate,
38 IntermediateNotCa,
40 IntermediateWrongKeyUsage,
42 RevokedLeaf,
44}
45
46impl ChainNegative {
47 pub fn variant_name(&self) -> String {
49 match self {
50 ChainNegative::HostnameMismatch { wrong_hostname } => {
51 format!("hostname_mismatch:{wrong_hostname}")
52 }
53 ChainNegative::UnknownCa => "unknown_ca".to_string(),
54 ChainNegative::ExpiredLeaf => "expired_leaf".to_string(),
55 ChainNegative::NotYetValidLeaf => "not_yet_valid_leaf".to_string(),
56 ChainNegative::ExpiredIntermediate => "expired_intermediate".to_string(),
57 ChainNegative::NotYetValidIntermediate => "not_yet_valid_intermediate".to_string(),
58 ChainNegative::IntermediateNotCa => "intermediate_not_ca".to_string(),
59 ChainNegative::IntermediateWrongKeyUsage => "intermediate_wrong_key_usage".to_string(),
60 ChainNegative::RevokedLeaf => "revoked_leaf".to_string(),
61 }
62 }
63
64 pub fn apply_to_spec(&self, base_spec: &ChainSpec) -> ChainSpec {
66 let mut spec = base_spec.clone();
67 match self {
68 ChainNegative::HostnameMismatch { wrong_hostname } => {
69 spec.leaf_cn = wrong_hostname.clone();
70 spec.leaf_sans = vec![wrong_hostname.clone()];
71 }
72 ChainNegative::UnknownCa => {
73 spec.root_cn = format!("{} Unknown Root CA", spec.leaf_cn);
75 }
76 ChainNegative::ExpiredLeaf => {
77 spec.leaf_validity_days = 1;
80 spec.leaf_not_before = Some(NotBeforeOffset::DaysAgo(730));
81 }
82 ChainNegative::NotYetValidLeaf => {
83 spec.leaf_not_before = Some(NotBeforeOffset::DaysFromNow(730));
84 }
85 ChainNegative::ExpiredIntermediate => {
86 spec.intermediate_validity_days = 1;
87 spec.intermediate_not_before = Some(NotBeforeOffset::DaysAgo(730));
88 }
89 ChainNegative::NotYetValidIntermediate => {
90 spec.intermediate_not_before = Some(NotBeforeOffset::DaysFromNow(730));
91 }
92 ChainNegative::IntermediateNotCa => {
93 spec.intermediate_is_ca = Some(false);
94 }
95 ChainNegative::IntermediateWrongKeyUsage => {
96 spec.intermediate_is_ca = Some(true);
97 spec.intermediate_key_usage = Some(KeyUsage {
98 key_cert_sign: false,
99 crl_sign: false,
100 digital_signature: true,
101 key_encipherment: false,
102 });
103 }
104 ChainNegative::RevokedLeaf => {
105 }
109 }
110 spec
111 }
112}