structured-xades 0.6.1

XAdES mechanisms over caller-owned XML and explicit resource accounting
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
415
416
417
418
419
420
//! Original signed commitments and their authenticated Reference associations.

use crate::binding::{Bound, Status};
use crate::container::XADES;
use crate::identifier;
use crate::properties::{self, DataObjectRole, Error, PropertyResult};
use crate::reference::{
    Completeness, Descriptor, LexicalSource, Parent, ReferenceXml, Resolution, VisitError,
};
use crate::xml::{Access, AccessError, Name, Snapshot, Xml};
use structured_asn1::memory::{OwnedVec, Work};

/// Schema choice, preserving explicit references and `AllSignedDataObjects`.
#[derive(Debug)]
pub enum Objects<N> {
    /// Original empty marker, not a caller-supplied all-objects assertion.
    All(N),
    /// Nonempty original `ObjectReference` simple-content elements in order.
    References(OwnedVec<N>),
}

/// Structural signed commitment; URI syntax and policy meaning remain separate.
pub struct Reading<N, D, G> {
    snapshot: Snapshot<D, G>,
    signature: N,
    node: N,
    kind: identifier::Reading<N>,
    objects: Objects<N>,
    qualifiers: Option<N>,
}

impl<N: Copy, D: Copy, G: Copy> Reading<N, D, G> {
    /// Original commitment property.
    #[must_use]
    pub fn node(&self) -> N {
        self.node
    }
    /// Original identifier. Its URI meaning is defined by registration/policy,
    /// not by a hard-coded common list of commitment types.
    #[must_use]
    pub fn kind(&self) -> &identifier::Reading<N> {
        &self.kind
    }
    /// Original schema choice.
    #[must_use]
    pub fn objects(&self) -> &Objects<N> {
        &self.objects
    }
    /// Original optional qualifiers container; each child was checked for the
    /// schema's `CommitmentTypeQualifier` role, whose `AnyType` payload stays open.
    #[must_use]
    pub fn qualifiers(&self) -> Option<N> {
        self.qualifiers
    }
}

/// Original commitment using this XML owner's identities.
pub type Original<X> = Reading<<X as Xml>::Id, <X as Xml>::Document, <X as Xml>::Generation>;

/// Reads the complete §5.2.3 schema choice from covered original properties.
/// A commitment Identifier must not carry Qualifier, including an empty one.
/// Current Work funds even skipped metadata and every retained reference.
/// <https://www.etsi.org/deliver/etsi_en/319100_319199/31913201/01.03.01_60/en_31913201v010301p.pdf>
pub fn read<X: Xml, W: Work>(
    access: &Access<'_, X, W>,
    signed: &properties::Reading<X>,
    index: usize,
) -> PropertyResult<Original<X>, X> {
    let node = selected(access, signed, index)?;
    let mut kind = None;
    let mut all = None;
    let mut references = access
        .query(node, |_, _, work| Ok(work.vector()))
        .map_err(Error::Access)?;
    let mut qualifiers = None;
    for child in access.children(node) {
        let child = child.map_err(Error::Child)?;
        if named(access, child, "CommitmentTypeId")? {
            if kind.is_some() {
                return Err(Error::Duplicate { node: child });
            }
            if all.is_some() || !references.is_empty() || qualifiers.is_some() {
                return Err(Error::Order { node: child });
            }
            let original = identifier::read(access, child)?;
            if original.qualifier.is_some() {
                return Err(Error::Forbidden {
                    node: original.identifier,
                });
            }
            kind = Some(original);
        } else if named(access, child, "ObjectReference")? {
            if kind.is_none() || qualifiers.is_some() {
                return Err(Error::Order { node: child });
            }
            if all.is_some() {
                return Err(Error::Unexpected { node: child });
            }
            properties::simple_content(access, child)?;
            references
                .try_push(child)
                .map_err(|cause| Error::Resource { node: child, cause })?;
        } else if named(access, child, "AllSignedDataObjects")? {
            if kind.is_none() || qualifiers.is_some() {
                return Err(Error::Order { node: child });
            }
            if all.is_some() {
                return Err(Error::Duplicate { node: child });
            }
            if !references.is_empty() {
                return Err(Error::Unexpected { node: child });
            }
            if let Some(nested) = access.children(child).next() {
                return Err(Error::Unexpected {
                    node: nested.map_err(Error::Child)?,
                });
            }
            all = Some(child);
        } else if named(access, child, "CommitmentTypeQualifiers")? {
            if qualifiers.is_some() {
                return Err(Error::Duplicate { node: child });
            }
            if kind.is_none() || (all.is_none() && references.is_empty()) {
                return Err(Error::Order { node: child });
            }
            for qualifier in access.children(child) {
                let qualifier = qualifier.map_err(Error::Child)?;
                if !named(access, qualifier, "CommitmentTypeQualifier")? {
                    return Err(Error::Unexpected { node: qualifier });
                }
            }
            qualifiers = Some(child);
        } else {
            return Err(Error::Unexpected { node: child });
        }
    }
    let kind = kind.ok_or(Error::Missing {
        parent: node,
        local: "CommitmentTypeId",
    })?;
    let objects = if let Some(all) = all {
        Objects::All(all)
    } else {
        if references.is_empty() {
            return Err(Error::Missing {
                parent: node,
                local: "ObjectReference or AllSignedDataObjects",
            });
        }
        Objects::References(references.into_owned())
    };
    Ok(Reading {
        snapshot: signed.snapshot(),
        signature: signed.signature(),
        node,
        kind,
        objects,
        qualifiers,
    })
}

