mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-12-02 20:20:35 -05:00
- `TrustStore::set_list(name, Option<list>)` had two sources for a list's name: a list loaded as "EU" but named "Other" could not be removed as "EU". Now `add_list(list)` uses the list's own name and `remove_list(name)` takes it back (`Session::add_trust_list`, `remove_trust_list`). - `x509::load_certificates` now refuses a PEM block that never ends (upstream, with the `.p7b` bundles). A block that begins while another is still open replaced it without a word, so a bundle spliced from two cut-short files loaded as the second one only. That is an error too; the trust list tests cover a cut inside the last block, a cut after the last complete one, and a splice. - The module docs say why non-CA entries stay in a list: the real EU file has 4 of 875 (three OCSP responder certificates, one time-stamp signer) under its CA services, so refusing them would refuse the real lists. They issue nothing in chain building and vouch only for themselves, which the signer checks still judge. Fallout of rebasing onto the current main: the parity evidence follows the tests into `tests/it/`, and twelve tests that others added build their `TrustStore` with `..TrustStore::default()`. Upstream's rule that a time-stamp authority's certificate must be issued for time stamping would have kept the time untrusted in our time-stamp test for a second reason; its authority now has that purpose, so the test checks our rule again (a list never vouches for a time-stamp authority: it fails if one could) and the user's own certificate still makes the time trusted. No catalog line and no attribution hash is touched: upstream no longer gates tests on translations, and the new wording stays English until translators get to it. Fallout of rebasing onto the main of Nov 4: `validate.rs` now imports its names one by one, so `TrustSource` is imported there, and the test time-stamp authority calls `timestamp::respond` with its new `TimestampSigner` / `TimestampIssuance` arguments. Fallout of rebasing onto the main of Nov 18: the x509 chain builder takes its pool as an iterator (upstream now hashes a candidate's issuer once per lookup, which the iterator keeps), the re-exports gain `TrustSource` next to upstream's `has_signatures`, and eleven more tests that others added in `pdf.rs`, `encrypted_signing.rs`, `revision_classification.rs` and `signed_structure.rs` build their `TrustStore` with `..TrustStore::default()`. Fallout of rebasing onto the main of Nov 26: four more tests that others added in `pdf.rs` build their `TrustStore` with `..TrustStore::default()`. The ROADMAP progress-log line is dropped: it was the only file in conflict, and AGENTS.md asks not to edit append-only logs; the same facts are in `crates/sign/README.md`, the module docs and the `sign.eutl` parity entry. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
158 lines
7.5 KiB
Rust
158 lines
7.5 KiB
Rust
//! The byte ranges of a signature on an encrypted document.
|
|
|
|
use std::sync::Arc;
|
|
|
|
use pdfcraft_cos::{Document, SaveOptions};
|
|
use pdfcraft_sign::{SignOptions, Status, TrustStore, pkcs12, signatures};
|
|
|
|
fn data(name: &str) -> Vec<u8> {
|
|
std::fs::read(format!("{}/tests/data/{name}", env!("CARGO_MANIFEST_DIR"))).unwrap()
|
|
}
|
|
|
|
/// One page with text and an unsigned signature field "Approval".
|
|
fn fixture() -> Vec<u8> {
|
|
fixture_with(b"<< /Type /Catalog /Pages 2 0 R /AcroForm << /Fields [6 0 R] >> >>", &[])
|
|
}
|
|
|
|
/// [`fixture`] with its catalog replaced by `catalog` and `more` objects from number 7 on.
|
|
fn fixture_with(catalog: &[u8], more: &[&[u8]]) -> Vec<u8> {
|
|
let mut objs: Vec<&[u8]> = vec![
|
|
catalog,
|
|
b"<< /Type /Pages /Kids [3 0 R] /Count 1 >>",
|
|
b"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 300 300] /Contents 4 0 R /Resources << /Font << /F1 5 0 R >> >> /Annots [6 0 R] >>",
|
|
b"<< /Length 44 >>\nstream\nBT /F1 14 Tf 20 250 Td (Contract text) Tj ET\nendstream",
|
|
b"<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>",
|
|
b"<< /Type /Annot /Subtype /Widget /FT /Sig /T (Approval) /Rect [150 20 280 70] /P 3 0 R /F 4 >>",
|
|
];
|
|
objs.extend_from_slice(more);
|
|
write_pdf(&objs)
|
|
}
|
|
|
|
/// A classic-xref PDF of `objs`, numbered from 1, with object 1 as the catalog.
|
|
fn write_pdf(objs: &[&[u8]]) -> Vec<u8> {
|
|
let mut out = b"%PDF-1.7\n".to_vec();
|
|
let mut offsets = Vec::new();
|
|
for (i, o) in objs.iter().enumerate() {
|
|
offsets.push(out.len());
|
|
out.extend_from_slice(format!("{} 0 obj\n", i + 1).as_bytes());
|
|
out.extend_from_slice(o);
|
|
out.extend_from_slice(b"\nendobj\n");
|
|
}
|
|
let xref = out.len();
|
|
out.extend_from_slice(format!("xref\n0 {}\n0000000000 65535 f \n", objs.len() + 1).as_bytes());
|
|
for o in offsets {
|
|
out.extend_from_slice(format!("{o:010} 00000 n \n").as_bytes());
|
|
}
|
|
out.extend_from_slice(format!("trailer\n<< /Size {} /Root 1 0 R >>\nstartxref\n{xref}\n%%EOF\n", objs.len() + 1).as_bytes());
|
|
out
|
|
}
|
|
|
|
fn open(b: &[u8]) -> Document {
|
|
Document::open(Arc::new(b.to_vec())).unwrap()
|
|
}
|
|
|
|
fn opts() -> SignOptions {
|
|
SignOptions {
|
|
page: 0,
|
|
rect: Some([20.0, 100.0, 220.0, 150.0]),
|
|
reason: Some("I approve this document".into()),
|
|
location: Some("London".into()),
|
|
date: "D:20261002120000+01'00'".into(),
|
|
..SignOptions::default()
|
|
}
|
|
}
|
|
|
|
/// `fixture()` protected with an empty user password (opens without asking, like most
|
|
/// "secured" forms) and an owner password, then saved.
|
|
fn protected(alg: pdfcraft_cos::Algorithm, permissions: i32) -> Vec<u8> {
|
|
protected_with(alg, "", permissions)
|
|
}
|
|
|
|
/// `fixture()` protected with the user password `user` and an owner password, then saved.
|
|
fn protected_with(alg: pdfcraft_cos::Algorithm, user: &str, permissions: i32) -> Vec<u8> {
|
|
let mut doc = open(&fixture());
|
|
doc.set_encryption_legacy(&pdfcraft_cos::NewEncryption {
|
|
algorithm: alg,
|
|
user_password: user,
|
|
owner_password: "owner",
|
|
permissions,
|
|
encrypt_metadata: true,
|
|
seed: [17; 32],
|
|
})
|
|
.unwrap();
|
|
pdfcraft_cos::write_full(&doc, &SaveOptions::default()).unwrap()
|
|
}
|
|
|
|
fn open_pw(b: &[u8], pw: Option<&str>) -> Document {
|
|
Document::open_with_password(Arc::new(b.to_vec()), pw).unwrap()
|
|
}
|
|
|
|
const ALL_ALGORITHMS: [pdfcraft_cos::Algorithm; 4] =
|
|
[pdfcraft_cos::Algorithm::Rc4_40, pdfcraft_cos::Algorithm::Rc4_128, pdfcraft_cos::Algorithm::Aes128, pdfcraft_cos::Algorithm::Aes256];
|
|
|
|
/// Where `needle` first appears in `haystack`.
|
|
fn find(haystack: &[u8], needle: &[u8], alg: pdfcraft_cos::Algorithm) -> usize {
|
|
haystack
|
|
.windows(needle.len())
|
|
.position(|w| w == needle)
|
|
.unwrap_or_else(|| panic!("{alg:?}: {:?} is not in the added bytes", String::from_utf8_lossy(needle)))
|
|
}
|
|
|
|
/// The four numbers of the signature dictionary's `/ByteRange` in the added revision.
|
|
fn byte_range(added: &[u8], alg: pdfcraft_cos::Algorithm) -> [usize; 4] {
|
|
let prefix = b"/ByteRange [";
|
|
let at = find(added, prefix, alg) + prefix.len();
|
|
let end = at + added[at..].iter().position(|b| *b == b']').unwrap_or_else(|| panic!("{alg:?}: the /ByteRange array is not closed"));
|
|
let text = std::str::from_utf8(&added[at..end]).unwrap_or_else(|_| panic!("{alg:?}: the /ByteRange numbers are not ASCII"));
|
|
let nums: Vec<usize> =
|
|
text.split_ascii_whitespace().map(|n| n.parse().unwrap_or_else(|_| panic!("{alg:?}: {n:?} is not a byte offset"))).collect();
|
|
assert_eq!(nums.len(), 4, "{alg:?}: /ByteRange has {} numbers, not 4", nums.len());
|
|
[nums[0], nums[1], nums[2], nums[3]]
|
|
}
|
|
|
|
/// The signature dictionary's `/ByteRange` gives the offsets and lengths of the parts of the
|
|
/// file its digest covers. For a signature on an encrypted document that is the whole earlier
|
|
/// revision, its encrypted objects included, leaving out only the hex text of `/Contents`.
|
|
#[test]
|
|
fn signed_encrypted_documents_cover_their_earlier_revision() {
|
|
let id = pkcs12::open(&data("ec-p256.p12"), "test").unwrap();
|
|
let trust = TrustStore { certs: vec![id.certificate.clone()], ..TrustStore::default() };
|
|
for alg in ALL_ALGORITHMS {
|
|
let base = protected(alg, -1);
|
|
let doc = open_pw(&base, None);
|
|
let signed = pdfcraft_sign::sign(&doc, &id, &opts()).unwrap();
|
|
assert!(signed.starts_with(&base), "{alg:?}: an incremental update");
|
|
let added = &signed[base.len()..];
|
|
// The hole: the `<...>` hex string of `/Contents`.
|
|
let at = base.len() + find(added, b"/Contents <", alg);
|
|
let hole_start = at + b"/Contents ".len();
|
|
let hole_end = hole_start + signed[hole_start..].iter().position(|b| *b == b'>').expect("a closed /Contents string") + 1;
|
|
let [a, b, c, d] = byte_range(added, alg);
|
|
assert_eq!(a, 0, "{alg:?}: the covered range starts at the start of the file");
|
|
assert_eq!(b, hole_start, "{alg:?}: the covered range ends at {hole_start}, where /Contents begins");
|
|
assert_eq!(c, hole_end, "{alg:?}: the second covered range starts at {hole_end}, where /Contents ends");
|
|
assert_eq!(c + d, signed.len(), "{alg:?}: the covered range ends at the file's end ({c} + {d} of {})", signed.len());
|
|
assert!(hole_start >= base.len(), "{alg:?}: the hole at {hole_start} comes after the whole earlier revision of {} bytes", base.len());
|
|
assert_eq!(signed[hole_start], b'<', "{alg:?}: the hole opens with '<' at {hole_start}");
|
|
assert_eq!(signed[hole_end - 1], b'>', "{alg:?}: the hole closes with '>' at {}", hole_end - 1);
|
|
assert!(
|
|
signed[hole_start + 1..hole_end - 1].iter().all(u8::is_ascii_hexdigit),
|
|
"{alg:?}: /Contents between {hole_start} and {hole_end} is hexadecimal"
|
|
);
|
|
let sigs = signatures(&open_pw(&signed, None), &signed, &trust);
|
|
let s = sigs.iter().find(|s| s.signed).expect("a signed field");
|
|
assert_eq!(s.status, Status::Valid, "{alg:?}: {:?}", s.details);
|
|
// One changed byte inside the covered range, in the earlier revision.
|
|
let mut tampered = signed.clone();
|
|
let flip = base.len() - 1;
|
|
tampered[flip] ^= 1;
|
|
let sigs = signatures(&open_pw(&tampered, None), &tampered, &trust);
|
|
let signed_sigs: Vec<_> = sigs.iter().filter(|s| s.signed).collect();
|
|
assert!(!signed_sigs.is_empty(), "{alg:?}: the tampered file still lists its signature");
|
|
assert!(
|
|
signed_sigs.iter().all(|s| s.status == Status::Invalid),
|
|
"{alg:?}: the byte changed at {flip}, inside the covered range, makes every signature invalid"
|
|
);
|
|
}
|
|
}
|