pdf_signer 0.3.3

Sign PDF documents with a PKCS#12 keystore and verify their signatures (PAdES B-B → B-LTA).
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
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
//! Incremental update writer.
//!
//! Instead of re-serializing the whole document (which would invalidate any
//! pre-existing signature), an incremental update keeps the original bytes
//! **verbatim** and appends:
//!   * the new / modified objects,
//!   * a fresh cross-reference table listing only those objects,
//!   * a trailer whose `/Prev` chains back to the previous xref and that
//!     carries the previous trailer's entries (`/Info`, `/ID`, …) forward.
//!
//! This is what makes multi-signature work: signing again only appends, so
//! earlier signatures keep covering an unchanged byte range.
//!
//! Offsets written into the new cross-reference section are relative to the
//! `%PDF-` header (PDF 32000-1 §7.5.2), which matters for files that carry
//! junk bytes before the header; `/ByteRange` offsets, by contrast, are always
//! absolute file offsets and are handled by the signer.

use std::collections::BTreeMap;

use lopdf::{Dictionary, Object, ObjectId, Stream, StringFormat};

use crate::util::find_sub;

/// Accumulates the objects to append and renders the updated file.
pub(crate) struct Incremental<'a> {
    original: &'a [u8],
    /// Offset of `%PDF-` in `original` (0 for a well-formed file).
    base: usize,
    objects: BTreeMap<u32, (u16, Object)>,
}

/// The rendered file plus the absolute offset at which each appended object's
/// `N G obj` header starts, so callers can locate placeholders inside a
/// specific object instead of searching the bytes.
pub(crate) struct Rendered {
    pub bytes: Vec<u8>,
    pub offsets: BTreeMap<u32, usize>,
}

/// One appended object for the cross-reference section: `(id, generation,
/// absolute offset)`.
type XrefEntry = (u32, u16, usize);

/// Trailer keys that belong to a specific cross-reference section (or to an
/// xref *stream* object) and must not be carried into the next trailer.
const SECTION_KEYS: &[&[u8]] = &[
    b"Size", b"Prev", b"XRefStm", b"Type", b"W", b"Index", b"Filter", b"Length",
    b"DecodeParms", b"DL", b"F", b"FFilter", b"FDecodeParms",
];

impl<'a> Incremental<'a> {
    pub(crate) fn new(original: &'a [u8]) -> Self {
        Self {
            original,
            base: header_offset(original),
            objects: BTreeMap::new(),
        }
    }

    /// Queue an object (new or modified) for the update section.
    pub(crate) fn add(&mut self, id: ObjectId, obj: Object) {
        self.objects.insert(id.0, (id.1, obj));
    }

    /// Render `original + appended update`. `size` is the new `/Size`,
    /// `root` the catalog id, `prev` the previous `startxref` offset, and
    /// `trailer` the previous trailer whose document-level entries (`/Info`,
    /// `/ID`, …) are carried forward. Emits a cross-reference **stream** when
    /// the original uses one, otherwise a traditional cross-reference **table**
    /// — matching the source's convention.
    pub(crate) fn render(
        &self,
        size: u32,
        root: ObjectId,
        prev: usize,
        trailer: &Dictionary,
    ) -> Rendered {
        let extra = carried_trailer(trailer);
        if uses_xref_stream(self.original, self.base) {
            self.render_xref_stream(size, root, prev, &extra)
        } else {
            self.render_xref_table(size, root, prev, &extra)
        }
    }

    /// Emit the queued objects, returning the buffer, the appended entries
    /// `(id, gen, absolute offset)` and the absolute offsets map.
    fn emit_objects(&self) -> (Vec<u8>, Vec<XrefEntry>, BTreeMap<u32, usize>) {
        let mut out = self.original.to_vec();
        if !out.ends_with(b"\n") {
            out.push(b'\n');
        }
        let mut entries: Vec<XrefEntry> = Vec::with_capacity(self.objects.len() + 1);
        let mut offsets = BTreeMap::new();
        for (&id, (gen, obj)) in &self.objects {
            let off = out.len();
            out.extend_from_slice(format!("{} {} obj\n", id, gen).as_bytes());
            write_object(&mut out, obj);
            out.extend_from_slice(b"\nendobj\n");
            entries.push((id, *gen, off));
            offsets.insert(id, off);
        }
        (out, entries, offsets)
    }

