use crate::Result;
#[cfg(feature = "alloc")]
use der::{Document, SecretDocument};
#[cfg(feature = "pem")]
use {
crate::LineEnding,
alloc::string::String,
der::{pem::PemLabel, zeroize::Zeroizing},
};
#[cfg(feature = "std")]
use std::path::Path;
#[cfg(all(feature = "alloc", feature = "pem"))]
use crate::{RsaPrivateKeyRef, RsaPublicKeyRef};
#[cfg(doc)]
use crate::{RsaPrivateKey, RsaPublicKey};
pub trait DecodeRsaPrivateKey: Sized {
fn from_pkcs1_der(bytes: &[u8]) -> Result<Self>;
#[cfg(feature = "pem")]
fn from_pkcs1_pem(s: &str) -> Result<Self> {
let (label, doc) = SecretDocument::from_pem(s)?;
RsaPrivateKeyRef::validate_pem_label(label)?;
Self::from_pkcs1_der(doc.as_bytes())
}
#[cfg(feature = "std")]
fn read_pkcs1_der_file(path: impl AsRef<Path>) -> Result<Self> {
Self::from_pkcs1_der(SecretDocument::read_der_file(path)?.as_bytes())
}
#[cfg(all(feature = "pem", feature = "std"))]
fn read_pkcs1_pem_file(path: impl AsRef<Path>) -> Result<Self> {
let (label, doc) = SecretDocument::read_pem_file(path)?;
RsaPrivateKeyRef::validate_pem_label(&label)?;
Self::from_pkcs1_der(doc.as_bytes())
}
}
pub trait DecodeRsaPublicKey: Sized {
fn from_pkcs1_der(bytes: &[u8]) -> Result<Self>;
#[cfg(feature = "pem")]
fn from_pkcs1_pem(s: &str) -> Result<Self> {
let (label, doc) = Document::from_pem(s)?;
RsaPublicKeyRef::validate_pem_label(label)?;
Self::from_pkcs1_der(doc.as_bytes())
}
#[cfg(feature = "std")]
fn read_pkcs1_der_file(path: impl AsRef<Path>) -> Result<Self> {
let doc = Document::read_der_file(path)?;
Self::from_pkcs1_der(doc.as_bytes())
}
#[cfg(all(feature = "pem", feature = "std"))]
fn read_pkcs1_pem_file(path: impl AsRef<Path>) -> Result<Self> {
let (label, doc) = Document::read_pem_file(path)?;
RsaPublicKeyRef::validate_pem_label(&label)?;
Self::from_pkcs1_der(doc.as_bytes())
}
}
#[cfg(feature = "alloc")]
pub trait EncodeRsaPrivateKey {
fn to_pkcs1_der(&self) -> Result<SecretDocument>;
#[cfg(feature = "pem")]
fn to_pkcs1_pem(&self, line_ending: LineEnding) -> Result<Zeroizing<String>> {
let doc = self.to_pkcs1_der()?;
Ok(doc.to_pem(RsaPrivateKeyRef::PEM_LABEL, line_ending)?)
}
#[cfg(feature = "std")]
fn write_pkcs1_der_file(&self, path: impl AsRef<Path>) -> Result<()> {
Ok(self.to_pkcs1_der()?.write_der_file(path)?)
}
#[cfg(all(feature = "pem", feature = "std"))]
fn write_pkcs1_pem_file(&self, path: impl AsRef<Path>, line_ending: LineEnding) -> Result<()> {
let doc = self.to_pkcs1_der()?;
Ok(doc.write_pem_file(path, RsaPrivateKeyRef::PEM_LABEL, line_ending)?)
}
}
#[cfg(feature = "alloc")]
pub trait EncodeRsaPublicKey {
fn to_pkcs1_der(&self) -> Result<Document>;
#[cfg(feature = "pem")]
fn to_pkcs1_pem(&self, line_ending: LineEnding) -> Result<String> {
let doc = self.to_pkcs1_der()?;
Ok(doc.to_pem(RsaPublicKeyRef::PEM_LABEL, line_ending)?)
}
#[cfg(feature = "std")]
fn write_pkcs1_der_file(&self, path: impl AsRef<Path>) -> Result<()> {
Ok(self.to_pkcs1_der()?.write_der_file(path)?)
}
#[cfg(all(feature = "pem", feature = "std"))]
fn write_pkcs1_pem_file(&self, path: impl AsRef<Path>, line_ending: LineEnding) -> Result<()> {
let doc = self.to_pkcs1_der()?;
Ok(doc.write_pem_file(path, RsaPublicKeyRef::PEM_LABEL, line_ending)?)
}
}