Sample code for 30+ languages & platforms
Lazarus Pascal Requires Chilkat v10.1.2+

Open Smartcard Certificate Store (or from USB Token)

See more Certificates Examples

Demonstrates how to open the certificate store of the smart card currently in the reader (or the USB token). Iterates over the certs found on the smartcard.

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

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

procedure RunDemo;
var
  success: Boolean;
  certStore: TCertStore;
  cert: TCert;
  i: Integer;
  numCerts: Integer;

begin
  success := False;

  certStore := TCertStore.Create;

  //  Access the certificates on the smart card or USB token via the Chilkat certificate store class.
  //  Note: Always pass the empty string to OpenSmartcard.

  success := certStore.OpenSmartcard('');
  if (success = False) then
    begin
      WriteLn(certStore.LastErrorText);
      Exit;
    end;
  WriteLn(certStore.LastErrorText);

  //  Iterate over certificates on the smartcard.
  cert := TCert.Create;
  i := 0;
  numCerts := certStore.NumCertificates;
  WriteLn('numCerts = ' + numCerts);

  while (i < numCerts) do
    begin
      certStore.GetCert(i,cert);
      WriteLn(cert.SubjectCN);
      WriteLn(cert.SerialNumber);
      if (cert.IsRsa() = True) then
        begin
          WriteLn('key type is RSA');
        end;
      if (cert.IsEcdsa() = True) then
        begin
          WriteLn('key type is ECDSA');
        end;
      WriteLn('has private key: ' + cert.HasPrivateKey());
      WriteLn('----');
      i := i + 1;
    end;



  certStore.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.