diff --git a/rcgen/src/certificate.rs b/rcgen/src/certificate.rs index dd62a362..400db88a 100644 --- a/rcgen/src/certificate.rs +++ b/rcgen/src/certificate.rs @@ -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 { + 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