Sample code for 30+ languages & platforms
Delphi DLL

SSH Authentication using an SSH Certificate

See more SSH Examples

Demonstrates how to authenticate using an SSH certificate.

Chilkat Delphi DLL Downloads

Delphi DLL
uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, SshKey, StringBuilder, Ssh;

...

procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
sbSshCert: HCkStringBuilder;
sbPrivKey: HCkStringBuilder;
key: HCkSshKey;
ssh: HCkSsh;
hostname: PWideChar;
port: Integer;

begin
success := False;

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

sbSshCert := CkStringBuilder_Create();
success := CkStringBuilder_LoadFile(sbSshCert,'qa_data/sshCert/user_ecdsa_key-cert.pub','utf-8');
if (success = False) then
  begin
    Memo1.Lines.Add('Failed to load user_ecdsa_key-cert.pub');
    Exit;
  end;

sbPrivKey := CkStringBuilder_Create();
success := CkStringBuilder_LoadFile(sbPrivKey,'qa_data/sshKeys/user_ecdsa_key','utf-8');
if (success = False) then
  begin
    Memo1.Lines.Add('Failed to load user_ecdsa_key');
    Exit;
  end;

key := CkSshKey_Create();
// Provide the password if the user_ecdsa_key is stored in an encrypted format.
CkSshKey_putPassword(key,'secret');
success := CkSshKey_FromOpenSshPrivateKey(key,CkStringBuilder__getAsString(sbPrivKey));
if (success = False) then
  begin
    Memo1.Lines.Add(CkSshKey__lastErrorText(key));
    Exit;
  end;

// Indicate that the SSH certificate is to be used for authentication.
// The UseSshCertificate method was added in Chilkat v11.0.0
CkSshKey_UseSshCertificate(key,CkStringBuilder__getAsString(sbSshCert));

ssh := CkSsh_Create();

hostname := 'ssh.example.com';
port := 22;
success := CkSsh_Connect(ssh,hostname,port);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkSsh__lastErrorText(ssh));
    Exit;
  end;

success := CkSsh_AuthenticatePk(ssh,'myLogin',key);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkSsh__lastErrorText(ssh));
    Exit;
  end;

Memo1.Lines.Add('Public-Key Authentication using an SSH Certificate was Successful!');

CkStringBuilder_Dispose(sbSshCert);
CkStringBuilder_Dispose(sbPrivKey);
CkSshKey_Dispose(key);
CkSsh_Dispose(ssh);

end;