Skip to content

Give every signing candidate a secure new second half of /ID - #140

Merged
bobsingor merged 2 commits into
embedpdf/mainfrom
embedpdf/fix/signing-id-secure-random
Dec 3, 2026
Merged

bobsingor merged 2 commits into
embedpdf/mainfrom
embedpdf/fix/signing-id-secure-random

Conversation

@bobsingor

@bobsingor bobsingor commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Cause

A remote key or a timestamp authority receives only the digest of the signed bytes. For a document made from a known template with a few values filled in, that digest let anyone confirm a guess at the values, because nothing in a signed revision was unpredictable:

  • the second half of the trailer /ID was the only bytes a guess could not reproduce, and it came from FX_Random, a Mersenne Twister seeded from 32 bits (clock, pid, a stack address), so all 16 bytes follow from the seed;
  • an incremental save of an encrypted file kept the old second half (is_incremental_ && encrypt_dict_ && pID2 in InitID), so a signed revision of an encrypted file held nothing new at all. Two candidates prepared with the same inputs and signing time had the identical digest.

Change

  • CPDF_Creator::InitID draws every new half of /ID from EPDF_FillSecureRandom (AppendNewIDHalf), for every save. When the source fails, the save fails; there is no weaker fallback. FX_Random no longer writes /ID.
  • CPDF_Creator::RequireNewChangingID(): SaveCandidateThrough calls it, so both EPDFSig_SaveCandidateToOwnedBuffer and EPDFSig_SaveCandidate write a new second half (ISO 32000's changing identifier), encrypted files included. The first half is kept: RC4 and AES-128 derive the file key from it and AES-256 from neither, so the signed file opens with its password as before. The signature covers the trailer, so the 16 bytes are inside the /ByteRange.
  • Other incremental saves of an encrypted file keep the old second half, as upstream does.
  • A setter rather than a CreateFlags bit: FPDF_SaveAsCopy passes its flags straight through to the creator, so a new bit would widen the public flag space.
  • WebAssembly: EPDF_FillSecureRandom reached the host's crypto.getRandomValues through Emscripten's getentropy, which aborts the module when the host has none. The abort skips the save's cleanup and reaches the caller as an exception. Fork PR 88 put that path on encrypted saves; this PR puts it on every save. The Wasm branch now calls crypto.getRandomValues through its own EM_JS bridge, which returns failure, so the save returns NULL like any failed save. Every browser (pages on http and workers included), Node 19+ and the edge runtimes have it. Chunks are 64 KiB on every platform, getRandomValues' limit.
  • No ABI change and no new export: EPDF_SIG_PREPARE is unchanged.
  • Ledger: save-security now covers new /ID halves; signatures states the candidate rule.

Tests

New in epdf_signature_embeddertest.cpp (*ChangingIdentifier):

  • CandidateSavesWriteANewChangingIdentifier: two candidates from the same document, same inputs and signing time, get different second halves and digests, each unlike the base's, with the base's first half kept; a base without /ID gets one whose halves are the same new 16 bytes. Signature and document timestamp, buffer and writer save.
  • CandidateSavesDifferOnlyInTheChangingIdentifier: the two signed files differ only inside the /ID array.
  • EncryptedCandidateSavesWriteANewChangingIdentifier: the same on RC4 40 and 128 bits (R2, R3), AES-128 (R4) and AES-256 (R5, R6). The result opens with its password, keeps its security handler revision, and the new revision's /M decrypts. Fails without the change (both candidates kept the base's second half and had the same digest).
  • EncryptedIncrementalSaveKeepsItsChangingIdentifier: an ordinary incremental save of an encrypted file keeps its second half.
  • The four tests are in testing/embedpdf_layer_mode_writers.txt.
  • epdf_secure_random_unittest.cpp: the large-buffer test now spans two chunks.

No fork suite runs WebAssembly, so the bridge was checked by hand: a wasm32 build of this branch, linked with the engine's link flags into a browser glue (run as a worker) and a Node glue. On both:

  • with Web Crypto, saves succeed and two saves get different /ID second halves;
  • with globalThis.crypto removed, a plain save and an AES-128 save return NULL, with no exception and nothing logged; the same document still renders, another one opens and renders;
  • with Web Crypto back, both saves succeed again;
  • with the heap past 2 GB, an AES-128 save reopens with its password and its 8 stream IVs are non-zero and all different.

