Normalize Certificate Serial Number

This commit is contained in:
Fabrice Reix 2023-02-28 13:52:11 +01:00 committed by jcamiel
parent d23f12604d
commit 689064e4c4
No known key found for this signature in database
GPG Key ID: 07FF11CFD55356CC
2 changed files with 47 additions and 11 deletions

View File

@ -91,10 +91,27 @@ fn parse_date(value: &str) -> Result<DateTime<Utc>, String> {
}
fn parse_serial_number(attributes: &HashMap<String, String>) -> Result<String, String> {
attributes
let value = attributes
.get("serial number")
.cloned()
.ok_or(format!("Missing serial number attribute in {attributes:?}"))
.ok_or(format!("Missing serial number attribute in {attributes:?}"))?;
let normalized_value = if value.contains(':') {
value
.split(':')
.filter(|e| !e.is_empty())
.collect::<Vec<&str>>()
.join(":")
} else {
value
.chars()
.collect::<Vec<char>>()
.chunks(2)
.map(|c| c.iter().collect::<String>())
.collect::<Vec<String>>()
.join(":")
};
Ok(normalized_value)
}
fn parse_attributes(data: &Vec<String>) -> HashMap<String, String> {
@ -149,6 +166,29 @@ mod tests {
)
}
#[test]
fn test_parse_serial_number() {
let mut attributes = HashMap::new();
attributes.insert(
"serial number".to_string(),
"1e:e8:b1:7f:1b:64:d8:d6:b3:de:87:01:03:d2:a4:f5:33:53:5a:b0:".to_string(),
);
assert_eq!(
parse_serial_number(&attributes).unwrap(),
"1e:e8:b1:7f:1b:64:d8:d6:b3:de:87:01:03:d2:a4:f5:33:53:5a:b0".to_string()
);
let mut attributes = HashMap::new();
attributes.insert(
"serial number".to_string(),
"1ee8b17f1b64d8d6b3de870103d2a4f533535ab0".to_string(),
);
assert_eq!(
parse_serial_number(&attributes).unwrap(),
"1e:e8:b1:7f:1b:64:d8:d6:b3:de:87:01:03:d2:a4:f5:33:53:5a:b0".to_string()
)
}
#[test]
fn test_try_from() {
assert_eq!(
@ -174,7 +214,8 @@ mod tests {
expire_date: chrono::DateTime::parse_from_rfc2822("Thu, 30 Oct 2025 08:29:52 GMT")
.unwrap()
.with_timezone(&chrono::Utc),
serial_number: "1ee8b17f1b64d8d6b3de870103d2a4f533535ab0".to_string()
serial_number: "1e:e8:b1:7f:1b:64:d8:d6:b3:de:87:01:03:d2:a4:f5:33:53:5a:b0"
.to_string()
}
);
assert_eq!(

View File

@ -643,14 +643,9 @@ fn test_cacert() {
.with_timezone(&chrono::Utc)
);
let serial_number = certificate.serial_number;
let serial_numbers = [
"1ee8b17f1b64d8d6b3de870103d2a4f533535ab0".to_string(),
"1e:e8:b1:7f:1b:64:d8:d6:b3:de:87:01:03:d2:a4:f5:33:53:5a:b0:".to_string(),
];
assert!(
serial_numbers.contains(&serial_number),
"actual serial_number is {serial_number}"
assert_eq!(
certificate.serial_number,
"1e:e8:b1:7f:1b:64:d8:d6:b3:de:87:01:03:d2:a4:f5:33:53:5a:b0".to_string()
);
}