PowerBuilder
PowerBuilder
SSH Authentication using X.509 Certificates
See more SSH Examples
Demonstrates authenticating with an SSH/SFTP server using the private key of an X.509 certificate. The certificate and key are loaded from a .pfx, the private key is exported as PEM, and that key is used for public-key authentication. See X.509v3 Certificates for SSH Authentication for more information.
Note: The file paths are relative to the application's current working directory. Supply the paths to your own files.
Background: This is not X.509 certificate authentication in the SSH-protocol sense — the server still performs ordinary public-key authentication. The certificate is simply a convenient container for a key pair your organization already manages through its PKI, so the same credential issued for other purposes can be reused for SSH. What the server needs installed is the corresponding public key, exactly as with any other key. Chilkat can also load such certificates from smart cards and USB tokens rather than a file.
Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Ssh
string ls_Hostname
integer li_Port
string ls_PfxPassword
oleobject loo_Cert
string ls_PrivKeyPem
oleobject loo_Key
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/SFTP server using the private key of an X.509
// certificate.
loo_Ssh = create oleobject
li_rc = loo_Ssh.ConnectToNewObject("Chilkat.Ssh")
if li_rc < 0 then
destroy loo_Ssh
MessageBox("Error","Connecting to COM object failed")
return
end if
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_Ssh
return
end if
// Load the certificate and its private key from a .pfx file. The PFX password should come
// from a secure source rather than being hard-coded.
// Note: Chilkat can load certificates and private keys from many sources, including smart
// cards and USB tokens (HSMs).
ls_PfxPassword = "myPfxPassword"
loo_Cert = create oleobject
li_rc = loo_Cert.ConnectToNewObject("Chilkat.Cert")
li_Success = loo_Cert.LoadPfxFile("qa_data/pfx/example.pfx",ls_PfxPassword)
if li_Success = 0 then
Write-Debug loo_Cert.LastErrorText
destroy loo_Ssh
destroy loo_Cert
return
end if
// Get the certificate's private key as PEM, to be used for SSH authentication. The
// corresponding public key is installed on the server.
ls_PrivKeyPem = loo_Cert.GetPrivateKeyPem()
if loo_Cert.LastMethodSuccess = 0 then
Write-Debug loo_Cert.LastErrorText
destroy loo_Ssh
destroy loo_Cert
return
end if
loo_Key = create oleobject
li_rc = loo_Key.ConnectToNewObject("Chilkat.SshKey")
li_Success = loo_Key.FromOpenSshPrivateKey(ls_PrivKeyPem)
if li_Success = 0 then
Write-Debug loo_Key.LastErrorText
destroy loo_Ssh
destroy loo_Cert
destroy loo_Key
return
end if
li_Success = loo_Ssh.AuthenticatePk("mySshLogin",loo_Key)
if li_Success = 0 then
Write-Debug loo_Ssh.LastErrorText
destroy loo_Ssh
destroy loo_Cert
destroy loo_Key
return
end if
Write-Debug "Public-key authentication successful."
loo_Ssh.Disconnect()
destroy loo_Ssh
destroy loo_Cert
destroy loo_Key