Xbase++
Xbase++
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 Xbase++ Downloads
LOCAL nSuccess
LOCAL oPkcs11
LOCAL cPin
LOCAL nUserType
LOCAL oJson
LOCAL nPriv_handle
LOCAL nPub_handle
LOCAL oKey
LOCAL cKeyType
LOCAL oSsh
LOCAL nPort
nSuccess := 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.
oPkcs11 := CreateObject("Chilkat.Pkcs11")
// The PKCS#11 driver supplied by your HSM vendor: a .dll on Windows, a .so on Linux, or a
// .dylib on macOS.
oPkcs11: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.
cPin := "0000"
// Normal user = 1
nUserType := 1
nSuccess := oPkcs11:QuickSession(nUserType, cPin)
IF (nSuccess == 0)
? oPkcs11:LastErrorText
oPkcs11:destroy()
RETURN
ENDIF
// Describe the private key object to be located on the HSM.
oJson := CreateObject("Chilkat.JsonObject")
oJson:UpdateString("class", "private_key")
oJson:UpdateString("label", "MySshKey")
nPriv_handle := oPkcs11:FindObject(oJson)
IF (nPriv_handle == 0)
? oPkcs11:LastErrorText
oPkcs11:destroy()
oJson:destroy()
RETURN
ENDIF
// Find the corresponding public key by changing the class in the same template.
oJson:UpdateString("class", "public_key")
nPub_handle := oPkcs11:FindObject(oJson)
IF (nPub_handle == 0)
? oPkcs11:LastErrorText
oPkcs11:destroy()
oJson:destroy()
RETURN
ENDIF
// Create an SSH key object that uses the HSM handles. The key type may be "rsa" or "ec".
oKey := CreateObject("Chilkat.SshKey")
cKeyType := "rsa"
nSuccess := oKey:UsePkcs11(oPkcs11, nPriv_handle, nPub_handle, cKeyType)
IF (nSuccess == 0)
? oKey:LastErrorText
oPkcs11:destroy()
oJson:destroy()
oKey:destroy()
RETURN
ENDIF
oSsh := CreateObject("Chilkat.Ssh")
nPort := 22
nSuccess := oSsh:Connect("ssh.example.com", nPort)
IF (nSuccess == 0)
? oSsh:LastErrorText
oPkcs11:destroy()
oJson:destroy()
oKey:destroy()
oSsh:destroy()
RETURN
ENDIF
// 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.
nSuccess := oSsh:AuthenticatePk("mySshLogin", oKey)
IF (nSuccess == 0)
? oSsh:LastErrorText
oPkcs11:destroy()
oJson:destroy()
oKey:destroy()
oSsh:destroy()
RETURN
ENDIF
? "Public-key authentication successful."
oSsh:Disconnect()
oPkcs11:Logout()
oPkcs11:CloseSession()
oPkcs11:destroy()
oJson:destroy()
oKey:destroy()
oSsh:destroy()