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

ZATCA Load Certificate and Private Key from PEM Files

See more ZATCA Examples

Demonstrates how to load a certificate and private key from a pair of PEM files.

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.PrivateKey;

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

procedure RunDemo;
var
  success: Boolean;
  cert: TCert;
  privKey: TPrivateKey;

begin
  success := False;

  //  The LoadFromFile method will automatically detect the file format..
  cert := TCert.Create;
  success := cert.LoadFromFile('qa_data/zatca/cert.pem');
  if (success <> True) then
    begin
      WriteLn(cert.LastErrorText);
      Exit;
    end;

  WriteLn(cert.SubjectCN);

  //  Load the private key.
  privKey := TPrivateKey.Create;
  success := privKey.LoadPemFile('qa_data/zatca/ec-secp256k1-priv-key.pem');
  if (success <> True) then
    begin
      WriteLn(privKey.LastErrorText);
      Exit;
    end;

  WriteLn('Key Type: ' + privKey.KeyType);

  //  Associate the private key with the certificate.
  success := cert.SetPrivateKey(privKey);
  if (success <> True) then
    begin
      WriteLn(cert.LastErrorText);
      Exit;
    end;

  WriteLn('Success.');


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