pdf2 0.2.0-alpha.1

A PDF 2.0 (ISO 32000-2) reader, writer and text extractor.
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
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
//! The standard security handler, revision 6 — AES-256 (§7.6).
//!
//! PDF 2.0 requires a conforming reader to support exactly this: `/V 5` with
//! `/R 6`, `/CFM /AESV3`, and AES-256 in CBC mode for strings and streams. The
//! deprecated revisions (RC4, AES-128, `/R 5`) are rejected rather than
//! half-implemented.
//!
//! Two details differ from the older revisions and are worth stating:
//!
//! - There is **no per-object key**. `/V 5` uses the 32-byte file key for every
//!   string and stream (ISO 32000-2 §7.6.4.3); the per-object derivation people
//!   remember belongs to revisions 2–4.
//! - Password verification uses Algorithm 2.B — a 64-round minimum key stretch
//!   over SHA-256/384/512 and AES-128-CBC — implemented in [`hash_r6`].

use aes::cipher::{
    block_padding::NoPadding, BlockDecrypt, BlockDecryptMut, BlockEncryptMut, KeyInit, KeyIvInit,
};
use aes::{Aes128, Aes256};
use sha2::{Digest, Sha256, Sha384, Sha512};

use crate::error::{Error, Result};
use crate::object::{Dictionary, Object, Stream};

/// Length of the file key, and of the AES key, for `/V 5`.
pub const KEY_LENGTH: usize = 32;

/// The standard security handler for one document.
#[derive(Debug, Clone)]
pub struct Encryption {
    /// The 32-byte file key derived from the password.
    file_key: [u8; KEY_LENGTH],
    /// Whether the XMP `/Metadata` stream is encrypted too.
    encrypt_metadata: bool,
}

impl Encryption {
    /// Build the handler from an `/Encrypt` dictionary and a password.
    ///
    /// Accepts `/V 5 /R 6` only; a wrong password is reported as an error, and
    /// any failure leaves the caller with no partial state.
    pub fn new(dict: &Dictionary, password: &[u8]) -> Result<Self> {
        match dict.get(b"Filter") {
            Some(Object::Name(name)) if name == b"Standard" => {}
            _ => {
                return Err(Error::Malformed {
                    what: "only the /Standard security handler is supported",
                    offset: None,
                })
            }
        }
        let version = integer(dict, b"V")?;
        if version != 5 {
            return Err(Error::Malformed {
                what: "encryption dictionary must be /V 5 (AES-256)",
                offset: None,
            });
        }
        let revision = integer(dict, b"R")?;
        if revision != 6 {
            return Err(Error::Malformed {
                what: "only revision 6 is supported (revision 5 is deprecated)",
                offset: None,
            });
        }
        let encrypt_metadata = match dict.get(b"EncryptMetadata") {
            Some(Object::Boolean(value)) => *value,
            Some(_) => {
                return Err(Error::Malformed {
                    what: "/EncryptMetadata must be a boolean",
                    offset: None,
                })
            }
            None => true,
        };
        if let Some(length) = dict.get(b"Length") {
            let length = length.as_integer().ok_or(Error::Malformed {
                what: "/Length must be an integer",
                offset: None,
            })?;
            if length != 256 {
                return Err(Error::Malformed {
                    what: "/V 5 requires a 256-bit key",
                    offset: None,
                });
            }
        }
        stream_crypt_filter(dict)?;

        let owner = bytes(dict, b"O", 48)?;
        let user = bytes(dict, b"U", 48)?;

        // Algorithm 2.A: the user password yields the file key directly, the
        // owner password unwraps it from `/OE`.
        let user_hash = hash_r6(password, &user[32..40], &[])?;
        let file_key = if user_hash == user[..32] {
            hash_r6(password, &user[40..48], &[])?
        } else {
            let owner_hash = hash_r6(password, &owner[32..40], &user[..48])?;
            if owner_hash != owner[..32] {
                return Err(Error::Malformed {
                    what: "wrong password for an encrypted document",
                    offset: None,
                });
            }
            if let Some(ue) = dict.get(b"OE") {
                let wrapped = ue.as_string().ok_or(Error::Malformed {
                    what: "/OE must be a string",
                    offset: None,
                })?;
                // The intermediate owner key uses the *key* salt, not the
                // validation salt the check above used.
                let owner_key = hash_r6(password, &owner[40..48], &user[..48])?;
                let mut buffer = wrapped.as_bytes().to_vec();
                if buffer.len() != 32 {
                    return Err(Error::Malformed {
                        what: "/OE must be 32 bytes",
                        offset: None,
                    });
                }
                let decrypted = cbc::Decryptor::<Aes256>::new_from_slices(&owner_key, &[0_u8; 16])
                    .map_err(|_| Error::malformed("AES-256 key length"))?
                    .decrypt_padded_mut::<NoPadding>(&mut buffer)
                    .map_err(|_| Error::Malformed {
                        what: "AES-256 decryption failed",
                        offset: None,
                    })?;
                let mut key = [0_u8; KEY_LENGTH];
                key.copy_from_slice(decrypted);
                key
            } else {
                // Without /OE there is no way to recover the file key from the
                // owner password.
                return Err(Error::Malformed {
                    what: "encrypted document without /OE",
                    offset: None,
                });
            }
        };

        let encryption = Self {
            file_key,
            encrypt_metadata,
        };
        if let Some(perms) = dict.get(b"Perms") {
            let perms = perms.as_string().ok_or(Error::Malformed {
                what: "/Perms must be a string",
                offset: None,
            })?;
            encryption.check_perms(perms.as_bytes())?;
        }
        Ok(encryption)
    }

