Delphi DLL
Delphi DLL
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 Delphi DLL Downloads
var
success: Boolean;
sbSshCert: HCkStringBuilder;
sbPrivKey: HCkStringBuilder;
key: HCkSshKey;
privKeyText: PWideChar;
sshCertText: PWideChar;
ssh: HCkSsh;
hostname: PWideChar;
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 := CkStringBuilder_Create();
success := CkStringBuilder_LoadFile(sbSshCert,'qa_data/sshCert/user_ecdsa_key-cert.pub','utf-8');
if (success = False) then
begin
Memo1.Lines.Add(CkStringBuilder__lastErrorText(sbSshCert));
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(CkStringBuilder__lastErrorText(sbPrivKey));
Exit;
end;
key := CkSshKey_Create();
// Set the password if the private key file is stored encrypted. This should come from a
// secure source rather than being hard-coded.
CkSshKey_putPassword(key,'myKeyPassword');
privKeyText := CkStringBuilder__getAsString(sbPrivKey);
success := CkSshKey_FromOpenSshPrivateKey(key,privKeyText);
if (success = False) then
begin
Memo1.Lines.Add(CkSshKey__lastErrorText(key));
Exit;
end;
// Indicate that the SSH certificate is to be used for authentication.
sshCertText := CkStringBuilder__getAsString(sbSshCert);
CkSshKey_UseSshCertificate(key,sshCertText);
ssh := CkSsh_Create();
hostname := 'ssh.example.com';
port := 22;
success := CkSsh_Connect(ssh,hostname,port);
if (success = False) then
begin
Memo1.Lines.Add(CkSsh__lastErrorText(ssh));
Exit;
end;
success := CkSsh_AuthenticatePk(ssh,'mySshLogin',key);
if (success = False) then
begin
Memo1.Lines.Add(CkSsh__lastErrorText(ssh));
Exit;
end;
Memo1.Lines.Add('Public-key authentication using an SSH certificate was successful.');
CkSsh_Disconnect(ssh);
CkStringBuilder_Dispose(sbSshCert);
CkStringBuilder_Dispose(sbPrivKey);
CkSshKey_Dispose(key);
CkSsh_Dispose(ssh);