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

Find Certificate by Serial Number

See more Cert Store Examples

Demonstrates how to find a certificate having the specified hexadecimal serial number.

Note: Requires Chilkat v10.1.2 or later.

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.CertStore,
  Chilkat.JsonObject;

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

procedure RunDemo;
var
  success: Boolean;
  certStore: TCertStore;
  argNotUsed: string;
  hexSerial: string;
  json: TJsonObject;
  cert: TCert;

begin
  success := False;

  certStore := TCertStore.Create;

  //  This example will search the certs on connected USB tokens and smartcards.
  argNotUsed := '';
  success := certStore.OpenSmartcard(argNotUsed);
  if (success = False) then
    begin
      WriteLn(certStore.LastErrorText);
      Exit;
    end;

  //  Find the certificate having a serial number = "48FC93B46055948D36A7C98A89D69416".
  hexSerial := '48FC93B46055948D36A7C98A89D69416';
  json := TJsonObject.Create;
  json.UpdateString('serial',hexSerial);

  cert := TCert.Create;
  success := certStore.FindCert(json,cert);
  if (success = True) then
    begin
      //  Show the serial number and subject CN
      WriteLn('Found: ' + cert.SerialNumber + ', ' + cert.SubjectCN);
    end
  else
    begin
      WriteLn('Not found.');
    end;


  certStore.Free;
  json.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.