diff options
Diffstat (limited to 'src/private_key.rs')
-rw-r--r-- | src/private_key.rs | 93 |
1 files changed, 60 insertions, 33 deletions
diff --git a/src/private_key.rs b/src/private_key.rs index 043ed02..b913c47 100644 --- a/src/private_key.rs +++ b/src/private_key.rs @@ -5,7 +5,10 @@ pub(crate) mod other_prime_info; use crate::{Error, Result, RsaPublicKey, Version}; use core::fmt; -use der::{asn1::UIntRef, Decode, DecodeValue, Encode, Header, Reader, Sequence, Tag}; +use der::{ + asn1::UintRef, Decode, DecodeValue, Encode, EncodeValue, Header, Length, Reader, Sequence, Tag, + Writer, +}; #[cfg(feature = "alloc")] use {self::other_prime_info::OtherPrimeInfo, alloc::vec::Vec, der::SecretDocument}; @@ -39,28 +42,28 @@ use der::pem::PemLabel; #[derive(Clone)] pub struct RsaPrivateKey<'a> { /// `n`: RSA modulus. - pub modulus: UIntRef<'a>, + pub modulus: UintRef<'a>, /// `e`: RSA public exponent. - pub public_exponent: UIntRef<'a>, + pub public_exponent: UintRef<'a>, /// `d`: RSA private exponent. - pub private_exponent: UIntRef<'a>, + pub private_exponent: UintRef<'a>, /// `p`: first prime factor of `n`. - pub prime1: UIntRef<'a>, + pub prime1: UintRef<'a>, /// `q`: Second prime factor of `n`. - pub prime2: UIntRef<'a>, + pub prime2: UintRef<'a>, /// First exponent: `d mod (p-1)`. - pub exponent1: UIntRef<'a>, + pub exponent1: UintRef<'a>, /// Second exponent: `d mod (q-1)`. - pub exponent2: UIntRef<'a>, + pub exponent2: UintRef<'a>, /// CRT coefficient: `(inverse of q) mod p`. - pub coefficient: UIntRef<'a>, + pub coefficient: UintRef<'a>, /// Additional primes `r_3`, ..., `r_u`, in order, if this is a multi-prime /// RSA key (i.e. `version` is `multi`). @@ -116,27 +119,37 @@ impl<'a> DecodeValue<'a> for RsaPrivateKey<'a> { } } -impl<'a> Sequence<'a> for RsaPrivateKey<'a> { - fn fields<F, T>(&self, f: F) -> der::Result<T> - where - F: FnOnce(&[&dyn Encode]) -> der::Result<T>, - { - f(&[ - &self.version(), - &self.modulus, - &self.public_exponent, - &self.private_exponent, - &self.prime1, - &self.prime2, - &self.exponent1, - &self.exponent2, - &self.coefficient, - #[cfg(feature = "alloc")] - &self.other_prime_infos, - ]) +impl EncodeValue for RsaPrivateKey<'_> { + fn value_len(&self) -> der::Result<Length> { + self.version().encoded_len()? + + self.modulus.encoded_len()? + + self.public_exponent.encoded_len()? + + self.private_exponent.encoded_len()? + + self.prime1.encoded_len()? + + self.prime2.encoded_len()? + + self.exponent1.encoded_len()? + + self.exponent2.encoded_len()? + + self.coefficient.encoded_len()? + + self.other_prime_infos.encoded_len()? + } + + fn encode_value(&self, writer: &mut impl Writer) -> der::Result<()> { + self.version().encode(writer)?; + self.modulus.encode(writer)?; + self.public_exponent.encode(writer)?; + self.private_exponent.encode(writer)?; + self.prime1.encode(writer)?; + self.prime2.encode(writer)?; + self.exponent1.encode(writer)?; + self.exponent2.encode(writer)?; + self.coefficient.encode(writer)?; + self.other_prime_infos.encode(writer)?; + Ok(()) } } +impl<'a> Sequence<'a> for RsaPrivateKey<'a> {} + impl<'a> From<RsaPrivateKey<'a>> for RsaPublicKey<'a> { fn from(private_key: RsaPrivateKey<'a>) -> RsaPublicKey<'a> { private_key.public_key() @@ -168,7 +181,6 @@ impl fmt::Debug for RsaPrivateKey<'_> { } #[cfg(feature = "alloc")] -#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))] impl TryFrom<RsaPrivateKey<'_>> for SecretDocument { type Error = Error; @@ -178,7 +190,6 @@ impl TryFrom<RsaPrivateKey<'_>> for SecretDocument { } #[cfg(feature = "alloc")] -#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))] impl TryFrom<&RsaPrivateKey<'_>> for SecretDocument { type Error = Error; @@ -188,12 +199,13 @@ impl TryFrom<&RsaPrivateKey<'_>> for SecretDocument { } #[cfg(feature = "pem")] -#[cfg_attr(docsrs, doc(cfg(feature = "pem")))] impl PemLabel for RsaPrivateKey<'_> { const PEM_LABEL: &'static str = "RSA PRIVATE KEY"; } /// Placeholder struct for `OtherPrimeInfos` in the no-`alloc` case. +/// +/// This type is unconstructable by design, but supports the same traits. #[cfg(not(feature = "alloc"))] #[derive(Clone)] #[non_exhaustive] @@ -202,15 +214,30 @@ pub struct OtherPrimeInfos<'a> { } #[cfg(not(feature = "alloc"))] -impl<'a> Decode<'a> for OtherPrimeInfos<'a> { - fn decode<R: Reader<'a>>(reader: &mut R) -> der::Result<Self> { +impl<'a> DecodeValue<'a> for OtherPrimeInfos<'a> { + fn decode_value<R: Reader<'a>>(reader: &mut R, _header: Header) -> der::Result<Self> { // Placeholder decoder that always returns an error. - // Use `Tag::Integer` to signal an unsupported version. + // Uses `Tag::Integer` to signal an unsupported version. Err(reader.error(der::ErrorKind::Value { tag: Tag::Integer })) } } #[cfg(not(feature = "alloc"))] +impl EncodeValue for OtherPrimeInfos<'_> { + fn value_len(&self) -> der::Result<Length> { + // Placeholder decoder that always returns an error. + // Uses `Tag::Integer` to signal an unsupported version. + Err(der::ErrorKind::Value { tag: Tag::Integer }.into()) + } + + fn encode_value(&self, _writer: &mut impl Writer) -> der::Result<()> { + // Placeholder decoder that always returns an error. + // Uses `Tag::Integer` to signal an unsupported version. + Err(der::ErrorKind::Value { tag: Tag::Integer }.into()) + } +} + +#[cfg(not(feature = "alloc"))] impl<'a> der::FixedTag for OtherPrimeInfos<'a> { const TAG: Tag = Tag::Sequence; } |