aboutsummaryrefslogtreecommitdiff
path: root/src/private_key.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/private_key.rs')
-rw-r--r--src/private_key.rs93
1 files changed, 33 insertions, 60 deletions
diff --git a/src/private_key.rs b/src/private_key.rs
index b913c47..043ed02 100644
--- a/src/private_key.rs
+++ b/src/private_key.rs
@@ -5,10 +5,7 @@ pub(crate) mod other_prime_info;
use crate::{Error, Result, RsaPublicKey, Version};
use core::fmt;
-use der::{
- asn1::UintRef, Decode, DecodeValue, Encode, EncodeValue, Header, Length, Reader, Sequence, Tag,
- Writer,
-};
+use der::{asn1::UIntRef, Decode, DecodeValue, Encode, Header, Reader, Sequence, Tag};
#[cfg(feature = "alloc")]
use {self::other_prime_info::OtherPrimeInfo, alloc::vec::Vec, der::SecretDocument};
@@ -42,28 +39,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`).
@@ -119,37 +116,27 @@ impl<'a> DecodeValue<'a> for RsaPrivateKey<'a> {
}
}
-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> {
+ 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<'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()
@@ -181,6 +168,7 @@ impl fmt::Debug for RsaPrivateKey<'_> {
}
#[cfg(feature = "alloc")]
+#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl TryFrom<RsaPrivateKey<'_>> for SecretDocument {
type Error = Error;
@@ -190,6 +178,7 @@ impl TryFrom<RsaPrivateKey<'_>> for SecretDocument {
}
#[cfg(feature = "alloc")]
+#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl TryFrom<&RsaPrivateKey<'_>> for SecretDocument {
type Error = Error;
@@ -199,13 +188,12 @@ 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]
@@ -214,30 +202,15 @@ pub struct OtherPrimeInfos<'a> {
}
#[cfg(not(feature = "alloc"))]
-impl<'a> DecodeValue<'a> for OtherPrimeInfos<'a> {
- fn decode_value<R: Reader<'a>>(reader: &mut R, _header: Header) -> der::Result<Self> {
+impl<'a> Decode<'a> for OtherPrimeInfos<'a> {
+ fn decode<R: Reader<'a>>(reader: &mut R) -> der::Result<Self> {
// Placeholder decoder that always returns an error.
- // Uses `Tag::Integer` to signal an unsupported version.
+ // Use `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;
}