2023-05-19 19:21:34 +00:00
|
|
|
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<u8>);
|
|
|
|
|
|
|
|
impl FromStr for Certificate {
|
|
|
|
type Err = pem::PemError;
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
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<Self, Self::Error> {
|
|
|
|
Ok(Certificate(c.to_der()?))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<&Certificate> for openssl::x509::X509 {
|
|
|
|
type Error = openssl::error::ErrorStack;
|
|
|
|
|
|
|
|
fn try_from(c: &Certificate) -> Result<Self, Self::Error> {
|
2023-05-20 12:34:45 +00:00
|
|
|
openssl::x509::X509::from_der(&c.0)
|
2023-05-19 19:21:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Certificate> for rustls::Certificate {
|
|
|
|
fn from(c: Certificate) -> Self {
|
|
|
|
rustls::Certificate(c.0)
|
|
|
|
}
|
|
|
|
}
|