/// Original instruction eligible for this commitment, not application acceptance.
#[derive(Debug, Clone, Copy)]
pub struct Object<N> {
    /// Original authenticated Reference node.
    pub reference: N,
    /// Its original `SignedInfo` or authenticated Manifest parent.
    pub parent: Parent<N>,
}

/// Original commitment and completed reference associations. This preserves
/// source facts, not URI lexical validity or acceptance of the claimed meaning.
pub struct Association<N, D, G> {
    original: Reading<N, D, G>,
    objects: OwnedVec<Object<N>>,
}

impl<N, D, G> Association<N, D, G> {
    /// Original covered commitment structure.
    #[must_use]
    pub fn original(&self) -> &Reading<N, D, G> {
        &self.original
    }
    /// Actual original eligible instructions in declared/catalogue order.
    #[must_use]
    pub fn objects(&self) -> &[Object<N>] {
        &self.objects
    }
}

/// Associated commitment using only this owner's stable identity domain; it
/// does not retain the nominal XML adapter or a temporary document-view borrow.
pub type Prepared<X> = Association<<X as Xml>::Id, <X as Xml>::Document, <X as Xml>::Generation>;

/// Reads and associates a commitment through the same two kernels used by
/// individual queries. Partial reference results are never returned on refusal.
pub fn prepare<X: ReferenceXml, W: Work>(
    access: &Access<'_, X, W>,
    bound: &Bound<X::Id, X::Document, X::Generation>,
    signed: &properties::Reading<X>,
    index: usize,
) -> Result<Prepared<X>, AssociationError<X::Error, X::Id>> {
    let original = read(access, signed, index).map_err(AssociationError::Property)?;
    let objects = associate(access, bound, &original)?;
    Ok(Association { original, objects })
}

/// Association refusal preserving resource, processing and lookup distinctions.
#[derive(Debug)]
pub enum AssociationError<E, N> {
    /// Original common source/schema/admission cause.
    Property(Error<E, N>),
    /// XML resolver/catalogue processing cause.
    Processing(E),
    /// Explicit original URI did not resolve to an authenticated instruction.
    Lookup {
        /// Original `ObjectReference` element.
        node: N,
        /// Exact lookup outcome.
        resolution: Resolution<()>,
    },
    /// Referenced data was not checked, or it is `SignedProperties`.
    Ineligible {
        /// Original Reference node.
        node: N,
    },
    /// Original catalogue was not completely processed.
    Incomplete {
        /// Original unprocessed instruction/container.
        node: N,
    },
}