    /// Whether the metadata stream is encrypted.
    pub fn encrypt_metadata(&self) -> bool {
        self.encrypt_metadata
    }

    /// Decrypt a string object.
    pub fn decrypt_string(&self, data: &[u8]) -> Result<Vec<u8>> {
        self.decrypt(data)
    }

    /// Decrypt a stream payload, honouring the exceptions in §7.6.1:
    /// cross-reference streams are never encrypted, and `/Metadata` is left in
    /// the clear when `/EncryptMetadata` is false.
    pub fn decrypt_stream(&self, stream: &Stream) -> Result<Vec<u8>> {
        match stream.dict().get(b"Type").and_then(Object::as_name) {
            Some(name) if name == b"XRef" => return Ok(stream.data().to_vec()),
            Some(name) if name == b"Metadata" && !self.encrypt_metadata => {
                return Ok(stream.data().to_vec())
            }
            _ => {}
        }
        self.decrypt(stream.data())
    }

    /// Decrypt every string inside an object graph.
    ///
    /// Stream *payloads* are left alone: they are decrypted when the stream is
    /// read through [`crate::Document::decode_stream`], so the object in the
    /// cache keeps the bytes the file holds. Strings inside a stream
    /// dictionary are decrypted, because they belong to the object graph.
    pub fn decrypt_object(&self, object: &Object) -> Result<Object> {
        match object {
            Object::String(string) => Ok(Object::String(crate::object::PdfString::literal(
                self.decrypt_string(string.as_bytes())?,
            ))),
            Object::Array(items) => {
                let mut decrypted = Vec::with_capacity(items.len());
                for item in items {
                    decrypted.push(self.decrypt_object(item)?);
                }
                Ok(Object::Array(decrypted))
            }
            Object::Dictionary(dict) => Ok(Object::Dictionary(self.decrypt_dictionary(dict)?)),
            Object::Stream(stream) => {
                let dict = self.decrypt_dictionary(stream.dict())?;
                Ok(Object::Stream(Stream::new(dict, stream.data().to_vec())))
            }
            other => Ok(other.clone()),
        }
    }

    fn decrypt_dictionary(&self, dict: &Dictionary) -> Result<Dictionary> {
        let mut decrypted = Dictionary::new();
        for (name, value) in dict.iter() {
            decrypted.insert(name.to_vec(), self.decrypt_object(value)?);
        }
        Ok(decrypted)
    }

    /// AES-256-CBC: a 16-byte IV followed by the ciphertext.
    fn decrypt(&self, data: &[u8]) -> Result<Vec<u8>> {
        if data.len() < 32 || !(data.len() - 16).is_multiple_of(16) {
            return Err(Error::Malformed {
                what: "AES-256 payload is not an IV followed by whole blocks",
                offset: None,
            });
        }
        let (iv, ciphertext) = data.split_at(16);
        let mut buffer = ciphertext.to_vec();
        let plaintext = cbc::Decryptor::<Aes256>::new_from_slices(&self.file_key, iv)
            .map_err(|_| Error::malformed("AES-256 key or IV length"))?
            .decrypt_padded_mut::<NoPadding>(&mut buffer)
            .map_err(|_| Error::Malformed {
                what: "AES-256 decryption failed",
                offset: None,
            })?;
        Ok(unpad(plaintext))
    }

    /// The `/Perms` tamper check: bytes 9–11 must read `adb`.
    fn check_perms(&self, perms: &[u8]) -> Result<()> {
        if perms.len() != 16 {
            return Err(Error::Malformed {
                what: "/Perms must be 16 bytes",
                offset: None,
            });
        }
        let mut block = [0_u8; 16];
        block.copy_from_slice(perms);
        // AES-256 in ECB mode is a single block decryption; the `cipher` crate
        // exposes that directly.
        let cipher = Aes256::new_from_slice(&self.file_key)
            .map_err(|_| Error::malformed("AES-256 key length"))?;
        cipher.decrypt_block((&mut block).into());
        if &block[9..12] != b"adb" {
            return Err(Error::Malformed {
                what: "encryption /Perms does not verify",
                offset: None,
            });
        }
        Ok(())
    }
}

