Sample code for 30+ languages & platforms
PowerBuilder

Load Particular CA Certs into a Java KeyStore

See more Java KeyStore (JKS) Examples

Opens a PEM file containing many CA root certificates, and creates a Java keystore containing a subset of the certificates.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Jks
oleobject loo_Troots
oleobject loo_SbDn
oleobject loo_SbAlias
integer li_CaseSensitive
integer i
integer li_NumCerts
integer li_NumAdded
oleobject loo_Cacert
integer li_NumJksCerts

li_Success = 0

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

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

loo_Troots = create oleobject
li_rc = loo_Troots.ConnectToNewObject("Chilkat.TrustedRoots")

// Load certificates from a file.
li_Success = loo_Troots.LoadCaCertsPem("qa_data/curl_cacert.pem")
if li_Success <> 1 then
    Write-Debug loo_Troots.LastErrorText
    destroy loo_Jks
    destroy loo_Troots
    return
end if

loo_SbDn = create oleobject
li_rc = loo_SbDn.ConnectToNewObject("Chilkat.StringBuilder")

loo_SbAlias = create oleobject
li_rc = loo_SbAlias.ConnectToNewObject("Chilkat.StringBuilder")

li_CaseSensitive = 0

i = 0
li_NumCerts = loo_Troots.NumCerts
li_NumAdded = 0
do while (i < li_NumCerts)
    loo_Cacert = loo_Troots.GetCert(i)
    loo_SbDn.Clear()
    loo_SbDn.Append(loo_Cacert.SubjectDN)
    if loo_SbDn.Contains("Entrust.net",li_CaseSensitive) = 1 then
        Write-Debug loo_Cacert.SubjectDN

        // The alias is an arbitrary unique string for each cert in the JKS.
        loo_SbAlias.Clear()
        loo_SbAlias.Append("cacert_")
        loo_SbAlias.AppendInt(i + 1)
        loo_Jks.AddTrustedCert(loo_Cacert,loo_SbAlias.GetAsString())
        li_NumAdded = li_NumAdded + 1
    end if

    destroy loo_Cacert
    i = i + 1
loop

// Verify the number of certs in the JKS equals the number we added.
li_NumJksCerts = loo_Jks.NumTrustedCerts
Write-Debug "NumTrustedCerts = " + string(li_NumJksCerts)
if li_NumJksCerts <> li_NumAdded then
    Write-Debug "Something is amiss!"
    destroy loo_Jks
    destroy loo_Troots
    destroy loo_SbDn
    destroy loo_SbAlias
    return
end if

// Save the JKS.
li_Success = loo_Jks.ToFile("myPassword","qa_data/jks/entrust_caCerts.jks")
if li_Success <> 1 then
    Write-Debug loo_Jks.LastErrorText
    destroy loo_Jks
    destroy loo_Troots
    destroy loo_SbDn
    destroy loo_SbAlias
    return
end if

Write-Debug "Success."

// The output of this program when tested was:

// C=US, O=Entrust.net, OU=www.entrust.net/CPS incorp. by ref. (limits liab.), OU=(c) 1999 Entrust.net Limited, CN=Entrust.net Secure Server Certification Authority
// O=Entrust.net, OU=www.entrust.net/CPS_2048 incorp. by ref. (limits liab.), OU=(c) 1999 Entrust.net Limited, CN=Entrust.net Certification Authority (2048)
// C=US, O="Entrust, Inc.", OU=www.entrust.net/CPS is incorporated by reference, OU="(c) 2006 Entrust, Inc.", CN=Entrust Root Certification Authority
// NumTrustedCerts = 3
// Success.


destroy loo_Jks
destroy loo_Troots
destroy loo_SbDn
destroy loo_SbAlias