/// Resolves explicit references or expands the original all-objects marker via
/// the same authenticated catalogue. No hash, implicit acquisition, fabricated
/// URI, re-verification or application-policy acceptance occurs here.
pub fn associate<X: ReferenceXml, W: Work>(
    access: &Access<'_, X, W>,
    bound: &Bound<X::Id, X::Document, X::Generation>,
    original: &Original<X>,
) -> AssociationResult<OwnedVec<Object<X::Id>>, X> {
    let property = AssociationError::Property;
    bind_original(access, bound, original)?;
    let mut output = access
        .query(original.node, |_, _, work| Ok(work.vector()))
        .map_err(|cause| property(Error::Access(cause)))?;
    let is_properties = |r: crate::binding::Reference<'_, X::Id>| match r.target {
        Some(target) => access
            .equal_id(target, bound.containers().signed())
            .map_err(|cause| {
                property(Error::Resource {
                    node: r.node,
                    cause,
                })
            }),
        None => Ok(false),
    };
    let mut retain = |descriptor: Descriptor<'_, X::Id>| {
        let r = descriptor.reference;
        if r.status != Status::Valid {
            return Err(AssociationError::Ineligible { node: r.node });
        }
        output
            .try_push(Object {
                reference: r.node,
                parent: descriptor.parent,
            })
            .map_err(|cause| {
                property(Error::Resource {
                    node: r.node,
                    cause,
                })
            })
    };
    match &original.objects {
        Objects::References(nodes) => {
            for index in 0..nodes.len() {
                admit(access, original.node).map_err(property)?;
                let node = nodes[index];
                let resolved = access
                    .query(node, |xml, snapshot, work| {
                        xml.resolve_reference(
                            snapshot,
                            original.signature,
                            node,
                            LexicalSource::SimpleContent,
                            work,
                            |descriptor| {
                                let r = descriptor.reference;
                                if r.status != Status::Valid || is_properties(r)? {
                                    return Err(AssociationError::Ineligible { node: r.node });
                                }
                                retain(descriptor)
                            },
                        )
                    })
                    .map_err(|cause| property(Error::Access(cause)))?;
                match resolved {
                    Resolution::Resolved(value) => value?,
                    other => {
                        return Err(AssociationError::Lookup {
                            node,
                            resolution: other.map(|_| ()),
                        });
                    }
                }
            }
        }
        Objects::All(_) => {
            let visited = access
                .query(original.node, |xml, snapshot, work| {
                    Ok(xml.visit_authenticated_references(
                        snapshot,
                        original.signature,
                        work,
                        |descriptor| {
                            if is_properties(descriptor.reference)? {
                                return Ok(());
                            }
                            retain(descriptor)
                        },
                    ))
                })
                .map_err(|cause| property(Error::Access(cause)))?;
            let complete = match visited {
                Ok(value) => value,
                Err(VisitError::Processing(cause)) => {
                    return Err(AssociationError::Processing(cause));
                }
                Err(VisitError::Visitor(cause)) => return Err(cause),
            };
            match complete {
                Completeness::Complete => {}
                Completeness::Unchecked(node) | Completeness::Incomplete(node) => {
                    return Err(AssociationError::Incomplete { node });
                }
            }
        }
    }
    Ok(output.into_owned())
}

fn bind_original<X: Xml, W: Work>(
    access: &Access<'_, X, W>,
    bound: &Bound<X::Id, X::Document, X::Generation>,
    original: &Original<X>,
) -> AssociationResult<(), X> {
    let property = AssociationError::Property;
    if access.snapshot() != original.snapshot || bound.containers().snapshot() != original.snapshot
    {
        return Err(property(Error::Access(AccessError::Changed {
            node: original.node,
        })));
    }
    if !access
        .equal_id(bound.containers().signature(), original.signature)
        .map_err(|cause| {
            property(Error::Resource {
                node: original.node,
                cause,
            })
        })?
    {
        return Err(property(Error::Access(AccessError::Changed {
            node: original.node,
        })));
    }
    Ok(())
}

/// Association result preserving this owner's processing and original node types.
pub type AssociationResult<T, X> = Result<T, AssociationError<<X as Xml>::Error, <X as Xml>::Id>>;

fn selected<X: Xml, W: Work>(
    access: &Access<'_, X, W>,
    signed: &properties::Reading<X>,
    index: usize,
) -> PropertyResult<X::Id, X> {
    if access.snapshot() != signed.snapshot() {
        return Err(Error::Access(AccessError::Changed {
            node: signed.parent(),
        }));
    }
    admit(access, signed.parent())?;
    let selection = || Error::Selection {
        parent: signed.parent(),
        index,
    };
    let entry = signed
        .data_objects()
        .and_then(|objects| objects.entries().get(index))
        .ok_or_else(selection)?;
    if entry.role != DataObjectRole::Commitment {
        return Err(selection());
    }
    Ok(entry.node)
}

fn admit<X: Xml, W: Work>(access: &Access<'_, X, W>, node: X::Id) -> PropertyResult<(), X> {
    access
        .query(node, |_, _, work| Ok(work.charge_work(1)))
        .map_err(Error::Access)?
        .map_err(|cause| Error::Resource { node, cause })
}

fn named<X: Xml, W: Work>(
    access: &Access<'_, X, W>,
    node: X::Id,
    local: &str,
) -> PropertyResult<bool, X> {
    access
        .has_name(
            node,
            Name {
                namespace: XADES,
                local,
            },
        )
        .map_err(Error::Access)
}