Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
SSH Authentication using an SSH Certificate
See more SSH Examples
Demonstrates how to authenticate using an SSH certificate.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;
ssh: TSsh;
hostname: string;
port: Integer;
begin
success := False;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
sbSshCert := TStringBuilder.Create;
success := sbSshCert.LoadFile('qa_data/sshCert/user_ecdsa_key-cert.pub','utf-8');
if (success = False) then
begin
WriteLn('Failed to load user_ecdsa_key-cert.pub');
Exit;
end;
sbPrivKey := TStringBuilder.Create;
success := sbPrivKey.LoadFile('qa_data/sshKeys/user_ecdsa_key','utf-8');
if (success = False) then
begin
WriteLn('Failed to load user_ecdsa_key');
Exit;
end;
key := TSshKey.Create;
// Provide the password if the user_ecdsa_key is stored in an encrypted format.
key.Password := 'secret';
success := key.FromOpenSshPrivateKey(sbPrivKey.GetAsString());
if (success = False) then
begin
WriteLn(key.LastErrorText);
Exit;
end;
// Indicate that the SSH certificate is to be used for authentication.
// The UseSshCertificate method was added in Chilkat v11.0.0
key.UseSshCertificate(sbSshCert.GetAsString());
ssh := TSsh.Create;
hostname := 'ssh.example.com';
port := 22;
success := ssh.Connect(hostname,port);
if (success <> True) then
begin
WriteLn(ssh.LastErrorText);
Exit;
end;
success := ssh.AuthenticatePk('myLogin',key);
if (success <> True) then
begin
WriteLn(ssh.LastErrorText);
Exit;
end;
WriteLn('Public-Key Authentication using an SSH Certificate was Successful!');
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.