Skip to content

Commit

Permalink
pkcs8: provide PrivateKeyInfo type alias with older API
Browse files Browse the repository at this point in the history
Signed-off-by: Arthur Gautier <[email protected]>
  • Loading branch information
baloo committed Aug 6, 2023
1 parent 07cc02b commit 4dd6f4a
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 59 deletions.
2 changes: 1 addition & 1 deletion pkcs8/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ pub(crate) mod encrypted_private_key_info;

pub use crate::{
error::{Error, Result},
private_key_info::{PrivateKeyInfo, PrivateKeyInfoRef},
private_key_info::{PrivateKeyInfo, PrivateKeyInfoInner},
traits::DecodePrivateKey,
version::Version,
};
Expand Down
55 changes: 26 additions & 29 deletions pkcs8/src/private_key_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ const PUBLIC_KEY_TAG: TagNumber = TagNumber::N1;
/// [RFC 5208 Section 5]: https://tools.ietf.org/html/rfc5208#section-5
/// [RFC 5958 Section 2]: https://datatracker.ietf.org/doc/html/rfc5958#section-2
#[derive(Clone)]
pub struct PrivateKeyInfo<Params, Key> {
pub struct PrivateKeyInfoInner<Params, Key> {
/// X.509 `AlgorithmIdentifier` for the private key type.
pub algorithm: AlgorithmIdentifier<Params>,

Expand All @@ -105,8 +105,8 @@ pub struct PrivateKeyInfo<Params, Key> {
pub public_key: Option<Key>,
}

impl<Params, Key> PrivateKeyInfo<Params, Key> {
/// Create a new PKCS#8 [`PrivateKeyInfo`] message.
impl<Params, Key> PrivateKeyInfoInner<Params, Key> {
/// Create a new PKCS#8 [`PrivateKeyInfoInner`] message.
///
/// This is a helper method which initializes `attributes` and `public_key`
/// to `None`, helpful if you aren't using those.
Expand All @@ -129,7 +129,7 @@ impl<Params, Key> PrivateKeyInfo<Params, Key> {
}
}
}
impl<'a, Params, Key> PrivateKeyInfo<Params, Key>
impl<'a, Params, Key> PrivateKeyInfoInner<Params, Key>
where
Params: der::Choice<'a> + Encode,
Key: From<&'a [u8]> + AsRef<[u8]>,
Expand Down Expand Up @@ -166,7 +166,7 @@ where
}
}

impl<'a, Params, Key> PrivateKeyInfo<Params, Key>
impl<'a, Params, Key> PrivateKeyInfoInner<Params, Key>
where
Params: der::Choice<'a> + Encode,
Key: AsRef<[u8]>,
Expand All @@ -186,15 +186,12 @@ where
}
}

impl<'a, Params, Key> DecodeValue<'a> for PrivateKeyInfo<Params, Key>
impl<'a, Params, Key> DecodeValue<'a> for PrivateKeyInfoInner<Params, Key>
where
Params: der::Choice<'a> + Encode,
Key: From<&'a [u8]>,
{
fn decode_value<R: Reader<'a>>(
reader: &mut R,
header: Header,
) -> der::Result<PrivateKeyInfo<Params, Key>> {
fn decode_value<R: Reader<'a>>(reader: &mut R, header: Header) -> der::Result<Self> {
reader.read_nested(header.length, |reader| {
// Parse and validate `version` INTEGER.
let version = Version::decode(reader)?;
Expand Down Expand Up @@ -235,7 +232,7 @@ where
}
}

impl<'a, Params, Key> EncodeValue for PrivateKeyInfo<Params, Key>
impl<'a, Params, Key> EncodeValue for PrivateKeyInfoInner<Params, Key>
where
Params: der::Choice<'a> + Encode,
Key: AsRef<[u8]>,
Expand All @@ -256,14 +253,14 @@ where
}
}

impl<'a, Params, Key> Sequence<'a> for PrivateKeyInfo<Params, Key>
impl<'a, Params, Key> Sequence<'a> for PrivateKeyInfoInner<Params, Key>
where
Params: der::Choice<'a> + Encode,
Key: From<&'a [u8]> + AsRef<[u8]>,
{
}

impl<'a, Params, Key> TryFrom<&'a [u8]> for PrivateKeyInfo<Params, Key>
impl<'a, Params, Key> TryFrom<&'a [u8]> for PrivateKeyInfoInner<Params, Key>
where
Params: der::Choice<'a> + Encode,
Key: From<&'a [u8]> + AsRef<[u8]>,
Expand All @@ -275,13 +272,13 @@ where
}
}

