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 pkcs8/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ pub use spki::{

#[cfg(feature = "alloc")]
pub use {
crate::{private_key_info::PrivateKeyInfoOwned, traits::EncodePrivateKey},
crate::{private_key_info::allocating::PrivateKeyInfoOwned, traits::EncodePrivateKey},
der::{Document, SecretDocument},
spki::EncodePublicKey,
};
Expand Down
42 changes: 34 additions & 8 deletions pkcs8/src/private_key_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,10 +377,6 @@ where
/// [`PrivateKeyInfo`] with [`AnyRef`] algorithm parameters, and `&[u8]` key.
pub type PrivateKeyInfoRef<'a> = PrivateKeyInfo<AnyRef<'a>, &'a OctetStringRef, BitStringRef<'a>>;

/// [`PrivateKeyInfo`] with [`Any`] algorithm parameters, and `Box<[u8]>` key.
#[cfg(feature = "alloc")]
pub type PrivateKeyInfoOwned = PrivateKeyInfo<Any, OctetString, BitString>;

/// [`BitStringLike`] marks object that will act like a `BitString`.
///
/// It will allow to get a [`BitStringRef`] that points back to the underlying bytes.
Expand All @@ -396,15 +392,39 @@ impl BitStringLike for BitStringRef<'_> {
}

#[cfg(feature = "alloc")]
mod allocating {
pub(crate) mod allocating {
use super::*;
use crate::{DecodePrivateKey, EncodePrivateKey};
use alloc::borrow::ToOwned;
use core::borrow::Borrow;
use der::referenced::*;

impl BitStringLike for BitString {
fn as_bit_string(&self) -> BitStringRef<'_> {
BitStringRef::from(self)
#[cfg(feature = "pem")]
use der::DecodePem;

/// [`PrivateKeyInfo`] with [`Any`] algorithm parameters, and `Box<[u8]>` key.
pub type PrivateKeyInfoOwned = PrivateKeyInfo<Any, OctetString, BitString>;

impl DecodePrivateKey for PrivateKeyInfoOwned {
fn from_pkcs8_der(bytes: &[u8]) -> Result<Self> {
Ok(Self::from_der(bytes)?)
}

#[cfg(feature = "pem")]
fn from_pkcs8_pem(pem: &str) -> Result<Self> {
Ok(Self::from_pem(pem)?)
}
}

impl EncodePrivateKey for PrivateKeyInfoOwned {
fn to_pkcs8_der(&self) -> Result<SecretDocument> {
self.try_into()
}
}

impl EncodePrivateKey for PrivateKeyInfoRef<'_> {
fn to_pkcs8_der(&self) -> Result<SecretDocument> {
self.try_into()
}
}

Expand All @@ -429,4 +449,10 @@ mod allocating {
}
}
}

impl BitStringLike for BitString {
fn as_bit_string(&self) -> BitStringRef<'_> {
BitStringRef::from(self)
}
}
}