    /// Traditional cross-reference table incremental update.
    fn render_xref_table(
        &self,
        size: u32,
        root: ObjectId,
        prev: usize,
        extra: &Dictionary,
    ) -> Rendered {
        let (mut out, entries, offsets) = self.emit_objects();

        // Cross-reference table (entries are already sorted by id); offsets are
        // relative to the %PDF- header.
        let xref_off = out.len() - self.base;
        out.extend_from_slice(b"xref\n");
        let mut i = 0;
        while i < entries.len() {
            let mut j = i;
            while j + 1 < entries.len() && entries[j + 1].0 == entries[j].0 + 1 {
                j += 1;
            }
            out.extend_from_slice(format!("{} {}\n", entries[i].0, j - i + 1).as_bytes());
            for e in &entries[i..=j] {
                // Each entry is exactly 20 bytes: "%010d %05d n\r\n".
                out.extend_from_slice(
                    format!("{:010} {:05} n\r\n", e.2 - self.base, e.1).as_bytes(),
                );
            }
            i = j + 1;
        }

        // Trailer + startxref.
        let mut trailer = Dictionary::new();
        trailer.set("Size", Object::Integer(size as i64));
        trailer.set("Root", Object::Reference(root));
        trailer.set("Prev", Object::Integer(prev as i64));
        for (k, v) in extra.iter() {
            trailer.set(k.clone(), v.clone());
        }
        out.extend_from_slice(b"trailer\n");
        write_dict(&mut out, &trailer);
        out.extend_from_slice(b"\nstartxref\n");
        out.extend_from_slice(format!("{}\n%%EOF\n", xref_off).as_bytes());
        Rendered { bytes: out, offsets }
    }

    /// Cross-reference **stream** incremental update (PDF 1.5+). The xref is a
    /// `/Type /XRef` stream object that also indexes itself.
    fn render_xref_stream(
        &self,
        size: u32,
        root: ObjectId,
        prev: usize,
        extra: &Dictionary,
    ) -> Rendered {
        let (mut out, mut entries, offsets) = self.emit_objects();

        // The xref stream is itself an object; it indexes itself.
        let xref_id = size; // next free id
        let xref_off = out.len();
        entries.push((xref_id, 0, xref_off));
        entries.sort_by_key(|e| e.0);

        // Field widths W = [1, n, 2], with the offset width chosen to fit the
        // largest offset (4 bytes below 4 GiB, 8 above).
        let max_off = entries.iter().map(|e| e.2 - self.base).max().unwrap_or(0);
        let off_w: usize = if max_off > u32::MAX as usize { 8 } else { 4 };
        let mut data = Vec::with_capacity(entries.len() * (3 + off_w));
        for (_, gen, off) in &entries {
            data.push(1u8); // type 1: in-use object
            let rel = (*off - self.base) as u64;
            data.extend_from_slice(&rel.to_be_bytes()[8 - off_w..]);
            data.extend_from_slice(&gen.to_be_bytes());
        }
        let compressed = zlib_compress(&data);

        // /Index subsections for the (sorted) object ids.
        let mut index = Vec::new();
        let mut i = 0;
        while i < entries.len() {
            let mut j = i;
            while j + 1 < entries.len() && entries[j + 1].0 == entries[j].0 + 1 {
                j += 1;
            }
            index.push(Object::Integer(entries[i].0 as i64));
            index.push(Object::Integer((j - i + 1) as i64));
            i = j + 1;
        }

        let mut dict = Dictionary::new();
        dict.set("Type", Object::Name(b"XRef".to_vec()));
        dict.set("Size", Object::Integer(xref_id as i64 + 1));
        dict.set("Root", Object::Reference(root));
        dict.set("Prev", Object::Integer(prev as i64));
        dict.set(
            "W",
            Object::Array(vec![
                Object::Integer(1),
                Object::Integer(off_w as i64),
                Object::Integer(2),
            ]),
        );
        dict.set("Index", Object::Array(index));
        for (k, v) in extra.iter() {
            dict.set(k.clone(), v.clone());
        }
        dict.set("Filter", Object::Name(b"FlateDecode".to_vec()));
        dict.set("Length", Object::Integer(compressed.len() as i64));

        out.extend_from_slice(format!("{} 0 obj\n", xref_id).as_bytes());
        write_dict(&mut out, &dict);
        out.extend_from_slice(b"\nstream\n");
        out.extend_from_slice(&compressed);
        out.extend_from_slice(b"\nendstream\nendobj\n");

        out.extend_from_slice(b"startxref\n");
        out.extend_from_slice(format!("{}\n%%EOF\n", xref_off - self.base).as_bytes());
        Rendered { bytes: out, offsets }
    }
}

