PowerBuilder
PowerBuilder
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 PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_SbSshCert
oleobject loo_SbPrivKey
oleobject loo_Key
string ls_PrivKeyText
string ls_SshCertText
oleobject loo_Ssh
string ls_Hostname
integer li_Port
li_Success = 0
// 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.
loo_SbSshCert = create oleobject
li_rc = loo_SbSshCert.ConnectToNewObject("Chilkat.StringBuilder")
if li_rc < 0 then
destroy loo_SbSshCert
MessageBox("Error","Connecting to COM object failed")
return
end if
li_Success = loo_SbSshCert.LoadFile("qa_data/sshCert/user_ecdsa_key-cert.pub","utf-8")
if li_Success = 0 then
Write-Debug loo_SbSshCert.LastErrorText
destroy loo_SbSshCert
return
end if
loo_SbPrivKey = create oleobject
li_rc = loo_SbPrivKey.ConnectToNewObject("Chilkat.StringBuilder")
li_Success = loo_SbPrivKey.LoadFile("qa_data/sshKeys/user_ecdsa_key","utf-8")
if li_Success = 0 then
Write-Debug loo_SbPrivKey.LastErrorText
destroy loo_SbSshCert
destroy loo_SbPrivKey
return
end if
loo_Key = create oleobject
li_rc = loo_Key.ConnectToNewObject("Chilkat.SshKey")
// Set the password if the private key file is stored encrypted. This should come from a
// secure source rather than being hard-coded.
loo_Key.Password = "myKeyPassword"
ls_PrivKeyText = loo_SbPrivKey.GetAsString()
li_Success = loo_Key.FromOpenSshPrivateKey(ls_PrivKeyText)
if li_Success = 0 then
Write-Debug loo_Key.LastErrorText
destroy loo_SbSshCert
destroy loo_SbPrivKey
destroy loo_Key
return
end if
// Indicate that the SSH certificate is to be used for authentication.
ls_SshCertText = loo_SbSshCert.GetAsString()
loo_Key.UseSshCertificate(ls_SshCertText)
loo_Ssh = create oleobject
li_rc = loo_Ssh.ConnectToNewObject("Chilkat.Ssh")
ls_Hostname = "ssh.example.com"
li_Port = 22
li_Success = loo_Ssh.Connect(ls_Hostname,li_Port)
if li_Success = 0 then
Write-Debug loo_Ssh.LastErrorText
destroy loo_SbSshCert
destroy loo_SbPrivKey
destroy loo_Key
destroy loo_Ssh
return
end if
li_Success = loo_Ssh.AuthenticatePk("mySshLogin",loo_Key)
if li_Success = 0 then
Write-Debug loo_Ssh.LastErrorText
destroy loo_SbSshCert
destroy loo_SbPrivKey
destroy loo_Key
destroy loo_Ssh
return
end if
Write-Debug "Public-key authentication using an SSH certificate was successful."
loo_Ssh.Disconnect()
destroy loo_SbSshCert
destroy loo_SbPrivKey
destroy loo_Key
destroy loo_Ssh