Sample code for 30+ languages & platforms
Lazarus Pascal

Get Certificates within XML Signature

See more XML Digital Signatures Examples

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

Chilkat Lazarus Pascal Downloads

Lazarus Pascal
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.StringBuilder,
  Chilkat.StringArray,
  Chilkat.Cert,
  Chilkat.XmlDSig;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  sbXml: TStringBuilder;
  dsig: TXmlDSig;
  i: Integer;
  saCerts: TStringArray;
  cert: TCert;
  bVerifyReferenceDigests: Boolean;
  bVerified: Boolean;
  j: Integer;

begin
  success := False;

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

  sbXml := TStringBuilder.Create;

  //  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 = False) then
    begin
      WriteLn('Failed to load the XML file..');
      Exit;
    end;

  dsig := TXmlDSig.Create;

  //  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);
  if (success = False) then
    begin
      WriteLn(dsig.LastErrorText);
      Exit;
    end;

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

  WriteLn('numSignatures = ' + dsig.NumSignatures);

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

      bVerifyReferenceDigests := True;
      bVerified := dsig.VerifySignature(bVerifyReferenceDigests);
      WriteLn('Signature ' + i + 1 + ' verified = ' + bVerified);

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

        end;

      i := i + 1;
    end;



  sbXml.Free;
  dsig.Free;
  saCerts.Free;
  cert.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.