You have a file and the DER timestamp response saved when it was stamped. This example accepts SHA-256 file imprints and verifies the signed time against a particular timestamp authority (TSA). It works offline.
Obtain the TSA's signing certificate as a DER file through a trusted channel. Keep it in application-controlled storage. Choose it independently of the response being verified. When the TSA rotates its signing key, keep the approved certificate for each period whose timestamps you accept.
uses
System.SysUtils,
stGC, stASN1Base, stDERCoder, stDERUtils, stSecUtils, stPkixExtension,
stSHA256, stSHA1, StreamSec.DSI.Arith, StreamSec.DSI.IFC,
StreamSec.DSI.PkixCert, StreamSec.DSI.Tsp;
function AllowedTsa(const aSigner, aTrustedTsa: iCertificate; aTimeUTC: TDateTime; out aReason: string): Boolean; var lPurpose: iExtension; begin Result := False; if not aSigner.SameCert(aTrustedTsa) then begin aReason := 'The response was signed by a different timestamp authority.'; Exit; end; lPurpose := aSigner.tbsCertificate.extensions.FindExtension(evCeExtKeyUsage); if not Assigned(lPurpose) or not lPurpose.critical or (lPurpose.extnValue.AsCeExtKeyUsage.ItemCount <> 1) then begin aReason := 'The timestamp purpose must be critical and exclusive.'; Exit; end; if not aSigner.tbsCertificate.validity.CheckValidityEx(aTimeUTC) then begin aReason := 'The TSA certificate was outside its validity period at genTime.'; Exit; end; Result := True; end;
The verifier below sets ExtKeyUsagePolicy to ekupRequire, which checks for the time-stamping purpose. AllowedTsa then checks that the actual signer matches your approved certificate, that this purpose is critical and exclusive, and that the signed time falls within the certificate's validity period.
function VerifyFileTimestamp(const aFile, aResponseFile, aTrustedTsaFile: string; out aTimeUTC: TDateTime; out aReason: string): Boolean; var lResponse: iTimeStampResp; lTrustedTsa: iCertificate; lClient: TstTimeStampClient; lInfo: iTSTInfo; lDigest: OctetString; begin Result := False; aTimeUTC := 0; aReason := ''; lResponse := tTimeStampResp.Create(gcamFirstAssignment); lResponse.GetStruct.LoadFromFile(aResponseFile, fmtDER); if not (lResponse.status.status in [cPKIStatusGranted, cPKIStatusGrantedWithMods]) or not lResponse.hasToken then begin aReason := 'The response did not grant a timestamp with a token.'; Exit; end; lTrustedTsa := tCertificate.Create(gcamFirstAssignment); lTrustedTsa.GetStruct.LoadFromFile(aTrustedTsaFile, fmtDER); lDigest := DigestFile(haSHA256, aFile); lClient := TstTimeStampClient.Create(nil); try lClient.TsaCertificate := lTrustedTsa; lClient.ExtKeyUsagePolicy := ekupRequire; if not lClient.VerifyTokenDigest(lResponse.token, lDigest, haSHA256, lInfo) then begin aReason := lClient.LastError; Exit; end; if not AllowedTsa(lClient.LastTsaCertificate, lTrustedTsa, lInfo.genTime, aReason) then Exit; aTimeUTC := lInfo.genTime; Result := True; finally lClient.Free; end; end;
Pass the original file path, the saved response path and the trusted TSA certificate path to VerifyFileTimestamp. Use the UTC time returned in aTimeUTC only when the function returns True. On False, aReason tells you which check failed. File access and decoding errors can raise exceptions.
DigestFile reads the file in blocks. Changing even a line ending changes the digest. VerifyTokenDigest checks the imprint algorithm and digest, the signed attributes and the token signature. Setting TsaCertificate also supplies the signer for a token that omits it. A token can carry its own signer, so the explicit comparison still matters.
A verified time records when the authority saw the file's digest. This sample trusts a specific signer. If your policy requires a CA chain or evidence of revocation status at the time of signing, check that evidence as well. See Check a certificate chain and its revocation and the PKIX notes.
TstTimeStampClient · StreamSec.DSI.Tsp · StreamSec.DSI.PkixCert