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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
//! What a certificate claims about being a qualified certificate.
//!
//! These statements are claims the issuer makes, not facts this crate can
//! confirm. Whether a certificate is qualified is decided by the trusted list,
//! against the service that issued it, at the moment in question. A statement
//! saying so is evidence about what the issuer intended, and the report has to
//! keep the two apart.
use crate::error::{Error, ErrorCode, Located, Result, gate};
use crate::extension::{self, Extension};
use crate::name::is_ia5;
use crate::oid;
use structured_asn1::memory::{Memory, OwnedVec, Work};
use structured_asn1::{Oid, Parser, Tag, universal};
/// One statement: what it is, and whatever was written with it.
#[derive(Debug, Clone, Copy)]
pub struct QcStatement<'a> {
kind: Oid<'a>,
info: Option<&'a [u8]>,
/// Where the information sits in the document, so an error found while
/// re-parsing it points at the statement rather than at the front of the
/// certificate.
at: u64,
/// Depth above the statement pair, retained for information decoders.
outer: u32,
}
impl<'a> QcStatement<'a> {
/// Which statement this is.
#[must_use]
pub fn kind(self) -> Oid<'a> {
self.kind
}
/// What was written with it, as encoded. What it means depends on the
/// statement, and the ones this crate reads are below.
#[must_use]
pub fn info(self) -> Option<&'a [u8]> {
self.info
}
}
/// How long the issuer says it keeps what is needed to check a certificate.
///
/// The statement is a bare `INTEGER` of years (ETSI TS 101 862, annex B), so
/// a period wider than the counter is inside the schema. It is kept as what
/// it is, longer than any interval a question will be asked over, rather
/// than refused as malformed or folded into a number nobody wrote.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RetentionPeriod {
/// The number of years the statement names.
Years(u32),
/// A period past any interval a report covers.
BeyondAnyInterval,
}
impl RetentionPeriod {
/// The years where an interval could reach them, and `None` where none
/// can.
#[must_use]
pub fn years(self) -> Option<u32> {
match self {
Self::Years(years) => Some(years),
Self::BeyondAnyInterval => None,
}
}
}
/// Which kind of qualified certificate this claims to be.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum QualifiedType {
/// For an electronic signature, which a natural person makes.
ElectronicSignature,
/// For an electronic seal, which a legal person makes.
ElectronicSeal,
/// For website authentication.
WebsiteAuthentication,
}
/// The statements a certificate carries.
#[derive(Debug)]
pub struct QcStatements<'a> {
statements: OwnedVec<QcStatement<'a>>,
parser: &'a Parser,
}
impl<'a> QcStatements<'a> {
/// Account retaining the statement collection's storage. Deferred
/// decoding uses the explicitly supplied current execution domain.
#[must_use]
pub fn memory(&self) -> &Memory {
self.statements.memory()
}
/// Re-read information at its original offset and depth under the same
/// document budget. Every lazy decoder goes through this boundary.
fn info_reader<'operation, W: Work>(
&self,
info: &'a [u8],
statement: &QcStatement<'a>,
execution: &'operation W,
) -> structured_asn1::Result<structured_asn1::ScopedReader<'a, 'operation>>
where
'a: 'operation,
{
execution.read_scoped_nested(
self.parser,
info,
statement.at,
structured_asn1::Encoding::Der,
statement.outer,
)
}
/// Every statement, in order.
#[must_use]
pub fn statements(&self) -> &[QcStatement<'a>] {
&self.statements
}
/// One statement by identifier, for the ones this crate does not read.
pub fn get<W: Work>(&self, kind: Oid<'_>, execution: &W) -> Result<Option<QcStatement<'a>>> {
for statement in &self.statements {
let work = statement.kind.as_bytes().len().max(kind.as_bytes().len()) as u64;
execution
.charge_work(1 + work)
.map_err(|reason| Error::resource(reason, STATEMENT_FIELD, statement.at))?;
if statement.kind == kind {
return Ok(Some(*statement));
}
}
Ok(None)
}
/// Whether the issuer claims this is a qualified certificate.
///
/// A claim, not a finding. The trusted list decides.
pub fn claims_compliance<W: Work>(&self, execution: &W) -> Result<bool> {
Ok(self.get(oid::QC_COMPLIANCE, execution)?.is_some())
}
/// Whether the issuer claims the private key is held in a qualified
/// device.
pub fn claims_qualified_device<W: Work>(&self, execution: &W) -> Result<bool> {
Ok(self.get(oid::QC_SSCD, execution)?.is_some())
}
/// Which kinds of qualified certificate this claims to be.
///
/// This is what separates a signature made by a person from a seal made by
/// an organization, which is a difference the report must never collapse.
pub fn types<W: Work>(&self, execution: &W) -> Result<OwnedVec<QualifiedType>> {
const FIELD: &str = "extensions.qcStatements.qcType";
// Statement information lives inside the DER certificate like every
// extension value, so its re-parses below are DER whatever parser
// carries the budget.
let Some(statement) = self.get(oid::QC_TYPE, execution)? else {
return Ok(execution.memory().vector());
};
let info = statement
.info
.ok_or_else(|| Error::new(ErrorCode::MissingField, FIELD, statement.at))?;
// Re-parsed from where it sits, so every offset below is an offset in
// the document, which is what the error type promises.
let mut reader = self.info_reader(info, &statement, execution).field(FIELD)?;
let sequence = reader
.next_element()
.field(FIELD)?
.ok_or_else(|| Error::new(ErrorCode::MissingField, FIELD, statement.at))?;
if sequence.tag() != Tag::constructed(universal::SEQUENCE) {
return Err(Error::new(
ErrorCode::Encoding(structured_asn1::ErrorCode::UnexpectedTag),
FIELD,
sequence.span().start,
));
}
let mut inside = reader.enter(&sequence).field(FIELD)?;
let mut types = execution.vector();
while !inside.is_empty() {
let identifier = inside.read_oid().field(FIELD)?;
// An identifier this crate does not know is a kind defined
// elsewhere. It is skipped rather than refused, and stays visible
// through the statement it was written in.
if identifier == oid::QCT_ESIGN {
types
.try_push(QualifiedType::ElectronicSignature)
.map_err(|reason| Error::resource(reason, FIELD, inside.offset()))?;
} else if identifier == oid::QCT_ESEAL {
types
.try_push(QualifiedType::ElectronicSeal)
.map_err(|reason| Error::resource(reason, FIELD, inside.offset()))?;
} else if identifier == oid::QCT_WEB {
types
.try_push(QualifiedType::WebsiteAuthentication)
.map_err(|reason| Error::resource(reason, FIELD, inside.offset()))?;
}
}
Ok(types.into_owned())
}
/// How long the issuer says it keeps what is needed to check this
/// certificate, in years.
pub fn retention_period<W: Work>(&self, execution: &W) -> Result<Option<RetentionPeriod>> {
const FIELD: &str = "extensions.qcStatements.qcRetentionPeriod";
let Some(statement) = self.get(oid::QC_RETENTION_PERIOD, execution)? else {
return Ok(None);
};
let info = statement
.info
.ok_or_else(|| Error::new(ErrorCode::MissingField, FIELD, statement.at))?;
let mut reader = self.info_reader(info, &statement, execution).field(FIELD)?;
let years = reader.read_integer().field(FIELD)?;
if !reader.is_empty() {
return Err(Error::new(ErrorCode::TrailingData, FIELD, statement.at));
}
// Below zero is not a duration at all. Above the counter is still a
// duration, and what it says is that nothing asks over that long.
if years.is_negative() {
return Err(Error::new(ErrorCode::MalformedValue, FIELD, statement.at));
}
Ok(Some(years.as_u32().map_or(
RetentionPeriod::BeyondAnyInterval,
RetentionPeriod::Years,
)))
}
/// Which kind of person the subject's identifiers are to be read as.
///
/// Says how to read an organization identifier or a serial number in the
/// subject's name, and getting it wrong turns an organization into a
/// person or the reverse.
pub fn semantics<W: Work>(&self, execution: &W) -> Result<Option<Oid<'a>>> {
// Both versions may be present, each saying which profile the
// certificate claims conformance with (RFC 3739 3.2.6), and each may
// carry an identifier. Two that differ say the subject's identifiers
// are to be read two ways, and the certificate does not say which:
// answering with either would put one of them in a report as though
// the document had settled it.
let mut said: Option<(Oid<'a>, u64)> = None;
for kind in [oid::PKIX_QC_SYNTAX_V2, oid::PKIX_QC_SYNTAX_V1] {
let Some(statement) = self.get(kind, execution)? else {
continue;
};
let Some(identifier) = self.semantics_parts(&statement, execution)?.0 else {
continue;
};
if said.is_some_and(|(said, _)| said != identifier) {
return Err(Error::new(
ErrorCode::MalformedValue,
"extensions.qcStatements.semanticsInformation",
statement.at,
));
}
said = Some((identifier, statement.at));
}
Ok(said.map(|(identifier, _)| identifier))
}
/// Reads one syntax statement's semantics information: the identifier if
/// one was written, and the registration authorities. The accessor above
/// prefers the newer statement, but where both appear each is validated on
/// its own: a well-formed preferred statement must not vouch for the other.
#[allow(clippy::type_complexity)]
fn semantics_parts<W: Work>(
&self,
statement: &QcStatement<'a>,
execution: &W,
) -> Result<(
Option<Oid<'a>>,
OwnedVec<crate::general_name::GeneralName<'a>>,
)> {
const FIELD: &str = "extensions.qcStatements.semanticsInformation";
// The information is optional even where the statement is present.
let Some(info) = statement.info else {
return Ok((None, execution.memory().vector()));
};
let mut reader = self.info_reader(info, statement, execution).field(FIELD)?;
let sequence = reader
.next_element()
.field(FIELD)?
.ok_or_else(|| Error::new(ErrorCode::MissingField, FIELD, statement.at))?;
if sequence.tag() != Tag::constructed(universal::SEQUENCE) {
return Err(Error::new(
ErrorCode::Encoding(structured_asn1::ErrorCode::UnexpectedTag),
FIELD,
sequence.span().start,
));
}
let mut inside = reader.enter(&sequence).field(FIELD)?;
// Both members are optional, but at least one must be present
// (RFC 3739, 3.2.6.1): empty, the structure says nothing while
// wearing the statement.
let Some(first) = inside.next_element().field(FIELD)? else {
return Err(Error::new(ErrorCode::MissingField, FIELD, statement.at));
};
// Both members are optional, but the structure is closed: after the
// identifier only the list of registration authorities may follow,
// and anything else riding along under a value that decides person
// versus organization is a malformed statement, not extra context.
let parts = if first.tag() == Tag::primitive(universal::OID) {
let identifier = inside.oid(&first).field(FIELD)?;
match inside.next_element().field(FIELD)? {
Some(authorities) => (
Some(identifier),
require_authorities(&inside, &authorities)?,
),
None => return Ok((Some(identifier), execution.memory().vector())),
}
} else {
(None, require_authorities(&inside, &first)?)
};
if !inside.is_empty() {
return Err(Error::new(ErrorCode::TrailingData, FIELD, statement.at));
}
Ok(parts)
}
/// Reads the value of a qualified statements extension.
pub fn read<W: Work>(extension: &Extension<'a>, execution: &W) -> Result<Self> {
const FIELD: &str = "extensions.qcStatements";
let mut inside = extension::sequence(extension, FIELD, execution)?;
let work = inside.execution_in(execution.memory());
let mut statements = work.vector();
let mut kinds = work.map::<Oid<'a>, Option<&'a [u8]>>();
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 kind = pair.read_oid().field(FIELD)?;
let info_element = pair.next_element().field(FIELD)?;
let info = info_element.map(structured_asn1::Element::as_bytes);
let info_at =
info_element.map_or_else(|| entry.span().start, |element| element.span().start);
// Exactly one element. This check is what lets every decoder above
// re-parse the information knowing it holds one value and nothing
// after it: a statement written as a structure plus trailing bytes
// is refused here, not misread there.
if !pair.is_empty() {
return Err(at(ErrorCode::TrailingData));
}
// The payload is preserved for a scheme that knows the
// statement, and that scheme clears a critical extension on the
// strength of it, so the whole of it has to be the encoding the
// document claims and not only its outer tag.
if let Some(element) = info_element {
crate::name::walk_der(&pair, &element, FIELD, crate::name::Strings::Checked)?;
}
// One statement written twice with different information leaves
// no answer to what the issuer claimed. Written twice with the
// same information it claims one thing twice: the schema is a
// SEQUENCE OF with no uniqueness constraint, so the repeat is
// folded rather than refused. A map rather than a scan, so a
// long list costs the work its size says and not its square.
if let Some(existing) = kinds
.try_insert(kind, info)
.map_err(|reason| Error::resource(reason, FIELD, entry.span().start))?
{
// Deciding which of the two it is walks both payloads, which
// are opaque and have no local ceiling, so the longer of them
// is charged to the shared work budget first.
let compared = existing
.map_or(0, <[u8]>::len)
.max(info.map_or(0, <[u8]>::len));
inside.charge(compared as u64).field(FIELD)?;
if existing != info {
return Err(at(ErrorCode::MalformedValue));
}
continue;
}
// The compliance and qualified-device statements are bare claims
// whose identifiers define no information value. A value there is
// malformed statement data, and a reader that kept the claim
// while carrying it would report a qualified certificate on the
// strength of a statement that is not one.
if info.is_some() && (kind == oid::QC_COMPLIANCE || kind == oid::QC_SSCD) {
return Err(at(ErrorCode::MalformedValue));
}
statements
.try_push(QcStatement {
kind,
info,
at: info_at,
outer: inside.depth(),
})
.map_err(|reason| Error::resource(reason, FIELD, entry.span().start))?;
}
// No minimum size: QCStatements is a bare SEQUENCE OF (RFC 3739
// 3.2.6), unlike the lists this crate refuses empty, whose schema
// writes SIZE (1..MAX). An empty one makes no qualified claim, which
// is an answer a report can give.
Ok(Self {
statements: statements.into_owned(),
parser: extension.parser(),
})
}
}
impl<'a> QcStatements<'a> {
/// Whether every statement was actually understood, for the gate that
/// decides what a critical extension blocked: each identifier is one
/// this crate names, and each structured payload has its shape. An error
/// is a spent budget and only that.
pub(crate) fn understood<E, W: Work>(
&self,
recognizer: &impl crate::extension::Recognizer<W, Error = E>,
execution: &W,
) -> crate::error::CallbackResult<bool, E> {
if gate(self.types(execution))?.is_none()
|| gate(self.retention_period(execution))?.is_none()
{
return Ok(false);
}
// Two syntax statements that name different semantics leave the
// subject's identifiers with two readings and the certificate with
// no answer between them. Each statement is well formed on its own,
// so this is asked once over the pair rather than inside the walk
// below.
if gate(self.semantics(execution))?.is_none() {
return Ok(false);
}
for statement in &self.statements {
let work = (statement.kind.as_bytes().len() as u64)
.checked_mul(8)
.and_then(|work| work.checked_add(1))
.ok_or_else(|| {
Error::resource(
structured_asn1::memory::MemoryError::WorkExceeded,
STATEMENT_FIELD,
statement.at,
)
})?;
execution
.charge_work(work)
.map_err(|reason| Error::resource(reason, STATEMENT_FIELD, statement.at))?;
let kind = statement.kind;
let statement_understood = if kind == oid::QC_TYPE {
self.qc_types_understood(statement, execution)?
} else if kind == oid::QC_LIMIT_VALUE {
self.limit_value_understood(statement, execution)?
} else if kind == oid::QC_PDS {
self.pds_understood(statement, execution)?
} else if kind == oid::QC_CC_LEGISLATION {
self.legislation_understood(statement, execution)?
} else if kind == oid::PKIX_QC_SYNTAX_V1 || kind == oid::PKIX_QC_SYNTAX_V2 {
// Each syntax statement on its own, not just the one the
// accessor prefers: with both present, a well-formed
// preferred statement must not hide the other's garbage.
// An identifier decides how the subject's identifiers are
// read; one this crate does not name is a reading this run
// cannot perform. And the registration authorities answer
// the other-name question every list of names answers: a
// type nobody claims was never interpreted. The accessor
// still reports both.
match gate(self.semantics_parts(statement, execution))? {
None => false,
Some((identifier, authorities)) => {
identifier.is_none_or(|identifier| {
identifier == oid::SEMANTICS_NATURAL_PERSON
|| identifier == oid::SEMANTICS_LEGAL_PERSON
}) && {
let mut known = true;
for name in &authorities {
execution.charge_work(1).map_err(|reason| {
Error::resource(reason, "qcStatements", statement.at)
})?;
if !crate::extension::other_name_known(name, recognizer, execution)?
{
known = false;
break;
}
}
known
}
}
}
} else {
// A statement this crate does not name may still be one the
// caller's scheme carries: a national statement is the
// scheme's knowledge, and only with neither does a critical
// list hold a claim nobody read.
is_known_statement(kind)
|| recognizer
.recognizes_statement(kind, execution)
.map_err(crate::error::CallbackError::Callback)?
};
if !statement_understood {
return Ok(false);
}
}
Ok(true)
}
/// The type statement of ETSI EN 319 412-5, read for the gate: every
/// identifier is one of the three purposes this crate names.
///
/// The accessor skips an unknown identifier so a report can still show
/// the rest; this gate cannot, because the payload's identifiers are what
/// carry the meaning, and a critical list declaring a purpose nobody here
/// can name was not understood (RFC 3739, 3.2.6: applications that do not
/// understand a statement in a critical extension reject the certificate).
fn qc_types_understood<W: Work>(
&self,
statement: &QcStatement<'a>,
execution: &W,
) -> Result<bool> {
let Some(mut inside) = self.payload_sequence(statement, execution)? else {
return Ok(false);
};
// read_oid rather than the tag-agnostic decoding helper: the schema
// says SEQUENCE OF OBJECT IDENTIFIER, and this gate must not depend
// on another accessor happening to check the tag first.
let mut any = false;
while !inside.is_empty() {
let Some(identifier) = gate(inside.read_oid().field(STATEMENT_FIELD))? else {
return Ok(false);
};
if ![oid::QCT_ESIGN, oid::QCT_ESEAL, oid::QCT_WEB].contains(&identifier) {
return Ok(false);
}
any = true;
}
// The statement declares which purpose the certificate is issued for
// (ETSI EN 319 412-5, clause 4.2.3): zero purposes declares nothing
// while wearing the statement, which is not a statement understood.
Ok(any)
}
/// The monetary limit of ETSI EN 319 412-5: a currency as a three-letter
/// code or a number, then the amount and the exponent.
fn limit_value_understood<W: Work>(
&self,
statement: &QcStatement<'a>,
execution: &W,
) -> Result<bool> {
let Some(mut inside) = self.payload_sequence(statement, execution)? else {
return Ok(false);
};
let Some(Some(currency)) = gate(inside.next_element().field(STATEMENT_FIELD))? else {
return Ok(false);
};
// The alphabetic choice is an ISO 4217 code, three uppercase
// letters; the numeric choice is INTEGER (1..999), that standard's
// numeric code space (ETSI EN 319 412-5, clause 4.3.2,
// Iso4217CurrencyCode). Either outside its form wears the type but
// names no currency. The universal tag is required first, because
// the decoding helper is deliberately tag-agnostic for implicitly
// tagged fields.
let currency_ok = (currency.tag() == Tag::primitive(universal::PRINTABLE_STRING)
&& currency.contents().len() == 3
&& currency.contents().iter().all(u8::is_ascii_uppercase))
|| (currency.tag() == Tag::primitive(universal::INTEGER)
&& gate(inside.integer(¤cy).field(STATEMENT_FIELD))?.is_some_and(|code| {
code.as_u32().is_some_and(|code| (1..=999).contains(&code))
}));
// The amount and the exponent are written `INTEGER`, with no range
// (ETSI EN 319 412-5, clause 4.3.2: `MonetaryValue ::= SEQUENCE {
// currency Iso4217CurrencyCode, amount INTEGER, exponent INTEGER }`,
// the currency being the only member the schema constrains). Neither
// is held to a sign here: a negative amount is inside the type, and
// refusing one would leave a critical extension unhandled over a
// value the schema names. What the issuer meant by it is the
// issuer's to answer, and this layer reports the claim rather than
// grading it.
Ok(currency_ok
&& gate(inside.read_integer().field(STATEMENT_FIELD))?.is_some()
&& gate(inside.read_integer().field(STATEMENT_FIELD))?.is_some()
&& inside.is_empty())
}
/// The disclosure locations of ETSI EN 319 412-5: at least one pair of a
/// location and a two-letter language. Locations are read, never fetched.
fn pds_understood<W: Work>(&self, statement: &QcStatement<'a>, execution: &W) -> Result<bool> {
let Some(mut inside) = self.payload_sequence(statement, execution)? else {
return Ok(false);
};
let mut any = false;
loop {
match gate(inside.next_element().field(STATEMENT_FIELD))? {
Some(Some(location)) => {
if location.tag() != Tag::constructed(universal::SEQUENCE) {
return Ok(false);
}
let Some(mut pair) = gate(inside.enter(&location).field(STATEMENT_FIELD))?
else {
return Ok(false);
};
let Some(Some(url)) = gate(pair.next_element().field(STATEMENT_FIELD))? else {
return Ok(false);
};
let Some(Some(language)) = gate(pair.next_element().field(STATEMENT_FIELD))?
else {
return Ok(false);
};
// The location is a URL, so it answers the same
// absolute-URI question every location field answers;
// the language is an ISO 639-1 code, two lowercase
// letters, the form the pair's schema names. The URL
// scans touch every content byte and the field has no
// local size bound, so the length is charged to the
// shared work budget first.
pair.charge(url.contents().len() as u64)
.field(STATEMENT_FIELD)?;
if url.tag() != Tag::primitive(universal::IA5_STRING)
|| !is_ia5(url.contents())
|| !core::str::from_utf8(url.contents())
.is_ok_and(crate::general_name::is_absolute_uri)
|| language.tag() != Tag::primitive(universal::PRINTABLE_STRING)
|| language.contents().len() != 2
|| !language.contents().iter().all(u8::is_ascii_lowercase)
|| !pair.is_empty()
{
return Ok(false);
}
any = true;
}
Some(None) => return Ok(any && inside.is_empty()),
None => return Ok(false),
}
}
}
/// The legislation countries of ETSI EN 319 412-5: at least one
/// two-letter country code.
fn legislation_understood<W: Work>(
&self,
statement: &QcStatement<'a>,
execution: &W,
) -> Result<bool> {
let Some(mut inside) = self.payload_sequence(statement, execution)? else {
return Ok(false);
};
let mut any = false;
loop {
match gate(inside.next_element().field(STATEMENT_FIELD))? {
Some(Some(country)) => {
// An ISO 3166-1 alpha-2 code, the form the entries are
// defined as: two uppercase letters. A digit or
// punctuation in the pair names no country.
if country.tag() != Tag::primitive(universal::PRINTABLE_STRING)
|| country.contents().len() != 2
|| !country.contents().iter().all(u8::is_ascii_uppercase)
{
return Ok(false);
}
any = true;
}
Some(None) => return Ok(any),
None => return Ok(false),
}
}
}
/// The one sequence a structured payload holds, or `None` where the
/// information is absent or not that. An error is a spent budget.
fn payload_sequence<'operation, W: Work>(
&self,
statement: &QcStatement<'a>,
execution: &'operation W,
) -> Result<Option<structured_asn1::ScopedReader<'a, 'operation>>>
where
'a: 'operation,
{
let Some(info) = statement.info else {
return Ok(None);
};
let Some(mut reader) = gate(
self.info_reader(info, statement, execution)
.field(STATEMENT_FIELD),
)?
else {
return Ok(None);
};
let Some(Some(sequence)) = gate(reader.next_element().field(STATEMENT_FIELD))? else {
return Ok(None);
};
if sequence.tag() != Tag::constructed(universal::SEQUENCE) || !reader.is_empty() {
return Ok(None);
}
gate(reader.enter(&sequence).field(STATEMENT_FIELD))
}
}
/// The field the statement gates above report a spent budget in.
const STATEMENT_FIELD: &str = "extensions.qcStatements";
/// Whether a qualified statement identifier is one this crate names.
fn is_known_statement(kind: Oid<'_>) -> bool {
[
oid::QC_COMPLIANCE,
oid::QC_RETENTION_PERIOD,
oid::QC_SSCD,
oid::QC_TYPE,
oid::PKIX_QC_SYNTAX_V1,
oid::PKIX_QC_SYNTAX_V2,
]
.contains(&kind)
}
/// Requires an element to be the list of registration authorities: a
/// non-empty sequence of names. The names are validated as names, since a
/// value that merely wears the sequence tag is the same malformed statement.
fn require_authorities<'a>(
parent: &structured_asn1::ScopedReader<'a, '_>,
element: &structured_asn1::Element<'a>,
) -> Result<OwnedVec<crate::general_name::GeneralName<'a>>> {
const FIELD: &str = "extensions.qcStatements.semanticsInformation";
if element.tag() != Tag::constructed(universal::SEQUENCE) {
return Err(Error::new(
ErrorCode::Encoding(structured_asn1::ErrorCode::UnexpectedTag),
FIELD,
element.span().start,
));
}
crate::general_name::read_all(parent, element, FIELD)
}
#[cfg(test)]
mod tests;