/// The entries of the previous trailer worth carrying into the new one:
/// everything but the per-section keys (and never `/Encrypt`, which the signer
/// refuses up front).
fn carried_trailer(trailer: &Dictionary) -> Dictionary {
    let mut extra = Dictionary::new();
    for (k, v) in trailer.iter() {
        if SECTION_KEYS.contains(&k.as_slice()) || k == b"Root" || k == b"Encrypt" {
            continue;
        }
        extra.set(k.clone(), v.clone());
    }
    extra
}

/// Offset of the `%PDF-` header within the first 1024 bytes (readers tolerate
/// leading junk, and file offsets are then relative to the header). 0 when the
/// header is missing or at the start.
pub(crate) fn header_offset(buf: &[u8]) -> usize {
    let window = &buf[..buf.len().min(1024)];
    find_sub(window, b"%PDF-").unwrap_or(0)
}

/// True if the file's most recent cross-reference is a stream (not an `xref`
/// table), i.e. `startxref` points at an object rather than the `xref` keyword.
fn uses_xref_stream(buf: &[u8], base: usize) -> bool {
    let Some(off) = last_startxref(buf) else {
        return false;
    };
    let mut i = off + base;
    while i < buf.len() && buf[i].is_ascii_whitespace() {
        i += 1;
    }
    i < buf.len() && !buf[i..].starts_with(b"xref")
}

/// Zlib-compress (PDF `FlateDecode`).
pub(crate) fn zlib_compress(data: &[u8]) -> Vec<u8> {
    use flate2::write::ZlibEncoder;
    use flate2::Compression;
    use std::io::Write;
    let mut encoder = ZlibEncoder::new(Vec::new(), Compression::default());
    encoder.write_all(data).expect("zlib write");
    encoder.finish().expect("zlib finish")
}

/// Value of the most recent `startxref` in `buf` (the previous xref offset,
/// relative to the `%PDF-` header as the file states it).
pub(crate) fn last_startxref(buf: &[u8]) -> Option<usize> {
    let needle = b"startxref";
    let pos = (0..=buf.len().saturating_sub(needle.len()))
        .rev()
        .find(|&i| &buf[i..i + needle.len()] == needle)?;
    let mut i = pos + needle.len();
    while i < buf.len() && buf[i].is_ascii_whitespace() {
        i += 1;
    }
    let mut n = 0usize;
    let mut any = false;
    while i < buf.len() && buf[i].is_ascii_digit() {
        n = n.checked_mul(10)?.checked_add((buf[i] - b'0') as usize)?;
        i += 1;
        any = true;
    }
    any.then_some(n)
}

// --- a minimal, byte-exact PDF object serializer ------------------------------

fn write_object(out: &mut Vec<u8>, obj: &Object) {
    match obj {
        Object::Null => out.extend_from_slice(b"null"),
        Object::Boolean(b) => out.extend_from_slice(if *b { b"true" } else { b"false" }),
        Object::Integer(i) => out.extend_from_slice(i.to_string().as_bytes()),
        Object::Real(r) => out.extend_from_slice(fmt_real(*r).as_bytes()),
        Object::Name(n) => write_name(out, n),
        Object::String(s, fmt) => write_string(out, s, *fmt),
        Object::Reference(id) => {
            out.extend_from_slice(format!("{} {} R", id.0, id.1).as_bytes())
        }
        Object::Array(a) => {
            out.push(b'[');
            for (i, e) in a.iter().enumerate() {
                if i > 0 {
                    out.push(b' ');
                }
                write_object(out, e);
            }
            out.push(b']');
        }
        Object::Dictionary(d) => write_dict(out, d),
        Object::Stream(s) => write_stream(out, s),
    }
}

fn write_dict(out: &mut Vec<u8>, d: &Dictionary) {
    out.extend_from_slice(b"<< ");
    for (k, v) in d.iter() {
        write_name(out, k);
        out.push(b' ');
        write_object(out, v);
        out.push(b' ');
    }
    out.extend_from_slice(b">>");
}

