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

Apple Keychain - Get Certificate by Common Name

See more Apple Keychain Examples

Retrieves the certificate from the Apple Keychain that matches the specified common name (CN).

Note: This example requires Chilkat v10.0.0 or greater.

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;

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

procedure RunDemo;
var
  success: Boolean;
  cert: TCert;
  cert2: TCert;
  hasPrivKey: Boolean;

begin
  success := False;

  //  On MacOS or iOS, this searches both the Default keychains and System keychains for the certificate
  //  having the specified CN.
  cert := TCert.Create;
  success := cert.LoadByCommonName('Certum CA');
  if (success = False) then
    begin
      WriteLn(cert.LastErrorText);
      Exit;
    end;

  WriteLn(cert.SubjectDN);

  //  Note: In the Apple Keychain, a certificate with its private key represents an identity because it combines the certificate and its private key.
  //  When Chilkat loads a certificate from the Apple Keychain, it also gets the associated private key.
  //  You can simply use the cert in other places in Chilkat where a cert w/ private key is required.
  cert2 := TCert.Create;
  success := cert2.LoadByCommonName('Chilkat Software, Inc.');
  if (success = False) then
    begin
      WriteLn(cert2.LastErrorText);
      Exit;
    end;

  WriteLn(cert2.SubjectDN);
  hasPrivKey := cert2.HasPrivateKey();
  WriteLn('Has private key: ' + hasPrivKey);

  WriteLn('Success.');


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