PrepareHash
security and signatures
Requires the Enterprise edition — a method of TPDFlibSignatureTimeStampProcess in the Enterprise unit PDFlibEntSignVerify
Description
Loads the source PDF, locates the /Contents placeholder of the signature in FieldName, decodes the CMS it holds and hashes the signature value of the selected signer with DigestAlgorithm. The hash is the message imprint a time-stamp authority needs.
On result 1 SignatureValueHashHex holds the digest as uppercase hexadecimal text and SignatureValueHashBytes as raw bytes, and the instance keeps the source bytes, the placeholder position and the original CMS for SetTimeStampToken and Apply.
The result code is also stored in ProcessResult. Requires the Enterprise edition; the method belongs to TPDFlibSignatureTimeStampProcess in unit PDFlibEntSignVerify.
Syntax
Delphi
Function TPDFlibSignatureTimeStampProcess.PrepareHash(SignerIndex: Integer= 0): Integer;Parameters
| SignerIndex | Zero-based index of the CMS signer inside the signature whose value is hashed; 0 selects the first signer |
|---|
Return values
| 1 | The signature value was hashed; the two hash fields and the cached state are filled |
|---|---|
| 2 | SourceDataType is dtFile and SourceFileName does not exist |
| 3 | The source or the signature contents are missing: SourceStream is Nil with dtStream, SourceString is empty with dtString, or the decoded /Contents of the selected signature is empty |
| 11 | SignerIndex is negative, or no signature value can be extracted from the CMS for this index, for example because the CMS cannot be decoded or the index is beyond the signer count |
| 12 | FieldName is not the name of a signature field, the object holding its signature value cannot be resolved, or its /Contents placeholder cannot be located in the source bytes |
| 14 | SourceDataType is dtFile and the document cannot be opened or parsed, for example because SourcePassword is wrong |
| 15 | SourceDataType is dtStream or dtString and the document cannot be parsed, for example because SourcePassword is wrong |
| 16 | SourceDataType is dtFile and the file cannot be read |
| 17 | The /Contents placeholder lies inside the byte range of another signature, so rewriting it would invalidate that signature; ContentsHexLength is set to 0 |
Remarks
A call with a non-negative SignerIndex clears the cached state and the two hash fields before it reads the source, and every path sets ProcessResult. The codes in the table are what the implementation returns; several conditions share a code.
The digest has to match the digest the time-stamp authority uses: SetTimeStampToken hashes the signature value again with the same DigestAlgorithm and accepts only a token whose hash algorithm and message imprint match. With dtFile the file is read twice, once to keep its bytes for Apply and once by the parser.
The signature has to leave room for the token: the reserved /Contents space is set when the signature is created, see SetSignProcessReserveContentsBytes. A placeholder that is too small is reported later by Apply with 13.
Code 17 protects later signatures and document time stamps, which sign over the /Contents bytes of earlier signatures. No exception is raised for the conditions in the table.
See also
TPDFlibSignatureTimeStampProcess, SetTimeStampToken, Apply, TPDFlibDigestAlgorithm, TPDFlibDataType, GetPAdESSignatureValueHashHex, SetSignProcessReserveContentsBytes