use std::convert::{From, TryFrom}; use std::fmt; use std::str::FromStr; use serde_with::{DeserializeFromStr, SerializeDisplay}; #[derive(Debug, Clone, PartialEq, DeserializeFromStr, SerializeDisplay)] /// DER-encoded X.509, like rustls::Certificate. pub struct Certificate(Vec); impl FromStr for Certificate { type Err = pem::PemError; fn from_str(s: &str) -> Result { Ok(Certificate(pem::parse(s)?.into_contents())) } } impl fmt::Display for Certificate { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { pem::Pem::new("CERTIFICATE", self.0.clone()).fmt(f) } } impl TryFrom<&openssl::x509::X509Ref> for Certificate { type Error = openssl::error::ErrorStack; fn try_from(c: &openssl::x509::X509Ref) -> Result { Ok(Certificate(c.to_der()?)) } } impl TryFrom<&Certificate> for openssl::x509::X509 { type Error = openssl::error::ErrorStack; fn try_from(c: &Certificate) -> Result { openssl::x509::X509::from_der(&c.0) } } impl From for rustls::Certificate { fn from(c: Certificate) -> Self { rustls::Certificate(c.0) } }