| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
use clubcard_crlite::{CRLiteClubcard, CRLiteKey, CRLiteStatus}; |
| 6 |
use sha2::{Digest, Sha256}; |
| 7 |
use std::env::args; |
| 8 |
use std::path::PathBuf; |
| 9 |
use std::process::ExitCode; |
| 10 |
use x509_parser::prelude::*; |
| 11 |
|
| 12 |
fn read_as_der(path: &PathBuf) -> Result<Vec<u8>, std::io::Error> { |
| 13 |
let bytes = std::fs::read(&path)?; |
| 14 |
match parse_x509_pem(&bytes) { |
| 15 |
Ok((_, pem)) => Ok(pem.contents), |
| 16 |
_ => Ok(bytes), |
| 17 |
} |
| 18 |
} |
| 19 |
|
| 20 |
fn parse_args() -> Option<(PathBuf, PathBuf, PathBuf)> { |
| 21 |
let mut args = args().map(PathBuf::from); |
| 22 |
let _name = args.next()?; |
| 23 |
Some((args.next()?, args.next()?, args.next()?)) |
| 24 |
} |
| 25 |
|
| 26 |
fn main() -> std::process::ExitCode { |
| 27 |
let Some((filter_path, issuer_cert_path, end_entity_cert_path)) = parse_args() else { |
| 28 |
eprintln!( |
| 29 |
"Usage: {} <filter> <issuer certificate> <end entity certificate>", |
| 30 |
args().next().unwrap() |
| 31 |
); |
| 32 |
return ExitCode::FAILURE; |
| 33 |
}; |
| 34 |
|
| 35 |
let Ok(filter_bytes) = std::fs::read(&filter_path) else { |
| 36 |
eprintln!("Could not read filter"); |
| 37 |
return ExitCode::FAILURE; |
| 38 |
}; |
| 39 |
|
| 40 |
let Ok(filter) = CRLiteClubcard::from_bytes(&filter_bytes) else { |
| 41 |
eprintln!("Could not parse filter"); |
| 42 |
return ExitCode::FAILURE; |
| 43 |
}; |
| 44 |
|
| 45 |
let Ok(issuer_bytes) = read_as_der(&issuer_cert_path) else { |
| 46 |
eprintln!("Could not read issuer certificate"); |
| 47 |
return ExitCode::FAILURE; |
| 48 |
}; |
| 49 |
|
| 50 |
let Ok((_, issuer)) = X509Certificate::from_der(&issuer_bytes) else { |
| 51 |
eprintln!("Could not parse issuer certificate"); |
| 52 |
return ExitCode::FAILURE; |
| 53 |
}; |
| 54 |
|
| 55 |
let Ok(cert_bytes) = read_as_der(&end_entity_cert_path) else { |
| 56 |
eprintln!("Could not read end-entity certificate"); |
| 57 |
return ExitCode::FAILURE; |
| 58 |
}; |
| 59 |
|
| 60 |
let Ok((_, cert)) = X509Certificate::from_der(&cert_bytes) else { |
| 61 |
eprintln!("Could not parse end-entity certificate"); |
| 62 |
return ExitCode::FAILURE; |
| 63 |
}; |
| 64 |
|
| 65 |
if cert.verify_signature(Some(issuer.public_key())).is_err() { |
| 66 |
eprintln!("Invalid signature (wrong issuer certificate?)"); |
| 67 |
return ExitCode::FAILURE; |
| 68 |
} |
| 69 |
|
| 70 |
if !cert.tbs_certificate.validity.is_valid() { |
| 71 |
eprintln!("End-entity certificate is expired"); |
| 72 |
return ExitCode::FAILURE; |
| 73 |
} |
| 74 |
|
| 75 |
let Ok(Some(sct_extension)) = cert |
| 76 |
.tbs_certificate |
| 77 |
.get_extension_unique(&x509_parser::oid_registry::OID_CT_LIST_SCT) |
| 78 |
else { |
| 79 |
eprintln!("End entity certificate has no SCTs"); |
| 80 |
return ExitCode::FAILURE; |
| 81 |
}; |
| 82 |
|
| 83 |
let ParsedExtension::SCT(scts) = sct_extension.parsed_extension() else { |
| 84 |
eprintln!("End entity certificate has no SCTs"); |
| 85 |
return ExitCode::FAILURE; |
| 86 |
}; |
| 87 |
|
| 88 |
let issuer_spki_hash: [u8; 32] = Sha256::digest(issuer.tbs_certificate.subject_pki.raw).into(); |
| 89 |
let serial = cert.tbs_certificate.raw_serial(); |
| 90 |
let key = CRLiteKey::new(&issuer_spki_hash, &serial); |
| 91 |
|
| 92 |
match filter.contains(&key, scts.iter().map(|sct| (sct.id.key_id, sct.timestamp))) { |
| 93 |
CRLiteStatus::Good => println!("Good"), |
| 94 |
CRLiteStatus::Revoked => println!("Revoked"), |
| 95 |
CRLiteStatus::NotEnrolled | CRLiteStatus::NotCovered => println!("Unknown"), |
| 96 |
}; |
| 97 |
|
| 98 |
ExitCode::SUCCESS |
| 99 |
} |