diff options
Diffstat (limited to 'src/traits.rs')
-rw-r--r-- | src/traits.rs | 53 |
1 files changed, 21 insertions, 32 deletions
diff --git a/src/traits.rs b/src/traits.rs index c70820c..cd3d04e 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -3,10 +3,7 @@ use crate::Result; #[cfg(feature = "alloc")] -use { - crate::{RsaPrivateKey, RsaPublicKey}, - der::SecretDocument, -}; +use der::{Document, SecretDocument}; #[cfg(feature = "pem")] use { @@ -16,13 +13,19 @@ use { }; #[cfg(feature = "pkcs8")] -use crate::{ALGORITHM_ID, ALGORITHM_OID}; +use { + crate::{ALGORITHM_ID, ALGORITHM_OID}, + der::asn1::BitStringRef, +}; #[cfg(feature = "std")] use std::path::Path; #[cfg(all(feature = "alloc", feature = "pkcs8"))] -use der::{Decode, Document}; +use der::Decode; + +#[cfg(all(feature = "alloc", any(feature = "pem", feature = "pkcs8")))] +use crate::{RsaPrivateKey, RsaPublicKey}; /// Parse an [`RsaPrivateKey`] from a PKCS#1-encoded document. pub trait DecodeRsaPrivateKey: Sized { @@ -38,7 +41,6 @@ pub trait DecodeRsaPrivateKey: Sized { /// -----BEGIN RSA PRIVATE KEY----- /// ``` #[cfg(feature = "pem")] - #[cfg_attr(docsrs, doc(cfg(feature = "pem")))] fn from_pkcs1_pem(s: &str) -> Result<Self> { let (label, doc) = SecretDocument::from_pem(s)?; RsaPrivateKey::validate_pem_label(label)?; @@ -48,15 +50,12 @@ pub trait DecodeRsaPrivateKey: Sized { /// Load PKCS#1 private key from an ASN.1 DER-encoded file on the local /// filesystem (binary format). #[cfg(feature = "std")] - #[cfg_attr(docsrs, doc(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()) } /// Load PKCS#1 private key from a PEM-encoded file on the local filesystem. #[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> { let (label, doc) = SecretDocument::read_pem_file(path)?; RsaPrivateKey::validate_pem_label(&label)?; @@ -78,7 +77,6 @@ pub trait DecodeRsaPublicKey: Sized { /// -----BEGIN RSA PUBLIC KEY----- /// ``` #[cfg(feature = "pem")] - #[cfg_attr(docsrs, doc(cfg(feature = "pem")))] fn from_pkcs1_pem(s: &str) -> Result<Self> { let (label, doc) = Document::from_pem(s)?; RsaPublicKey::validate_pem_label(label)?; @@ -88,7 +86,6 @@ pub trait DecodeRsaPublicKey: Sized { /// Load [`RsaPublicKey`] from an ASN.1 DER-encoded file on the local /// filesystem (binary format). #[cfg(feature = "std")] - #[cfg_attr(docsrs, doc(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()) @@ -96,8 +93,6 @@ pub trait DecodeRsaPublicKey: Sized { /// Load [`RsaPublicKey`] from a PEM-encoded file on the local filesystem. #[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> { let (label, doc) = Document::read_pem_file(path)?; RsaPublicKey::validate_pem_label(&label)?; @@ -107,14 +102,12 @@ pub trait DecodeRsaPublicKey: Sized { /// Serialize a [`RsaPrivateKey`] to a PKCS#1 encoded document. #[cfg(feature = "alloc")] -#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))] pub trait EncodeRsaPrivateKey { /// Serialize a [`SecretDocument`] containing a PKCS#1-encoded private key. fn to_pkcs1_der(&self) -> Result<SecretDocument>; /// Serialize this private key as PEM-encoded PKCS#1 with the given [`LineEnding`]. #[cfg(feature = "pem")] - #[cfg_attr(docsrs, doc(cfg(feature = "pem")))] fn to_pkcs1_pem(&self, line_ending: LineEnding) -> Result<Zeroizing<String>> { let doc = self.to_pkcs1_der()?; Ok(doc.to_pem(RsaPrivateKey::PEM_LABEL, line_ending)?) @@ -122,14 +115,12 @@ pub trait EncodeRsaPrivateKey { /// Write ASN.1 DER-encoded PKCS#1 private key to the given path. #[cfg(feature = "std")] - #[cfg_attr(docsrs, doc(cfg(feature = "std")))] fn write_pkcs1_der_file(&self, path: impl AsRef<Path>) -> Result<()> { Ok(self.to_pkcs1_der()?.write_der_file(path)?) } /// Write ASN.1 DER-encoded PKCS#1 private key to the given 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<()> { let doc = self.to_pkcs1_der()?; Ok(doc.write_pem_file(path, RsaPrivateKey::PEM_LABEL, line_ending)?) @@ -138,14 +129,12 @@ pub trait EncodeRsaPrivateKey { /// Serialize a [`RsaPublicKey`] to a PKCS#1-encoded document. #[cfg(feature = "alloc")] -#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))] pub trait EncodeRsaPublicKey { /// Serialize a [`Document`] containing a PKCS#1-encoded public key. fn to_pkcs1_der(&self) -> Result<Document>; /// Serialize this public key as PEM-encoded PKCS#1 with the given line ending. #[cfg(feature = "pem")] - #[cfg_attr(docsrs, doc(cfg(feature = "pem")))] fn to_pkcs1_pem(&self, line_ending: LineEnding) -> Result<String> { let doc = self.to_pkcs1_der()?; Ok(doc.to_pem(RsaPublicKey::PEM_LABEL, line_ending)?) @@ -153,14 +142,12 @@ pub trait EncodeRsaPublicKey { /// Write ASN.1 DER-encoded public key to the given path. #[cfg(feature = "std")] - #[cfg_attr(docsrs, doc(cfg(feature = "std")))] fn write_pkcs1_der_file(&self, path: impl AsRef<Path>) -> Result<()> { Ok(self.to_pkcs1_der()?.write_der_file(path)?) } /// Write ASN.1 DER-encoded public key to the given 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<()> { let doc = self.to_pkcs1_der()?; Ok(doc.write_pem_file(path, RsaPublicKey::PEM_LABEL, line_ending)?) @@ -168,8 +155,10 @@ pub trait EncodeRsaPublicKey { } #[cfg(feature = "pkcs8")] -#[cfg_attr(docsrs, doc(cfg(feature = "pkcs8")))] -impl<T: pkcs8::DecodePrivateKey> DecodeRsaPrivateKey for T { +impl<T> DecodeRsaPrivateKey for T +where + T: for<'a> TryFrom<pkcs8::PrivateKeyInfo<'a>, Error = pkcs8::Error>, +{ fn from_pkcs1_der(private_key: &[u8]) -> Result<Self> { Ok(Self::try_from(pkcs8::PrivateKeyInfo { algorithm: ALGORITHM_ID, @@ -180,18 +169,19 @@ impl<T: pkcs8::DecodePrivateKey> DecodeRsaPrivateKey for T { } #[cfg(feature = "pkcs8")] -#[cfg_attr(docsrs, doc(cfg(feature = "pkcs8")))] -impl<T: pkcs8::DecodePublicKey> DecodeRsaPublicKey for T { +impl<T> DecodeRsaPublicKey for T +where + T: for<'a> TryFrom<pkcs8::SubjectPublicKeyInfoRef<'a>, Error = pkcs8::spki::Error>, +{ fn from_pkcs1_der(public_key: &[u8]) -> Result<Self> { - Ok(Self::try_from(pkcs8::SubjectPublicKeyInfo { + Ok(Self::try_from(pkcs8::SubjectPublicKeyInfoRef { algorithm: ALGORITHM_ID, - subject_public_key: public_key, + subject_public_key: BitStringRef::from_bytes(public_key)?, })?) } } #[cfg(all(feature = "alloc", feature = "pkcs8"))] -#[cfg_attr(docsrs, doc(cfg(all(feature = "alloc", feature = "pkcs8"))))] impl<T: pkcs8::EncodePrivateKey> EncodeRsaPrivateKey for T { fn to_pkcs1_der(&self) -> Result<SecretDocument> { let pkcs8_doc = self.to_pkcs8_der()?; @@ -202,12 +192,11 @@ impl<T: pkcs8::EncodePrivateKey> EncodeRsaPrivateKey for T { } #[cfg(all(feature = "alloc", feature = "pkcs8"))] -#[cfg_attr(docsrs, doc(cfg(all(feature = "alloc", feature = "pkcs8"))))] impl<T: pkcs8::EncodePublicKey> EncodeRsaPublicKey for T { fn to_pkcs1_der(&self) -> Result<Document> { let doc = self.to_public_key_der()?; - let spki = pkcs8::SubjectPublicKeyInfo::from_der(doc.as_bytes())?; + let spki = pkcs8::SubjectPublicKeyInfoRef::from_der(doc.as_bytes())?; spki.algorithm.assert_algorithm_oid(ALGORITHM_OID)?; - RsaPublicKey::from_der(spki.subject_public_key)?.try_into() + RsaPublicKey::from_der(spki.subject_public_key.raw_bytes())?.try_into() } } |