Sample code for 30+ languages & platforms
PowerBuilder

Import an SSH Key to an HSM using PKCS11

See more PKCS11 Examples

Demonstrates how to import an SSH private key to an HSM (smartcard or token).

Note: This example requires Chilkat v9.5.0.96 or later.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Pkcs11
string ls_Pin
long li_UserType
oleobject loo_JsonTemplate
oleobject loo_SshKey
string ls_PpkContents
long li_PrivKeyHandle

li_Success = 0

//  This example requires the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

//  Note: Chilkat's PKCS11 implementation runs on Windows, Linux, Mac OS X, 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

//  Use the PKCS11 driver (.dll, .so, .dylib) for your particular HSM.
//  For example:
loo_Pkcs11.SharedLibPath = "C:/Program Files (x86)/Gemalto/IDGo 800 PKCS#11/IDPrimePKCS11.dll"

//  Use your HSM's PIN.
ls_Pin = "0000"

//  Normal user = 1
li_UserType = 1

//  Establish a logged-on user session with the HSM.
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

//  Create a PKCS11 template for importing the SSH key.
loo_JsonTemplate = create oleobject
li_rc = loo_JsonTemplate.ConnectToNewObject("Chilkat.JsonObject")

//  Indicate the key is to be stored on the token (i.e. it is not a session-only key)
loo_JsonTemplate.UpdateBool("token",1)
//  The key should have the ability to sign
loo_JsonTemplate.UpdateBool("sign",1)

//  Let's provide a few attributes to help us find the this key at a later time.
//  See SSH Public-Key Authentication using an HSM

//  The ID is byte data, so it should be base64 or hex.
//  Specify "id" if passing base64 data, "id_hex" for hexidecimal, or "id_ascii" for directly copying the ascii bytes of the string.
//  You can provide any ID of your choice.  It is optional.
loo_JsonTemplate.UpdateString("id_hex","0A0B0C0D01020304")

//  Optionally specify a label.
loo_JsonTemplate.UpdateString("label","MySshKey")

//  Load the SSH key to be imported to the HSM (smartcard or token)
loo_SshKey = create oleobject
li_rc = loo_SshKey.ConnectToNewObject("Chilkat.SshKey")

loo_SshKey.Password = "password_of_the_encrypted_ppk_file"
ls_PpkContents = loo_SshKey.LoadText("c:/my_ssh_keys/someSshKey.ppk")
li_Success = loo_SshKey.FromPuttyPrivateKey(ls_PpkContents)
if li_Success = 0 then
    Write-Debug loo_SshKey.LastErrorText
    destroy loo_Pkcs11
    destroy loo_JsonTemplate
    destroy loo_SshKey
    return
end if

//  Import the SSH private key onto the HSM.
//  The PKCS11 handle to the imported private key is returned.
//  A 0 is returned on failure.
li_PrivKeyHandle = loo_Pkcs11.ImportSshKey(loo_SshKey,loo_JsonTemplate)
if li_PrivKeyHandle = 0 then
    Write-Debug loo_Pkcs11.LastErrorText
    destroy loo_Pkcs11
    destroy loo_JsonTemplate
    destroy loo_SshKey
    return
end if

//  The private key handle is only valid during the PKCS11 session.
//  If you wish to use the private key in another PKCS11 session,
//  you'll first need to find it.  See  SSH Public-Key Authentication using a Smartcard
Write-Debug "private key handle: " + string(li_PrivKeyHandle)

Write-Debug "Successfully imported the SSH key onto the HSM."

loo_Pkcs11.Logout()
loo_Pkcs11.CloseSession()


destroy loo_Pkcs11
destroy loo_JsonTemplate
destroy loo_SshKey