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

SSH Tunnel Set Allowed SSH Algorithms

See more SSH Tunnel Examples

Demonstrates the Chilkat SshTunnel.SetAllowedAlgorithms method, which restricts the SSH connection to a specific set of algorithms. A JsonObject supplies comma-separated allow-lists for the kex, hostKey, cipher, and mac categories. It must be called before Connect.

Important: You typically should not set allowed algorithms explicitly. By default Chilkat orders algorithms according to best practices and accounts for known vulnerabilities.

Background: SSH negotiates a mutually supported algorithm from each category at connection time, and pinning that choice makes an application brittle: if the server later drops a weak algorithm, the two sides may fail to agree and the tunnel stops working. The legitimate reasons to override are a security policy mandating specific algorithms or a compatibility problem with an old server; otherwise let the library's defaults evolve with current guidance.

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.SshTunnel,
  Chilkat.JsonObject;

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

procedure RunDemo;
var
  success: Boolean;
  tunnel: TSshTunnel;
  json: TJsonObject;
  sshPort: Integer;
  waitForThreadExit: Boolean;

begin
  success := False;

  //  Demonstrates the SshTunnel.SetAllowedAlgorithms method, which restricts the SSH connection to a
  //  specific set of algorithms.  The only argument is a JsonObject.  It must be called before
  //  Connect.
  //  
  //  Note: You typically should NOT set allowed algorithms explicitly.  By default Chilkat orders
  //  algorithms according to best practices and accounts for known vulnerabilities.  Hard-coding
  //  them can make an application brittle if a server later changes its allowed algorithms.

  tunnel := TSshTunnel.Create;

  //  List the allowed algorithms for each category, in order of preference.
  json := TJsonObject.Create;
  json.UpdateString('kex','curve25519-sha256@libssh.org,ecdh-sha2-nistp256');
  json.UpdateString('hostKey','ssh-ed25519,ecdsa-sha2-nistp256');
  json.UpdateString('cipher','chacha20-poly1305@openssh.com,aes256-ctr');
  json.UpdateString('mac','hmac-sha2-256,hmac-sha2-512');

  //  Apply the allow-list before connecting.
  success := tunnel.SetAllowedAlgorithms(json);
  if (success = False) then
    begin
      WriteLn(tunnel.LastErrorText);
      Exit;
    end;

  tunnel.DestHostname := 'db.internal.example.com';
  tunnel.DestPort := 5432;

  sshPort := 22;
  success := tunnel.Connect('ssh.example.com',sshPort);
  if (success = False) then
    begin
      WriteLn(tunnel.LastErrorText);
      Exit;
    end;
  WriteLn('Connected.');

  waitForThreadExit := True;
  success := tunnel.CloseTunnel(waitForThreadExit);
  if (success = False) then
    begin
      WriteLn(tunnel.LastErrorText);
      Exit;
    end;


  tunnel.Free;
  json.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.