Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

Subject Alternative Name

See more Certificates Examples

Demonstrates the usage of the SubjectAlternativeName property to get the certificate SAN (subject alternative name) as XML.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
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.Cert,
  Chilkat.Xml;

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

procedure RunDemo;
var
  success: Boolean;
  cert: TCert;
  subjectAltNameXml: string;
  xml: TXml;
  oid: string;
  name: string;
  i: Integer;
  count_i: Integer;
  rfc822Name: string;

begin
  success := False;

  //  Note: Not all certificates contain a Subject Alternative Name.
  //  If it does not, the SubjectAlternativeName property will contain the empty string.

  cert := TCert.Create;

  success := cert.LoadFromFile('qa_data/certs/testIcpBrasil.cer');
  if (success = False) then
    begin
      WriteLn(cert.LastErrorText);
      Exit;
    end;

  subjectAltNameXml := cert.SubjectAlternativeName;

  WriteLn(subjectAltNameXml);

  //  Here's a sample of the subjectAltNameXml:

  //  <?xml version="1.0" encoding="utf-8"?>
  //  <SubjectAltName>
  //      <name type="oid" oid="2.16.76.1.3.4">...</name>
  //      <name type="oid" oid="2.16.76.1.3.2">...</name>
  //      <name type="oid" oid="2.16.76.1.3.3">...</name>
  //      <name type="oid" oid="2.16.76.1.3.7">...</name>
  //      <rfc822Name>...</rfc822Name>
  //  </SubjectAltName>

  //  The XML can be parsed like this:

  xml := TXml.Create;

  xml.LoadXml(subjectAltNameXml);

  i := 0;
  count_i := xml.NumChildrenHavingTag('name');
  while i < count_i do
    begin
      xml.I := i;
      oid := xml.ChilkatPath('name[i]|(oid)');
      name := xml.GetChildContent('name[i]');
      i := i + 1;
    end;

  rfc822Name := xml.GetChildContent('rfc822Name');


  cert.Free;
  xml.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.