use chrono::NaiveDateTime;
use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum KeyAlgorithm {
RSA,
ECDSA,
Ed25519,
Ed448,
Other(String),
}
impl KeyAlgorithm {
#[must_use]
pub fn as_str(&self) -> &str {
match self {
KeyAlgorithm::RSA => "RSA",
KeyAlgorithm::ECDSA => "ECDSA",
KeyAlgorithm::Ed25519 => "Ed25519",
KeyAlgorithm::Ed448 => "Ed448",
KeyAlgorithm::Other(oid) => oid,
}
}
#[must_use]
pub fn from_oid(oid_str: &str) -> Self {
if oid_str == "1.2.840.113549.1.1.1" {
KeyAlgorithm::RSA
} else if oid_str == "1.2.840.10045.2.1" {
KeyAlgorithm::ECDSA
} else if oid_str == "1.3.101.112" {
KeyAlgorithm::Ed25519
} else if oid_str == "1.3.101.113" {
KeyAlgorithm::Ed448
} else {
KeyAlgorithm::Other(oid_str.to_string())
}
}
}
impl fmt::Display for KeyAlgorithm {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum TlsVersion {
Tls10,
Tls11,
Tls12,
Tls13,
Ssl30,
Unknown,
}
impl TlsVersion {
#[must_use]
pub fn as_str(&self) -> &'static str {
match self {
TlsVersion::Tls10 => "TLSv1.0",
TlsVersion::Tls11 => "TLSv1.1",
TlsVersion::Tls12 => "TLSv1.2",
TlsVersion::Tls13 => "TLSv1.3",
TlsVersion::Ssl30 => "SSLv3",
TlsVersion::Unknown => "Unknown",
}
}
}
impl fmt::Display for TlsVersion {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Default)]
pub struct CertificateInfo {
pub tls_version: Option<TlsVersion>,
pub subject: Option<String>,
pub issuer: Option<String>,
pub valid_from: Option<NaiveDateTime>,
pub valid_to: Option<NaiveDateTime>,
pub oids: Option<std::collections::HashSet<String>>,
pub cipher_suite: Option<String>,
pub key_algorithm: Option<KeyAlgorithm>,
pub subject_alternative_names: Option<Vec<String>>,
pub fingerprint_sha256: Option<String>,
pub serial_number: Option<String>,
pub is_self_signed: Option<bool>,
pub is_wildcard: Option<bool>,
}
#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;
#[test]
fn test_key_algorithm_from_oid_maps_known_algorithms() {
assert_eq!(
KeyAlgorithm::from_oid("1.2.840.113549.1.1.1"),
KeyAlgorithm::RSA
);
assert_eq!(
KeyAlgorithm::from_oid("1.2.840.10045.2.1"),
KeyAlgorithm::ECDSA
);
assert_eq!(KeyAlgorithm::from_oid("1.3.101.112"), KeyAlgorithm::Ed25519);
assert_eq!(KeyAlgorithm::from_oid("1.3.101.113"), KeyAlgorithm::Ed448);
}
#[test]
fn test_key_algorithm_from_oid_preserves_unknown_value() {
assert_eq!(
KeyAlgorithm::from_oid("1.2.3.4.5"),
KeyAlgorithm::Other("1.2.3.4.5".to_string())
);
assert_eq!(
KeyAlgorithm::Other("1.2.3.4.5".to_string()).to_string(),
"1.2.3.4.5"
);
}
#[test]
fn test_key_algorithm_from_oid_requires_exact_match() {
assert_eq!(
KeyAlgorithm::from_oid("prefix-1.2.840.113549.1.1.1"),
KeyAlgorithm::Other("prefix-1.2.840.113549.1.1.1".to_string())
);
}
#[test]
fn test_tls_version_display_contract() {
assert_eq!(TlsVersion::Tls10.as_str(), "TLSv1.0");
assert_eq!(TlsVersion::Tls12.to_string(), "TLSv1.2");
assert_eq!(TlsVersion::Tls13.to_string(), "TLSv1.3");
assert_eq!(TlsVersion::Ssl30.to_string(), "SSLv3");
}
}