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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
//! Where things related to a certificate are published.
//!
//! Every location here is text. Nothing in this crate resolves one, and
//! nothing above it may resolve one without a person choosing to: a parser
//! that fetched a location a document named would ask a server chosen by
//! whoever wrote the document to confirm what the document says, and would
//! tell that server which document is being looked at.
use crate::error::{Error, ErrorCode, Located, Result};
use crate::extension::{self, Extension};
use crate::general_name::{self, GeneralName};
use crate::name::{self, RelativeName};
use crate::oid;
use structured_asn1::memory::{OwnedVec, Work};
use structured_asn1::{BitString, Element, Oid, ScopedReader as Reader, Tag, universal};
/// One published service or document, and where it is.
#[derive(Debug)]
pub struct AccessDescription<'a> {
method: Oid<'a>,
location: GeneralName<'a>,
at: u64,
}
impl<'a> AccessDescription<'a> {
/// What sits at the location.
#[must_use]
pub fn method(&self) -> Oid<'a> {
self.method
}
/// Where it is, unresolved.
#[must_use]
pub fn location(&self) -> &GeneralName<'a> {
&self.location
}
}
/// The access descriptions of one extension, whether about the issuer or about
/// the subject.
#[derive(Debug)]
pub struct InfoAccess<'a> {
descriptions: OwnedVec<AccessDescription<'a>>,
field: &'static str,
}
impl<'a> InfoAccess<'a> {
/// Every description, in order.
#[must_use]
pub fn descriptions(&self) -> &[AccessDescription<'a>] {
&self.descriptions
}
/// The locations published for one method. Each comparison is admitted
/// in the current execution domain; an error is yielded once and ends
/// the iterator. Returned locations borrow this collection.
pub fn locations_for<'n, 'q, W: Work>(
&'n self,
method: Oid<'q>,
execution: &'q W,
) -> impl Iterator<Item = Result<&'n GeneralName<'a>>> + 'q
where
'n: 'q,
{
let mut descriptions = self.descriptions.iter();
let mut failed = false;
core::iter::from_fn(move || {
if failed {
return None;
}
for description in descriptions.by_ref() {
let work = 1 + description
.method
.as_bytes()
.len()
.max(method.as_bytes().len()) as u64;
if let Err(reason) = execution.charge_work(work) {
failed = true;
return Some(Err(Error::resource(reason, self.field, description.at)));
}
if description.method == method {
return Some(Ok(description.location()));
}
}
None
})
}
/// Reads the value of an authority or subject information access
/// extension, which have the same shape.
pub fn read<W: Work>(extension: &Extension<'a>, execution: &W) -> Result<Self> {
let field = if extension.kind() == oid::SUBJECT_INFO_ACCESS {
"extensions.subjectInfoAccess"
} else {
"extensions.authorityInfoAccess"
};
let mut inside = extension::sequence(extension, field, execution)?;
let work = inside.execution_in(execution.memory());
let mut descriptions = work.vector();
while let Some(entry) = inside.next_element().field(field)? {
let at = |code| Error::new(code, field, entry.span().start);
if entry.tag() != Tag::constructed(universal::SEQUENCE) {
return Err(at(ErrorCode::Encoding(
structured_asn1::ErrorCode::UnexpectedTag,
)));
}
let mut pair = inside.enter(&entry).field(field)?;
let method = pair.read_oid().field(field)?;
let location = pair
.next_element()
.field(field)?
.ok_or_else(|| at(ErrorCode::MissingField))?;
let location = general_name::read(&pair, &location, field)?;
if !pair.is_empty() {
return Err(at(ErrorCode::TrailingData));
}
descriptions
.try_push(AccessDescription {
method,
location,
at: entry.span().start,
})
.map_err(|reason| Error::resource(reason, field, entry.span().start))?;
}
if descriptions.is_empty() {
return Err(Error::new(
ErrorCode::EmptySequence,
field,
extension.span().start,
));
}
Ok(Self {
descriptions: descriptions.into_owned(),
field,
})
}
}
/// One place a revocation list covering this certificate is published.
#[derive(Debug)]
pub struct DistributionPoint<'a> {
full_name: OwnedVec<GeneralName<'a>>,
name_relative_to_issuer: Option<RelativeName<'a>>,
reasons: Option<BitString<'a>>,
issuer: OwnedVec<GeneralName<'a>>,
}
impl<'a> DistributionPoint<'a> {
/// Where the list is, where the point names it outright.
#[must_use]
pub fn full_name(&self) -> &[GeneralName<'a>] {
&self.full_name
}
/// Where the list is, where the point names it relative to whoever issues
/// it rather than outright.
#[must_use]
pub fn name_relative_to_issuer(&self) -> Option<&RelativeName<'a>> {
self.name_relative_to_issuer.as_ref()
}
/// Which reasons for revocation this list covers, where the point says.
/// Absent means all of them, and a point that names a few is not a list a
/// complete answer can be built from on its own.
#[must_use]
pub fn reasons(&self) -> Option<BitString<'a>> {
self.reasons
}
/// Who issues the list, where that is not the issuer of the certificate.
#[must_use]
pub fn issuer(&self) -> &[GeneralName<'a>] {
&self.issuer
}
}
/// The places revocation lists covering this certificate are published.
#[derive(Debug)]
pub struct DistributionPoints<'a> {
points: OwnedVec<DistributionPoint<'a>>,
}
impl<'a> DistributionPoints<'a> {
/// Every point, in order.
#[must_use]
pub fn points(&self) -> &[DistributionPoint<'a>] {
&self.points
}
/// Reads the value of a distribution points extension, which the freshest
/// revocation list is published in the same shape as.
pub fn read<W: Work>(extension: &Extension<'a>, execution: &W) -> Result<Self> {
let field = if extension.kind() == oid::FRESHEST_CRL {
"extensions.freshestCRL"
} else {
"extensions.cRLDistributionPoints"
};
let mut inside = extension::sequence(extension, field, execution)?;
let work = inside.execution_in(execution.memory());
let mut points = work.vector();
while let Some(entry) = inside.next_element().field(field)? {
points
.try_push(read_point(&inside, &entry, field)?)
.map_err(|reason| Error::resource(reason, field, entry.span().start))?;
}
if points.is_empty() {
return Err(Error::new(
ErrorCode::EmptySequence,
field,
extension.span().start,
));
}
Ok(Self {
points: points.into_owned(),
})
}
}
fn read_point<'a>(
parent: &Reader<'a, '_>,
element: &Element<'a>,
field: &'static str,
) -> Result<DistributionPoint<'a>> {
let fail = |code| Error::new(code, field, element.span().start);
if element.tag() != Tag::constructed(universal::SEQUENCE) {
return Err(fail(ErrorCode::Encoding(
structured_asn1::ErrorCode::UnexpectedTag,
)));
}
let mut inside = parent.enter(element).field(field)?;
let mut point = DistributionPoint {
full_name: parent.memory().vector(),
name_relative_to_issuer: None,
reasons: None,
issuer: parent.memory().vector(),
};
// The three fields are written in the order of their numbers, once each:
// a tag at or below the last one read is not this schema.
let mut last_number = None;
while let Some(entry) = inside.next_element().field(field)? {
let tag = extension::context_of(entry.tag());
match tag {
Some((0, true)) if last_number.is_none() => {
read_point_name(&inside, &entry, field, &mut point)?;
}
Some((1, false)) if last_number < Some(1) => {
let reasons = inside.bit_string(&entry).field(field)?;
// The zero walk below touches every content byte, so it is
// charged to the shared work budget first.
inside
.charge(reasons.as_bytes().len() as u64)
.field(field)?;
// Present while naming no reason, the field covers none while
// claiming to narrow the list: a reader would take the point
// for a partial list and a full one at once. Absent is how
// "all reasons" is said. Written as zero bits, as zero bytes,
// or with only bit zero, which the schema names "unused" and
// gives no meaning (RFC 5280 4.2.1.13), it says the same
// nothing: a scope whose only member means nothing has no
// members.
if !(1..=8).any(|bit| reasons.bit(bit)) {
return Err(Error::new(
ErrorCode::MalformedValue,
field,
entry.span().start,
));
}
// A named bit list under DER: X.690 11.2.2 trims trailing
// zero bits, and RFC 5280 4.2.1.13 names bits 0 through 8, so
// a longer string carries a bit with no name. unused(0) is
// deliberately readable: it is a named bit of the schema, and
// RFC 5280 gives it no meaning but no prohibition either;
// what a set bit means is the revocation evidence's question.
if inside.encoding().is_der()
&& (reasons.len() > 9
|| (!reasons.is_empty() && !reasons.bit(reasons.len() - 1)))
{
return Err(Error::new(
ErrorCode::MalformedValue,
field,
entry.span().start,
));
}
point.reasons = Some(reasons);
}
Some((2, true)) if last_number < Some(2) => {
let issuer = general_name::read_all(&inside, &entry, field)?;
// The profile is explicit that this is exclusive: a present
// cRLIssuer "MUST only contain the distinguished name (DN)
// from the issuer field of the CRL" (RFC 5280 4.2.1.13), so
// every member is required to be a directory name, not
// merely one of them. Any other form cannot be matched
// against a list's issuer field anyway. Several directory
// names stay permitted: the same section contemplates them
// by forbidding only the relative alternative "when
// cRLIssuer contains more than one distinguished name", the
// check below, because the relative fragment appends to
// exactly one name and with two there is no answer to which
// list the point names.
if !issuer
.iter()
.all(|name| matches!(name, general_name::GeneralName::Directory(_)))
|| (point.name_relative_to_issuer.is_some() && issuer.len() > 1)
{
return Err(Error::new(
ErrorCode::MalformedValue,
field,
entry.span().start,
));
}
point.issuer = issuer;
}
_ => {
return Err(Error::new(
ErrorCode::UnexpectedField,
field,
entry.span().start,
));
}
}
last_number = tag.map(|(number, _)| number);
}
// A point must say where the list is or who issues it. With neither, it
// names no source a list could ever be resolved from, and reporting it as
// parsed would count a malformed extension as a working one. A relative
// name WITHOUT a cRLIssuer stays valid on its own: the profile appends
// the fragment to the certificate issuer's distinguished name when the
// issuer field is absent (RFC 5280 4.2.1.13), so the point still names a
// resolvable source. Reasons beside a cRLIssuer with no point name stay
// valid too: the only shape rule the profile states is that a point
// "MUST NOT consist of only the reasons field; either distributionPoint
// or cRLIssuer MUST be present" (RFC 5280 4.2.1.13), and no sentence
// requires a point name beside reasons; the list is then located
// through the issuer's directory entry, its reason scope intact.
if point.full_name.is_empty()
&& point.name_relative_to_issuer.is_none()
&& point.issuer.is_empty()
{
return Err(fail(ErrorCode::MalformedValue));
}
Ok(point)
}
/// Reads the name of a point, which is a choice between naming the list
/// outright and naming it under whoever issues it. A choice cannot take the
/// tag of its position, so the field wraps it.
#[derive(Debug)]
pub(crate) enum PointName<'a> {
Full(OwnedVec<GeneralName<'a>>),
Relative(RelativeName<'a>),
}
/// Shared `DistributionPointName` grammar for certificate DPs and CRL IDPs.
pub(crate) fn read_distribution_name<'a>(
parent: &Reader<'a, '_>,
element: &Element<'a>,
field: &'static str,
) -> Result<PointName<'a>> {
let fail = |code| Error::new(code, field, element.span().start);
let mut inside = parent.enter(element).field(field)?;
let chosen = inside
.next_element()
.field(field)?
.ok_or_else(|| fail(ErrorCode::MissingField))?;
let name = match extension::context_of(chosen.tag()) {
Some((0, true)) => PointName::Full(general_name::read_locations(&inside, &chosen, field)?),
Some((1, true)) => {
let relative = name::read_relative(&inside, &chosen, field)?;
for attribute in relative.attributes() {
if !name::well_formed_in(attribute, &inside, field)? {
return Err(fail(ErrorCode::MalformedValue));
}
}
PointName::Relative(relative)
}
_ => {
return Err(fail(ErrorCode::Encoding(
structured_asn1::ErrorCode::UnexpectedTag,
)));
}
};
if !inside.is_empty() {
return Err(fail(ErrorCode::TrailingData));
}
Ok(name)
}
fn read_point_name<'a>(
parent: &Reader<'a, '_>,
element: &Element<'a>,
field: &'static str,
point: &mut DistributionPoint<'a>,
) -> Result<()> {
match read_distribution_name(parent, element, field)? {
PointName::Full(names) => point.full_name = names,
PointName::Relative(name) => point.name_relative_to_issuer = Some(name),
}
Ok(())
}
#[cfg(test)]
mod tests;