x509_certificate/
rfc3447.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
5//! ASN.1 types defined in RFC 3447.
6
7use {
8    crate::rfc5280::AlgorithmIdentifier,
9    bcder::{
10        decode::{Constructed, DecodeError, Source},
11        encode::{self, PrimitiveContent, Values},
12        Mode, OctetString, Unsigned,
13    },
14    std::{
15        io::Write,
16        ops::{Deref, DerefMut},
17    },
18};
19
20/// Digest information.
21///
22/// ```asn.1
23/// DigestInfo ::= SEQUENCE {
24///     digestAlgorithm DigestAlgorithm,
25///     digest OCTET STRING
26/// }
27///
28/// DigestAlgorithm ::=
29///     AlgorithmIdentifier { {PKCS1-v1-5DigestAlgorithms} }
30/// ```
31#[derive(Clone)]
32pub struct DigestInfo {
33    pub algorithm: AlgorithmIdentifier,
34    pub digest: OctetString,
35}
36
37impl DigestInfo {
38    pub fn take_from<S: Source>(cons: &mut Constructed<S>) -> Result<Self, DecodeError<S::Error>> {
39        cons.take_sequence(|cons| {
40            let algorithm = AlgorithmIdentifier::take_from(cons)?;
41            let digest = OctetString::take_from(cons)?;
42
43            Ok(Self { algorithm, digest })
44        })
45    }
46
47    pub fn encode_ref(&self) -> impl Values + '_ {
48        encode::sequence((&self.algorithm, self.digest.encode_ref()))
49    }
50}
51
52impl Values for DigestInfo {
53    fn encoded_len(&self, mode: Mode) -> usize {
54        self.encode_ref().encoded_len(mode)
55    }
56
57    fn write_encoded<W: Write>(&self, mode: Mode, target: &mut W) -> Result<(), std::io::Error> {
58        self.encode_ref().write_encoded(mode, target)
59    }
60}
61
62/// Other prime info
63///
64/// ```asn.1
65/// OtherPrimeInfo ::= SEQUENCE {
66///     prime             INTEGER,  -- ri
67///     exponent          INTEGER,  -- di
68///     coefficient       INTEGER   -- ti
69/// }
70/// ```
71#[derive(Clone, Debug)]
72pub struct OtherPrimeInfo {
73    pub ri: Unsigned,
74    pub di: Unsigned,
75    pub ti: Unsigned,
76}
77
78impl OtherPrimeInfo {
79    pub fn take_opt_from<S: Source>(
80        cons: &mut Constructed<S>,
81    ) -> Result<Option<Self>, DecodeError<S::Error>> {
82        cons.take_opt_sequence(|cons| Self::from_sequence(cons))
83    }
84
85    pub fn take_from<S: Source>(cons: &mut Constructed<S>) -> Result<Self, DecodeError<S::Error>> {
86        cons.take_sequence(|cons| Self::from_sequence(cons))
87    }
88
89    fn from_sequence<S: Source>(cons: &mut Constructed<S>) -> Result<Self, DecodeError<S::Error>> {
90        let ri = Unsigned::take_from(cons)?;
91        let di = Unsigned::take_from(cons)?;
92        let ti = Unsigned::take_from(cons)?;
93
94        Ok(Self { ri, di, ti })
95    }
96
97    pub fn encode_ref(&self) -> impl Values + '_ {
98        encode::sequence((self.ri.encode(), self.di.encode(), self.ti.encode()))
99    }
100}
101
102impl Values for OtherPrimeInfo {
103    fn encoded_len(&self, mode: Mode) -> usize {
104        self.encode_ref().encoded_len(mode)
105    }
106
107    fn write_encoded<W: Write>(&self, mode: Mode, target: &mut W) -> Result<(), std::io::Error> {
108        self.encode_ref().write_encoded(mode, target)
109    }
110}
111
112/// ```asn.1
113/// OtherPrimeInfos ::= SEQUENCE SIZE(1..MAX) OF OtherPrimeInfo
114/// ```
115#[derive(Clone, Debug)]
116pub struct OtherPrimeInfos(Vec<OtherPrimeInfo>);
117
118impl Deref for OtherPrimeInfos {
119    type Target = Vec<OtherPrimeInfo>;
120
121    fn deref(&self) -> &Self::Target {
122        &self.0
123    }
124}
125
126impl DerefMut for OtherPrimeInfos {
127    fn deref_mut(&mut self) -> &mut Self::Target {
128        &mut self.0
129    }
130}
131
132impl OtherPrimeInfos {
133    pub fn take_opt_from<S: Source>(
134        cons: &mut Constructed<S>,
135    ) -> Result<Option<Self>, DecodeError<S::Error>> {
136        cons.take_opt_sequence(|cons| Self::from_sequence(cons))
137    }
138
139    pub fn take_from<S: Source>(cons: &mut Constructed<S>) -> Result<Self, DecodeError<S::Error>> {
140        cons.take_sequence(|cons| Self::from_sequence(cons))
141    }
142
143    pub fn from_sequence<S: Source>(
144        cons: &mut Constructed<S>,
145    ) -> Result<Self, DecodeError<S::Error>> {
146        let mut vals = Vec::new();
147
148        while let Some(info) = OtherPrimeInfo::take_opt_from(cons)? {
149            vals.push(info);
150        }
151
152        Ok(Self(vals))
153    }
154
155    pub fn encode_ref(&self) -> impl Values + '_ {
156        encode::sequence(&self.0)
157    }
158}
159
160impl Values for OtherPrimeInfos {
161    fn encoded_len(&self, mode: Mode) -> usize {
162        self.encode_ref().encoded_len(mode)
163    }
164
165    fn write_encoded<W: Write>(&self, mode: Mode, target: &mut W) -> Result<(), std::io::Error> {
166        self.encode_ref().write_encoded(mode, target)
167    }
168}
169
170/// An RSA private key.
171///
172/// ```ASN.1
173/// RSAPrivateKey ::= SEQUENCE {
174///     version           Version,
175///     modulus           INTEGER,  -- n
176///     publicExponent    INTEGER,  -- e
177///     privateExponent   INTEGER,  -- d
178///     prime1            INTEGER,  -- p
179///     prime2            INTEGER,  -- q
180///     exponent1         INTEGER,  -- d mod (p-1)
181///     exponent2         INTEGER,  -- d mod (q-1)
182///     coefficient       INTEGER,  -- (inverse of q) mod p
183///     otherPrimeInfos   OtherPrimeInfos OPTIONAL
184/// }
185/// ```
186#[derive(Clone, Debug)]
187pub struct RsaPrivateKey {
188    pub version: Unsigned,
189    pub n: Unsigned,
190    pub e: Unsigned,
191    pub d: Unsigned,
192    pub p: Unsigned,
193    pub q: Unsigned,
194    pub dp: Unsigned,
195    pub dq: Unsigned,
196    pub q_inv: Unsigned,
197    pub other_prime_infos: Option<OtherPrimeInfos>,
198}
199
200impl RsaPrivateKey {
201    pub fn take_from<S: Source>(cons: &mut Constructed<S>) -> Result<Self, DecodeError<S::Error>> {
202        cons.take_sequence(|cons| {
203            let version = Unsigned::take_from(cons)?;
204            let n = Unsigned::take_from(cons)?;
205            let e = Unsigned::take_from(cons)?;
206            let d = Unsigned::take_from(cons)?;
207            let p = Unsigned::take_from(cons)?;
208            let q = Unsigned::take_from(cons)?;
209            let dp = Unsigned::take_from(cons)?;
210            let dq = Unsigned::take_from(cons)?;
211            let q_inv = Unsigned::take_from(cons)?;
212            let other_prime_infos = OtherPrimeInfos::take_opt_from(cons)?;
213
214            Ok(Self {
215                version,
216                n,
217                e,
218                d,
219                p,
220                q,
221                dp,
222                dq,
223                q_inv,
224                other_prime_infos,
225            })
226        })
227    }
228
229    pub fn encode_ref(&self) -> impl Values + '_ {
230        encode::sequence((
231            self.version.encode(),
232            self.n.encode(),
233            self.e.encode(),
234            self.d.encode(),
235            self.p.encode(),
236            self.q.encode(),
237            self.dp.encode(),
238            self.dq.encode(),
239            self.q_inv.encode(),
240            self.other_prime_infos.as_ref().map(|x| x.encode_ref()),
241        ))
242    }
243}
244
245impl Values for RsaPrivateKey {
246    fn encoded_len(&self, mode: Mode) -> usize {
247        self.encode_ref().encoded_len(mode)
248    }
249
250    fn write_encoded<W: Write>(&self, mode: Mode, target: &mut W) -> Result<(), std::io::Error> {
251        self.encode_ref().write_encoded(mode, target)
252    }
253}