diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-18 02:49:50 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-18 02:49:50 +0000 |
commit | 9835e2ae736235810b4ea1c162ca5e65c547e770 (patch) | |
tree | 3fcebf40ed70e581d776a8a4c65923e8ec20e026 /vendor/pkcs8/src/private_key_info.rs | |
parent | Releasing progress-linux version 1.70.0+dfsg2-1~progress7.99u1. (diff) | |
download | rustc-9835e2ae736235810b4ea1c162ca5e65c547e770.tar.xz rustc-9835e2ae736235810b4ea1c162ca5e65c547e770.zip |
Merging upstream version 1.71.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/pkcs8/src/private_key_info.rs')
-rw-r--r-- | vendor/pkcs8/src/private_key_info.rs | 74 |
1 files changed, 38 insertions, 36 deletions
diff --git a/vendor/pkcs8/src/private_key_info.rs b/vendor/pkcs8/src/private_key_info.rs index 52f0878d7..ecae624df 100644 --- a/vendor/pkcs8/src/private_key_info.rs +++ b/vendor/pkcs8/src/private_key_info.rs @@ -1,10 +1,11 @@ //! PKCS#8 `PrivateKeyInfo`. -use crate::{AlgorithmIdentifier, Error, Result, Version}; +use crate::{AlgorithmIdentifierRef, Error, Result, Version}; use core::fmt; use der::{ asn1::{AnyRef, BitStringRef, ContextSpecific, OctetStringRef}, - Decode, DecodeValue, Encode, Header, Reader, Sequence, TagMode, TagNumber, + Decode, DecodeValue, Encode, EncodeValue, Header, Length, Reader, Sequence, TagMode, TagNumber, + Writer, }; #[cfg(feature = "alloc")] @@ -29,7 +30,7 @@ const PUBLIC_KEY_TAG: TagNumber = TagNumber::N1; /// PKCS#8 `PrivateKeyInfo`. /// -/// ASN.1 structure containing an [`AlgorithmIdentifier`], private key +/// ASN.1 structure containing an `AlgorithmIdentifier`, private key /// data in an algorithm specific format, and optional attributes /// (ignored by this implementation). /// @@ -90,8 +91,8 @@ const PUBLIC_KEY_TAG: TagNumber = TagNumber::N1; /// [RFC 5958 Section 2]: https://datatracker.ietf.org/doc/html/rfc5958#section-2 #[derive(Clone)] pub struct PrivateKeyInfo<'a> { - /// X.509 [`AlgorithmIdentifier`] for the private key type. - pub algorithm: AlgorithmIdentifier<'a>, + /// X.509 `AlgorithmIdentifier` for the private key type. + pub algorithm: AlgorithmIdentifierRef<'a>, /// Private key data. pub private_key: &'a [u8], @@ -105,7 +106,7 @@ impl<'a> PrivateKeyInfo<'a> { /// /// This is a helper method which initializes `attributes` and `public_key` /// to `None`, helpful if you aren't using those. - pub fn new(algorithm: AlgorithmIdentifier<'a>, private_key: &'a [u8]) -> Self { + pub fn new(algorithm: AlgorithmIdentifierRef<'a>, private_key: &'a [u8]) -> Self { Self { algorithm, private_key, @@ -134,28 +135,39 @@ impl<'a> PrivateKeyInfo<'a> { /// - p: 1 /// - Cipher: AES-256-CBC (best available option for PKCS#5 encryption) #[cfg(feature = "encryption")] - #[cfg_attr(docsrs, doc(cfg(feature = "encryption")))] pub fn encrypt( &self, rng: impl CryptoRng + RngCore, password: impl AsRef<[u8]>, ) -> Result<SecretDocument> { - let der = Zeroizing::new(self.to_vec()?); + let der = Zeroizing::new(self.to_der()?); EncryptedPrivateKeyInfo::encrypt(rng, password, der.as_ref()) } /// Encrypt this private key using a symmetric encryption key derived /// from the provided password and [`pbes2::Parameters`]. #[cfg(feature = "encryption")] - #[cfg_attr(docsrs, doc(cfg(feature = "encryption")))] pub fn encrypt_with_params( &self, pbes2_params: pbes2::Parameters<'_>, password: impl AsRef<[u8]>, ) -> Result<SecretDocument> { - let der = Zeroizing::new(self.to_vec()?); + let der = Zeroizing::new(self.to_der()?); EncryptedPrivateKeyInfo::encrypt_with(pbes2_params, password, der.as_ref()) } + + /// Get a `BIT STRING` representation of the public key, if present. + fn public_key_bit_string(&self) -> der::Result<Option<ContextSpecific<BitStringRef<'a>>>> { + self.public_key + .map(|pk| { + BitStringRef::from_bytes(pk).map(|value| ContextSpecific { + tag_number: PUBLIC_KEY_TAG, + tag_mode: TagMode::Implicit, + value, + }) + }) + .transpose() + } } impl<'a> DecodeValue<'a> for PrivateKeyInfo<'a> { @@ -201,29 +213,25 @@ impl<'a> DecodeValue<'a> for PrivateKeyInfo<'a> { } } -impl<'a> Sequence<'a> for PrivateKeyInfo<'a> { - fn fields<F, T>(&self, f: F) -> der::Result<T> - where - F: FnOnce(&[&dyn Encode]) -> der::Result<T>, - { - f(&[ - &u8::from(self.version()), - &self.algorithm, - &OctetStringRef::new(self.private_key)?, - &self - .public_key - .map(|pk| { - BitStringRef::from_bytes(pk).map(|value| ContextSpecific { - tag_number: PUBLIC_KEY_TAG, - tag_mode: TagMode::Implicit, - value, - }) - }) - .transpose()?, - ]) +impl EncodeValue for PrivateKeyInfo<'_> { + fn value_len(&self) -> der::Result<Length> { + self.version().encoded_len()? + + self.algorithm.encoded_len()? + + OctetStringRef::new(self.private_key)?.encoded_len()? + + self.public_key_bit_string()?.encoded_len()? + } + + fn encode_value(&self, writer: &mut impl Writer) -> der::Result<()> { + self.version().encode(writer)?; + self.algorithm.encode(writer)?; + OctetStringRef::new(self.private_key)?.encode(writer)?; + self.public_key_bit_string()?.encode(writer)?; + Ok(()) } } +impl<'a> Sequence<'a> for PrivateKeyInfo<'a> {} + impl<'a> TryFrom<&'a [u8]> for PrivateKeyInfo<'a> { type Error = Error; @@ -243,7 +251,6 @@ impl<'a> fmt::Debug for PrivateKeyInfo<'a> { } #[cfg(feature = "alloc")] -#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))] impl TryFrom<PrivateKeyInfo<'_>> for SecretDocument { type Error = Error; @@ -253,7 +260,6 @@ impl TryFrom<PrivateKeyInfo<'_>> for SecretDocument { } #[cfg(feature = "alloc")] -#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))] impl TryFrom<&PrivateKeyInfo<'_>> for SecretDocument { type Error = Error; @@ -263,13 +269,11 @@ impl TryFrom<&PrivateKeyInfo<'_>> for SecretDocument { } #[cfg(feature = "pem")] -#[cfg_attr(docsrs, doc(cfg(feature = "pem")))] impl PemLabel for PrivateKeyInfo<'_> { const PEM_LABEL: &'static str = "PRIVATE KEY"; } #[cfg(feature = "subtle")] -#[cfg_attr(docsrs, doc(cfg(feature = "subtle")))] impl<'a> ConstantTimeEq for PrivateKeyInfo<'a> { fn ct_eq(&self, other: &Self) -> Choice { // NOTE: public fields are not compared in constant time @@ -281,11 +285,9 @@ impl<'a> ConstantTimeEq for PrivateKeyInfo<'a> { } #[cfg(feature = "subtle")] -#[cfg_attr(docsrs, doc(cfg(feature = "subtle")))] impl<'a> Eq for PrivateKeyInfo<'a> {} #[cfg(feature = "subtle")] -#[cfg_attr(docsrs, doc(cfg(feature = "subtle")))] impl<'a> PartialEq for PrivateKeyInfo<'a> { fn eq(&self, other: &Self) -> bool { self.ct_eq(other).into() |