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
2 changes: 1 addition & 1 deletion pkcs1/src/pkcs8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl<T: DecodePrivateKey> DecodeRsaPrivateKey for T {
parameters: Some(Null.into()),
};

Ok(Self::from_pkcs8_private_key_info(PrivateKeyInfo {
Ok(Self::try_from(PrivateKeyInfo {
algorithm,
private_key,
public_key: None,
Expand Down
8 changes: 2 additions & 6 deletions pkcs8/src/document/private_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,6 @@ impl PrivateKeyDocument {
}

impl DecodePrivateKey for PrivateKeyDocument {
fn from_pkcs8_private_key_info(private_key: PrivateKeyInfo<'_>) -> Result<Self> {
Ok(Self::from_msg(&private_key)?)
}

fn from_pkcs8_der(bytes: &[u8]) -> Result<Self> {
Ok(Self::from_der(bytes)?)
}
Expand Down Expand Up @@ -147,15 +143,15 @@ impl TryFrom<PrivateKeyInfo<'_>> for PrivateKeyDocument {
type Error = Error;

fn try_from(private_key_info: PrivateKeyInfo<'_>) -> Result<PrivateKeyDocument> {
PrivateKeyDocument::from_pkcs8_private_key_info(private_key_info)
Self::try_from(&private_key_info)
}
}

impl TryFrom<&PrivateKeyInfo<'_>> for PrivateKeyDocument {
type Error = Error;

fn try_from(private_key_info: &PrivateKeyInfo<'_>) -> Result<PrivateKeyDocument> {
PrivateKeyDocument::from_pkcs8_private_key_info(private_key_info.clone())
Ok(Self::from_msg(private_key_info)?)
}
}

Expand Down
11 changes: 4 additions & 7 deletions pkcs8/src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Traits for parsing objects from PKCS#8 encoded documents

use crate::{PrivateKeyInfo, Result};
use crate::{Error, PrivateKeyInfo, Result};

#[cfg(feature = "alloc")]
use {crate::PrivateKeyDocument, der::Document};
Expand All @@ -17,14 +17,11 @@ use std::path::Path;
use {crate::LineEnding, alloc::string::String, zeroize::Zeroizing};

/// Parse a private key object from a PKCS#8 encoded document.
pub trait DecodePrivateKey: Sized {
/// Parse the [`PrivateKeyInfo`] from a PKCS#8-encoded document.
fn from_pkcs8_private_key_info(private_key_info: PrivateKeyInfo<'_>) -> Result<Self>;

pub trait DecodePrivateKey: for<'a> TryFrom<PrivateKeyInfo<'a>, Error = Error> + Sized {
/// Deserialize PKCS#8 private key from ASN.1 DER-encoded data
/// (binary format).
fn from_pkcs8_der(bytes: &[u8]) -> Result<Self> {
Self::from_pkcs8_private_key_info(PrivateKeyInfo::try_from(bytes)?)
Self::try_from(PrivateKeyInfo::try_from(bytes)?)
}

/// Deserialize encrypted PKCS#8 private key from ASN.1 DER-encoded data
Expand All @@ -41,7 +38,7 @@ pub trait DecodePrivateKey: Sized {
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
fn from_pkcs8_doc(doc: &PrivateKeyDocument) -> Result<Self> {
Self::from_pkcs8_private_key_info(doc.decode())
Self::try_from(doc.decode())
}

/// Deserialize PKCS#8-encoded private key from PEM.
Expand Down
2 changes: 1 addition & 1 deletion sec1/src/pkcs8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl<T: DecodePrivateKey> DecodeEcPrivateKey for T {
parameters: params_oid.as_ref().map(Into::into),
};

Ok(Self::from_pkcs8_private_key_info(PrivateKeyInfo {
Ok(Self::try_from(PrivateKeyInfo {
algorithm,
private_key,
public_key: None,
Expand Down