Contents · Forum Home · StreamSec Tools 4.1.5.365 documentation

Configure and verify an SSH server's host key

An SSH server proves its identity by signing the key exchange with its host key. Give each client an authenticated copy of the server's public key so it can check the signature before sending a user name or password.

The components on this page are normally placed on a form or a data module at design time and set up in the Object Inspector. The sample creates them in code instead, because code shows at a glance which properties are changed from their default values.

Load the same private key on every start

uses
  System.SysUtils, System.Classes, stPkcs5, stSHA256, stAES, stRandom, stDrbgAesCtr,
  StreamSec.DSI.ArithComp,
  StreamSec.DSI.Arith, StreamSec.DSI.ECArith, StreamSec.DSI.ECC,
  StreamSec.DSI.ECC.NISTCurves, StreamSec.DSI.Pkcs8Format,
  StreamSec.DSI.PKTypes, StreamSec.DSI.CustomKeyFormat, StreamSec.SSH.KeyFormat,
  StreamSec.SSH.SocketServer, StreamSec.SSH.SocketClient;

Provision a P-256 ECDSA private key once and keep it in an encrypted PKCS #8 PEM file readable by the service account. The key and CSR example shows how to generate and save that key. SSH uses the key directly.

function LoadSshHostKey(aOwner: TComponent; const aPrivateFile: string;
  const aPassword: AnsiString): TstECDSAKey;
begin
  Result := TstECDSAKey.Create(aOwner);
  try
    Result.PrivateKeyFormat.SelectedOptionNames := 'PKCS8-Encrypted-PEM-DSS';
    Result.PrivateKeyPassword := aPassword;
    Result.PrivateKeyFileName := aPrivateFile;
    Result.Enabled := True;
    if not Result.Enabled then
      raise eKeyFormatError.CreateFmt('Cannot load SSH host key from %s', [aPrivateFile]);
  except
    Result.Free;
    raise;
  end;
end;

procedure UseSshHostKey(aServer: TstSSHSocketServer; aKey: TstECDSAKey);
begin
  aServer.ServerECDSAKey := aKey;
  aServer.ServerKeyOptions.SelectedOptionNames := 'ecdsa-sha2-nistp256';
end;

During startup, pass the server as the owner to LoadSshHostKey, then pass the returned key to UseSshHostKey before making the listener active. Set the bind address, port, key exchange, ciphers and MACs on the server. Run an SFTP server shows these settings for a P-256 host key. Reopening the same file preserves the identity that clients have pinned. If loading fails, stop startup and check the file and its password. The sample uses PrivateKeyPassword. For files with a different password encoding, use OnPassword.

Give the public key to clients

procedure ExportSshHostPin(aKey: TstECDSAKey; aDestination: TStream);
var
  lFormat: iDSIDLPublicKeyFormat;
begin
  lFormat := tSSHBase64ECDSAKeyFormat.Create;
  lFormat.SaveToStream(aKey as iDLPublicKey, aDestination);
end;

During provisioning, pass the loaded key and an output file stream to ExportSshHostPin. It writes an RFC 4716 public-key block. Keep the private file on the server. Give the public file and the server name it belongs to to the client administrator through an authenticated channel, such as the application's existing configuration distribution service.

Verify the host before signing in

function LoadSshHostPin(aOwner: TComponent; const aPublicFile: string): TstECDSAPublicKey;
begin
  Result := TstECDSAPublicKey.Create(aOwner);
  try
    Result.PublicKeyFormat.SelectedOptionNames := 'SSH-Base64-ECDSA';
    Result.PublicKeyFileName := aPublicFile;
    Result.Enabled := True;
    if not Result.Enabled then
      raise eKeyFormatError.CreateFmt('Cannot load SSH host pin from %s', [aPublicFile]);
  except
    Result.Free;
    raise;
  end;
end;

procedure VerifySshHost(aClient: TstSSHSocketClient; aPin: TstECDSAPublicKey);
begin
  if (aPin = nil) or not aPin.Enabled then
    raise eKeyFormatError.Create('An enabled SSH host pin is required');
  aClient.ServerECDSAKey := aPin;
  aClient.ServerKeyOptions.SelectedOptionNames := 'ecdsa-sha2-nistp256';
  aClient.ConnectToHost;
end;

Configure the client's host, port, key exchange, ciphers and MACs. Select that host's public file from your trusted configuration, pass the client as the owner to LoadSshHostPin, and pass the returned pin to VerifySshHost. Keep the pin assigned for the life of the connection. With this configuration, a different P-256 host key causes ConnectToHost to raise. Call Login only after verification succeeds.

For a planned key change, deliver the new public key through the authenticated channel and coordinate its activation with the server. The SFTP client example shows how to keep keys for several servers in a named list. The SSH notes describe SSH host keys and the algorithm providers.

Reference

TstSSHSocketServer · TstSSHSocketClient · TstECDSAKey · TstECDSAPublicKey · StreamSec.DSI.ArithComp · StreamSec.DSI.Pkcs8Format · StreamSec.SSH.KeyFormat · StreamSec.SSH.SocketServer · StreamSec.SSH.SocketClient

Related tasks