Skip to content

Commit 49c5bc2

Browse files
committed
Derive Sequence for pkcs1 types
This reduces the amount of custom code that is written. Signed-off-by: Nathaniel McCallum <nathaniel@profian.com>
1 parent bd86224 commit 49c5bc2

2 files changed

Lines changed: 4 additions & 45 deletions

File tree

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! PKCS#1 OtherPrimeInfo support.
22
3-
use der::{asn1::UIntBytes, Decodable, Decoder, Encodable, Sequence};
3+
use der::{asn1::UIntBytes, Sequence};
44

55
/// PKCS#1 OtherPrimeInfo as defined in [RFC 8017 Appendix 1.2].
66
///
@@ -15,7 +15,7 @@ use der::{asn1::UIntBytes, Decodable, Decoder, Encodable, Sequence};
1515
/// ```
1616
///
1717
/// [RFC 8017 Appendix 1.2]: https://datatracker.ietf.org/doc/html/rfc8017#appendix-A.1.2
18-
#[derive(Clone)]
18+
#[derive(Clone, Sequence)]
1919
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
2020
pub struct OtherPrimeInfo<'a> {
2121
/// Prime factor `r_i` of `n`, where `i` >= 3.
@@ -27,24 +27,3 @@ pub struct OtherPrimeInfo<'a> {
2727
/// CRT coefficient: `t_i = (r_1 * r_2 * ... * r_(i-1))^(-1) mod r_i`.
2828
pub coefficient: UIntBytes<'a>,
2929
}
30-
31-
impl<'a> Decodable<'a> for OtherPrimeInfo<'a> {
32-
fn decode(decoder: &mut Decoder<'a>) -> der::Result<Self> {
33-
decoder.sequence(|decoder| {
34-
Ok(Self {
35-
prime: decoder.decode()?,
36-
exponent: decoder.decode()?,
37-
coefficient: decoder.decode()?,
38-
})
39-
})
40-
}
41-
}
42-
43-
impl<'a> Sequence<'a> for OtherPrimeInfo<'a> {
44-
fn fields<F, T>(&self, f: F) -> der::Result<T>
45-
where
46-
F: FnOnce(&[&dyn Encodable]) -> der::Result<T>,
47-
{
48-
f(&[&self.prime, &self.exponent, &self.coefficient])
49-
}
50-
}

pkcs1/src/public_key.rs

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
pub(crate) mod document;
55

66
use crate::{Error, Result};
7-
use der::{asn1::UIntBytes, Decodable, Decoder, Encodable, Sequence};
7+
use der::{asn1::UIntBytes, Decodable, Sequence};
88

99
#[cfg(feature = "alloc")]
1010
use crate::RsaPublicKeyDocument;
@@ -24,7 +24,7 @@ use {crate::LineEnding, alloc::string::String, der::Document};
2424
/// ```
2525
///
2626
/// [RFC 8017 Appendix 1.1]: https://datatracker.ietf.org/doc/html/rfc8017#appendix-A.1.1
27-
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
27+
#[derive(Copy, Clone, Debug, Eq, PartialEq, Sequence)]
2828
pub struct RsaPublicKey<'a> {
2929
/// `n`: RSA modulus
3030
pub modulus: UIntBytes<'a>,
@@ -50,26 +50,6 @@ impl<'a> RsaPublicKey<'a> {
5050
}
5151
}
5252

53-
impl<'a> Decodable<'a> for RsaPublicKey<'a> {
54-
fn decode(decoder: &mut Decoder<'a>) -> der::Result<Self> {
55-
decoder.sequence(|decoder| {
56-
Ok(Self {
57-
modulus: decoder.decode()?,
58-
public_exponent: decoder.decode()?,
59-
})
60-
})
61-
}
62-
}
63-
64-
impl<'a> Sequence<'a> for RsaPublicKey<'a> {
65-
fn fields<F, T>(&self, f: F) -> der::Result<T>
66-
where
67-
F: FnOnce(&[&dyn Encodable]) -> der::Result<T>,
68-
{
69-
f(&[&self.modulus, &self.public_exponent])
70-
}
71-
}
72-
7353
impl<'a> TryFrom<&'a [u8]> for RsaPublicKey<'a> {
7454
type Error = Error;
7555

0 commit comments

Comments
 (0)