/// Algorithm 2.B: the revision 6 password hash.
///
/// `udata` is empty for the user password and the 48-byte `/U` value for the
/// owner password. Returns the first 32 bytes of the final digest.
pub fn hash_r6(password: &[u8], salt: &[u8], udata: &[u8]) -> Result<[u8; KEY_LENGTH]> {
    let mut initial = Sha256::new();
    initial.update(password);
    initial.update(salt);
    initial.update(udata);
    let mut key = initial.finalize().to_vec();

    let mut unit = Vec::with_capacity(password.len() + key.len() + udata.len());
    unit.extend_from_slice(password);
    unit.extend_from_slice(&key);
    unit.extend_from_slice(udata);

    let mut round: u32 = 0;
    loop {
        // K1 is (password + K + udata) repeated 64 times.
        let mut block = Vec::with_capacity(unit.len() * 64);
        for _ in 0..64 {
            block.extend_from_slice(&unit);
        }
        let block_len = block.len();
        let encrypted = cbc::Encryptor::<Aes128>::new_from_slices(&key[0..16], &key[16..32])
            .map_err(|_| Error::malformed("AES-128 key or IV length"))?
            .encrypt_padded_mut::<NoPadding>(&mut block, block_len)
            .map_err(|_| Error::Malformed {
                what: "AES-128 encryption failed during password hashing",
                offset: None,
            })?
            .to_vec();
        let checksum: u32 = encrypted[..16].iter().map(|byte| u32::from(*byte)).sum();
        key = match checksum % 3 {
            0 => Sha256::digest(&encrypted).to_vec(),
            1 => Sha384::digest(&encrypted).to_vec(),
            _ => Sha512::digest(&encrypted).to_vec(),
        };
        // The winning digest feeds the next round, so rebuild the repeated unit.
        unit.clear();
        unit.extend_from_slice(password);
        unit.extend_from_slice(&key);
        unit.extend_from_slice(udata);

        round += 1;
        let last = u32::from(encrypted[encrypted.len() - 1]);
        if round >= 64 && last <= round - 32 {
            break;
        }
        if round > 10_000 {
            return Err(Error::Malformed {
                what: "password hashing did not converge",
                offset: None,
            });
        }
    }
    let mut key_bytes = [0_u8; KEY_LENGTH];
    key_bytes.copy_from_slice(&key[..KEY_LENGTH]);
    Ok(key_bytes)
}

/// Remove PKCS#7 padding when it is valid; leave the data alone otherwise,
/// because some writers pad with zeros or not at all.
fn unpad(data: &[u8]) -> Vec<u8> {
    if data.is_empty() {
        return Vec::new();
    }
    let padding = usize::from(data[data.len() - 1]);
    if padding == 0 || padding > 16 || padding > data.len() {
        return data.to_vec();
    }
    if data[data.len() - padding..]
        .iter()
        .all(|byte| usize::from(*byte) == padding)
    {
        return data[..data.len() - padding].to_vec();
    }
    data.to_vec()
}

fn stream_crypt_filter(dict: &Dictionary) -> Result<()> {
    for key in [&b"StmF"[..], &b"StrF"[..]] {
        match dict.get(key) {
            None => {}
            Some(Object::Name(name)) if name == b"Identity" => {
                return Err(Error::Malformed {
                    what: "an Identity crypt filter is not an encrypted document",
                    offset: None,
                })
            }
            Some(Object::Name(_)) => {}
            Some(_) => {
                return Err(Error::Malformed {
                    what: "crypt filter entries must be names",
                    offset: None,
                })
            }
        }
    }
    Ok(())
}

fn integer(dict: &Dictionary, key: &[u8]) -> Result<i64> {
    dict.get(key)
        .and_then(Object::as_integer)
        .ok_or(Error::Malformed {
            what: "encryption dictionary without /V and /R integers",
            offset: None,
        })
}

fn bytes(dict: &Dictionary, key: &[u8], length: usize) -> Result<Vec<u8>> {
    let value = dict
        .get(key)
        .and_then(Object::as_string)
        .ok_or(Error::Malformed {
            what: "encryption dictionary without /U and /O strings",
            offset: None,
        })?;
    if value.as_bytes().len() != length {
        return Err(Error::Malformed {
            what: "/U and /O must be 48 bytes for revision 6",
            offset: None,
        });
    }
    Ok(value.as_bytes().to_vec())
}

#[cfg(test)]
mod tests {
    use super::{hash_r6, unpad, Encryption};
    use crate::object::{Dictionary, Object, PdfString};

