Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add function to return a Certificate from DER format #293

Closed
wants to merge 18 commits into from
22 changes: 22 additions & 0 deletions rcgen/src/certificate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,28 @@ pub struct Certificate {
}

impl Certificate {

/// Create a `Certificate` from a DER encoded certificate.
/// Make sure the certificate match the format of x509-parser in rcgen,
/// or the generated `Certificate` will be different.
/// A safe way is to load the DER certificate generated from rcgen.
#[cfg(feature = "x509-parser")]
pub fn from_der(der: &[u8]) -> Result<Self, Error> {
use x509_parser::prelude::{FromDer, X509Certificate};

let der = der.to_owned().into();
let params = CertificateParams::from_ca_cert_der(&der)?;
let (_, x509_cert) = X509Certificate::from_der(&der).unwrap();

let x509_spki_der = x509_cert.public_key().raw.to_vec();

Ok(Certificate {
params,
subject_public_key_info: x509_spki_der,
der,
})
}

/// Returns the certificate parameters
pub fn params(&self) -> &CertificateParams {
&self.params
Expand Down
Loading