Sign an unencrypted PDF and report the length of the revision that the signer approved. A PDF can carry several approvals. Each signer covers the document as it existed when their signature field was added.
Use an enabled certificate manager that can find the private key for the signing certificate. Supply a seekable input stream and a distinct field name for each approval, such as Approval and Review.
uses
System.Classes, System.SysUtils, stSecUtils, stDERUtils,
StreamSec.DSI.PkixCert, StreamSec.DSI.PkixKeyManager, StreamSec.DSI.CAdES,
StreamSec.Pdf.Signer, TaskCadesSign;
function SignPdfRevision(aKeys: TstCertificateManager; const aSigner: iCertificate; const aFieldName: string; aPdf: TStream): TMemoryStream; var lSigner: TstPdfSigner; begin lSigner := TstPdfSigner.Create(nil); try lSigner.KeysAndCerts := aKeys; lSigner.FieldName := aFieldName; lSigner.ContentDigestAlg := haSHA256; lSigner.SubFilter := psfEtsiCadesDetached; lSigner.ExtKeyUsagePolicy := ekupAllowUnconstrained; aPdf.Position := 0; Result := TMemoryStream.Create; try if not lSigner.SignData(aSigner, aPdf, Result) then raise ePdfSignError.Create(lSigner.LastError); Result.Position := 0; except Result.Free; raise; end; finally lSigner.Free; end; end;
psfEtsiCadesDetached selects the PAdES baseline encoding. The signer appends a PDF revision and puts the detached CMS signature in its /Contents field. The original PDF remains available in the input stream. Save the returned memory stream to a new PDF file with SaveToFile, then free the stream. Keep separate copies when you need to retain each revision.
Configure the verification manager with the trusted certificates, chain filter and issuer-and-serial CMS index. Obtain the expected signer's public certificate from your own trusted records and apply your chain and revocation policy. The signing and verification components below accept a certificate with no extended key usage or one that permits document signing.
Use VerifyCadesStreams from Create and verify a CAdES baseline signature for a file in the TaskCadesSign unit. It checks the detached CMS signature against the supplied bytes and compares the verified signer to the expected certificate.
function CheckApproval(const aInfo: tPdfSignatureInfo; aSize: Int64; aKeys: TstCertificateManager; const aExpected: iCertificate): Boolean; var lContent, lSignature: TBytesStream; lLevel: tCAdESLevel; begin Result := False; if (aInfo.fSubFilter <> 'ETSI.CAdES.detached') or not aInfo.fGapIsContentsString then Exit; if (aInfo.fByteRange[0] <> 0) or (aInfo.fByteRange[1] <= 0) or (aInfo.fByteRange[2] <= aInfo.fByteRange[1]) or (aInfo.fByteRange[2] > aSize) or (aInfo.fByteRange[3] <= 0) or (aInfo.fByteRange[3] > aSize - aInfo.fByteRange[2]) then Exit; lContent := TBytesStream.Create(OSToBytes(aInfo.fSignedBytes)); try lSignature := TBytesStream.Create(OSToBytes(aInfo.fContents)); try Result := VerifyCadesStreams(aKeys, aExpected, lContent, lSignature, lLevel); finally lSignature.Free; end; finally lContent.Free; end; end;
The four /ByteRange numbers are two zero-based offset-and-length pairs. The first span starts at byte zero. The second starts after the reserved signature slot. Its offset plus its length is the end of this signed revision, even if the file now contains another revision.
function VerifyPdfRevision(aKeys: TstCertificateManager; const aExpected: iCertificate; aPdf: TStream; aIndex: Integer; out aSignedLength: Int64; out aLaterChanges: tPdfModificationClass): Boolean; var lVerifier: TstPdfVerifier; lInfo: tPdfSignatureInfo; begin Result := False; aSignedLength := 0; aLaterChanges := pmcUnparsable; lVerifier := TstPdfVerifier.Create(nil); try lVerifier.KeysAndCerts := aKeys; lVerifier.ExtKeyUsagePolicy := ekupAllowUnconstrained; aPdf.Position := 0; if lVerifier.VerifyData(aPdf) <> pssValid then Exit; if (aIndex < 0) or (aIndex >= lVerifier.SignatureCount) then Exit; lInfo := lVerifier.Signatures[aIndex]; if not CheckApproval(lInfo, aPdf.Size, aKeys, aExpected) then Exit; aSignedLength := lInfo.fByteRange[2] + lInfo.fByteRange[3]; aLaterChanges := lVerifier.ModificationOf[aIndex]; Result := True; finally lVerifier.Free; end; end;
Call VerifyPdfRevision with a stable, seekable PDF stream and the zero-based index of the approval in the PDF field list. The sample requires VerifyData to return pssValid across the document, then checks the selected field against the expected signer. A missing field, a changed covered byte or a different signer gives False. Treat a parsing or I/O exception as a verification failure as well.
Read the two outputs only on True.
aSignedLength equals the PDF size, the selected signer approved the current revision.aSignedLength is smaller and aLaterChanges is pmcSignatureOnly, the verifier classified the later changes as signature-related. For example, after a second approval, the first field still covers the earlier revision. Check the second field against its own expected signer to identify who approved the current revision.Check a certificate chain and its revocation covers certificate policy. Verify an RFC 3161 timestamp for a file covers a separate time-stamping task.
TstPdfSigner · TstPdfVerifier · TstCertificateManager · StreamSec.Pdf.Signer · StreamSec.DSI.PkixCert · StreamSec.DSI.PkixKeyManager · StreamSec.DSI.CAdES