Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
SSH Authentication using an SSH Certificate
See more SSH Examples
Demonstrates authenticating with an SSH server using an SSH certificate. The private key is imported into an SshKey object, UseSshCertificate attaches the certificate, and AuthenticatePk performs the authentication. See Understanding SSH Certificates for background.
Note: The file paths are relative to the application's current working directory. Supply the paths to your own files.
Background: An SSH certificate is a public key signed by a certificate authority, and it solves the scaling problem of ordinary public-key authentication: instead of copying every user's public key into
authorized_keys on every server, each server simply trusts the CA. Certificates also carry an expiration, so access lapses automatically rather than lingering until someone remembers to remove a key. Note this is distinct from X.509 certificates — the SSH certificate format is its own thing.Chilkat Pascal (Lazarus/Delphi) Downloads
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.SshKey,
Chilkat.StringBuilder,
Chilkat.Ssh;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
sbSshCert: TStringBuilder;
sbPrivKey: TStringBuilder;
key: TSshKey;
privKeyText: string;
sshCertText: string;
ssh: TSsh;
hostname: string;
port: Integer;
begin
success := False;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Demonstrates authenticating with an SSH server using an SSH certificate. An SSH certificate
// is a signed public key: the server trusts the certificate authority that signed it, rather
// than holding a copy of each user's public key.
sbSshCert := TStringBuilder.Create;
success := sbSshCert.LoadFile('qa_data/sshCert/user_ecdsa_key-cert.pub','utf-8');
if (success = False) then
begin
WriteLn(sbSshCert.LastErrorText);
Exit;
end;
sbPrivKey := TStringBuilder.Create;
success := sbPrivKey.LoadFile('qa_data/sshKeys/user_ecdsa_key','utf-8');
if (success = False) then
begin
WriteLn(sbPrivKey.LastErrorText);
Exit;
end;
key := TSshKey.Create;
// Set the password if the private key file is stored encrypted. This should come from a
// secure source rather than being hard-coded.
key.Password := 'myKeyPassword';
privKeyText := sbPrivKey.GetAsString();
success := key.FromOpenSshPrivateKey(privKeyText);
if (success = False) then
begin
WriteLn(key.LastErrorText);
Exit;
end;
// Indicate that the SSH certificate is to be used for authentication.
sshCertText := sbSshCert.GetAsString();
key.UseSshCertificate(sshCertText);
ssh := TSsh.Create;
hostname := 'ssh.example.com';
port := 22;
success := ssh.Connect(hostname,port);
if (success = False) then
begin
WriteLn(ssh.LastErrorText);
Exit;
end;
success := ssh.AuthenticatePk('mySshLogin',key);
if (success = False) then
begin
WriteLn(ssh.LastErrorText);
Exit;
end;
WriteLn('Public-key authentication using an SSH certificate was successful.');
ssh.Disconnect();
sbSshCert.Free;
sbPrivKey.Free;
key.Free;
ssh.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.