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

Set an FTPS Client Certificate (Cert object)

See more FTP Examples

Demonstrates the Chilkat Ftp2.SetSslClientCert method, which configures a client certificate for FTPS servers that request mutual TLS authentication. The only argument is a Cert that must have access to its private key.

Note: The local paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.

Background: Some high-security FTPS deployments require the client to present its own certificate during the TLS handshake — mutual TLS — as a second factor alongside the username and password. Because that happens while connecting, the certificate must be set before Connect. The Cert must carry its private key, since the client proves possession of it during the handshake.

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

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

procedure RunDemo;
var
  success: Boolean;
  ftp: TFtp2;
  pfxPassword: string;
  cert: TCert;

begin
  success := False;

  //  Demonstrates the Ftp2.SetSslClientCert method, which configures a client certificate for FTPS
  //  servers that request mutual TLS authentication.  The only argument is a Cert object that must
  //  have access to its private key.

  ftp := TFtp2.Create;

  ftp.Hostname := 'ftp.example.com';
  ftp.Username := 'myFtpLogin';

  //  Normally you would not hard-code the password in source.  You should instead obtain it
  //  from an interactive prompt, environment variable, or a secrets vault.
  ftp.Password := 'myPassword';

  //  Use explicit FTPS (AUTH TLS) on the standard FTP port.
  ftp.AuthTls := True;

  //  Load the client certificate (with its private key) from a PFX file.
  //  The PFX password should come from a secure source rather than being hard-coded.
  pfxPassword := 'myPfxPassword';
  cert := TCert.Create;
  success := cert.LoadPfxFile('qa_data/client.pfx',pfxPassword);
  if (success = False) then
    begin
      WriteLn(cert.LastErrorText);
      Exit;
    end;

  //  Provide the client certificate before connecting.
  success := ftp.SetSslClientCert(cert);
  if (success = False) then
    begin
      WriteLn(ftp.LastErrorText);
      Exit;
    end;

  success := ftp.Connect();
  if (success = False) then
    begin
      WriteLn(ftp.LastErrorText);
      Exit;
    end;
  WriteLn('Connected with a client certificate.');

  success := ftp.Disconnect();
  if (success = False) then
    begin
      WriteLn(ftp.LastErrorText);
      Exit;
    end;


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