structured-pades 0.17.1

Original-source PDF signatures and provider-neutral PAdES baseline validation
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
//! Permission categories derive from actual graph edges, never object numbers.

use super::{AssessmentError, Outcome, Permission};
use crate::{
    document::{Document, GraphObject},
    error::{Error, ErrorKind, ReadFailure, Syntax},
    fields::Fields,
    model::admit,
    object::Type,
    range::RangeSource,
};
use structured_asn1::memory::Work;
mod pages;
#[cfg(test)]
mod tests;

enum FieldChange {
    Permitted(bool),
    Refused(Outcome),
}

impl<S: RangeSource> Document<'_, S> {
    async fn existing_field<'a, 's>(
        &self,
        before: &Fields<'a, 's, S::Document, S::Generation>,
        after: &Fields<'a, 's, S::Document, S::Generation>,
        (ordinal, index): (usize, usize),
        permission: Permission,
        work: &impl Work,
    ) -> Result<FieldChange, ReadFailure<S::Error>> {
        let field = &before.values()[ordinal];
        let mut added_signature = false;
        let current = &after.values()[index];
        if field.is_signature() != current.is_signature() {
            return Ok(FieldChange::Refused(Outcome::Forbidden));
        }
        if !self
            .same_dictionary_except(
                field.object(),
                current.object(),
                &[b"V", b"AP", b"AS", b"Kids", b"Parent", b"P"],
                work,
            )
            .await?
        {
            return Ok(FieldChange::Refused(Outcome::Forbidden));
        }
        if field.object().get(b"P", work)?.map(GraphObject::identity)
            != current.object().get(b"P", work)?.map(GraphObject::identity)
        {
            return Ok(FieldChange::Refused(Outcome::Forbidden));
        }
        if field.parent().map(|at| before.values()[at].name())
            != current.parent().map(|at| after.values()[at].name())
        {
            return Ok(FieldChange::Refused(Outcome::Forbidden));
        }
        let first = before.value(ordinal, work)?;
        let second = after.value(index, work)?;
        let same = self.same_optional(first, second, work).await?;
        if !same {
            if field.is_signature() {
                if first.is_some_and(|value| value.direct().kind() != Type::Null) {
                    return Ok(FieldChange::Refused(Outcome::Forbidden));
                }
                if !timestamp(second, work)? && permission == Permission::NoChanges {
                    return Ok(FieldChange::Refused(Outcome::Forbidden));
                }
                if !signature(second, work)? {
                    return Ok(FieldChange::Refused(Outcome::Indeterminate));
                }
                added_signature = true;
            } else {
                if permission == Permission::NoChanges {
                    return Ok(FieldChange::Refused(Outcome::Forbidden));
                }
                if before
                    .inherited(ordinal, b"Ff", work)?
                    .map(|value| value.direct().unsigned(work))
                    .transpose()?
                    .unwrap_or(0)
                    & 1
                    != 0
                {
                    return Ok(FieldChange::Refused(Outcome::Forbidden));
                }
                if !form_value(after.inherited(index, b"FT", work)?, second, work)? {
                    return Ok(FieldChange::Refused(Outcome::Indeterminate));
                }
            }
        }
        let first_ap = field.object().get(b"AP", work)?;
        let second_ap = current.object().get(b"AP", work)?;
        if !self.same_optional(first_ap, second_ap, work).await? {
            // Appearance updates require an actual permitted value change.
            // Graphics semantics not established here remain unavailable.
            if same {
                return Ok(FieldChange::Refused(Outcome::Forbidden));
            }
            if !appearance(second_ap, work)? {
                return Ok(FieldChange::Refused(Outcome::Indeterminate));
            }
        }
        let old_state = field.object().get(b"AS", work)?;
        let new_state = current.object().get(b"AS", work)?;
        if !self.same_optional(old_state, new_state, work).await?
            && (same || !state(new_state, second, work)?)
        {
            return Ok(FieldChange::Refused(Outcome::Indeterminate));
        }
        Ok(FieldChange::Permitted(added_signature))
    }

    async fn form_changes(
        &self,
        old: GraphObject<'_, '_, S::Document, S::Generation>,
        new: GraphObject<'_, '_, S::Document, S::Generation>,
        added_signature: bool,
        copied: bool,
        work: &impl Work,
    ) -> Result<Outcome, ReadFailure<S::Error>> {
        let first_form = old.get(b"AcroForm", work)?;
        let second_form = new.get(b"AcroForm", work)?;
        match (first_form, second_form) {
            (Some(a), Some(b)) => {
                if !self
                    .same_dictionary_except(a, b, &[b"Fields", b"SigFlags"], work)
                    .await?
                {
                    return Ok(Outcome::Indeterminate);
                }
                let first = flags(a, work)?;
                let second = flags(b, work)?;
                if first != second && (!added_signature || first & !second != 0 || second & !3 != 0)
                {
                    return Ok(Outcome::Forbidden);
                }
            }
            (None, None) => {}
            (None, Some(form)) if added_signature || copied => {
                let members = form.members(work).ok_or(Error {
                    kind: ErrorKind::Syntax(Syntax::Dictionary),
                    location: form.direct().location(),
                })?;
                let mut key = true;
                for member in members {
                    let member = member?;
                    if key
                        && !member.direct().name_equals(b"Fields", work)?
                        && !member.direct().name_equals(b"SigFlags", work)?
                    {
                        return Ok(Outcome::Indeterminate);
                    }
                    key = !key;
                }
            }
            _ => return Ok(Outcome::Forbidden),
        }
        Ok(Outcome::Permitted)
    }

    #[expect(
        clippy::result_large_err,
        reason = "validation-update interpretation retains the actual material error and its original encoded enclosure rather than flattening or boxing it"
    )]
    pub(super) async fn classify_changes<'a, 's>(
        &self,
        before: &Fields<'a, 's, S::Document, S::Generation>,
        after: &Fields<'a, 's, S::Document, S::Generation>,
        permission: Permission,
        work: &impl Work,
    ) -> Result<Outcome, AssessmentError<S::Error>> {
        // ISO32000-1 §12.8.2.2/Table254 and EN319142-1 §5.4.3.
        // Exemptions are attached to catalog/field edges. A shared appearance
        // or DSS object reached through page contents is still compared there.
        // https://opensource.adobe.com/dc-acrobat-sdk-docs/pdfstandards/PDF32000_2008.pdf
        let old = before.graph.root();
        let new = after.graph.root();
        if !self
            .same_dictionary_except(old, new, &[b"DSS", b"AcroForm", b"Pages"], work)
            .await?
        {
            return Ok(Outcome::Forbidden);
        }
        let mut copies = work.map();
        let pages = self
            .classify_pages(before, after, permission, &mut copies, work)
            .await?;
        if pages != Outcome::Permitted {
            return Ok(pages);
        }

        let mut added_signature = false;
        for (ordinal, field) in before.values().iter().enumerate() {
            admit(work, field.object().direct().location(), 1)?;
            let Some(index) = after.get(field.name(), work)? else {
                return Ok(Outcome::Forbidden);
            };
            match self
                .existing_field(before, after, (ordinal, index), permission, work)
                .await?
            {
                FieldChange::Permitted(added) => {
                    if added {
                        added_signature = true;
                    }
                }
                FieldChange::Refused(outcome) => return Ok(outcome),
            }
        }
        for (ordinal, field) in after.values().iter().enumerate() {
            admit(work, field.object().direct().location(), 1)?;
            if before.get(field.name(), work)?.is_some() {
                continue;
            }
            let value = after.value(ordinal, work)?;
            let object = field.object();
            if copies
                .get(&(object.owner, object.node.0, 0), work)
                .map_err(|cause| Error {
                    kind: ErrorKind::Resource(cause),
                    location: object.direct().location(),
                })?
                .is_some()
            {
                if field.is_signature() && signature(value, work)? {
                    added_signature = true;
                }
                continue;
            }
            if !field.is_signature() || !signature(value, work)? {
                return Ok(Outcome::Forbidden);
            }
            if !timestamp(value, work)? && permission == Permission::NoChanges {
                return Ok(Outcome::Forbidden);
            }
            added_signature = true;
        }
        let form = self
            .form_changes(old, new, added_signature, !copies.is_empty(), work)
            .await?;
        if form != Outcome::Permitted {
            return Ok(form);
        }
        // DSS is a validation-update exemption, but all references and values
        // must still be a readable same-revision validation structure.
        let validation = self.validation(after.graph.revision(), work).await?;
        let parsed = validation.parse(work).map_err(AssessmentError::Material)?;
        if !parsed.all_values_readable() || !validation.all_vri_material_in_dss() {
            return Ok(Outcome::Indeterminate);
        }
        Ok(Outcome::Permitted)
    }

    async fn same_optional<'a, 's>(
        &self,
        a: Option<GraphObject<'a, 's, S::Document, S::Generation>>,
        b: Option<GraphObject<'a, 's, S::Document, S::Generation>>,
        work: &impl Work,
    ) -> Result<bool, ReadFailure<S::Error>> {
        match (a, b) {
            (Some(a), Some(b)) => self.same_graph_values(a, b, work).await,
            (None, None) => Ok(true),
            _ => Ok(false),
        }
    }
}

