Sample code for 30+ languages & platforms
PowerBuilder

Get the Certificate with Private Key from a Java KeyStore

See more Java KeyStore (JKS) Examples

Load a Chilkat certificate object from a Java KeyStore.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Jks
string ls_Password
oleobject loo_Chain
oleobject loo_Cert
oleobject loo_Crypt

li_Success = 0

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

// Most of the time a .jks contains one certificate with it's associated private key.
// (Similar to how a .pfx/.p12 usually contains a particular certificate with private key.)
// This example demonstrates how to get the certificate with private key such that it can be used
// by other Chilkat classes wherever a cert w/ private key is needed.
loo_Jks = create oleobject
li_rc = loo_Jks.ConnectToNewObject("Chilkat.JavaKeyStore")
if li_rc < 0 then
    destroy loo_Jks
    MessageBox("Error","Connecting to COM object failed")
    return
end if
ls_Password = "secret"
li_Success = loo_Jks.LoadFile(ls_Password,"qa_data/jks/test_secret.jks")
if li_Success = 0 then
    Write-Debug loo_Jks.LastErrorText
    destroy loo_Jks
    return
end if

// Make sure we have a private key.
if loo_Jks.NumPrivateKeys < 1 then
    Write-Debug "No private key available."
    destroy loo_Jks
    return
end if

// -------------------------------------------------------------------------
// Get the certificate chain associated with the 1st (and probably only) private key in the JKS.

loo_Chain = create oleobject
li_rc = loo_Chain.ConnectToNewObject("Chilkat.CertChain")

li_Success = loo_Jks.CertChainAt(0,loo_Chain)
if li_Success = 0 then
    Write-Debug loo_Jks.LastErrorText
    destroy loo_Jks
    destroy loo_Chain
    return
end if

loo_Cert = create oleobject
li_rc = loo_Cert.ConnectToNewObject("Chilkat.Cert")

li_Success = loo_Chain.CertAt(0,loo_Cert)
if li_Success = 0 then
    Write-Debug loo_Chain.LastErrorText
    destroy loo_Jks
    destroy loo_Chain
    destroy loo_Cert
    return
end if

// Verify again that this cert has a private key.
if loo_Cert.HasPrivateKey() <> 1 then
    Write-Debug "Certificate has no associated private key."
    destroy loo_Jks
    destroy loo_Chain
    destroy loo_Cert
    return
end if

// We now have the cert object with it's associated private key, and it can be used in other Chilkat classes where needed.
// For example..

loo_Crypt = create oleobject
li_rc = loo_Crypt.ConnectToNewObject("Chilkat.Crypt2")

li_Success = loo_Crypt.SetSigningCert(loo_Cert)
if li_Success = 0 then
    Write-Debug loo_Crypt.LastErrorText
    destroy loo_Jks
    destroy loo_Chain
    destroy loo_Cert
    destroy loo_Crypt
    return
end if

// ...
// ...


destroy loo_Jks
destroy loo_Chain
destroy loo_Cert
destroy loo_Crypt