impl<Params, Key> fmt::Debug for PrivateKeyInfo<Params, Key>
impl<Params, Key> fmt::Debug for PrivateKeyInfoInner<Params, Key>
where
Params: fmt::Debug,
Key: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("PrivateKeyInfo")
f.debug_struct("PrivateKeyInfoInner")
.field("version", &self.version())
.field("algorithm", &self.algorithm)
.field("public_key", &self.public_key)
Expand All @@ -290,38 +287,38 @@ where
}

#[cfg(feature = "alloc")]
impl<'a, Params, Key> TryFrom<PrivateKeyInfo<Params, Key>> for SecretDocument
impl<'a, Params, Key> TryFrom<PrivateKeyInfoInner<Params, Key>> for SecretDocument
where
Params: der::Choice<'a> + Encode,
Key: From<&'a [u8]> + AsRef<[u8]>,
{
type Error = Error;

fn try_from(private_key: PrivateKeyInfo<Params, Key>) -> Result<SecretDocument> {
fn try_from(private_key: PrivateKeyInfoInner<Params, Key>) -> Result<SecretDocument> {
SecretDocument::try_from(&private_key)
}
}

#[cfg(feature = "alloc")]
impl<'a, Params, Key> TryFrom<&PrivateKeyInfo<Params, Key>> for SecretDocument
impl<'a, Params, Key> TryFrom<&PrivateKeyInfoInner<Params, Key>> for SecretDocument
where
Params: der::Choice<'a> + Encode,
Key: From<&'a [u8]> + AsRef<[u8]>,
{
type Error = Error;

fn try_from(private_key: &PrivateKeyInfo<Params, Key>) -> Result<SecretDocument> {
fn try_from(private_key: &PrivateKeyInfoInner<Params, Key>) -> Result<SecretDocument> {
Ok(Self::encode_msg(private_key)?)
}
}

#[cfg(feature = "pem")]
impl<Params, Key> PemLabel for PrivateKeyInfo<Params, Key> {
impl<Params, Key> PemLabel for PrivateKeyInfoInner<Params, Key> {
const PEM_LABEL: &'static str = "PRIVATE KEY";
}

#[cfg(feature = "subtle")]
impl<Params, Key> ConstantTimeEq for PrivateKeyInfo<Params, Key>
impl<Params, Key> ConstantTimeEq for PrivateKeyInfoInner<Params, Key>
where
Params: Eq,
Key: PartialEq + AsRef<[u8]>,
Expand All @@ -337,15 +334,15 @@ where
}

#[cfg(feature = "subtle")]
impl<Params, Key> Eq for PrivateKeyInfo<Params, Key>
impl<Params, Key> Eq for PrivateKeyInfoInner<Params, Key>
where
Params: Eq,
Key: AsRef<[u8]> + Eq,
{
}

#[cfg(feature = "subtle")]
impl<Params, Key> PartialEq for PrivateKeyInfo<Params, Key>
impl<Params, Key> PartialEq for PrivateKeyInfoInner<Params, Key>
where
Params: Eq,
Key: PartialEq + AsRef<[u8]>,
Expand All @@ -355,19 +352,19 @@ where
}
}

/// [`PrivateKeyInfo`] with [`AnyRef`] algorithm parameters, and `&[u8]` key.
pub type PrivateKeyInfoRef<'a> = PrivateKeyInfo<AnyRef<'a>, &'a [u8]>;
/// [`PrivateKeyInfoInner`] with [`AnyRef`] algorithm parameters, and `&[u8]` key.
pub type PrivateKeyInfo<'a> = PrivateKeyInfoInner<AnyRef<'a>, &'a [u8]>;

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

