Xbase++
Xbase++
Add a Private Key and Certificate Chain to a PFX
See more PFX/P12 Examples
Demonstrates Pfx.AddPrivateKey, which adds a private key together with its certificate chain to the PFX object.
The file path is relative to the application's current working directory. An absolute path may also be used. Supply the path appropriate to your own environment.
Background. The chain must contain at least one certificate; the certificate at index 0 is treated as the leaf. The chain is built here with Cert.BuildCertChain.
Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oPfx
LOCAL oPrivKey
LOCAL oCert
LOCAL oChain
nSuccess := 0
oPfx := CreateObject("Chilkat.Pfx")
// The file path is relative to the application's current working directory. An absolute path
// may also be used. Supply the path appropriate to your own environment.
// Load the private key.
oPrivKey := CreateObject("Chilkat.PrivateKey")
nSuccess := oPrivKey:LoadPemFile("qa_data/private_key.pem")
IF (nSuccess == 0)
? oPrivKey:LastErrorText
oPfx:destroy()
oPrivKey:destroy()
RETURN
ENDIF
// Load the certificate and build its chain of authority. The chain must contain at least one
// certificate; the certificate at index 0 is treated as the leaf.
oCert := CreateObject("Chilkat.Cert")
nSuccess := oCert:LoadFromFile("qa_data/cert.pem")
IF (nSuccess == 0)
? oCert:LastErrorText
oPfx:destroy()
oPrivKey:destroy()
oCert:destroy()
RETURN
ENDIF
oChain := CreateObject("Chilkat.CertChain")
nSuccess := oCert:BuildCertChain(oChain)
IF (nSuccess == 0)
? oCert:LastErrorText
oPfx:destroy()
oPrivKey:destroy()
oCert:destroy()
oChain:destroy()
RETURN
ENDIF
// Add the private key and its certificate chain to the PFX.
nSuccess := oPfx:AddPrivateKey(oPrivKey, oChain)
IF (nSuccess == 0)
? oPfx:LastErrorText
oPfx:destroy()
oPrivKey:destroy()
oCert:destroy()
oChain:destroy()
RETURN
ENDIF
? "Private key and certificate chain added to the PFX."
oPfx:destroy()
oPrivKey:destroy()
oCert:destroy()
oChain:destroy()