fn timestamp<D: Copy + Eq, G: Copy + Eq>(
    value: Option<GraphObject<'_, '_, D, G>>,
    work: &impl Work,
) -> crate::Result<bool> {
    let Some(value) = value else { return Ok(false) };
    if value.direct().kind() != Type::Dictionary {
        return Ok(false);
    }
    Ok(value
        .get(b"Type", work)?
        .is_some_and(|kind| kind.direct().kind() == Type::Name)
        && value
            .get(b"Type", work)?
            .expect("present Type")
            .direct()
            .name_equals(b"DocTimeStamp", work)?)
}

fn signature<D: Copy + Eq, G: Copy + Eq>(
    value: Option<GraphObject<'_, '_, D, G>>,
    work: &impl Work,
) -> crate::Result<bool> {
    let Some(value) = value else { return Ok(false) };
    if value.direct().kind() != Type::Dictionary {
        return Ok(false);
    }
    Ok(value
        .get(b"ByteRange", work)?
        .is_some_and(|value| value.direct().kind() == Type::Array)
        && value
            .get(b"Contents", work)?
            .is_some_and(|value| value.direct().kind() == Type::String))
}

fn flags<D: Copy + Eq, G: Copy + Eq>(
    form: GraphObject<'_, '_, D, G>,
    work: &impl Work,
) -> crate::Result<u64> {
    form.get(b"SigFlags", work)?
        .map(|value| value.direct().unsigned(work))
        .transpose()
        .map(|value| value.unwrap_or(0))
}

