Sample code for 30+ languages & platforms
Delphi ActiveX

Get Certificates within XML Signature

See more XML Digital Signatures Examples

Demonstrates how to get the certificates contained within an XML signature.

Chilkat Delphi ActiveX Downloads

Delphi ActiveX
uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Chilkat_TLB;

...

procedure TForm1.Button1Click(Sender: TObject);
var
success: Integer;
sbXml: TChilkatStringBuilder;
dsig: TChilkatXmlDSig;
i: Integer;
saCerts: TCkStringArray;
cert: TChilkatCert;
bVerifyReferenceDigests: Integer;
bVerified: Integer;
j: Integer;

begin
success := 0;

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

sbXml := TChilkatStringBuilder.Create(Self);

// Load XML containing one or more signatures.
success := sbXml.LoadFile('qa_data/xml_dsig_valid_samples/multipleSigners/sp.pdf.XAdES.xml','utf-8');
if (success = 0) then
  begin
    Memo1.Lines.Add('Failed to load the XML file..');
    Exit;
  end;

dsig := TChilkatXmlDSig.Create(Self);

// First load the XML containing the signatures to be verified.
// Note that this particular Signature already contains the RSA public key that will be used
// for verification.
success := dsig.LoadSignatureSb(sbXml.ControlInterface);
if (success <> 1) then
  begin
    Memo1.Lines.Add(dsig.LastErrorText);
    Exit;
  end;

// For each signature, verify and also get the certificate(s) contained within each Signature.
i := 0;
saCerts := TCkStringArray.Create(Self);
cert := TChilkatCert.Create(Self);

Memo1.Lines.Add('numSignatures = ' + IntToStr(dsig.NumSignatures));

while i < dsig.NumSignatures do
  begin
    // Select the Nth signature by setting the Selector property.
    dsig.Selector := i;

    bVerifyReferenceDigests := 1;
    bVerified := dsig.VerifySignature(bVerifyReferenceDigests);
    Memo1.Lines.Add('Signature ' + IntToStr(i + 1) + ' verified = ' + IntToStr(Ord(bVerified)));

    // Get the certificates embedded in this signature.
    saCerts.Clear();
    success := dsig.GetCerts(saCerts.ControlInterface);
    if (success = 1) then
      begin
        j := 0;
        while j < saCerts.Count do
          begin
            success := cert.LoadFromBase64(saCerts.GetString(j));
            if (success = 1) then
              begin
                Memo1.Lines.Add('    ' + cert.SubjectDN);
              end;
            j := j + 1;
          end;

      end;

    i := i + 1;
  end;
end;