PowerBuilder
PowerBuilder
Add Private Key to Java Keystore
See more Java KeyStore (JKS) Examples
Adds a private key to an existing Java keystore.Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Jks
string ls_JksPassword
string ls_JksPath
oleobject loo_Cert
oleobject loo_CertVault
oleobject loo_PrivKey
string ls_Alias
oleobject loo_Pfx
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 = "myJksPassword"
ls_JksPath = "/someDir/keyStore.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
// A JKS private key entry consists of both the private key,
// it's associated certificate (which contains the matching public key
// within the X.509 of the certificate), and the certificates in the
// chain of authentication to the root.
//
// Therefore, to add a private key entry to a JKS requires
// a Chilkat certificate object that has a private key and which also
// has the certificate chain (up to the root) available.
// There are many ways to get a Chilkat certificate object
// that contains (within it) the private key and the certificate chain
// This example will show two possibilities:
// (1) Where the cert and issuing root are provided in PEM format in .crt files,
// and the private key is also provided in unencrypted PEM format (.key file).
// (2) Where the cert, private key, and issuing root are provided in a single PFX.
// First for the .crt / .key files:
loo_Cert = create oleobject
li_rc = loo_Cert.ConnectToNewObject("Chilkat.Cert")
// Chilkat will automatically determine the format of the cert file and load it correctly.
li_Success = loo_Cert.LoadFromFile("/mycerts/alice.crt")
if li_Success <> 1 then
Write-Debug loo_Cert.LastErrorText
destroy loo_Jks
destroy loo_Cert
return
end if
// Certificates required for building the chain of authentication can be
// added to an XML certificate vault object, and then provided as
// a source for obtaining certs when building the chain.
loo_CertVault = create oleobject
li_rc = loo_CertVault.ConnectToNewObject("Chilkat.XmlCertVault")
li_Success = loo_CertVault.AddCertFile("/mycerts/ca.crt")
if li_Success <> 1 then
Write-Debug loo_CertVault.LastErrorText
destroy loo_Jks
destroy loo_Cert
destroy loo_CertVault
return
end if
li_Success = loo_Cert.UseCertVault(loo_CertVault)
if li_Success <> 1 then
Write-Debug loo_Cert.LastErrorText
destroy loo_Jks
destroy loo_Cert
destroy loo_CertVault
return
end if
// Now provide the associated private key to the certificate object.
// The Chilkat private key class provides methods for loading from many formats (both
// encrypted and unencrypted).
loo_PrivKey = create oleobject
li_rc = loo_PrivKey.ConnectToNewObject("Chilkat.PrivateKey")
li_Success = loo_PrivKey.LoadPemFile("/mycerts/alice.key")
if li_Success <> 1 then
Write-Debug loo_PrivKey.LastErrorText
destroy loo_Jks
destroy loo_Cert
destroy loo_CertVault
destroy loo_PrivKey
return
end if
// Provide the certificate object with the private key:
li_Success = loo_Cert.SetPrivateKey(loo_PrivKey)
if li_Success <> 1 then
Write-Debug loo_Cert.LastErrorText
destroy loo_Jks
destroy loo_Cert
destroy loo_CertVault
destroy loo_PrivKey
return
end if
// Our certificate object now contains all that we need to add it as a private key entry
// to the Java keystore:
ls_Alias = "alice"
li_Success = loo_Jks.AddPrivateKey(loo_Cert,ls_Alias,ls_JksPassword)
if li_Success <> 1 then
Write-Debug loo_Jks.LastErrorText
destroy loo_Jks
destroy loo_Cert
destroy loo_CertVault
destroy loo_PrivKey
return
end if
// Write the updated JKS, which contains the new private key entry w/ certificate chain.
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
destroy loo_CertVault
destroy loo_PrivKey
return
end if
Write-Debug "Added new private key entry (from .crt and .key files) to the JKS!"
// Now let's add a new private key entry from a PFX that contains a single
// private key with associated cert and cert chain.
loo_Pfx = create oleobject
li_rc = loo_Pfx.ConnectToNewObject("Chilkat.Pfx")
li_Success = loo_Pfx.LoadPfxFile("/myPfxFiles/my.pfx","pfxPassword")
if li_Success <> 1 then
Write-Debug loo_Pfx.LastErrorText
destroy loo_Jks
destroy loo_Cert
destroy loo_CertVault
destroy loo_PrivKey
destroy loo_Pfx
return
end if
// This is easy -- simply add the PFX to the JKS
ls_Alias = "bob"
li_Success = loo_Jks.AddPfx(loo_Pfx,ls_Alias,ls_JksPassword)
if li_Success <> 1 then
Write-Debug loo_Jks.LastErrorText
destroy loo_Jks
destroy loo_Cert
destroy loo_CertVault
destroy loo_PrivKey
destroy loo_Pfx
return
end if
// Write the updated JKS, which contains the new private key entry w/ certificate chain
// that came from the PFX.
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
destroy loo_CertVault
destroy loo_PrivKey
destroy loo_Pfx
return
end if
Write-Debug "Added new private key entry (from PFX) to the JKS!"
destroy loo_Jks
destroy loo_Cert
destroy loo_CertVault
destroy loo_PrivKey
destroy loo_Pfx