fn form_value<D: Copy + Eq, G: Copy + Eq>(
    kind: Option<GraphObject<'_, '_, D, G>>,
    value: Option<GraphObject<'_, '_, D, G>>,
    work: &impl Work,
) -> crate::Result<bool> {
    let Some(value) = value else { return Ok(true) };
    if value.direct().kind() == Type::Null {
        return Ok(true);
    }
    let Some(kind) = kind else { return Ok(false) };
    if kind.direct().name_equals(b"Tx", work)? {
        return Ok(value.direct().kind() == Type::String);
    }
    if kind.direct().name_equals(b"Btn", work)? {
        return Ok(value.direct().kind() == Type::Name);
    }
    if kind.direct().name_equals(b"Ch", work)? {
        if value.direct().kind() == Type::String {
            return Ok(true);
        }
        if value.direct().kind() != Type::Array {
            return Ok(false);
        }
        for entry in value.members(work).expect("array") {
            let Some(entry) = entry?.resolved(work)? else {
                return Ok(false);
            };
            if entry.direct().kind() != Type::String {
                return Ok(false);
            }
        }
        return Ok(true);
    }
    Ok(false)
}

fn state<D: Copy + Eq, G: Copy + Eq>(
    state: Option<GraphObject<'_, '_, D, G>>,
    value: Option<GraphObject<'_, '_, D, G>>,
    work: &impl Work,
) -> crate::Result<bool> {
    let Some(state) = state else { return Ok(true) };
    if state.direct().kind() != Type::Name {
        return Ok(false);
    }
    if state.direct().name_equals(b"Off", work)? {
        return Ok(true);
    }
    let Some(value) = value else { return Ok(false) };
    let a = state.direct();
    let b = value.direct();
    if b.kind() != Type::Name {
        return Ok(false);
    }
    Ok(crate::dictionary::compare_names_between(
        &a.owner.arena,
        a.node,
        &b.owner.arena,
        b.node,
        work,
    )? == core::cmp::Ordering::Equal)
}

fn appearance<D: Copy + Eq, G: Copy + Eq>(
    value: Option<GraphObject<'_, '_, D, G>>,
    work: &impl Work,
) -> crate::Result<bool> {
    let Some(value) = value else { return Ok(true) };
    if value.direct().kind() != Type::Dictionary {
        return Ok(false);
    }
    for name in [b"N".as_slice(), b"R", b"D"] {
        let Some(entry) = value.get(name, work)? else {
            continue;
        };
        if entry.direct().kind() != Type::Dictionary {
            return Ok(false);
        }
        if entry.get(b"Subtype", work)?.is_some() {
            if !form(entry, work)? {
                return Ok(false);
            }
        } else {
            let mut members = entry.members(work).expect("appearance state dictionary");
            while let Some(key) = members.next() {
                if key?.direct().kind() != Type::Name {
                    return Ok(false);
                }
                let Some(value) = members.next() else {
                    return Ok(false);
                };
                let Some(value) = value?.resolved(work)? else {
                    return Ok(false);
                };
                if !form(value, work)? {
                    return Ok(false);
                }
            }
        }
    }
    Ok(true)
}

fn form<D: Copy + Eq, G: Copy + Eq>(
    value: GraphObject<'_, '_, D, G>,
    work: &impl Work,
) -> crate::Result<bool> {
    if value.direct().kind() != Type::Dictionary {
        return Ok(false);
    }
    let reading = value.graph.nodes[value.owner]
        .reading
        .as_ref()
        .expect("graph owner");
    if value.node.0 != reading.value.root.0 || reading.stream.is_none() {
        return Ok(false);
    }
    let Some(kind) = value.get(b"Subtype", work)? else {
        return Ok(false);
    };
    kind.direct().name_equals(b"Form", work)
}