TPDFlibDSSProcess

security and signatures

Requires the Enterprise edition — the class is declared in the Enterprise unit PDFlibEntSignVerify, which other editions do not include

Description

TPDFlibDSSProcess writes a PAdES Document Security Store into an existing signed PDF: the certificates, CRLs and OCSP responses collected in the process become the /Certs, /CRLs and /OCSPs arrays of a /DSS dictionary in the document catalogue, with optional /VRI entries that assign validation material to one signature. The result is the source bytes followed by one incremental update, so the signed revision is not changed.

The class descends directly from TObject and has only public fields and methods. The caller sets the source and the target, adds the validation material with AddCertificate, AddCRL, AddOCSP and AddVRIEntry, calls Execute and reads the returned code, which Execute also stores in ProcessResult.

It is the post-hoc long-term validation path: the document is signed first and the validation material is attached later, once the certificate chain has been validated. All material has to be DER-encoded.

Requires the Enterprise edition. The class is declared in the Enterprise unit PDFlibEntSignVerify; add the unit to the uses clause. The Enterprise class methods NewPAdESDSSProcessFromFile, AddPAdESDSSCertificate and EndPAdESDSSProcessToFile with their variants drive an instance of this class through a process identifier.

Syntax

Delphi

TPDFlibDSSProcess = Class
Public
  SourceDataType: TPDFlibDataType;
  SourceFileName: WideString;
  SourceStream: TStream;
  SourceString: AnsiString;
  SourcePassword: WideString;
  TargetDataType: TPDFlibDataType;
  TargetFileName: WideString;
  TargetStream: TStream;
  TargetString: AnsiString;
  Certs: Array Of AnsiString;
  CRLs: Array Of AnsiString;
  OCSPs: Array Of AnsiString;
  VRIEntries: Array Of TPAdESVRIEntry;
  ProcessResult: Integer;
  Constructor Create;
  Procedure AddCertificate(Const CertDER: AnsiString);
  Procedure AddCRL(Const CRLDER: AnsiString);
  Procedure AddOCSP(Const OCSPDER: AnsiString);
  Procedure AddVRIEntry(Const SigContentsHashHex, CertIdxCSV, CRLIdxCSV, OCSPIdxCSV: AnsiString);
  Function Execute: Integer;
End;

Fields

SourceDataTypeWhere the source PDF comes from: dtFile reads SourceFileName, dtStream reads SourceStream and dtString reads SourceString. Create sets dtFile
SourceFileNamePath of the source PDF, used with dtFile. Execute returns 2 when the file does not exist and 3 when it cannot be opened or copied
SourceStreamStream with the source PDF, used with dtStream. Execute reads it from position 0 into a memory stream of its own and does not free it. Nil gives an empty source and result 3
SourceStringRaw bytes of the source PDF, used with dtString. An empty string gives an empty source and result 3
SourcePasswordPassword that opens the source PDF; leave it empty for a document that is not encrypted. A password that does not open an encrypted document gives result 3
TargetDataTypeWhere the augmented PDF goes: dtFile writes TargetFileName, dtStream writes TargetStream and dtString stores it in TargetString. Create sets dtFile
TargetFileNamePath of the output file, used with dtFile. The file is created or overwritten; a failed create or write gives result 10
TargetStreamStream that receives the augmented PDF, used with dtStream. Execute truncates it to position 0 and writes the complete document; it is not freed. Nil gives result 10
TargetStringReceives the complete augmented PDF as raw bytes with dtString; it is not changed otherwise
CertsDER-encoded certificates for the /Certs array of the /DSS dictionary. Filled by AddCertificate; empty after Create
CRLsDER-encoded certificate revocation lists for the /CRLs array of the /DSS dictionary. Filled by AddCRL; empty after Create
OCSPsDER-encoded OCSP responses for the /OCSPs array of the /DSS dictionary. Filled by AddOCSP; empty after Create
VRIEntriesTPAdESVRIEntry records for the /VRI dictionary; each refers to items of Certs, CRLs and OCSPs by zero-based index. Filled by AddVRIEntry; empty after Create
ProcessResultResult code of the most recent Execute call, the same value Execute returns; 0 until Execute runs

Result codes

1Execute: the /DSS dictionary was written and the augmented PDF was delivered to the target
2Execute: SourceDataType is dtFile and SourceFileName does not exist
3Execute: the source cannot be read or parsed (opening or copying the file failed, SourceDataType has another value, the reader rejected the data, or SourcePassword does not open the encrypted document)
10Execute: the target could not be written (TargetFileName could not be created or written, or TargetStream is Nil with dtStream)
11Execute: the document has no catalogue, so the /DSS dictionary could not be added; nothing is written to the target

Remarks

Workflow: create the object, set SourceDataType and the matching source field (plus SourcePassword for an encrypted document), set TargetDataType and the matching target field, add the material, call Execute and check its result or ProcessResult. The source is only read; the target receives the complete augmented PDF.

Each certificate, CRL and OCSP response is written verbatim as the payload of one new stream object, without compression, and nothing in the class parses or validates it. The AddPAdESDSSCertificate, AddPAdESDSSCRL and AddPAdESDSSOCSP methods of the Enterprise class check that the data is a single DER SEQUENCE before they hand it to this class.

When the catalogue already has a /DSS dictionary, its arrays are extended and its /VRI dictionary receives the new entries, so validation material of earlier revisions is kept. Writing the store also raises the PAdES /Extensions /ESIC declaration to extension level 1 unless a higher level is already declared.

A /VRI entry can only reference material that is added in the same Execute call; the indexes count from zero in the order the material was added. Execute writes the key of each entry, a /Type /VRI marker and the references, see TPAdESVRIEntry.

The caller owns SourceStream and TargetStream: the process reads the first from position 0, truncates the second to position 0 and rewrites it, and frees neither. With TargetDataType set to dtFile an existing TargetFileName is overwritten.

The failures listed for Execute are reported as result codes and do not raise. Execute leaves the material arrays as they are, only Create empties them.

Example

Uses PDFlibEntSignVerify;

Var
  Process: TPDFlibDSSProcess;
  SignerCertDER, IssuerCertDER, IssuerCRLDER, OCSPResponseDER: AnsiString;
Begin
  Process:= TPDFlibDSSProcess.Create;
  Try
    Process.SourceFileName:= 'signed.pdf';
    Process.TargetFileName:= 'signed-lt.pdf';
    Process.AddCertificate(SignerCertDER);
    Process.AddCertificate(IssuerCertDER);
    Process.AddCRL(IssuerCRLDER);
    Process.AddOCSP(OCSPResponseDER);
    If Process.Execute= 1 Then
      WriteLn('Document Security Store written');
  Finally
    Process.Free;
  End;
End;

See also

Execute, AddCertificate, AddCRL, AddOCSP, AddVRIEntry, TPAdESVRIEntry, TPDFlibDataType, TPDFlibSignatureTimeStampProcess, EndPAdESDSSProcessToFile, GetPAdESDSSProcessResult, ApplyPAdESLongTermValidation