#[cfg(feature = "alloc")]
mod allocating {
use super::*;
use der::referenced::*;

impl<'a> RefToOwned<'a> for PrivateKeyInfoRef<'a> {
impl<'a> RefToOwned<'a> for PrivateKeyInfo<'a> {
type Owned = PrivateKeyInfoOwned;
fn ref_to_owned(&self) -> Self::Owned {
PrivateKeyInfoOwned {
Expand All @@ -379,9 +376,9 @@ mod allocating {
}

impl OwnedToRef for PrivateKeyInfoOwned {
type Borrowed<'a> = PrivateKeyInfoRef<'a>;
type Borrowed<'a> = PrivateKeyInfo<'a>;
fn owned_to_ref(&self) -> Self::Borrowed<'_> {
PrivateKeyInfoRef {
PrivateKeyInfo {
algorithm: self.algorithm.owned_to_ref(),
private_key: self.private_key.owned_to_ref(),
public_key: self.public_key.owned_to_ref(),
Expand Down
14 changes: 7 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::{Error, PrivateKeyInfoRef, Result};
use crate::{Error, PrivateKeyInfo, Result};

#[cfg(feature = "alloc")]
use der::SecretDocument;
Expand Down Expand Up @@ -49,7 +49,7 @@ pub trait DecodePrivateKey: Sized {
fn from_pkcs8_pem(s: &str) -> Result<Self> {
// Validate PEM label
let label = pem::decode_label(s.as_bytes())?;
PrivateKeyInfoRef::validate_pem_label(label)?;
PrivateKeyInfo::validate_pem_label(label)?;

let doc = SecretDocument::from_pem(s)?.1;
Self::from_pkcs8_der(doc.as_bytes())
Expand Down Expand Up @@ -81,17 +81,17 @@ pub trait DecodePrivateKey: Sized {
#[cfg(all(feature = "pem", feature = "std"))]
fn read_pkcs8_pem_file(path: impl AsRef<Path>) -> Result<Self> {
let (label, doc) = SecretDocument::read_pem_file(path)?;
PrivateKeyInfoRef::validate_pem_label(&label)?;
PrivateKeyInfo::validate_pem_label(&label)?;
Self::from_pkcs8_der(doc.as_bytes())
}
}

impl<T> DecodePrivateKey for T
where
T: for<'a> TryFrom<PrivateKeyInfoRef<'a>, Error = Error>,
T: for<'a> TryFrom<PrivateKeyInfo<'a>, Error = Error>,
{
fn from_pkcs8_der(bytes: &[u8]) -> Result<Self> {
Self::try_from(PrivateKeyInfoRef::try_from(bytes)?)
Self::try_from(PrivateKeyInfo::try_from(bytes)?)
}
}

Expand All @@ -116,7 +116,7 @@ pub trait EncodePrivateKey {
#[cfg(feature = "pem")]
fn to_pkcs8_pem(&self, line_ending: LineEnding) -> Result<Zeroizing<String>> {
let doc = self.to_pkcs8_der()?;
Ok(doc.to_pem(PrivateKeyInfoRef::PEM_LABEL, line_ending)?)
Ok(doc.to_pem(PrivateKeyInfo::PEM_LABEL, line_ending)?)
}

/// Serialize this private key as an encrypted PEM-encoded PKCS#8 private
Expand All @@ -142,6 +142,6 @@ pub trait EncodePrivateKey {
#[cfg(all(feature = "pem", feature = "std"))]
fn write_pkcs8_pem_file(&self, path: impl AsRef<Path>, line_ending: LineEnding) -> Result<()> {
let doc = self.to_pkcs8_der()?;
Ok(doc.write_pem_file(path, PrivateKeyInfoRef::PEM_LABEL, line_ending)?)
Ok(doc.write_pem_file(path, PrivateKeyInfo::PEM_LABEL, line_ending)?)
}
}
6 changes: 3 additions & 3 deletions pkcs8/tests/encrypted_private_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#![cfg(feature = "pkcs5")]

use hex_literal::hex;
use pkcs8::{pkcs5::pbes2, EncryptedPrivateKeyInfo, PrivateKeyInfoRef};
use pkcs8::{pkcs5::pbes2, EncryptedPrivateKeyInfo, PrivateKeyInfo};

#[cfg(feature = "alloc")]
use der::Encode;
Expand Down Expand Up @@ -168,7 +168,7 @@ fn encrypt_ed25519_der_encpriv_aes256_pbkdf2_sha256() {
)
.unwrap();

let pk_plaintext = PrivateKeyInfoRef::try_from(ED25519_DER_PLAINTEXT_EXAMPLE).unwrap();
let pk_plaintext = PrivateKeyInfo::try_from(ED25519_DER_PLAINTEXT_EXAMPLE).unwrap();
let pk_encrypted = pk_plaintext
.encrypt_with_params(pbes2_params, PASSWORD)
.unwrap();
Expand All @@ -189,7 +189,7 @@ fn encrypt_ed25519_der_encpriv_aes256_scrypt() {
)
.unwrap();

let pk_plaintext = PrivateKeyInfoRef::try_from(ED25519_DER_PLAINTEXT_EXAMPLE).unwrap();
let pk_plaintext = PrivateKeyInfo::try_from(ED25519_DER_PLAINTEXT_EXAMPLE).unwrap();
let pk_encrypted = pk_plaintext
.encrypt_with_params(scrypt_params, PASSWORD)
.unwrap();
Expand Down
Loading

0 comments on commit 4dd6f4a

Please sign in to comment.