|
1 | 1 | use std::{collections::HashMap, fs}; |
2 | 2 |
|
3 | 3 | use anyhow::{Context, ensure}; |
| 4 | +use cert_ext::CertExt; |
4 | 5 | use clap::Parser; |
5 | 6 | use cli_args::Cli; |
6 | 7 | use openssl::x509::X509; |
7 | 8 | use stackable_secret_operator_utils::pkcs12::pkcs12_truststore; |
8 | 9 | use tracing::{info, level_filters::LevelFilter, warn}; |
9 | 10 |
|
| 11 | +mod cert_ext; |
10 | 12 | mod cli_args; |
11 | 13 | mod parsers; |
12 | 14 |
|
@@ -44,30 +46,32 @@ pub fn main() -> anyhow::Result<()> { |
44 | 46 | let mut certificates = HashMap::<Vec<u8>, X509>::new(); |
45 | 47 | for (source, certificates_list) in certificate_sources.into_iter() { |
46 | 48 | for certificate in certificates_list { |
47 | | - let serial_bn = certificate |
48 | | - .serial_number() |
49 | | - .to_bn() |
50 | | - .context("failed to get certificate serial number as BigNumber")?; |
51 | | - let serial = serial_bn.to_vec(); |
52 | | - if let Some(existing) = certificates.get(&serial) { |
| 49 | + let sha256 = certificate.sha256_digest()?; |
| 50 | + |
| 51 | + if let Some(existing) = certificates.get(&*sha256) { |
53 | 52 | warn!( |
54 | | - serial = ?serial_bn.to_hex_str(), |
55 | 53 | ?source, |
| 54 | + sha25 = hex::encode(sha256), |
| 55 | + existing.not_before = ?existing.not_before(), |
56 | 56 | existing.not_after = ?existing.not_after(), |
57 | 57 | existing.subject = ?existing.subject_name(), |
| 58 | + existing.serial = ?existing.serial_as_hex()?, |
| 59 | + new.not_before = ?certificate.not_before(), |
58 | 60 | new.not_after = ?certificate.not_after(), |
59 | 61 | new.subject = ?certificate.subject_name(), |
60 | | - "Skipped certificate as it was already added", |
| 62 | + new.serial = ?existing.serial_as_hex()?, |
| 63 | + "Skipped certificate as a cert with the same SHA256 hash was already added", |
61 | 64 | ); |
62 | 65 | } else { |
63 | 66 | info!( |
64 | | - serial = ?serial_bn.to_hex_str(), |
65 | | - not_after = ?certificate.not_after(), |
66 | 67 | subject = ?certificate.subject_name(), |
| 68 | + not_before = ?certificate.not_before(), |
| 69 | + not_after = ?certificate.not_after(), |
| 70 | + serial = ?certificate.serial_as_hex()?, |
67 | 71 | ?source, |
68 | 72 | "Added certificate" |
69 | 73 | ); |
70 | | - certificates.insert(serial, certificate); |
| 74 | + certificates.insert(sha256.to_vec(), certificate); |
71 | 75 | } |
72 | 76 | } |
73 | 77 | } |
|
0 commit comments