parent
209daacf1b
commit
4f98a9a244
@ -1,6 +1,8 @@ |
||||
pub mod manager; |
||||
pub mod store; |
||||
|
||||
pub type PrivateKey = openssl::pkey::PKey<openssl::pkey::Private>; |
||||
mod private_key; |
||||
pub use self::private_key::PrivateKey; |
||||
|
||||
pub type Certificate = openssl::x509::X509; |
||||
mod certificate; |
||||
pub use self::certificate::Certificate; |
||||
|
@ -0,0 +1,45 @@ |
||||
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> { |
||||
Ok(openssl::x509::X509::from_der(&c.0)?) |
||||
} |
||||
} |
||||
|
||||
impl From<Certificate> for rustls::Certificate { |
||||
fn from(c: Certificate) -> Self { |
||||
rustls::Certificate(c.0) |
||||
} |
||||
} |
@ -0,0 +1,55 @@ |
||||
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 ASN.1 in either PKCS#8, PKCS#1, or Sec1 format, like rustls::PrivateKey.
|
||||
pub struct PrivateKey(Vec<u8>); |
||||
|
||||
impl PrivateKey { |
||||
pub fn new() -> PrivateKey { |
||||
acme2::gen_rsa_private_key(4096) |
||||
.expect("RSA private key generated") |
||||
.as_ref() |
||||
.try_into() |
||||
.expect("RSA private key converted to internal representation") |
||||
} |
||||
} |
||||
|
||||
impl FromStr for PrivateKey { |
||||
type Err = pem::PemError; |
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> { |
||||
Ok(PrivateKey(pem::parse(s)?.into_contents())) |
||||
} |
||||
} |
||||
|
||||
impl fmt::Display for PrivateKey { |
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
||||
pem::Pem::new("PRIVATE KEY", self.0.clone()).fmt(f) |
||||
} |
||||
} |
||||
|
||||
impl TryFrom<&openssl::pkey::PKeyRef<openssl::pkey::Private>> for PrivateKey { |
||||
type Error = openssl::error::ErrorStack; |
||||
|
||||
fn try_from(k: &openssl::pkey::PKeyRef<openssl::pkey::Private>) -> Result<Self, Self::Error> { |
||||
Ok(PrivateKey(k.private_key_to_der()?)) |
||||
} |
||||
} |
||||
|
||||
impl TryFrom<&PrivateKey> for openssl::pkey::PKey<openssl::pkey::Private> { |
||||
type Error = openssl::error::ErrorStack; |
||||
|
||||
fn try_from(k: &PrivateKey) -> Result<Self, Self::Error> { |
||||
Ok(openssl::pkey::PKey::private_key_from_der(&k.0)?) |
||||
} |
||||
} |
||||
|
||||
impl From<PrivateKey> for rustls::PrivateKey { |
||||
fn from(k: PrivateKey) -> Self { |
||||
rustls::PrivateKey(k.0) |
||||
} |
||||
} |
Loading…
Reference in new issue