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

Verify FTP SSL Server Certificate

See more FTP Examples

This example demonstrates how to verify the FTP server's certificate and authenticity. The intent is to verify the authenticity of the server before passing a login/password to it.

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;
  cert: TCert;

begin
  success := False;

  //  This example requires the Chilkat API to have been previously unlocked.
  //  See Global Unlock Sample for sample code.

  ftp := TFtp2.Create;

  ftp.Hostname := 'ftp.myftpserver.com';
  ftp.Username := 'myUsername';
  ftp.Password := 'myPassword';

  //  Establish an AUTH SSL secure channel after connection
  //  on the standard FTP port 21.
  ftp.AuthSsl := True;

  //  The Ssl property is for establishing an implicit SSL connection
  //  on port 990.  Do not set it.
  ftp.Ssl := False;

  //  Indicate that the FTP server must have a verifiable SSL certificate.
  //  Do not accept self-signed certs or certificates that are
  //  expired, revoked, or cannot be verified to a root authority:
  ftp.RequireSslCertVerify := True;

  //  You may also set a requirement.  In this example,
  //  the certificate's Common Name (CN) must match the
  //  required string exactly:
  ftp.SetSslCertRequirement('subjectcn','Chilkat Software, Inc.');

  //  Connect and login to the FTP server.
  success := ftp.Connect();
  if (success = False) then
    begin
      WriteLn(ftp.LastErrorText);
      Exit;
    end;

  //  After logging on, you may examine the FTP server's cert:
  cert := TCert.Create;
  success := ftp.GetServerCert(cert);
  if (success = False) then
    begin
      WriteLn('No server certificate!');
    end
  else
    begin
      //  Display the distinguished name of the SSL cert.
      WriteLn(cert.SubjectDN);
    end;

  WriteLn('Secure FTP Channel Established!');

  //  Do whatever you're doing to do ...
  //  upload files, download files, etc...

  success := ftp.Disconnect();


  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.