    #[test]
    fn padding_is_stripped_only_when_it_is_valid() {
        assert_eq!(unpad(&[1, 2, 3, 0x01]), vec![1, 2, 3]);
        assert_eq!(unpad(&[1, 2, 0x02, 0x02]), vec![1, 2]);
        assert_eq!(unpad(&[1, 2, 3, 0x00]), vec![1, 2, 3, 0x00]);
        assert_eq!(unpad(&[1, 2, 3, 0x05]), vec![1, 2, 3, 0x05]);
        assert_eq!(unpad(&[0x11]), vec![0x11]);
        assert_eq!(unpad(&[]), Vec::<u8>::new());
    }

    #[test]
    fn the_r6_hash_is_stable_and_32_bytes() {
        // A fixed vector pins the algorithm so a refactor cannot silently
        // change it; the value was produced by this implementation and is
        // checked by the encrypted fixture round-trip as well.
        let hash = hash_r6(b"user", b"12345678", &[]).expect("hash");
        assert_eq!(hash.len(), 32);
        let again = hash_r6(b"user", b"12345678", &[]).expect("hash");
        assert_eq!(hash, again, "hashing is deterministic");
        let other = hash_r6(b"other", b"12345678", &[]).expect("hash");
        assert_ne!(hash, other);
        let salt = hash_r6(b"user", b"87654321", &[]).expect("hash");
        assert_ne!(hash, salt, "the salt changes the result");
        assert_eq!(
            hash.to_vec(),
            hash_r6(b"user", b"12345678", &[]).expect("hash").to_vec()
        );
    }

    fn encrypt_dict(perm_value: i64) -> Dictionary {
        let mut dict = Dictionary::new();
        dict.insert("Filter", Object::name("Standard"));
        dict.insert("V", Object::Integer(5));
        dict.insert("R", Object::Integer(6));
        dict.insert("Length", Object::Integer(256));
        dict.insert("EncryptMetadata", Object::Boolean(true));
        dict.insert("U", Object::String(PdfString::hex(vec![0; 48])));
        dict.insert("O", Object::String(PdfString::hex(vec![0; 48])));
        dict.insert("P", Object::Integer(perm_value));
        dict
    }

    #[test]
    fn unsupported_versions_are_rejected() {
        let mut dict = encrypt_dict(-1);
        dict.insert("V", Object::Integer(4));
        assert!(Encryption::new(&dict, b"").is_err());

        let mut dict = encrypt_dict(-1);
        dict.insert("R", Object::Integer(5));
        assert!(Encryption::new(&dict, b"").is_err());

        let mut dict = encrypt_dict(-1);
        dict.insert("Filter", Object::name("PublicKey"));
        assert!(Encryption::new(&dict, b"").is_err());

        let mut dict = encrypt_dict(-1);
        dict.insert("Length", Object::Integer(128));
        assert!(Encryption::new(&dict, b"").is_err());

        let mut dict = encrypt_dict(-1);
        dict.insert("StmF", Object::name("Identity"));
        assert!(Encryption::new(&dict, b"").is_err());
    }

    #[test]
    fn a_wrong_password_is_an_error_without_parsing_anything_else() {
        let dict = encrypt_dict(-1);
        let error = Encryption::new(&dict, b"password").expect_err("wrong password");
        assert!(error.to_string().contains("password"), "{error}");
    }

    #[test]
    fn truncated_payloads_are_errors() {
        let dict = encrypt_dict(-1);
        // Fabricate a handler: no password check is attempted for the payload
        // paths, so a wrong-password rejection is bypassed by construction.
        let encryption = super::Encryption {
            file_key: [7; 32],
            encrypt_metadata: true,
        };
        assert!(encryption.decrypt_string(b"short").is_err());
        assert!(encryption.decrypt_string(&[0; 47]).is_err());
        assert!(dict.contains_key(b"U"));
    }
}

#[cfg(test)]
mod nist {
    use aes::cipher::{block_padding::NoPadding, BlockEncryptMut, KeyIvInit};

    fn hex(text: &str) -> Vec<u8> {
        text.as_bytes()
            .chunks(2)
            .map(|pair| {
                u8::from_str_radix(std::str::from_utf8(pair).expect("hex"), 16).expect("hex")
            })
            .collect()
    }

    #[test]
    fn aes256_cbc_matches_nist_sp_800_38a() {
        let key = hex("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4");
        let iv = hex("000102030405060708090a0b0c0d0e0f");
        let mut data = hex("6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e51");
        let expected = hex("f58c4c04d6e5f1ba779eabfb5f7bfbd69cfc4e967edb808d679f777bc6702c7d");
        let len = data.len();
        let encrypted = cbc::Encryptor::<aes::Aes256>::new_from_slices(&key, &iv)
            .expect("key")
            .encrypt_padded_mut::<NoPadding>(&mut data, len)
            .expect("encrypt");
        assert_eq!(encrypted, expected.as_slice());
    }
}