Contents · Forum Home · StreamSec Tools 4.1.5.365 documentation

Add long-term validation data to a timestamped CAdES signature

Start with the original file and its detached B-T signature. You can add the supporting certificates and revocation evidence later, without the signer's private key. The output is a new signature file for the same document. Keep the B-T original.

The components on this page are normally placed on a form or a data module at design time and set up in the Object Inspector. The sample creates them in code instead, because code shows at a glance which properties are changed from their default values.

Gather validation evidence

Build and validate the certificate paths for the signer and timestamp authority using your trusted roots. Collect the certificates and signed revocation evidence needed for those paths. Before packaging a CRL, validate its issuer, signature and scope, and check the status of each relevant certificate. For this workflow, collect a CRL whose thisUpdate follows the verified timestamp and whose validity interval covers your validation time.

uses
  System.Classes, System.SysUtils, stDERUtils, stDERCoder, stGC, stASN1Base,
  StreamSec.DSI.PkixCert, StreamSec.DSI.PkixKeyManager, StreamSec.DSI.CMS,
  StreamSec.DSI.CAdES, StreamSec.DSI.Tsp, TaskCadesSign, TaskTimestamp;
function CadesCrlEvidence(const aCertificates: array of iCertificate;
  const aCrls: array of OctetString): tAdESValidationMaterial;
var
  lCert: iCertificate;
  lCrl: OctetString;
begin
  Result := Default(tAdESValidationMaterial);
  for lCert in aCertificates do
    AdESAddCert(Result, lCert.GetStruct.EncodedContentsOS(tDERCoder.DefaultCoder, True));
  for lCrl in aCrls do
    AdESAddCrl(Result, lCrl);
end;

Pass the validated certificates and whole DER-encoded CRLs to CadesCrlEvidence. If your policy uses OCSP, call AdESAddOcsp with a validated, complete DER-encoded OCSP response. The library extracts the BasicOCSPResponse for the CMS attribute. ExtendToLT packages the evidence you supply. Checking that it covers the right certificates and times is the caller's responsibility.

Check the signature and its timestamp

This example handles one signer. Supply the expected document signer's certificate and an independently trusted TSA certificate. It uses VerifyCadesStreams from Create and verify a CAdES baseline signature for a file to check the original content and signer. The RFC 3161 imprint in a B-T signature covers the signer's signature bytes. VerifySignatureTimeStamp checks that imprint and the token's signature. AllowedTsa from Verify an RFC 3161 timestamp for a file checks the TSA identity, timestamping purpose and certificate validity at the token time.

function CheckSignatureTimestamp(aCms: TStream; const aTrustedTsa: iCertificate;
  out aError: string): Boolean;
var
  lCms: iContentInfo;
  lTsa: TstTimeStampClient;
  lTime: iTSTInfo;
begin
  aCms.Position := 0;
  lCms := tContentInfo.Create(gcamFirstAssignment);
  lCms.GetStruct.LoadFromStream(aCms, fmtDER);
  lTsa := TstTimeStampClient.Create(nil);
  try
    lTsa.TsaCertificate := aTrustedTsa;
    lTsa.ExtKeyUsagePolicy := ekupRequire;
    Result := VerifySignatureTimeStamp(lCms.content.AsSignedData.signerInfos[0],
      lTsa, lTime);
    if Result then
      Result := AllowedTsa(lTsa.LastTsaCertificate, aTrustedTsa, lTime.genTime, aError)
    else
      aError := 'Signature timestamp did not verify. ' + lTsa.LastError;
  finally
    lTsa.Free;
  end;
end;

Extend the signature and check the result

function ExtendVerifiedCadesLT(aKeys: TstCertificateManager;
  const aSigner, aTrustedTsa: iCertificate; aContent, aTimestamped: TStream;
  const aMaterial: tAdESValidationMaterial; aResult: TMemoryStream;
  out aError: string): Boolean;
var
  lLevel: tCAdESLevel;
  lExtended: TMemoryStream;
begin
  Result := False;
  aError := '';
  if (aKeys = nil) or (aSigner = nil) or (aTrustedTsa = nil) or
     (aContent = nil) or (aTimestamped = nil) or (aResult = nil) or
     (aContent = aTimestamped) or (aContent = aResult) or (aTimestamped = aResult) then begin
    aError := 'Supply the expected certificates and separate input and output streams.';
    Exit;
  end;
  aContent.Position := 0;
  aTimestamped.Position := 0;
  if not VerifyCadesStreams(aKeys, aSigner, aContent, aTimestamped, lLevel) or
     (lLevel <> calBT) then begin
    aError := 'A verified, detached B-T signature is required.';
    Exit;
  end;
  if not CheckSignatureTimestamp(aTimestamped, aTrustedTsa, aError) then Exit;
  if (Length(aMaterial.fCerts) = 0) or
     ((Length(aMaterial.fCrls) = 0) and (Length(aMaterial.fOcsps) = 0)) then begin
    aError := 'Supply supporting certificates and revocation evidence.';
    Exit;
  end;

  lExtended := TMemoryStream.Create;
  try
    aTimestamped.Position := 0;
    if not ExtendToLT(aTimestamped, lExtended, aMaterial, aError) then Exit;
    aContent.Position := 0;
    lExtended.Position := 0;
    if not VerifyCadesStreams(aKeys, aSigner, aContent, lExtended, lLevel) or
       (lLevel <> calBLT) then begin
      aError := 'The extended signature did not verify at B-LT.';
      Exit;
    end;
    if not CheckSignatureTimestamp(lExtended, aTrustedTsa, aError) then Exit;
    lExtended.Position := 0;
    aResult.LoadFromStream(lExtended);
    aResult.Position := 0;
    Result := True;
  finally
    lExtended.Free;
  end;
end;

Pass seekable input streams and a separate memory stream for the result. On success, save the result to a new signature file. On False, inspect the error text. A stream or decoding exception also stops the operation. The sample checks the content signature and timestamp again after embedding the evidence. Reading calBLT confirms that the encoded signature carries the B-LT attributes. Historical trust decisions still depend on the evidence and trust policy described above.

Reference

TstCertificateManager · TstTimeStampClient · StreamSec.DSI.CAdES · StreamSec.DSI.CMS · StreamSec.DSI.PkixCms · StreamSec.DSI.PkixKeyManager · StreamSec.DSI.Tsp

Related tasks