structured-cades 0.14.0

Source-bound CAdES baseline composition over CMS and long-term validation facts
Documentation
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
//! Original selection phases; every output keeps the caller's scoped owner.

use super::{
    Edition, FIELD, Header, Part, SelectionError, archive_kind, category, covered_container,
};
use crate::{
    Error,
    prepare::{
        Preparation,
        encoding::{AttributeEncoding, SignerView},
    },
    preservation::Kind,
    source::Node,
};
use core::ops::Range;
use structured_asn1::{
    Oid, Reader,
    memory::{MemoryError, OwnedMap, OwnedVec, Work},
};
use structured_ltv::timestamp::Purpose;

fn resource(at: Node, cause: MemoryError) -> SelectionError {
    SelectionError::Processing(Error::resource(cause, FIELD, at.span().start))
}

pub(super) fn choose(
    prepared: &Preparation<'_, '_>,
    ordinal: usize,
    edition: Edition,
    previous: &[usize],
    later: &[super::Later<'_, '_, '_>],
    operation: &Reader<'_>,
) -> Result<OwnedVec<bool>, SelectionError> {
    let target = &prepared.timestamps()[ordinal];
    let work = operation.execution();
    let refusal = |cause| resource(target.property(), cause);
    let mut chosen = work.vector();
    chosen
        .try_reserve_exact(prepared.timestamps().len())
        .map_err(refusal)?;
    for _ in prepared.timestamps() {
        chosen.try_push(false).map_err(refusal)?;
    }
    for &index in previous {
        work.charge_work(1).map_err(refusal)?;
        let entry = prepared
            .timestamps()
            .get(index)
            .ok_or(SelectionError::WrongOccurrence)?;
        if index == ordinal {
            return Err(SelectionError::SelfIncluded);
        }
        if entry.owner() != target.owner()
            || entry.purpose() != Purpose::Archive
            || entry.value().is_none()
            || chosen[index]
        {
            return Err(SelectionError::WrongOccurrence);
        }
        if edition == Edition::Rfc3126 && entry.preservation_kind() != Some(Kind::ArchiveV1) {
            return Err(SelectionError::WrongEdition);
        }
        chosen[index] = true;
    }
    for (index, entry) in prepared.timestamps().iter().enumerate() {
        work.charge_work(1).map_err(refusal)?;
        if entry.owner() != target.owner()
            || index == ordinal
            || chosen[index]
            || entry.purpose() != Purpose::Archive
        {
            continue;
        }
        let mut proven = false;
        for proof in later {
            if proof.permits(prepared, entry, target, operation)? {
                proven = true;
                break;
            }
        }
        if !proven {
            return Err(SelectionError::UnprovenLater {
                property: entry.property(),
            });
        }
    }
    Ok(chosen.into_owned())
}

pub(super) struct Output<'out, 'input, W: Work, Q: Work> {
    parts: &'out mut OwnedVec<Part<'input>, W>,
    covered: &'out mut OwnedMap<Node, bool, W>,
    work: &'out Q,
    at: Node,
}
impl<'o, 's, W: Work, Q: Work> Output<'o, 's, W, Q> {
    pub(super) fn new(
        parts: &'o mut OwnedVec<Part<'s>, W>,
        covered: &'o mut OwnedMap<Node, bool, W>,
        work: &'o Q,
        at: Node,
    ) -> Self {
        Self {
            parts,
            covered,
            work,
            at,
        }
    }
    pub(super) fn prefix<'m, 'd: 's>(
        &mut self,
        prepared: &'s Preparation<'m, 'd>,
        view: SignerView<'s, 'd>,
        owner: Node,
        edition: Edition,
        operation: &Reader<'d>,
    ) -> Result<usize, SelectionError> {
        let refusal = |cause| resource(self.at, cause);
        if edition != Edition::Rfc3126 {
            self.parts
                .try_push(Part::Current(
                    prepared.document().message().encap_content_encoding(),
                ))
                .map_err(refusal)?;
        }
        let payload_at = self.parts.len();
        if edition == Edition::Rfc3126 {
            if let Some((_, bytes)) = view.signed() {
                self.parts.try_push(Part::Current(bytes)).map_err(refusal)?;
            }
            for piece in prepared.signature_of(owner, operation)? {
                self.parts
                    .try_push(Part::Current(piece.as_bytes()))
                    .map_err(refusal)?;
            }
        } else {
            for bytes in [
                prepared.document().message().certificate_encoding(),
                prepared.document().message().revocation_encoding(),
            ]
            .into_iter()
            .flatten()
            {
                self.parts.try_push(Part::Current(bytes)).map_err(refusal)?;
            }
            self.parts
                .try_push(Part::Current(view.fields()))
                .map_err(refusal)?;
        }
        Ok(payload_at)
    }
    pub(super) fn unsigned<'m, 'd: 's>(
        &mut self,
        prepared: &'s Preparation<'m, 'd>,
        view: SignerView<'s, 'd>,
        edition: Edition,
        chosen: &[bool],
    ) -> Result<(), SelectionError> {
        let at = self.at;
        let refusal = |cause| resource(at, cause);
        // RFC3126 fixes the category order. Later editions retain physical
        // attribute order; neither branch sorts or re-encodes original values.
        let passes = if edition == Edition::Rfc3126 { 8 } else { 1 };
        let mut certificate_values = false;
        let mut revocation_values = false;
        for pass in 0..passes {
            for index in 0..view.attributes() {
                self.work.charge_work(1).map_err(refusal)?;
                let attribute = view.attribute(index).expect("bounded original catalogue");
                let category = if edition == Edition::Rfc3126 {
                    self.work
                        .charge_work(1 + attribute.kind.as_bytes().len().min(11) as u64)
                        .map_err(refusal)?;
                    let last = [14, 21, 22, 23, 24, 25, 26, 27][pass];
                    if attribute.kind
                        != Oid::from_bytes(&[42, 134, 72, 134, 247, 13, 1, 9, 16, 2, last])
                    {
                        continue;
                    }
                    Some(pass)
                } else {
                    self.work
                        .charge_work(8 * (1 + attribute.kind.as_bytes().len().min(11) as u64))
                        .map_err(refusal)?;
                    category(attribute.kind)
                };
                certificate_values |= category == Some(3);
                revocation_values |= category == Some(4);
                if archive_kind(attribute.kind).is_none()
                    && attribute.kind != structured_cms::oid::ARCHIVE_TIMESTAMP_V3
                {
                    self.ordinary(prepared, attribute, edition)?;
                } else {
                    self.archive(prepared, attribute, edition, chosen)?;
                }
            }
        }
        if edition == Edition::Rfc3126 && (!certificate_values || !revocation_values) {
            return Err(SelectionError::PriorNotPreserved);
        }
        Ok(())
    }
    fn ordinary<'m, 'd: 's>(
        &mut self,
        prepared: &'s Preparation<'m, 'd>,
        attribute: AttributeEncoding<'d>,
        edition: Edition,
    ) -> Result<(), SelectionError> {
        let refusal = |cause| resource(self.at, cause);
        self.parts
            .try_push(Part::Current(if edition == Edition::Rfc3126 {
                attribute.contents
            } else {
                attribute.whole
            }))
            .map_err(refusal)?;
        self.covered
            .try_insert(Node::of(attribute.span), edition != Edition::Rfc3126)
            .map_err(refusal)?;
        if let Some(value) = attribute.single {
            self.covered
                .try_insert(Node::of(value), true)
                .map_err(refusal)?;
        }
        if let Some(ranges) = &prepared.timestamp_ranges
            && let Some(range) = ranges
                .get(&Node::of(attribute.span), self.work)
                .map_err(refusal)?
        {
            for ordinal in range.clone() {
                self.work.charge_work(1).map_err(refusal)?;
                if let Some(value) = prepared.timestamps()[ordinal].value() {
                    self.covered
                        .try_insert(Node::of(value.span()), true)
                        .map_err(refusal)?;
                }
            }
        }
        Ok(())
    }
    fn archive<'m, 'd: 's>(
        &mut self,
        prepared: &'s Preparation<'m, 'd>,
        attribute: AttributeEncoding<'d>,
        edition: Edition,
        chosen: &[bool],
    ) -> Result<(), SelectionError> {
        let at = self.at;
        let refusal = |cause| resource(at, cause);
        let ranges = prepared
            .timestamp_ranges
            .as_ref()
            .ok_or(SelectionError::WrongOccurrence)?;
        let range = ranges
            .get(&Node::of(attribute.span), self.work)
            .map_err(refusal)?
            .ok_or(SelectionError::WrongOccurrence)?;
        let mut count = 0usize;
        let mut length = 0u64;
        for value in range.clone() {
            self.work.charge_work(1).map_err(refusal)?;
            if chosen[value] {
                let element = prepared.timestamps()[value]
                    .value()
                    .ok_or(SelectionError::WrongOccurrence)?;
                length = length
                    .checked_add(element.as_bytes().len() as u64)
                    .ok_or(SelectionError::LengthOverflow)?;
                count += 1;
            }
        }
        if count == 0 {
            return Ok(());
        }
        if count == range.len() {
            self.parts
                .try_push(Part::Current(if edition == Edition::Rfc3126 {
                    attribute.contents
                } else {
                    attribute.whole
                }))
                .map_err(refusal)?;
            self.covered
                .try_insert(Node::of(attribute.span), edition != Edition::Rfc3126)
                .map_err(refusal)?;
        } else {
            self.partial(prepared, attribute, edition, chosen, range.clone(), length)?;
        }
        for value in range.clone() {
            self.work.charge_work(1).map_err(refusal)?;
            if chosen[value] {
                let element = prepared.timestamps()[value]
                    .value()
                    .expect("checked readable member");
                self.covered
                    .try_insert(Node::of(element.span()), true)
                    .map_err(refusal)?;
            }
        }
        Ok(())
    }
    fn partial<'m, 'd: 's>(
        &mut self,
        prepared: &'s Preparation<'m, 'd>,
        attribute: AttributeEncoding<'d>,
        edition: Edition,
        chosen: &[bool],
        range: Range<usize>,
        length: u64,
    ) -> Result<(), SelectionError> {
        let refusal = |cause| resource(self.at, cause);
        let set_header = Header::new(0x31, length);
        let body_length = (attribute.kind_encoding.len() as u64)
            .checked_add(u64::from(set_header.length))
            .and_then(|n| n.checked_add(length))
            .ok_or(SelectionError::LengthOverflow)?;
        if edition != Edition::Rfc3126 {
            self.parts
                .try_push(Part::Framing(Header::new(0x30, body_length)))
                .map_err(refusal)?;
        }
        self.parts
            .try_push(Part::Current(attribute.kind_encoding))
            .map_err(refusal)?;
        self.parts
            .try_push(Part::Framing(set_header))
            .map_err(refusal)?;
        for value in range {
            self.work.charge_work(1).map_err(refusal)?;
            if chosen[value] {
                self.parts
                    .try_push(Part::Current(
                        prepared.timestamps()[value]
                            .value()
                            .expect("checked readable member")
                            .as_bytes(),
                    ))
                    .map_err(refusal)?;
            }
        }
        Ok(())
    }
    pub(super) fn context<'m, 'd: 's>(
        &mut self,
        prepared: &'s Preparation<'m, 'd>,
        view: SignerView<'s, 'd>,
        owner: Node,
        edition: Edition,
    ) -> Result<(), SelectionError> {
        let refusal = |cause| resource(self.at, cause);
        if edition != Edition::Rfc3126 {
            for value in prepared.document().message().certificates() {
                self.covered
                    .try_insert(Node::of(value.span()), true)
                    .map_err(refusal)?;
            }
            for value in prepared.document().message().revocations() {
                self.covered
                    .try_insert(Node::of(value.span()), true)
                    .map_err(refusal)?;
            }
        }
        for counter in prepared
            .counters()
            .iter()
            .chain(prepared.parallel_counters())
        {
            self.work.charge_work(1).map_err(refusal)?;
            if covered_container(view, counter.node(), self.covered, self.work).map_err(refusal)? {
                self.covered
                    .try_insert(counter.node(), true)
                    .map_err(refusal)?;
            }
        }
        // TS101733 V1.5.1 §6.4.1 and RFC5126 §6.4.1 cover complete
        // countersignature attributes, including every nested timestamp.
        // Exact coverage lookups cannot infer those descendant obligations
        // from the counter node alone. The original enclosing attribute must
        // be wholly covered; partial archive values never gain whole coverage.
        // https://www.rfc-editor.org/rfc/rfc5126#section-6.4.1
        for entry in prepared.timestamps() {
            self.work.charge_work(1).map_err(refusal)?;
            if entry.owner() == owner {
                if let Some((signed, _)) = view.signed() {
                    let span = entry.property().span();
                    if span.start >= signed.start && span.end() <= signed.end() {
                        self.covered
                            .try_insert(entry.property(), true)
                            .map_err(refusal)?;
                        if let Some(value) = entry.value() {
                            self.covered
                                .try_insert(Node::of(value.span()), true)
                                .map_err(refusal)?;
                        }
                    }
                }
                continue;
            }
            if covered_container(view, entry.property(), self.covered, self.work)
                .map_err(refusal)?
            {
                self.covered
                    .try_insert(entry.property(), true)
                    .map_err(refusal)?;
                if let Some(value) = entry.value() {
                    self.covered
                        .try_insert(Node::of(value.span()), true)
                        .map_err(refusal)?;
                }
            }
        }
        Ok(())
    }
}