The same probe on the released runtime: the save throws (RuntimeError: Aborted(no cryptographic support found…), or a TypeError once Emscripten has cached its source).

Checked

  • macOS: unit 1416 passed; embedder 1930 run, 1909 passed, 9 skipped, 12 failed (the known macOS golden failures: eight FPDFProgressiveRenderEmbedderTest, EPDFMeasureEmbedderTest.CaptionAppearanceGolden, FPDFEditEmbedderTest.SetTextRenderMode, FPDFViewEmbedderTest.RenderBug664284WithNoNativeText, RenderHelloWorldWithFlags).
  • Linux x64 (Docker, release image): unit 1423 passed; embedder 1932 run, 1923 passed, 9 skipped, none failed.
  • Layer mode on EPDFSignatureEmbedderTest.* (macOS): 58 pass; no existing writer count moved.
  • Save checker (pikepdf) on a full macOS embedder dump: 49 saved PDFs, no problems.
  • check-fork-tags.py --strict and check-c-api.py: pass.
  • Not run: the pixel runner (nothing here renders).
  • Second commit (5c160db19f, the WebAssembly bridge): macOS unit 1416 passed; the wasm32 build checked as above; tags and C API pass. The native sources are unchanged apart from the unit test, so the embedder and Linux suites were not rerun.

Left

  • Release and the pin in embed-pdf-viewer, then the engine's conformance tests on both engines and the docs (step 3 of the signing reserve-and-salt plan), and a runtime test there that a save without Web Crypto fails and the runtime goes on.
  • Acrobat check of an encrypted file signed twice and of a certified file signed again (step 4).

A remote key or a timestamp authority receives only the digest of the
signed bytes. For a document made from a known template with a few values
filled in, that digest confirmed a guess at the values: the only bytes a
guess could not reproduce were the second half of the trailer's /ID, drawn
by FX_Random (a Mersenne Twister seeded from 32 bits), and an incremental
save of an encrypted file kept the old second half, so its signed revision
held nothing new at all.

- CPDF_Creator::InitID draws every new half of /ID from
  EPDF_FillSecureRandom, for every save, and the save fails when the
  source fails. FX_Random no longer writes /ID.
- CPDF_Creator::RequireNewChangingID(): the candidate saves
  (EPDFSig_SaveCandidateToOwnedBuffer and EPDFSig_SaveCandidate) always
  write a new second half, encrypted files included. RC4 and AES-128
  derive the file key from the first half and AES-256 from neither, so
  the signed file opens with its password as before. Other incremental
  saves of an encrypted file keep the old second half, as upstream does.

Tests (epdf_signature_embeddertest.cpp, *ChangingIdentifier):
- two candidates with the same inputs and signing time get different
  second halves and digests, each unlike the base's, with the first half
  kept; a base without /ID gets a new one. Signature and document
  timestamp, buffer and writer save.
- the two signed files differ only inside the /ID array.
- the same on encrypted files (RC4 40 and 128 bits, AES-128, AES-256 R5
  and R6): the result opens with its password, keeps its security handler
  and /M of the new revision decrypts. Without the change this test fails:
  both candidates had the base's second half and the same digest.
- an ordinary incremental save of an encrypted file keeps its second half.
EPDF_FillSecureRandom promises false when the random source fails, but in
WebAssembly it called getentropy(), which Emscripten implements with
crypto.getRandomValues and which aborts the module when the host has none.
The abort skips the save's cleanup and reaches the caller as a RuntimeError.
Since the previous commit every save draws from this source, not only
encrypted ones.

The WebAssembly branch now calls the host's crypto.getRandomValues through
its own EM_JS bridge, which returns 0 when there is none or it throws, so
the save fails like any other failed save. Every browser (pages on http and
workers included), Node 19+ and the edge runtimes have it. Chunks are now
64 KiB on every platform, getRandomValues' limit; the large-buffer unit
test grows past one chunk.
@bobsingor
bobsingor merged commit 6160587 into embedpdf/main Dec 3, 2026
1 check passed
@bobsingor
bobsingor deleted the embedpdf/fix/signing-id-secure-random branch December 3, 2026 20:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant