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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
use crate::error::{X509Error, X509Result};
use crate::extensions::*;
use crate::time::ASN1Time;
use crate::utils::format_serial;
use crate::x509::{
parse_serial, parse_signature_value, AlgorithmIdentifier, ReasonCode, X509Name, X509Version,
};
use asn1_rs::{BitString, FromDer};
use der_parser::ber::Tag;
use der_parser::der::*;
use der_parser::num_bigint::BigUint;
use der_parser::oid::Oid;
use nom::combinator::{all_consuming, complete, map, opt};
use nom::multi::many0;
use nom::Offset;
use oid_registry::*;
use std::collections::HashMap;
#[derive(Clone, Debug)]
pub struct CertificateRevocationList<'a> {
pub tbs_cert_list: TbsCertList<'a>,
pub signature_algorithm: AlgorithmIdentifier<'a>,
pub signature_value: BitString<'a>,
}
impl<'a> CertificateRevocationList<'a> {
pub fn version(&self) -> Option<X509Version> {
self.tbs_cert_list.version
}
#[inline]
pub fn issuer(&self) -> &X509Name {
&self.tbs_cert_list.issuer
}
#[inline]
pub fn last_update(&self) -> ASN1Time {
self.tbs_cert_list.this_update
}
#[inline]
pub fn next_update(&self) -> Option<ASN1Time> {
self.tbs_cert_list.next_update
}
pub fn iter_revoked_certificates(&self) -> impl Iterator<Item = &RevokedCertificate<'a>> {
self.tbs_cert_list.revoked_certificates.iter()
}
#[inline]
pub fn extensions(&self) -> &[X509Extension] {
&self.tbs_cert_list.extensions
}
pub fn crl_number(&self) -> Option<&BigUint> {
self.extensions()
.iter()
.find(|&ext| ext.oid == OID_X509_EXT_BASIC_CONSTRAINTS)
.and_then(|ext| match ext.parsed_extension {
ParsedExtension::CRLNumber(ref num) => Some(num),
_ => None,
})
}
}
impl<'a> FromDer<'a, X509Error> for CertificateRevocationList<'a> {
fn from_der(i: &'a [u8]) -> X509Result<Self> {
parse_der_sequence_defined_g(|i, _| {
let (i, tbs_cert_list) = TbsCertList::from_der(i)?;
let (i, signature_algorithm) = AlgorithmIdentifier::from_der(i)?;
let (i, signature_value) = parse_signature_value(i)?;
let crl = CertificateRevocationList {
tbs_cert_list,
signature_algorithm,
signature_value,
};
Ok((i, crl))
})(i)
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct TbsCertList<'a> {
pub version: Option<X509Version>,
pub signature: AlgorithmIdentifier<'a>,
pub issuer: X509Name<'a>,
pub this_update: ASN1Time,
pub next_update: Option<ASN1Time>,
pub revoked_certificates: Vec<RevokedCertificate<'a>>,
extensions: Vec<X509Extension<'a>>,
pub(crate) raw: &'a [u8],
}
impl<'a> TbsCertList<'a> {
#[inline]
pub fn extensions(&self) -> &[X509Extension] {
&self.extensions
}
#[inline]
pub fn iter_extensions(&self) -> impl Iterator<Item = &X509Extension> {
self.extensions.iter()
}
pub fn find_extension(&self, oid: &Oid) -> Option<&X509Extension> {
self.extensions.iter().find(|&ext| ext.oid == *oid)
}
pub fn extensions_map(&self) -> Result<HashMap<Oid, &X509Extension>, X509Error> {
self.extensions
.iter()
.try_fold(HashMap::new(), |mut m, ext| {
if m.contains_key(&ext.oid) {
return Err(X509Error::DuplicateExtensions);
}
m.insert(ext.oid.clone(), ext);
Ok(m)
})
}
}
impl<'a> AsRef<[u8]> for TbsCertList<'a> {
fn as_ref(&self) -> &[u8] {
self.raw
}
}
impl<'a> FromDer<'a, X509Error> for TbsCertList<'a> {
fn from_der(i: &'a [u8]) -> X509Result<Self> {
let start_i = i;
parse_der_sequence_defined_g(move |i, _| {
let (i, version) =
opt(map(parse_der_u32, X509Version))(i).or(Err(X509Error::InvalidVersion))?;
let (i, signature) = AlgorithmIdentifier::from_der(i)?;
let (i, issuer) = X509Name::from_der(i)?;
let (i, this_update) = ASN1Time::from_der(i)?;
let (i, next_update) = ASN1Time::from_der_opt(i)?;
let (i, revoked_certificates) = opt(complete(parse_revoked_certificates))(i)?;
let (i, extensions) = parse_extensions(i, Tag(0))?;
let len = start_i.offset(i);
let tbs = TbsCertList {
version,
signature,
issuer,
this_update,
next_update,
revoked_certificates: revoked_certificates.unwrap_or_default(),
extensions,
raw: &start_i[..len],
};
Ok((i, tbs))
})(i)
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct RevokedCertificate<'a> {
pub user_certificate: BigUint,
pub revocation_date: ASN1Time,
extensions: Vec<X509Extension<'a>>,
pub(crate) raw_serial: &'a [u8],
}
impl<'a> RevokedCertificate<'a> {
pub fn serial(&self) -> &BigUint {
&self.user_certificate
}
#[inline]
pub fn extensions(&self) -> &[X509Extension] {
&self.extensions
}
#[inline]
pub fn iter_extensions(&self) -> impl Iterator<Item = &X509Extension> {
self.extensions.iter()
}
pub fn find_extension(&self, oid: &Oid) -> Option<&X509Extension> {
self.extensions.iter().find(|&ext| ext.oid == *oid)
}
pub fn extensions_map(&self) -> Result<HashMap<Oid, &X509Extension>, X509Error> {
self.extensions
.iter()
.try_fold(HashMap::new(), |mut m, ext| {
if m.contains_key(&ext.oid) {
return Err(X509Error::DuplicateExtensions);
}
m.insert(ext.oid.clone(), ext);
Ok(m)
})
}
pub fn raw_serial(&self) -> &[u8] {
self.raw_serial
}
pub fn raw_serial_as_string(&self) -> String {
format_serial(self.raw_serial)
}
pub fn reason_code(&self) -> Option<(bool, ReasonCode)> {
self.find_extension(&OID_X509_EXT_REASON_CODE)
.and_then(|ext| match ext.parsed_extension {
ParsedExtension::ReasonCode(code) => Some((ext.critical, code)),
_ => None,
})
}
pub fn invalidity_date(&self) -> Option<(bool, ASN1Time)> {
self.find_extension(&OID_X509_EXT_INVALIDITY_DATE)
.and_then(|ext| match ext.parsed_extension {
ParsedExtension::InvalidityDate(date) => Some((ext.critical, date)),
_ => None,
})
}
}
impl<'a> FromDer<'a, X509Error> for RevokedCertificate<'a> {
fn from_der(i: &'a [u8]) -> X509Result<Self> {
parse_der_sequence_defined_g(|i, _| {
let (i, (raw_serial, user_certificate)) = parse_serial(i)?;
let (i, revocation_date) = ASN1Time::from_der(i)?;
let (i, extensions) = opt(complete(parse_extension_sequence))(i)?;
let revoked = RevokedCertificate {
user_certificate,
revocation_date,
extensions: extensions.unwrap_or_default(),
raw_serial,
};
Ok((i, revoked))
})(i)
}
}
fn parse_revoked_certificates(i: &[u8]) -> X509Result<Vec<RevokedCertificate>> {
parse_der_sequence_defined_g(|a, _| {
all_consuming(many0(complete(RevokedCertificate::from_der)))(a)
})(i)
}