diff --git a/pkcs1/src/traits.rs b/pkcs1/src/traits.rs index c70820cb7..b9899564a 100644 --- a/pkcs1/src/traits.rs +++ b/pkcs1/src/traits.rs @@ -169,7 +169,10 @@ pub trait EncodeRsaPublicKey { #[cfg(feature = "pkcs8")] #[cfg_attr(docsrs, doc(cfg(feature = "pkcs8")))] -impl DecodeRsaPrivateKey for T { +impl DecodeRsaPrivateKey for T +where + T: for<'a> TryFrom, Error = pkcs8::Error>, +{ fn from_pkcs1_der(private_key: &[u8]) -> Result { Ok(Self::try_from(pkcs8::PrivateKeyInfo { algorithm: ALGORITHM_ID, @@ -181,7 +184,10 @@ impl DecodeRsaPrivateKey for T { #[cfg(feature = "pkcs8")] #[cfg_attr(docsrs, doc(cfg(feature = "pkcs8")))] -impl DecodeRsaPublicKey for T { +impl DecodeRsaPublicKey for T +where + T: for<'a> TryFrom, Error = pkcs8::Error>, +{ fn from_pkcs1_der(public_key: &[u8]) -> Result { Ok(Self::try_from(pkcs8::SubjectPublicKeyInfo { algorithm: ALGORITHM_ID, diff --git a/pkcs8/src/traits.rs b/pkcs8/src/traits.rs index dd86b90ef..120c3c80f 100644 --- a/pkcs8/src/traits.rs +++ b/pkcs8/src/traits.rs @@ -21,12 +21,10 @@ use der::pem::PemLabel; use std::path::Path; /// Parse a private key object from a PKCS#8 encoded document. -pub trait DecodePrivateKey: for<'a> TryFrom, Error = Error> + Sized { +pub trait DecodePrivateKey: Sized { /// Deserialize PKCS#8 private key from ASN.1 DER-encoded data /// (binary format). - fn from_pkcs8_der(bytes: &[u8]) -> Result { - Self::try_from(PrivateKeyInfo::try_from(bytes)?) - } + fn from_pkcs8_der(bytes: &[u8]) -> Result; /// Deserialize encrypted PKCS#8 private key from ASN.1 DER-encoded data /// (binary format) and attempt to decrypt it using the provided password. @@ -87,6 +85,15 @@ pub trait DecodePrivateKey: for<'a> TryFrom, Error = Error> + } } +impl DecodePrivateKey for T +where + T: for<'a> TryFrom, Error = Error>, +{ + fn from_pkcs8_der(bytes: &[u8]) -> Result { + Self::try_from(PrivateKeyInfo::try_from(bytes)?) + } +} + /// Serialize a private key object to a PKCS#8 encoded document. #[cfg(feature = "alloc")] #[cfg_attr(docsrs, doc(cfg(feature = "alloc")))] diff --git a/pkcs8/tests/traits.rs b/pkcs8/tests/traits.rs index 1c8a969bc..f1bc9f955 100644 --- a/pkcs8/tests/traits.rs +++ b/pkcs8/tests/traits.rs @@ -30,12 +30,6 @@ impl AsRef<[u8]> for MockKey { } } -impl DecodePrivateKey for MockKey { - fn from_pkcs8_der(bytes: &[u8]) -> Result { - Ok(MockKey(bytes.to_vec())) - } -} - impl EncodePrivateKey for MockKey { fn to_pkcs8_der(&self) -> Result { Ok(SecretDocument::try_from(self.as_ref())?) diff --git a/sec1/src/traits.rs b/sec1/src/traits.rs index cf0d9711e..08f91d554 100644 --- a/sec1/src/traits.rs +++ b/sec1/src/traits.rs @@ -97,7 +97,10 @@ pub trait EncodeEcPrivateKey { #[cfg(feature = "pkcs8")] #[cfg_attr(docsrs, doc(cfg(feature = "pkcs8")))] -impl DecodeEcPrivateKey for T { +impl DecodeEcPrivateKey for T +where + T: for<'a> TryFrom, Error = pkcs8::Error>, +{ fn from_sec1_der(private_key: &[u8]) -> Result { let params_oid = EcPrivateKey::from_der(private_key)? .parameters diff --git a/spki/src/traits.rs b/spki/src/traits.rs index c16e3974d..2bafcf51b 100644 --- a/spki/src/traits.rs +++ b/spki/src/traits.rs @@ -15,14 +15,10 @@ use { use std::path::Path; /// Parse a public key object from an encoded SPKI document. -pub trait DecodePublicKey: - for<'a> TryFrom, Error = Error> + Sized -{ +pub trait DecodePublicKey: Sized { /// Deserialize object from ASN.1 DER-encoded [`SubjectPublicKeyInfo`] /// (binary format). - fn from_public_key_der(bytes: &[u8]) -> Result { - Self::try_from(SubjectPublicKeyInfo::try_from(bytes)?) - } + fn from_public_key_der(bytes: &[u8]) -> Result; /// Deserialize PEM-encoded [`SubjectPublicKeyInfo`]. /// @@ -58,6 +54,15 @@ pub trait DecodePublicKey: } } +impl DecodePublicKey for T +where + T: for<'a> TryFrom, Error = Error>, +{ + fn from_public_key_der(bytes: &[u8]) -> Result { + Self::try_from(SubjectPublicKeyInfo::try_from(bytes)?) + } +} + /// Serialize a public key object to a SPKI-encoded document. #[cfg(feature = "alloc")] #[cfg_attr(docsrs, doc(cfg(feature = "alloc")))] diff --git a/spki/tests/traits.rs b/spki/tests/traits.rs index 597399e44..3c3cec95a 100644 --- a/spki/tests/traits.rs +++ b/spki/tests/traits.rs @@ -30,12 +30,6 @@ impl AsRef<[u8]> for MockKey { } } -impl DecodePublicKey for MockKey { - fn from_public_key_der(bytes: &[u8]) -> Result { - Ok(MockKey(bytes.to_vec())) - } -} - impl EncodePublicKey for MockKey { fn to_public_key_der(&self) -> Result { Ok(Document::from_der(self.as_ref())?)