Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
SSH Tunnel Authenticate with Secure Strings
See more SSH Examples
Demonstrates SSH tunnel password authentication using SecureString objects, with the credentials read from an encrypted JSON config file and decrypted directly into secure strings rather than being hard-coded in source.
Background: The
SshTunnel class runs the tunnel on a background thread, so it authenticates once and then serves connections until closed — which makes protecting the stored credential especially worthwhile. Decrypting straight into a SecureString avoids ever holding the password in an ordinary string, and the value stays encrypted in memory under a randomly generated session key. In production the decryption key itself would come from a key vault or OS keystore rather than a literal.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.SshTunnel,
Chilkat.SecureString,
Chilkat.JsonObject,
Chilkat.Crypt2;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
json: TJsonObject;
crypt: TCrypt2;
ssLogin: TSecureString;
ssPassword: TSecureString;
tunnel: TSshTunnel;
port: Integer;
waitForThreadExit: Boolean;
begin
success := False;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Demonstrates SSH tunnel password authentication using SecureString objects, with the
// credentials read from an encrypted config file rather than hard-coded in source.
// Imagine the login and password were previously encrypted and saved in a JSON config file:
// {
// "ssh_login": "2+qylFfC56Ck7OQQt/U2/w==",
// "ssh_password": "5neIq9Jmn0E3p71N6Yc8TA=="
// }
json := TJsonObject.Create;
success := json.LoadFile('qa_data/passwords/ssh.json');
if (success = False) then
begin
WriteLn(json.LastErrorText);
Exit;
end;
// These are the encryption settings used when the credentials were encrypted. In a real
// application the key would come from a secure source, not a literal.
crypt := TCrypt2.Create;
crypt.CryptAlgorithm := 'aes';
crypt.CipherMode := 'cbc';
crypt.KeyLength := 128;
crypt.SetEncodedKey('000102030405060708090A0B0C0D0E0F','hex');
crypt.SetEncodedIV('000102030405060708090A0B0C0D0E0F','hex');
crypt.EncodingMode := 'base64';
// Decrypt directly into SecureString objects. The values stay encrypted in memory, now under
// a randomly generated session key.
ssLogin := TSecureString.Create;
ssPassword := TSecureString.Create;
crypt.DecryptSecureENC(json.StringOf('ssh_login'),ssLogin);
crypt.DecryptSecureENC(json.StringOf('ssh_password'),ssPassword);
tunnel := TSshTunnel.Create;
port := 22;
success := tunnel.Connect('ssh.example.com',port);
if (success = False) then
begin
WriteLn(tunnel.LastErrorText);
Exit;
end;
// Authenticate using the secure strings.
success := tunnel.AuthenticateSecPw(ssLogin,ssPassword);
if (success = False) then
begin
WriteLn(tunnel.LastErrorText);
Exit;
end;
WriteLn('SSH tunnel authentication successful.');
// ... use the tunnel ...
waitForThreadExit := True;
success := tunnel.CloseTunnel(waitForThreadExit);
if (success = False) then
begin
WriteLn(tunnel.LastErrorText);
Exit;
end;
json.Free;
crypt.Free;
ssLogin.Free;
ssPassword.Free;
tunnel.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.