TPDFlibSignatureTimeStampProcess
Security and signatures
Requires the Enterprise edition — the class is declared in the Enterprise unit PDFlibEntSignVerify, which other editions do not include
Description
TPDFlibSignatureTimeStampProcess adds a signature time stamp to an existing signature. PrepareHash hashes the signature value of one CMS signer; the caller sends that hash to a time-stamp authority; SetTimeStampToken validates the returned RFC 3161 token, attaches it to the signer as an unsigned id-aa-signatureTimeStampToken attribute and rebuilds the CMS; Apply writes the rebuilt CMS into the /Contents placeholder of the signature and delivers the patched PDF.
The class descends directly from TObject. Its public fields are filled in by the caller before the first step. The class validates and rewrites the CMS but does not contact the time-stamp authority: the token request is the caller's part of the workflow.
The rebuilt CMS has to fit into the /Contents placeholder of the signature, so the signature has to be created with reserved space, see SetSignProcessReserveContentsBytes.
Requires the Enterprise edition. The class is declared in the Enterprise unit PDFlibEntSignVerify; add the unit to the uses clause. The Enterprise class methods from NewPAdESSignatureTimeStampProcessFromFile to EndPAdESSignatureTimeStampProcessToFile drive an instance of this class through a process identifier.
Syntax
Delphi
TPDFlibSignatureTimeStampProcess = Class
Public
SourceDataType: TPDFlibDataType;
SourceFileName: WideString;
SourceStream: TStream;
SourceString: AnsiString;
SourcePassword: WideString;
TargetDataType: TPDFlibDataType;
TargetFileName: WideString;
TargetStream: TStream;
TargetString: AnsiString;
FieldName: WideString;
DigestAlgorithm: TPDFlibDigestAlgorithm;
NewCMSBytes: AnsiString;
SignatureValueHashHex: AnsiString;
SignatureValueHashBytes: AnsiString;
ProcessResult: Integer;
FullSource: AnsiString;
ContentsHexStart: Int64;
ContentsHexLength: Int64;
OriginalCMS: AnsiString;
Constructor Create;
Function PrepareHash(SignerIndex: Integer= 0): Integer;
Function SetTimeStampToken(Const TimeStampTokenDER: AnsiString;
SignerIndex: Integer): Integer;
Function Apply: Integer;
End;Fields
| SourceDataType | Where the source PDF comes from for PrepareHash: dtFile reads SourceFileName, dtStream reads SourceStream and dtString reads SourceString. Create sets dtFile |
|---|---|
| SourceFileName | Path of the source PDF, used with dtFile. PrepareHash returns 2 when the file does not exist, 16 when it cannot be read and 14 when it cannot be opened or parsed |
| SourceStream | Stream with the source PDF, used with dtStream. PrepareHash reads it from position 0 and does not free it; Nil gives result 3 |
| SourceString | Raw bytes of the source PDF, used with dtString; an empty string gives result 3 |
| SourcePassword | Password that opens the source PDF; leave it empty for a document that is not encrypted. When the document cannot be opened or parsed with it, PrepareHash returns 14 for a file and 15 for a stream or string |
| TargetDataType | Where Apply writes the patched PDF: dtFile writes TargetFileName, dtStream writes TargetStream and dtString stores it in TargetString. Create sets dtFile |
| TargetFileName | Path of the output file, used with dtFile. Apply creates or overwrites it; a failed create or write gives result 10 |
| TargetStream | Stream that receives the patched PDF, used with dtStream. Apply truncates it to position 0 and writes the complete document; it is not freed. Nil gives result 10 |
| TargetString | Receives the complete patched PDF as raw bytes with dtString |
| FieldName | Name of the signature field whose signature value is hashed. Empty after Create; a name that is not a signature field gives result 12 from PrepareHash |
| DigestAlgorithm | Digest for the signature value, as a TPDFlibDigestAlgorithm value. Create sets daSHA256; daSHA1, daSHA384, daSHA512, daSHA3_256, daSHA3_384, daSHA3_512 and daSM3 are used as set, every other value hashes with SHA-256 |
| NewCMSBytes | The rebuilt CMS that Apply writes into the /Contents placeholder. SetTimeStampToken fills it on success; a caller that builds the CMS in another way can assign it directly. Empty gives result 11 from Apply |
| SignatureValueHashHex | Uppercase hexadecimal digest of the signature value, ready to be sent to a time-stamp authority as the message imprint. PrepareHash clears it before it reads the source and fills it on result 1; empty after Create |
| SignatureValueHashBytes | The same digest as raw bytes, filled by PrepareHash on result 1. SetTimeStampToken hashes the signature value again instead of reading this field |
| ProcessResult | Result code of the most recent PrepareHash, SetTimeStampToken or Apply call; 0 until one of them runs |
| FullSource | Cached state from PrepareHash: the complete source PDF bytes. Apply patches the /Contents range in a copy of them |
| ContentsHexStart | Cached state from PrepareHash: index in FullSource of the first hexadecimal character inside the /Contents placeholder, counted from 1 |
| ContentsHexLength | Cached state from PrepareHash: number of hexadecimal characters in the placeholder, twice the byte length of the original CMS. PrepareHash sets it to 0 when it returns 17 |
| OriginalCMS | Cached state from PrepareHash: the CMS decoded from /Contents, cut to the length its outer SEQUENCE declares. While it is empty, SetTimeStampToken runs PrepareHash first and Apply returns 11 |
Result codes
| 1 | PrepareHash: the signature value was hashed. SetTimeStampToken: the token was accepted and NewCMSBytes was filled. Apply: the patched PDF was delivered to the target |
|---|---|
| 2 | PrepareHash: SourceDataType is dtFile and SourceFileName does not exist |
| 3 | PrepareHash: SourceStream is Nil with dtStream, SourceString is empty with dtString, or the decoded /Contents of the signature is empty |
| 10 | Apply: TargetFileName could not be created or written, or TargetStream is Nil with dtStream |
| 11 | PrepareHash: SignerIndex is negative or no signature value can be extracted for it. SetTimeStampToken: SignerIndex is negative, no signature value can be extracted, the token failed a check, or the attribute could not be inserted. Apply: OriginalCMS is empty, ContentsHexLength is 0 or NewCMSBytes is empty |
| 12 | PrepareHash: FieldName is not a signature field, the object holding its signature value cannot be resolved, or its /Contents placeholder cannot be located |
| 13 | Apply: the hexadecimal form of NewCMSBytes is longer than the /Contents placeholder |
| 14 | PrepareHash: with dtFile the document cannot be opened or parsed |
| 15 | PrepareHash: with dtStream or dtString the document cannot be parsed |
| 16 | PrepareHash: with dtFile the source file cannot be read |
| 17 | PrepareHash: the /Contents placeholder lies inside the byte range of another signature; ContentsHexLength is set to 0 |
| Other | SetTimeStampToken only: OriginalCMS was empty and the PrepareHash call it made failed; the value is the code of that call, 2, 3, 12, 14, 15, 16 or 17 (an 11 of that call is the 11 above) |
Remarks
Call order: Create, then the source (SourceDataType with the matching field, and SourcePassword for an encrypted document), FieldName, DigestAlgorithm and the target (TargetDataType with the matching field). Call PrepareHash for the signer and, when it returns 1, send SignatureValueHashHex as the message imprint to the time-stamp authority.
Pass the token returned by the authority to SetTimeStampToken; on result 1 NewCMSBytes holds the rebuilt CMS. Then call Apply, which replaces the /Contents bytes and writes the patched PDF to the target. Nothing is written before Apply.
PrepareHash stores the source bytes, the position of the placeholder and the original CMS in the class; SetTimeStampToken and Apply use this cached state and do not parse the document again. A new PrepareHash call discards the state and starts over. ProcessResult always holds the code of the most recent of the three calls.
When OriginalCMS is empty, SetTimeStampToken calls PrepareHash itself with the same SignerIndex and returns the code of that call when it fails; Apply returns 11 in this state.
Apply encodes NewCMSBytes as uppercase hexadecimal and pads it with 00 up to the full width of the placeholder, so the /ByteRange of the signature stays valid; no other byte of the source changes. A rebuilt CMS that is too large for the placeholder makes Apply return 13.
PrepareHash returns 17 when another signature covers the /Contents placeholder with its byte range, because a later signature or document time stamp would be invalidated by the rewrite.
SetTimeStampToken replaces an existing signature time-stamp attribute of the signer, keeps the other unsigned attributes and writes the attribute set in sorted DER order. The signed attributes and the signature value are left as they are.
The caller owns SourceStream and TargetStream; the process frees neither. PrepareHash reads SourceStream from position 0, Apply truncates TargetStream to position 0 and rewrites it, and with dtFile it creates or overwrites TargetFileName.
In the example, RequestTimeStamp stands for the caller's own request to its time-stamp authority.
Example
Uses PDFlibEntSignVerify;
Var
Process: TPDFlibSignatureTimeStampProcess;
TokenDER: AnsiString;
Begin
Process:= TPDFlibSignatureTimeStampProcess.Create;
Try
Process.SourceFileName:= 'signed.pdf';
Process.TargetFileName:= 'signed-t.pdf';
Process.FieldName:= 'Signature1';
If Process.PrepareHash(0)= 1 Then
Begin
TokenDER:= RequestTimeStamp(Process.SignatureValueHashHex);
If Process.SetTimeStampToken(TokenDER, 0)= 1 Then
Process.Apply;
End;
Finally
Process.Free;
End;
End;See also
PrepareHash, SetTimeStampToken, Apply, TPDFlibDigestAlgorithm, TPDFlibDataType, TPDFlibDSSProcess, SetSignProcessReserveContentsBytes, SetPAdESSignatureTimeStampToken, EndPAdESSignatureTimeStampProcessToFile, GetPAdESSignatureTimeStampProcessResult, TPDFlibSignatureVerifier