PowerBuilder
PowerBuilder
PKCS11 Import an Existing RSA Public Key onto the HSM
See more PKCS11 Examples
Demonstrates how to import an existing RSA Public Key onto a smart card or token.Note: This example requires Chilkat v9.5.0.96 or later.
Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Pkcs11
string ls_Pin
integer li_UserType
oleobject loo_Rsa
oleobject loo_PrivKey
oleobject loo_Xml
oleobject loo_PubKey
oleobject loo_Attrs
integer li_ObjHandle
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.
// (The format of the path will change with the operating system. Obviously, "C:/" is not used on non-Windows systems.
loo_Pkcs11.SharedLibPath = "C:/Program Files (x86)/Gemalto/IDGo 800 PKCS#11/IDPrimePKCS1164.dll"
// Establish a logged-on session.
ls_Pin = "0000"
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
// Generate a new 2048-bit RSA key.
loo_Rsa = create oleobject
li_rc = loo_Rsa.ConnectToNewObject("Chilkat.Rsa")
loo_PrivKey = create oleobject
li_rc = loo_PrivKey.ConnectToNewObject("Chilkat.PrivateKey")
li_Success = loo_Rsa.GenKey(2048,loo_PrivKey)
if li_Success = 0 then
Write-Debug loo_Rsa.LastErrorText
destroy loo_Pkcs11
destroy loo_Rsa
destroy loo_PrivKey
return
end if
// Get the public key information as XML, so we can access the modulus and exponent.
loo_Xml = create oleobject
li_rc = loo_Xml.ConnectToNewObject("Chilkat.Xml")
loo_PubKey = create oleobject
li_rc = loo_PubKey.ConnectToNewObject("Chilkat.PublicKey")
loo_PrivKey.ToPublicKey(loo_PubKey)
loo_Xml.LoadXml(loo_PubKey.GetXml())
loo_Attrs = create oleobject
li_rc = loo_Attrs.ConnectToNewObject("Chilkat.JsonObject")
// Specify the type of object, and the type of key.
loo_Attrs.UpdateString("class","CKO_PUBLIC_KEY")
loo_Attrs.UpdateString("key_type","CKK_RSA")
// Add an optional label if desired.
loo_Attrs.UpdateString("label","RSA Public Key 1")
// Allow the key to be use for verify, wrapping, and encryption operations.
loo_Attrs.UpdateBool("verify",1)
loo_Attrs.UpdateBool("wrap",1)
loo_Attrs.UpdateBool("encrypt",1)
// Make this a session-only public key.
// To store the public key on the token so that it persists after the PKCS11 session, set token = 1.
loo_Attrs.UpdateBool("token",0)
// Provide the RSA public key material
loo_Attrs.UpdateString("modulus",loo_Xml.GetChildContent("Modulus"))
loo_Attrs.UpdateString("public_exponent",loo_Xml.GetChildContent("Exponent"))
// Create the RSA public key.
// Returns the PKCS11 object handle of the created key.
li_ObjHandle = loo_Pkcs11.CreatePkcs11Object(loo_Attrs)
if li_ObjHandle = 0 then
Write-Debug loo_Pkcs11.LastErrorText
Write-Debug "Failed."
else
Write-Debug "PKCS11 object handle = " + string(li_ObjHandle)
Write-Debug "Successfully imported an RSA key.."
end if
loo_Pkcs11.Logout()
loo_Pkcs11.CloseSession()
destroy loo_Pkcs11
destroy loo_Rsa
destroy loo_PrivKey
destroy loo_Xml
destroy loo_PubKey
destroy loo_Attrs