Sample code for 30+ languages & platforms
PowerBuilder

SSH HSM Public Key Authentication

See more SSH Examples

Demonstrates SSH public-key authentication using a private key stored on an HSM — a USB token or smart card — accessed through PKCS#11. A session is opened with the vendor's driver, the key handles are located, and an SshKey object is bound to them with UsePkcs11.

Background: The point of an HSM is that the private key is generated on the device and cannot be exported: the signing operation happens on the hardware, so the key material never reaches your application's memory or disk. Even a fully compromised host cannot yield a copy of the key. PKCS#11 is the vendor-neutral interface to such devices, which is why the driver path and the object-finding template are the only vendor-specific parts of this example.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Pkcs11
string ls_Pin
integer li_UserType
oleobject loo_Json
integer li_Priv_handle
integer li_Pub_handle
oleobject loo_Key
string ls_KeyType
oleobject loo_Ssh
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 SSH public-key authentication using a private key stored on an HSM (a USB token
//  or smart card) accessed through PKCS#11.
//  
//  Note: Chilkat's PKCS#11 implementation runs on Windows, Linux, macOS, and other supported
//  operating systems.

loo_Pkcs11 = create oleobject
li_rc = loo_Pkcs11.ConnectToNewObject("Chilkat.Pkcs11")
if li_rc < 0 then
    destroy loo_Pkcs11
    MessageBox("Error","Connecting to COM object failed")
    return
end if

//  The PKCS#11 driver supplied by your HSM vendor: a .dll on Windows, a .so on Linux, or a
//  .dylib on macOS.
loo_Pkcs11.SharedLibPath = "C:/Program Files (x86)/Gemalto/IDGo 800 PKCS#11/IDPrimePKCS1164.dll"

//  The PIN should come from a secure source rather than being hard-coded.
ls_Pin = "0000"

//  Normal user = 1
li_UserType = 1

li_Success = loo_Pkcs11.QuickSession(li_UserType,ls_Pin)
if li_Success = 0 then
    Write-Debug loo_Pkcs11.LastErrorText
    destroy loo_Pkcs11
    return
end if

//  Describe the private key object to be located on the HSM.
loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

loo_Json.UpdateString("class","private_key")
loo_Json.UpdateString("label","MySshKey")

li_Priv_handle = loo_Pkcs11.FindObject(loo_Json)
if li_Priv_handle = 0 then
    Write-Debug loo_Pkcs11.LastErrorText
    destroy loo_Pkcs11
    destroy loo_Json
    return
end if

//  Find the corresponding public key by changing the class in the same template.
loo_Json.UpdateString("class","public_key")

li_Pub_handle = loo_Pkcs11.FindObject(loo_Json)
if li_Pub_handle = 0 then
    Write-Debug loo_Pkcs11.LastErrorText
    destroy loo_Pkcs11
    destroy loo_Json
    return
end if

//  Create an SSH key object that uses the HSM handles.  The key type may be "rsa" or "ec".
loo_Key = create oleobject
li_rc = loo_Key.ConnectToNewObject("Chilkat.SshKey")

ls_KeyType = "rsa"
li_Success = loo_Key.UsePkcs11(loo_Pkcs11,li_Priv_handle,li_Pub_handle,ls_KeyType)
if li_Success = 0 then
    Write-Debug loo_Key.LastErrorText
    destroy loo_Pkcs11
    destroy loo_Json
    destroy loo_Key
    return
end if

loo_Ssh = create oleobject
li_rc = loo_Ssh.ConnectToNewObject("Chilkat.Ssh")

li_Port = 22
li_Success = loo_Ssh.Connect("ssh.example.com",li_Port)
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Pkcs11
    destroy loo_Json
    destroy loo_Key
    destroy loo_Ssh
    return
end if

//  The corresponding public key must already be installed on the SSH server for the account.
//  The signing operation happens on the HSM -- the private key never leaves the device.
li_Success = loo_Ssh.AuthenticatePk("mySshLogin",loo_Key)
if li_Success = 0 then
    Write-Debug loo_Ssh.LastErrorText
    destroy loo_Pkcs11
    destroy loo_Json
    destroy loo_Key
    destroy loo_Ssh
    return
end if

Write-Debug "Public-key authentication successful."

loo_Ssh.Disconnect()

loo_Pkcs11.Logout()
loo_Pkcs11.CloseSession()


destroy loo_Pkcs11
destroy loo_Json
destroy loo_Key
destroy loo_Ssh