fn write_stream(out: &mut Vec<u8>, s: &Stream) {
    let mut dict = s.dict.clone();
    dict.set("Length", Object::Integer(s.content.len() as i64));
    write_dict(out, &dict);
    out.extend_from_slice(b"\nstream\n");
    out.extend_from_slice(&s.content);
    out.extend_from_slice(b"\nendstream");
}

fn write_name(out: &mut Vec<u8>, name: &[u8]) {
    out.push(b'/');
    for &b in name {
        let regular = (0x21..0x7f).contains(&b)
            && !matches!(
                b,
                b'(' | b')' | b'<' | b'>' | b'[' | b']' | b'{' | b'}' | b'/' | b'%' | b'#'
            );
        if regular {
            out.push(b);
        } else {
            out.extend_from_slice(format!("#{:02X}", b).as_bytes());
        }
    }
}

fn write_string(out: &mut Vec<u8>, s: &[u8], fmt: StringFormat) {
    match fmt {
        StringFormat::Hexadecimal => {
            out.push(b'<');
            for &b in s {
                out.extend_from_slice(format!("{:02x}", b).as_bytes());
            }
            out.push(b'>');
        }
        StringFormat::Literal => {
            out.push(b'(');
            for &b in s {
                match b {
                    b'(' | b')' | b'\\' => {
                        out.push(b'\\');
                        out.push(b);
                    }
                    b'\n' => out.extend_from_slice(b"\\n"),
                    b'\r' => out.extend_from_slice(b"\\r"),
                    _ => out.push(b),
                }
            }
            out.push(b')');
        }
    }
}

/// Format a real without scientific notation (PDF forbids it). Non-finite
/// values are rejected upstream (`sign.rs` validates appearance geometry);
/// here they degrade to `0` rather than emitting an invalid token.
fn fmt_real(r: f32) -> String {
    if !r.is_finite() {
        return "0".to_string();
    }
    if r == r.trunc() {
        format!("{}", r as i64)
    } else {
        let s = format!("{r}");
        if s.contains(['e', 'E']) {
            format!("{r:.6}")
        } else {
            s
        }
    }
}

/// Encode text for a PDF **text string** entry (`/Reason`, `/Name`, `/T`, …):
/// plain ASCII is written as a literal string; anything else as UTF-16BE with
/// the `FE FF` byte-order mark (PDF 32000-1 §7.9.2.2), so accented and
/// non-Latin characters survive every viewer instead of being misread as
/// PDFDocEncoding bytes.
pub(crate) fn text_string(s: &str) -> Object {
    if s.is_ascii() {
        Object::String(s.as_bytes().to_vec(), StringFormat::Literal)
    } else {
        let mut bytes = vec![0xFE, 0xFF];
        for unit in s.encode_utf16() {
            bytes.extend_from_slice(&unit.to_be_bytes());
        }
        Object::String(bytes, StringFormat::Hexadecimal)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn text_string_uses_utf16_for_non_ascii() {
        assert!(matches!(text_string("Approved"), Object::String(_, StringFormat::Literal)));
        match text_string("Aprovação") {
            Object::String(b, StringFormat::Hexadecimal) => {
                assert_eq!(&b[..2], &[0xFE, 0xFF]);
                let units: Vec<u16> = b[2..]
                    .chunks(2)
                    .map(|c| u16::from_be_bytes([c[0], c[1]]))
                    .collect();
                assert_eq!(String::from_utf16(&units).unwrap(), "Aprovação");
            }
            other => panic!("unexpected {other:?}"),
        }
    }

    #[test]
    fn header_offset_tolerates_leading_junk() {
        assert_eq!(header_offset(b"%PDF-1.7\n"), 0);
        assert_eq!(header_offset(b"JUNK\n%PDF-1.7\n"), 5);
    }

    #[test]
    fn carried_trailer_drops_section_keys() {
        let mut t = Dictionary::new();
        t.set("Size", Object::Integer(9));
        t.set("Root", Object::Reference((1, 0)));
        t.set("Info", Object::Reference((2, 0)));
        t.set("Prev", Object::Integer(100));
        t.set("Encrypt", Object::Reference((3, 0)));
        t.set("W", Object::Array(vec![]));
        let c = carried_trailer(&t);
        assert!(c.has(b"Info"));
        for k in ["Size", "Root", "Prev", "Encrypt", "W"] {
            assert!(!c.has(k.as_bytes()), "{k} must not be carried");
        }
    }
}