Contents · Forum Home · StreamSec Tools 4.1.5.364 documentation

Check a certificate chain and its revocation

A certificate is trusted when it chains, signature by signature, to a certificate you trust explicitly - a trust anchor - and when no certificate in the chain has been revoked. TsmX509TrustedCertificates checks both.

The TLS components do all of this themselves during the handshake. This page is for checking a certificate outside TLS: a signed document, a code signature, a certificate a user uploads.

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.

Check the chain

function CheckChain(const aRootFile, aIntermediateFile, aLeafFile: string;
  const aCrlFiles: array of string; aPolicy: TRevocationPolicy): TCertStatusCode;
var
  lStore: TsmX509TrustedCertificates;
  lStatus: TCertStatusCode;
  lCrlFile: string;
  lCrl: iCertificateList;
begin
  lStore := TsmX509TrustedCertificates.Create(nil);
  try
    { rpHardFail refuses a certificate whose revocation status cannot be
      established; rpSoftFail accepts it, but still refuses one a list
      says is revoked. }
    lStore.RevocationPolicy := aPolicy;
    { The trust anchor: True = trusted explicitly, on your say-so. }
    lStore.AddCertificate(LoadCertificate(aRootFile), True, lStatus);
    { The revocation lists, before the certificates they speak about. }
    for lCrlFile in aCrlFiles do begin
      lCrl := tCertificateList.Create(gcamFirstAssignment);
      lCrl.GetStruct.LoadFromFile(lCrlFile, fmtDER);
      lStore.AddCRL(lCrl, lStatus);
    end;
    { The intermediate: False = trusted only if it verifies up to the anchor. }
    lStore.AddCertificate(LoadCertificate(aIntermediateFile), False, lStatus);
    Result := crcSyntax;
    lStore.VerifyCertificate(LoadCertificate(aLeafFile), False, Result);
  finally
    lStore.Free;
  end;
end;

The certificates are read as in Read a certificate file.

The verdict

VerifyCertificate gives a TCertStatusCode: crcOK when the chain is complete and nothing in it is known to be revoked. Under the default rpSoftFail that includes a certificate whose status could not be established; rpHardFail gives crcOK only when it was. Among the others:

Revocation lists and the policy

A revocation list (CRL) is published by a CA and speaks only about the certificates that CA issued. To check a root-intermediate-leaf chain completely, you need the root's list (for the intermediate) and the intermediate's list (for the leaf).

A list must be signed by the CA it speaks for; a forged one is refused. The TLS notes describe fetching lists from the addresses in the certificates, and OCSP.

Reference

TsmX509TrustedCertificates · StreamSec.Mobile.X509Comp

Related tasks