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

Apple Keychain - List Certs on Smartcards and USB Tokens

See more Apple Keychain Examples

Iterates over the certificatse on connected smartcards and USB tokens via the Apple Keychain.

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;

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

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

begin
  success := False;

  certStore := TCertStore.Create;

  //  On MacOS and iOS, the OpenSmartcard method opens the Keychain.
  //  The argument passed to OpenSmartcard is ignored.
  success := certStore.OpenSmartcard('');
  if (success = False) then
    begin
      WriteLn(certStore.LastErrorText);
      Exit;
    end;

  numCerts := certStore.NumCertificates;
  WriteLn('numCerts = ' + numCerts);

  cert := TCert.Create;
  i := 0;
  while i < numCerts do
    begin
      //  Note: Chilkat also gets the associated private key if it exists.
      //  You can simply use the cert in other places in Chilkat where a cert w/ private key is required.
      certStore.GetCert(i,cert);
      WriteLn(cert.SubjectDN);
      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.CloseCertStore();


  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.