x509_certificate/
rfc8017.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 primitives defined by RFC 8017.
6
7use bcder::{
8    decode::{Constructed, DecodeError, Source},
9    encode::{self, PrimitiveContent, Values},
10    Unsigned,
11};
12
13/// RSA Public Key.
14///
15/// ```ASN.1
16/// RSAPublicKey ::= SEQUENCE {
17///   modulus           INTEGER,  -- n
18///   publicExponent    INTEGER   -- e
19/// }
20/// ```
21#[derive(Clone, Debug, Eq, PartialEq)]
22pub struct RsaPublicKey {
23    pub modulus: Unsigned,
24    pub public_exponent: Unsigned,
25}
26
27impl RsaPublicKey {
28    pub fn take_from<S: Source>(cons: &mut Constructed<S>) -> Result<Self, DecodeError<S::Error>> {
29        cons.take_sequence(|cons| {
30            let modulus = Unsigned::take_from(cons)?;
31            let public_exponent = Unsigned::take_from(cons)?;
32
33            Ok(Self {
34                modulus,
35                public_exponent,
36            })
37        })
38    }
39
40    pub fn encode_ref(&self) -> impl Values + '_ {
41        encode::sequence((self.modulus.encode(), self.public_exponent.encode()))
42    }
43}