Repository navigation
Give every signing candidate a secure new second half of /ID - #140
Merged
Merged
Conversation
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.
Open
5 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
/IDwas the only bytes a guess could not reproduce, and it came fromFX_Random, a Mersenne Twister seeded from 32 bits (clock, pid, a stack address), so all 16 bytes follow from the seed;is_incremental_ && encrypt_dict_ && pID2inInitID), 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::InitIDdraws every new half of/IDfromEPDF_FillSecureRandom(AppendNewIDHalf), for every save. When the source fails, the save fails; there is no weaker fallback.FX_Randomno longer writes/ID.CPDF_Creator::RequireNewChangingID():SaveCandidateThroughcalls it, so bothEPDFSig_SaveCandidateToOwnedBufferandEPDFSig_SaveCandidatewrite 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.CreateFlagsbit:FPDF_SaveAsCopypasses its flags straight through to the creator, so a new bit would widen the public flag space.EPDF_FillSecureRandomreached the host'scrypto.getRandomValuesthrough Emscripten'sgetentropy, 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 callscrypto.getRandomValuesthrough its ownEM_JSbridge, 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.EPDF_SIG_PREPAREis unchanged.save-securitynow covers new/IDhalves;signaturesstates 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/IDgets 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/IDarray.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/Mdecrypts. 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.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:
/IDsecond halves;globalThis.cryptoremoved, 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;The same probe on the released runtime: the save throws (
RuntimeError: Aborted(no cryptographic support found…), or aTypeErroronce Emscripten has cached its source).Checked
FPDFProgressiveRenderEmbedderTest,EPDFMeasureEmbedderTest.CaptionAppearanceGolden,FPDFEditEmbedderTest.SetTextRenderMode,FPDFViewEmbedderTest.RenderBug664284WithNoNativeText,RenderHelloWorldWithFlags).EPDFSignatureEmbedderTest.*(macOS): 58 pass; no existing writer count moved.check-fork-tags.py --strictandcheck-c-api.py: pass.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