Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions pkcs1/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,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,
Expand All @@ -181,7 +184,10 @@ 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: pkcs8::DecodePublicKey> DecodeRsaPublicKey for T
where
T: for<'a> TryFrom<pkcs8::SubjectPublicKeyInfo<'a>, Error = pkcs8::Error>,
{
fn from_pkcs1_der(public_key: &[u8]) -> Result<Self> {
Ok(Self::try_from(pkcs8::SubjectPublicKeyInfo {
algorithm: ALGORITHM_ID,
Expand Down
15 changes: 11 additions & 4 deletions pkcs8/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PrivateKeyInfo<'a>, 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> {
Self::try_from(PrivateKeyInfo::try_from(bytes)?)
}
fn from_pkcs8_der(bytes: &[u8]) -> Result<Self>;

/// Deserialize encrypted PKCS#8 private key from ASN.1 DER-encoded data
/// (binary format) and attempt to decrypt it using the provided password.
Expand Down Expand Up @@ -87,6 +85,15 @@ pub trait DecodePrivateKey: for<'a> TryFrom<PrivateKeyInfo<'a>, Error = Error> +
}
}

impl<T> DecodePrivateKey for T
where
T: for<'a> TryFrom<PrivateKeyInfo<'a>, Error = Error>,
{
fn from_pkcs8_der(bytes: &[u8]) -> Result<Self> {
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")))]
Expand Down
6 changes: 0 additions & 6 deletions pkcs8/tests/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,6 @@ impl AsRef<[u8]> for MockKey {
}
}

impl DecodePrivateKey for MockKey {
fn from_pkcs8_der(bytes: &[u8]) -> Result<MockKey> {
Ok(MockKey(bytes.to_vec()))
}
}

impl EncodePrivateKey for MockKey {
fn to_pkcs8_der(&self) -> Result<SecretDocument> {
Ok(SecretDocument::try_from(self.as_ref())?)
Expand Down
5 changes: 4 additions & 1 deletion sec1/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ pub trait EncodeEcPrivateKey {

#[cfg(feature = "pkcs8")]
#[cfg_attr(docsrs, doc(cfg(feature = "pkcs8")))]
impl<T: pkcs8::DecodePrivateKey> DecodeEcPrivateKey for T {
impl<T> DecodeEcPrivateKey for T
where
T: for<'a> TryFrom<pkcs8::PrivateKeyInfo<'a>, Error = pkcs8::Error>,
{
fn from_sec1_der(private_key: &[u8]) -> Result<Self> {
let params_oid = EcPrivateKey::from_der(private_key)?
.parameters
Expand Down
17 changes: 11 additions & 6 deletions spki/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SubjectPublicKeyInfo<'a>, 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> {
Self::try_from(SubjectPublicKeyInfo::try_from(bytes)?)
}
fn from_public_key_der(bytes: &[u8]) -> Result<Self>;

/// Deserialize PEM-encoded [`SubjectPublicKeyInfo`].
///
Expand Down Expand Up @@ -58,6 +54,15 @@ pub trait DecodePublicKey:
}
}

impl<T> DecodePublicKey for T
where
T: for<'a> TryFrom<SubjectPublicKeyInfo<'a>, Error = Error>,
{
fn from_public_key_der(bytes: &[u8]) -> Result<Self> {
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")))]
Expand Down
6 changes: 0 additions & 6 deletions spki/tests/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,6 @@ impl AsRef<[u8]> for MockKey {
}
}

impl DecodePublicKey for MockKey {
fn from_public_key_der(bytes: &[u8]) -> Result<MockKey> {
Ok(MockKey(bytes.to_vec()))
}
}

impl EncodePublicKey for MockKey {
fn to_public_key_der(&self) -> Result<Document> {
Ok(Document::from_der(self.as_ref())?)
Expand Down