use crate::Result;
#[cfg(feature = "alloc")]
use crate::{RsaPrivateKeyDocument, RsaPublicKeyDocument};
#[cfg(feature = "pem")]
use {crate::LineEnding, alloc::string::String};
#[cfg(feature = "pkcs8")]
use crate::{ALGORITHM_ID, ALGORITHM_OID};
#[cfg(feature = "std")]
use std::path::Path;
#[cfg(all(feature = "alloc", feature = "pkcs8"))]
use der::Document;
#[cfg(any(feature = "pem", feature = "std"))]
use zeroize::Zeroizing;
pub trait DecodeRsaPrivateKey: Sized {
fn from_pkcs1_der(bytes: &[u8]) -> Result<Self>;
#[cfg(feature = "pem")]
#[cfg_attr(docsrs, doc(cfg(feature = "pem")))]
fn from_pkcs1_pem(s: &str) -> Result<Self> {
RsaPrivateKeyDocument::from_pkcs1_pem(s).and_then(|doc| Self::from_pkcs1_der(doc.as_der()))
}
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
fn read_pkcs1_der_file(path: impl AsRef<Path>) -> Result<Self> {
RsaPrivateKeyDocument::read_pkcs1_der_file(path)
.and_then(|doc| Self::from_pkcs1_der(doc.as_der()))
}
#[cfg(all(feature = "pem", feature = "std"))]
#[cfg_attr(docsrs, doc(cfg(feature = "pem")))]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
fn read_pkcs1_pem_file(path: impl AsRef<Path>) -> Result<Self> {
RsaPrivateKeyDocument::read_pkcs1_pem_file(path)
.and_then(|doc| Self::from_pkcs1_der(doc.as_der()))
}
}
pub trait DecodeRsaPublicKey: Sized {
fn from_pkcs1_der(bytes: &[u8]) -> Result<Self>;
#[cfg(feature = "pem")]
#[cfg_attr(docsrs, doc(cfg(feature = "pem")))]
fn from_pkcs1_pem(s: &str) -> Result<Self> {
RsaPublicKeyDocument::from_pkcs1_pem(s).and_then(|doc| Self::from_pkcs1_der(doc.as_der()))
}
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
fn read_pkcs1_der_file(path: impl AsRef<Path>) -> Result<Self> {
RsaPublicKeyDocument::read_pkcs1_der_file(path)
.and_then(|doc| Self::from_pkcs1_der(doc.as_der()))
}
#[cfg(all(feature = "pem", feature = "std"))]
#[cfg_attr(docsrs, doc(cfg(feature = "pem")))]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
fn read_pkcs1_pem_file(path: impl AsRef<Path>) -> Result<Self> {
RsaPublicKeyDocument::read_pkcs1_pem_file(path)
.and_then(|doc| Self::from_pkcs1_der(doc.as_der()))
}
}
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
pub trait EncodeRsaPrivateKey {
fn to_pkcs1_der(&self) -> Result<RsaPrivateKeyDocument>;
#[cfg(feature = "pem")]
#[cfg_attr(docsrs, doc(cfg(feature = "pem")))]
fn to_pkcs1_pem(&self, line_ending: LineEnding) -> Result<Zeroizing<String>> {
self.to_pkcs1_der()?.to_pkcs1_pem(line_ending)
}
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
fn write_pkcs1_der_file(&self, path: impl AsRef<Path>) -> Result<()> {
self.to_pkcs1_der()?.write_pkcs1_der_file(path)
}
#[cfg(all(feature = "pem", feature = "std"))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "pem", feature = "std"))))]
fn write_pkcs1_pem_file(&self, path: impl AsRef<Path>, line_ending: LineEnding) -> Result<()> {
self.to_pkcs1_der()?.write_pkcs1_pem_file(path, line_ending)
}
}
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
pub trait EncodeRsaPublicKey {
fn to_pkcs1_der(&self) -> Result<RsaPublicKeyDocument>;
#[cfg(feature = "pem")]
#[cfg_attr(docsrs, doc(cfg(feature = "pem")))]
fn to_pkcs1_pem(&self, line_ending: LineEnding) -> Result<String> {
self.to_pkcs1_der()?.to_pkcs1_pem(line_ending)
}
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
fn write_pkcs1_der_file(&self, path: impl AsRef<Path>) -> Result<()> {
self.to_pkcs1_der()?.write_pkcs1_der_file(path)
}
#[cfg(all(feature = "pem", feature = "std"))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "pem", feature = "std"))))]
fn write_pkcs1_pem_file(&self, path: impl AsRef<Path>, line_ending: LineEnding) -> Result<()> {
self.to_pkcs1_der()?.write_pkcs1_pem_file(path, line_ending)
}
}
#[cfg(feature = "pkcs8")]
#[cfg_attr(docsrs, doc(cfg(feature = "pkcs8")))]
impl<T: pkcs8::DecodePrivateKey> DecodeRsaPrivateKey for T {
fn from_pkcs1_der(private_key: &[u8]) -> Result<Self> {
let algorithm = pkcs8::AlgorithmIdentifier {
oid: ALGORITHM_OID,
parameters: Some(der::asn1::Null.into()),
};
Ok(Self::try_from(pkcs8::PrivateKeyInfo {
algorithm,
private_key,
public_key: None,
})?)
}
}
#[cfg(feature = "pkcs8")]
#[cfg_attr(docsrs, doc(cfg(feature = "pkcs8")))]
impl<T: pkcs8::DecodePublicKey> DecodeRsaPublicKey for T {
fn from_pkcs1_der(public_key: &[u8]) -> Result<Self> {
Ok(Self::try_from(pkcs8::SubjectPublicKeyInfo {
algorithm: ALGORITHM_ID,
subject_public_key: public_key,
})?)
}
}
#[cfg(all(feature = "alloc", feature = "pkcs8"))]
#[cfg_attr(docsrs, doc(all(feature = "alloc", feature = "pkcs8")))]
impl<T: pkcs8::EncodePrivateKey> EncodeRsaPrivateKey for T {
fn to_pkcs1_der(&self) -> Result<RsaPrivateKeyDocument> {
let doc = self.to_pkcs8_der()?;
Ok(RsaPrivateKeyDocument::from_der(doc.decode().private_key)?)
}
}
#[cfg(all(feature = "alloc", feature = "pkcs8"))]
#[cfg_attr(docsrs, doc(all(feature = "alloc", feature = "pkcs8")))]
impl<T: pkcs8::EncodePublicKey> EncodeRsaPublicKey for T {
fn to_pkcs1_der(&self) -> Result<RsaPublicKeyDocument> {
let doc = self.to_public_key_der()?;
Ok(RsaPublicKeyDocument::from_der(
doc.decode().subject_public_key,
)?)
}
}