1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

//! ASN.1 primitives from RFC 2986.

use {
    crate::{
        rfc3280::Name,
        rfc5280::{AlgorithmIdentifier, SubjectPublicKeyInfo},
        rfc5652::Attribute,
        rfc5958::Attributes,
    },
    bcder::{
        decode::{Constructed, DecodeError, Source},
        encode::{self, PrimitiveContent, Values},
        BitString, Integer, Mode, Tag,
    },
    std::io::Write,
};

#[derive(Clone, Copy, Debug)]
pub enum Version {
    V1 = 0,
}

impl From<Version> for u8 {
    fn from(v: Version) -> u8 {
        match v {
            Version::V1 => 0,
        }
    }
}

impl Version {
    pub fn take_from<S: Source>(cons: &mut Constructed<S>) -> Result<Self, DecodeError<S::Error>> {
        match cons.take_primitive_if(Tag::INTEGER, Integer::i8_from_primitive)? {
            0 => Ok(Self::V1),
            _ => Err(cons.content_err("unexpected non-integer when parsing Version")),
        }
    }

    pub fn encode(self) -> impl Values {
        u8::from(self).encode()
    }
}

/// Certificate request info.
///
/// ```asn.1
/// CertificationRequestInfo ::= SEQUENCE {
///   version       INTEGER { v1(0) } (v1,...),
///   subject       Name,
///   subjectPKInfo SubjectPublicKeyInfo{{ PKInfoAlgorithms }},
///   attributes    [0] Attributes{{ CRIAttributes }}
/// }
/// ```
#[derive(Clone)]
pub struct CertificationRequestInfo {
    pub version: Version,
    pub subject: Name,
    pub subject_public_key_info: SubjectPublicKeyInfo,
    pub attributes: Attributes,
}

impl CertificationRequestInfo {
    pub fn take_from<S: Source>(cons: &mut Constructed<S>) -> Result<Self, DecodeError<S::Error>> {
        cons.take_sequence(|cons| Self::from_sequence(cons))
    }

    pub fn from_sequence<S: Source>(
        cons: &mut Constructed<S>,
    ) -> Result<Self, DecodeError<S::Error>> {
        let version = Version::take_from(cons)?;
        let subject = Name::take_from(cons)?;
        let subject_public_key_info = SubjectPublicKeyInfo::take_from(cons)?;
        let attributes = cons.take_constructed_if(Tag::CTX_0, |cons| {
            let mut attributes = Attributes::default();

            while let Some(attribute) = Attribute::take_opt_from(cons)? {
                attributes.push(attribute);
            }

            Ok(attributes)
        })?;

        Ok(Self {
            version,
            subject,
            subject_public_key_info,
            attributes,
        })
    }

    pub fn encode_ref(&self) -> impl Values + '_ {
        encode::sequence((
            self.version.encode(),
            self.subject.encode_ref(),
            self.subject_public_key_info.encode_ref(),
            self.attributes.encode_ref_as(Tag::CTX_0),
        ))
    }
}

impl Values for CertificationRequestInfo {
    fn encoded_len(&self, mode: Mode) -> usize {
        self.encode_ref().encoded_len(mode)
    }

    fn write_encoded<W: Write>(&self, mode: Mode, target: &mut W) -> Result<(), std::io::Error> {
        self.encode_ref().write_encoded(mode, target)
    }
}

/// Certificate request.
///
/// ```asn.1
/// CertificationRequest ::= SEQUENCE {
///   certificationRequestInfo CertificationRequestInfo,
///   signatureAlgorithm AlgorithmIdentifier{{ SignatureAlgorithms }},
///   signature          BIT STRING
/// }
/// ```
#[derive(Clone)]
pub struct CertificationRequest {
    pub certificate_request_info: CertificationRequestInfo,
    pub signature_algorithm: AlgorithmIdentifier,
    pub signature: BitString,
}

impl CertificationRequest {
    pub fn take_from<S: Source>(cons: &mut Constructed<S>) -> Result<Self, DecodeError<S::Error>> {
        cons.take_sequence(|cons| Self::from_sequence(cons))
    }

    pub fn from_sequence<S: Source>(
        cons: &mut Constructed<S>,
    ) -> Result<Self, DecodeError<S::Error>> {
        let certificate_request_info = CertificationRequestInfo::take_from(cons)?;
        let signature_algorithm = AlgorithmIdentifier::take_from(cons)?;
        let signature = BitString::take_from(cons)?;

        Ok(Self {
            certificate_request_info,
            signature_algorithm,
            signature,
        })
    }

    pub fn encode_ref(&self) -> impl Values + '_ {
        encode::sequence((
            self.certificate_request_info.encode_ref(),
            &self.signature_algorithm,
            self.signature.encode_ref(),
        ))
    }

    /// Encode this data structure to DER.
    pub fn encode_der(&self) -> Result<Vec<u8>, std::io::Error> {
        let mut buffer = vec![];
        self.write_encoded(Mode::Der, &mut buffer)?;

        Ok(buffer)
    }

    /// Encode the data structure to PEM.
    pub fn encode_pem(&self) -> Result<String, std::io::Error> {
        Ok(pem::Pem::new("CERTIFICATE REQUEST", self.encode_der()?).to_string())
    }
}

impl Values for CertificationRequest {
    fn encoded_len(&self, mode: Mode) -> usize {
        self.encode_ref().encoded_len(mode)
    }

    fn write_encoded<W: Write>(&self, mode: Mode, target: &mut W) -> Result<(), std::io::Error> {
        self.encode_ref().write_encoded(mode, target)
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn rsa_parse() {
        let der = include_bytes!("testdata/csr-rsa2048.der");

        let csr = Constructed::decode(der.as_ref(), Mode::Der, |cons| {
            CertificationRequest::take_from(cons)
        })
        .unwrap();

        let mut encoded = vec![];
        csr.write_encoded(Mode::Der, &mut encoded).unwrap();

        assert_eq!(&encoded, der);
    }
}