Skip to content

Commit 1a52afe

Browse files
authored
pkcs8: impl Decode/EncodePrivateKey for PrivateKeyInfoOwned (#2306)
1 parent e65d751 commit 1a52afe

2 files changed

Lines changed: 35 additions & 9 deletions

File tree

pkcs8/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ pub use spki::{
8989

9090
#[cfg(feature = "alloc")]
9191
pub use {
92-
crate::{private_key_info::PrivateKeyInfoOwned, traits::EncodePrivateKey},
92+
crate::{private_key_info::allocating::PrivateKeyInfoOwned, traits::EncodePrivateKey},
9393
der::{Document, SecretDocument},
9494
spki::EncodePublicKey,
9595
};

pkcs8/src/private_key_info.rs

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -377,10 +377,6 @@ where
377377
/// [`PrivateKeyInfo`] with [`AnyRef`] algorithm parameters, and `&[u8]` key.
378378
pub type PrivateKeyInfoRef<'a> = PrivateKeyInfo<AnyRef<'a>, &'a OctetStringRef, BitStringRef<'a>>;
379379

380-
/// [`PrivateKeyInfo`] with [`Any`] algorithm parameters, and `Box<[u8]>` key.
381-
#[cfg(feature = "alloc")]
382-
pub type PrivateKeyInfoOwned = PrivateKeyInfo<Any, OctetString, BitString>;
383-
384380
/// [`BitStringLike`] marks object that will act like a `BitString`.
385381
///
386382
/// It will allow to get a [`BitStringRef`] that points back to the underlying bytes.
@@ -396,15 +392,39 @@ impl BitStringLike for BitStringRef<'_> {
396392
}
397393

398394
#[cfg(feature = "alloc")]
399-
mod allocating {
395+
pub(crate) mod allocating {
400396
use super::*;
397+
use crate::{DecodePrivateKey, EncodePrivateKey};
401398
use alloc::borrow::ToOwned;
402399
use core::borrow::Borrow;
403400
use der::referenced::*;
404401

405-
impl BitStringLike for BitString {
406-
fn as_bit_string(&self) -> BitStringRef<'_> {
407-
BitStringRef::from(self)
402+
#[cfg(feature = "pem")]
403+
use der::DecodePem;
404+
405+
/// [`PrivateKeyInfo`] with [`Any`] algorithm parameters, and `Box<[u8]>` key.
406+
pub type PrivateKeyInfoOwned = PrivateKeyInfo<Any, OctetString, BitString>;
407+
408+
impl DecodePrivateKey for PrivateKeyInfoOwned {
409+
fn from_pkcs8_der(bytes: &[u8]) -> Result<Self> {
410+
Ok(Self::from_der(bytes)?)
411+
}
412+
413+
#[cfg(feature = "pem")]
414+
fn from_pkcs8_pem(pem: &str) -> Result<Self> {
415+
Ok(Self::from_pem(pem)?)
416+
}
417+
}
418+
419+
impl EncodePrivateKey for PrivateKeyInfoOwned {
420+
fn to_pkcs8_der(&self) -> Result<SecretDocument> {
421+
self.try_into()
422+
}
423+
}
424+
425+
impl EncodePrivateKey for PrivateKeyInfoRef<'_> {
426+
fn to_pkcs8_der(&self) -> Result<SecretDocument> {
427+
self.try_into()
408428
}
409429
}
410430

@@ -429,4 +449,10 @@ mod allocating {
429449
}
430450
}
431451
}
452+
453+
impl BitStringLike for BitString {
454+
fn as_bit_string(&self) -> BitStringRef<'_> {
455+
BitStringRef::from(self)
456+
}
457+
}
432458
}

0 commit comments

Comments
 (0)