From 4dd6f4a1a5af82a23ad51cd4627456275f78491c Mon Sep 17 00:00:00 2001 From: Arthur Gautier Date: Sun, 6 Aug 2023 03:49:22 +0000 Subject: [PATCH] pkcs8: provide `PrivateKeyInfo` type alias with older API Signed-off-by: Arthur Gautier --- pkcs8/src/lib.rs | 2 +- pkcs8/src/private_key_info.rs | 55 +++++++++++++--------------- pkcs8/src/traits.rs | 14 +++---- pkcs8/tests/encrypted_private_key.rs | 6 +-- pkcs8/tests/private_key.rs | 30 +++++++-------- pkcs8/tests/traits.rs | 8 ++-- 6 files changed, 56 insertions(+), 59 deletions(-) diff --git a/pkcs8/src/lib.rs b/pkcs8/src/lib.rs index 6578e3a18..05d969172 100644 --- a/pkcs8/src/lib.rs +++ b/pkcs8/src/lib.rs @@ -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, }; diff --git a/pkcs8/src/private_key_info.rs b/pkcs8/src/private_key_info.rs index 4f41dc5e0..5f47dee66 100644 --- a/pkcs8/src/private_key_info.rs +++ b/pkcs8/src/private_key_info.rs @@ -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 { +pub struct PrivateKeyInfoInner { /// X.509 `AlgorithmIdentifier` for the private key type. pub algorithm: AlgorithmIdentifier, @@ -105,8 +105,8 @@ pub struct PrivateKeyInfo { pub public_key: Option, } -impl PrivateKeyInfo { - /// Create a new PKCS#8 [`PrivateKeyInfo`] message. +impl PrivateKeyInfoInner { + /// 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. @@ -129,7 +129,7 @@ impl PrivateKeyInfo { } } } -impl<'a, Params, Key> PrivateKeyInfo +impl<'a, Params, Key> PrivateKeyInfoInner where Params: der::Choice<'a> + Encode, Key: From<&'a [u8]> + AsRef<[u8]>, @@ -166,7 +166,7 @@ where } } -impl<'a, Params, Key> PrivateKeyInfo +impl<'a, Params, Key> PrivateKeyInfoInner where Params: der::Choice<'a> + Encode, Key: AsRef<[u8]>, @@ -186,15 +186,12 @@ where } } -impl<'a, Params, Key> DecodeValue<'a> for PrivateKeyInfo +impl<'a, Params, Key> DecodeValue<'a> for PrivateKeyInfoInner where Params: der::Choice<'a> + Encode, Key: From<&'a [u8]>, { - fn decode_value>( - reader: &mut R, - header: Header, - ) -> der::Result> { + fn decode_value>(reader: &mut R, header: Header) -> der::Result { reader.read_nested(header.length, |reader| { // Parse and validate `version` INTEGER. let version = Version::decode(reader)?; @@ -235,7 +232,7 @@ where } } -impl<'a, Params, Key> EncodeValue for PrivateKeyInfo +impl<'a, Params, Key> EncodeValue for PrivateKeyInfoInner where Params: der::Choice<'a> + Encode, Key: AsRef<[u8]>, @@ -256,14 +253,14 @@ where } } -impl<'a, Params, Key> Sequence<'a> for PrivateKeyInfo +impl<'a, Params, Key> Sequence<'a> for PrivateKeyInfoInner where Params: der::Choice<'a> + Encode, Key: From<&'a [u8]> + AsRef<[u8]>, { } -impl<'a, Params, Key> TryFrom<&'a [u8]> for PrivateKeyInfo +impl<'a, Params, Key> TryFrom<&'a [u8]> for PrivateKeyInfoInner where Params: der::Choice<'a> + Encode, Key: From<&'a [u8]> + AsRef<[u8]>, @@ -275,13 +272,13 @@ where } } -impl fmt::Debug for PrivateKeyInfo +impl fmt::Debug for PrivateKeyInfoInner 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) @@ -290,38 +287,38 @@ where } #[cfg(feature = "alloc")] -impl<'a, Params, Key> TryFrom> for SecretDocument +impl<'a, Params, Key> TryFrom> for SecretDocument where Params: der::Choice<'a> + Encode, Key: From<&'a [u8]> + AsRef<[u8]>, { type Error = Error; - fn try_from(private_key: PrivateKeyInfo) -> Result { + fn try_from(private_key: PrivateKeyInfoInner) -> Result { SecretDocument::try_from(&private_key) } } #[cfg(feature = "alloc")] -impl<'a, Params, Key> TryFrom<&PrivateKeyInfo> for SecretDocument +impl<'a, Params, Key> TryFrom<&PrivateKeyInfoInner> for SecretDocument where Params: der::Choice<'a> + Encode, Key: From<&'a [u8]> + AsRef<[u8]>, { type Error = Error; - fn try_from(private_key: &PrivateKeyInfo) -> Result { + fn try_from(private_key: &PrivateKeyInfoInner) -> Result { Ok(Self::encode_msg(private_key)?) } } #[cfg(feature = "pem")] -impl PemLabel for PrivateKeyInfo { +impl PemLabel for PrivateKeyInfoInner { const PEM_LABEL: &'static str = "PRIVATE KEY"; } #[cfg(feature = "subtle")] -impl ConstantTimeEq for PrivateKeyInfo +impl ConstantTimeEq for PrivateKeyInfoInner where Params: Eq, Key: PartialEq + AsRef<[u8]>, @@ -337,7 +334,7 @@ where } #[cfg(feature = "subtle")] -impl Eq for PrivateKeyInfo +impl Eq for PrivateKeyInfoInner where Params: Eq, Key: AsRef<[u8]> + Eq, @@ -345,7 +342,7 @@ where } #[cfg(feature = "subtle")] -impl PartialEq for PrivateKeyInfo +impl PartialEq for PrivateKeyInfoInner where Params: Eq, Key: PartialEq + AsRef<[u8]>, @@ -355,19 +352,19 @@ where } } -/// [`PrivateKeyInfo`] with [`AnyRef`] algorithm parameters, and `&[u8]` key. -pub type PrivateKeyInfoRef<'a> = PrivateKeyInfo, &'a [u8]>; +/// [`PrivateKeyInfoInner`] with [`AnyRef`] algorithm parameters, and `&[u8]` key. +pub type PrivateKeyInfo<'a> = PrivateKeyInfoInner, &'a [u8]>; /// [`PrivateKeyInfo`] with [`Any`] algorithm parameters, and `Box<[u8]>` key. #[cfg(feature = "alloc")] -pub type PrivateKeyInfoOwned = PrivateKeyInfo>; +pub type PrivateKeyInfoOwned = PrivateKeyInfoInner>; #[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 { @@ -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(), diff --git a/pkcs8/src/traits.rs b/pkcs8/src/traits.rs index 312b2dbd8..f6165f696 100644 --- a/pkcs8/src/traits.rs +++ b/pkcs8/src/traits.rs @@ -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; @@ -49,7 +49,7 @@ pub trait DecodePrivateKey: Sized { fn from_pkcs8_pem(s: &str) -> Result { // 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()) @@ -81,17 +81,17 @@ pub trait DecodePrivateKey: Sized { #[cfg(all(feature = "pem", feature = "std"))] fn read_pkcs8_pem_file(path: impl AsRef) -> Result { 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 DecodePrivateKey for T where - T: for<'a> TryFrom, Error = Error>, + T: for<'a> TryFrom, Error = Error>, { fn from_pkcs8_der(bytes: &[u8]) -> Result { - Self::try_from(PrivateKeyInfoRef::try_from(bytes)?) + Self::try_from(PrivateKeyInfo::try_from(bytes)?) } } @@ -116,7 +116,7 @@ pub trait EncodePrivateKey { #[cfg(feature = "pem")] fn to_pkcs8_pem(&self, line_ending: LineEnding) -> Result> { 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 @@ -142,6 +142,6 @@ pub trait EncodePrivateKey { #[cfg(all(feature = "pem", feature = "std"))] fn write_pkcs8_pem_file(&self, path: impl AsRef, 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)?) } } diff --git a/pkcs8/tests/encrypted_private_key.rs b/pkcs8/tests/encrypted_private_key.rs index ea8dbda03..dbe0a18e7 100644 --- a/pkcs8/tests/encrypted_private_key.rs +++ b/pkcs8/tests/encrypted_private_key.rs @@ -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; @@ -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(); @@ -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(); diff --git a/pkcs8/tests/private_key.rs b/pkcs8/tests/private_key.rs index b71f06d48..22b8e4bec 100644 --- a/pkcs8/tests/private_key.rs +++ b/pkcs8/tests/private_key.rs @@ -2,7 +2,7 @@ use der::asn1::ObjectIdentifier; use hex_literal::hex; -use pkcs8::{PrivateKeyInfoRef, Version}; +use pkcs8::{PrivateKeyInfo, Version}; #[cfg(feature = "alloc")] use der::Encode; @@ -43,7 +43,7 @@ const X25519_PEM_EXAMPLE: &str = include_str!("examples/x25519-priv.pem"); #[test] fn decode_ec_p256_der() { - let pk = PrivateKeyInfoRef::try_from(EC_P256_DER_EXAMPLE).unwrap(); + let pk = PrivateKeyInfo::try_from(EC_P256_DER_EXAMPLE).unwrap(); assert_eq!(pk.version(), Version::V1); assert_eq!(pk.algorithm.oid, "1.2.840.10045.2.1".parse().unwrap()); @@ -66,7 +66,7 @@ fn decode_ec_p256_der() { // https://datatracker.ietf.org/doc/html/rfc8410#section-10.3 #[test] fn decode_ed25519_der_v1() { - let pk = PrivateKeyInfoRef::try_from(ED25519_DER_V1_EXAMPLE).unwrap(); + let pk = PrivateKeyInfo::try_from(ED25519_DER_V1_EXAMPLE).unwrap(); assert_eq!(pk.version(), Version::V1); assert_eq!(pk.algorithm.oid, "1.3.101.112".parse().unwrap()); assert_eq!(pk.algorithm.parameters, None); @@ -90,7 +90,7 @@ fn decode_ed25519_der_v2() { const PUB_KEY: [u8; 32] = hex!("19BF44096984CDFE8541BAC167DC3B96C85086AA30B6B6CB0C5C38AD703166E1"); - let pk = PrivateKeyInfoRef::try_from(ED25519_DER_V2_EXAMPLE).unwrap(); + let pk = PrivateKeyInfo::try_from(ED25519_DER_V2_EXAMPLE).unwrap(); assert_eq!(pk.version(), Version::V2); assert_eq!(pk.algorithm.oid, "1.3.101.112".parse().unwrap()); assert_eq!(pk.algorithm.parameters, None); @@ -103,7 +103,7 @@ fn decode_ed25519_der_v2() { #[test] fn decode_rsa_2048_der() { - let pk = PrivateKeyInfoRef::try_from(RSA_2048_DER_EXAMPLE).unwrap(); + let pk = PrivateKeyInfo::try_from(RSA_2048_DER_EXAMPLE).unwrap(); assert_eq!(pk.version(), Version::V1); assert_eq!(pk.algorithm.oid, "1.2.840.113549.1.1.1".parse().unwrap()); assert!(pk.algorithm.parameters.unwrap().is_null()); @@ -115,7 +115,7 @@ fn decode_rsa_2048_der() { #[test] fn decode_x25519_der() { - let pk = PrivateKeyInfoRef::try_from(X25519_DER_EXAMPLE).unwrap(); + let pk = PrivateKeyInfo::try_from(X25519_DER_EXAMPLE).unwrap(); assert_eq!(pk.version(), Version::V1); assert_eq!(pk.algorithm.oid, "1.3.101.110".parse().unwrap()); assert_eq!(pk.algorithm.parameters, None); @@ -131,7 +131,7 @@ fn decode_x25519_der() { #[test] #[cfg(feature = "alloc")] fn encode_ec_p256_der() { - let pk = PrivateKeyInfoRef::try_from(EC_P256_DER_EXAMPLE).unwrap(); + let pk = PrivateKeyInfo::try_from(EC_P256_DER_EXAMPLE).unwrap(); let pk_encoded = pk.to_der().unwrap(); assert_eq!(EC_P256_DER_EXAMPLE, pk_encoded); } @@ -139,52 +139,52 @@ fn encode_ec_p256_der() { #[test] #[cfg(feature = "alloc")] fn encode_ed25519_der_v1() { - let pk = PrivateKeyInfoRef::try_from(ED25519_DER_V1_EXAMPLE).unwrap(); + let pk = PrivateKeyInfo::try_from(ED25519_DER_V1_EXAMPLE).unwrap(); assert_eq!(ED25519_DER_V1_EXAMPLE, pk.to_der().unwrap()); } #[test] #[cfg(all(feature = "alloc", feature = "subtle"))] fn encode_ed25519_der_v2() { - let private_key = PrivateKeyInfoRef::try_from(ED25519_DER_V2_EXAMPLE).unwrap(); + let private_key = PrivateKeyInfo::try_from(ED25519_DER_V2_EXAMPLE).unwrap(); let private_der = private_key.to_der().unwrap(); assert_eq!( private_key, - PrivateKeyInfoRef::try_from(private_der.as_ref()).unwrap() + PrivateKeyInfo::try_from(private_der.as_ref()).unwrap() ); } #[test] #[cfg(feature = "alloc")] fn encode_rsa_2048_der() { - let pk = PrivateKeyInfoRef::try_from(RSA_2048_DER_EXAMPLE).unwrap(); + let pk = PrivateKeyInfo::try_from(RSA_2048_DER_EXAMPLE).unwrap(); assert_eq!(RSA_2048_DER_EXAMPLE, &pk.to_der().unwrap()); } #[test] #[cfg(feature = "pem")] fn encode_ec_p256_pem() { - let pk = PrivateKeyInfoRef::try_from(EC_P256_DER_EXAMPLE).unwrap(); + let pk = PrivateKeyInfo::try_from(EC_P256_DER_EXAMPLE).unwrap(); assert_eq!(EC_P256_PEM_EXAMPLE, pk.to_pem(LineEnding::LF).unwrap()); } #[test] #[cfg(feature = "pem")] fn encode_ed25519_pem() { - let pk = PrivateKeyInfoRef::try_from(ED25519_DER_V1_EXAMPLE).unwrap(); + let pk = PrivateKeyInfo::try_from(ED25519_DER_V1_EXAMPLE).unwrap(); assert_eq!(ED25519_PEM_V1_EXAMPLE, pk.to_pem(LineEnding::LF).unwrap()); } #[test] #[cfg(feature = "pem")] fn encode_rsa_2048_pem() { - let pk = PrivateKeyInfoRef::try_from(RSA_2048_DER_EXAMPLE).unwrap(); + let pk = PrivateKeyInfo::try_from(RSA_2048_DER_EXAMPLE).unwrap(); assert_eq!(RSA_2048_PEM_EXAMPLE, pk.to_pem(LineEnding::LF).unwrap()); } #[test] #[cfg(feature = "pem")] fn encode_x25519_pem() { - let pk = PrivateKeyInfoRef::try_from(X25519_DER_EXAMPLE).unwrap(); + let pk = PrivateKeyInfo::try_from(X25519_DER_EXAMPLE).unwrap(); assert_eq!(X25519_PEM_EXAMPLE, pk.to_pem(LineEnding::LF).unwrap()); } diff --git a/pkcs8/tests/traits.rs b/pkcs8/tests/traits.rs index c9e63d124..4a603bb94 100644 --- a/pkcs8/tests/traits.rs +++ b/pkcs8/tests/traits.rs @@ -3,7 +3,7 @@ #![cfg(any(feature = "pem", feature = "std"))] use der::Encode; -use pkcs8::{DecodePrivateKey, EncodePrivateKey, Error, PrivateKeyInfoRef, Result, SecretDocument}; +use pkcs8::{DecodePrivateKey, EncodePrivateKey, Error, PrivateKeyInfo, Result, SecretDocument}; #[cfg(feature = "pem")] use pkcs8::der::pem::LineEnding; @@ -14,7 +14,7 @@ use tempfile::tempdir; #[cfg(all(feature = "pem", feature = "std"))] use std::fs; -/// Ed25519 `PrivateKeyInfoRef` encoded as ASN.1 DER +/// Ed25519 `PrivateKeyInfo` encoded as ASN.1 DER const ED25519_DER_EXAMPLE: &[u8] = include_bytes!("examples/ed25519-priv-pkcs8v1.der"); /// Ed25519 private key encoded as PEM @@ -36,10 +36,10 @@ impl EncodePrivateKey for MockKey { } } -impl TryFrom> for MockKey { +impl TryFrom> for MockKey { type Error = Error; - fn try_from(pkcs8: PrivateKeyInfoRef<'_>) -> Result { + fn try_from(pkcs8: PrivateKeyInfo<'_>) -> Result { Ok(MockKey(pkcs8.to_der()?)) } }