1use std::fmt::Display;
2
3use x509_parser::x509::X509Version;
4
5#[derive(Clone, Debug)]
6pub struct Certificate {
7 pub issuer: String,
8 pub subject: String,
9 pub val_not_before: String,
10 pub val_not_after: String,
11 pub version: X509Version,
12}
13
14impl Certificate {
15 pub fn from_der(bytes: &[u8]) -> Self {
16 use x509_parser::prelude::*;
17
18 if let Ok((_, cert)) = X509Certificate::from_der(bytes) {
19 let validity = cert.validity.clone();
20 let val_not_before = validity.not_before.to_datetime().to_string();
21 let val_not_after = validity.not_after.to_datetime().to_string();
22 let certificate = Certificate {
23 issuer: cert.issuer().to_string(),
24 subject: cert.subject().to_string(),
25 val_not_after,
26 val_not_before,
27 version: cert.version(),
28 };
29
30 return certificate;
31 }
32
33 panic!("Failed to parse certificate");
34 }
35}
36
37impl Display for Certificate {
38 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39 write!(
40 f,
41 "Issuer: {}\nSubject: {}\nNot Before: {}\nNot After: {}\nVersion: {}",
42 self.issuer, self.subject, self.val_not_before, self.val_not_after, self.version
43 )
44 }
45}