use super::spec::{ChainSpec, KeyUsage, NotBeforeOffset};
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub enum ChainNegative {
HostnameMismatch {
wrong_hostname: String,
},
UnknownCa,
ExpiredLeaf,
NotYetValidLeaf,
ExpiredIntermediate,
NotYetValidIntermediate,
IntermediateNotCa,
IntermediateWrongKeyUsage,
RevokedLeaf,
}
impl ChainNegative {
pub fn variant_name(&self) -> String {
match self {
ChainNegative::HostnameMismatch { wrong_hostname } => {
format!("hostname_mismatch:{wrong_hostname}")
}
ChainNegative::UnknownCa => "unknown_ca".to_string(),
ChainNegative::ExpiredLeaf => "expired_leaf".to_string(),
ChainNegative::NotYetValidLeaf => "not_yet_valid_leaf".to_string(),
ChainNegative::ExpiredIntermediate => "expired_intermediate".to_string(),
ChainNegative::NotYetValidIntermediate => "not_yet_valid_intermediate".to_string(),
ChainNegative::IntermediateNotCa => "intermediate_not_ca".to_string(),
ChainNegative::IntermediateWrongKeyUsage => "intermediate_wrong_key_usage".to_string(),
ChainNegative::RevokedLeaf => "revoked_leaf".to_string(),
}
}
pub fn apply_to_spec(&self, base_spec: &ChainSpec) -> ChainSpec {
let mut spec = base_spec.clone();
match self {
ChainNegative::HostnameMismatch { wrong_hostname } => {
spec.leaf_cn = wrong_hostname.clone();
spec.leaf_sans = vec![wrong_hostname.clone()];
}
ChainNegative::UnknownCa => {
spec.root_cn = format!("{} Unknown Root CA", spec.leaf_cn);
}
ChainNegative::ExpiredLeaf => {
spec.leaf_validity_days = 1;
spec.leaf_not_before = Some(NotBeforeOffset::DaysAgo(730));
}
ChainNegative::NotYetValidLeaf => {
spec.leaf_not_before = Some(NotBeforeOffset::DaysFromNow(730));
}
ChainNegative::ExpiredIntermediate => {
spec.intermediate_validity_days = 1;
spec.intermediate_not_before = Some(NotBeforeOffset::DaysAgo(730));
}
ChainNegative::NotYetValidIntermediate => {
spec.intermediate_not_before = Some(NotBeforeOffset::DaysFromNow(730));
}
ChainNegative::IntermediateNotCa => {
spec.intermediate_is_ca = Some(false);
}
ChainNegative::IntermediateWrongKeyUsage => {
spec.intermediate_is_ca = Some(true);
spec.intermediate_key_usage = Some(KeyUsage {
key_cert_sign: false,
crl_sign: false,
digital_signature: true,
key_encipherment: false,
});
}
ChainNegative::RevokedLeaf => {
}
}
spec
}
}