Sample code for 30+ languages & platforms
PowerBuilder

Add Trusted Certificate to JKS

See more Java KeyStore (JKS) Examples

Adds a trusted certificate to a Java keystore file.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Jks
string ls_JksPassword
string ls_JksPath
oleobject loo_Cert
string ls_Alias

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

ls_JksPassword = "secret"
ls_JksPath = "/myJksTrustedCerts/cacerts.jks"

// Load the Java keystore from a file.
li_Success = loo_Jks.LoadFile(ls_JksPassword,ls_JksPath)
if li_Success <> 1 then
    Write-Debug loo_Jks.LastErrorText
    destroy loo_Jks
    return
end if

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

// The cert's LoadFrommFile method can load a certificate from
// virtually any format.  It will automatically determine the format
// and load appropriately.
li_Success = loo_Cert.LoadFromFile("/certFiles/myNewTrustedCert.pem")
if li_Success <> 1 then
    Write-Debug loo_Cert.LastErrorText
    destroy loo_Jks
    destroy loo_Cert
    return
end if

// The alias can be anything.  It's basically just a label 
// used within the JKS associated with the entry.  It should
// be unique among aliases within the JKS file.
ls_Alias = "habanero"

li_Success = loo_Jks.AddTrustedCert(loo_Cert,ls_Alias)
if li_Success <> 1 then
    Write-Debug loo_Jks.LastErrorText
    destroy loo_Jks
    destroy loo_Cert
    return
end if

// Write the JKS containing the new certificate.
li_Success = loo_Jks.ToFile(ls_JksPassword,ls_JksPath)
if li_Success <> 1 then
    Write-Debug loo_Jks.LastErrorText
    destroy loo_Jks
    destroy loo_Cert
    return
end if

Write-Debug "Added a trusted certificate to the JKS."


destroy loo_Jks
destroy loo_Cert