Skip to content

Commit

Permalink
distinct PEM and X509 variants for CertificateError
Browse files Browse the repository at this point in the history
  • Loading branch information
Keksoj committed Mar 15, 2024
1 parent 123cb1e commit a6fde1e
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions command/src/certificate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ use crate::{
#[derive(thiserror::Error, Debug)]
pub enum CertificateError {
#[error("Could not parse PEM certificate from bytes: {0}")]
InvalidCertificate(String),
ParsePEMCertificate(String),
#[error("Could not parse X509 certificate from bytes: {0}")]
ParseX509Certificate(String),
#[error("failed to parse tls version '{0}'")]
InvalidTlsVersion(String),
#[error("failed to parse fingerprint, {0}")]
Expand All @@ -40,14 +42,14 @@ pub enum CertificateError {
/// (a.k.a [`Pem`])
pub fn parse_pem(certificate: &[u8]) -> Result<Pem, CertificateError> {
let (_, pem) = parse_x509_pem(certificate)
.map_err(|err| CertificateError::InvalidCertificate(err.to_string()))?;
.map_err(|err| CertificateError::ParsePEMCertificate(err.to_string()))?;

Ok(pem)
}

pub fn parse_x509(pem_bytes: &[u8]) -> Result<X509Certificate, CertificateError> {
parse_x509_certificate(pem_bytes)
.map_err(|nom_e| CertificateError::InvalidCertificate(nom_e.to_string()))
.map_err(|nom_e| CertificateError::ParseX509Certificate(nom_e.to_string()))
.map(|t| t.1)
}

Expand Down Expand Up @@ -174,10 +176,9 @@ pub fn calculate_fingerprint_from_der(certificate: &[u8]) -> Vec<u8> {

/// Compute fingerprint from a certificate that is encoded in pem format
pub fn calculate_fingerprint(certificate: &[u8]) -> Result<Vec<u8>, CertificateError> {
let parsed_certificate = parse_pem(certificate)
.map_err(|parse_error| CertificateError::InvalidCertificate(parse_error.to_string()))?;

Ok(calculate_fingerprint_from_der(&parsed_certificate.contents))
let parsed_certificate = parse_pem(certificate)?;
let fingerprint = calculate_fingerprint_from_der(&parsed_certificate.contents);
Ok(fingerprint)
}

pub fn split_certificate_chain(mut chain: String) -> Vec<String> {
Expand Down

0 comments on commit a6fde1e

Please sign in to comment.