DataFlex
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoPfx
Variant vPrivKey
Handle hoPrivKey
Handle hoCert
Variant vChain
Handle hoChain
String sTemp1
Move False To iSuccess
Get Create (RefClass(cComChilkatPfx)) To hoPfx
If (Not(IsComObjectCreated(hoPfx))) Begin
Send CreateComObject of hoPfx
End
// 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.
Get Create (RefClass(cComChilkatPrivateKey)) To hoPrivKey
If (Not(IsComObjectCreated(hoPrivKey))) Begin
Send CreateComObject of hoPrivKey
End
Get ComLoadPemFile Of hoPrivKey "qa_data/private_key.pem" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPrivKey To sTemp1
Showln sTemp1
Procedure_Return
End
// 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.
Get Create (RefClass(cComChilkatCert)) To hoCert
If (Not(IsComObjectCreated(hoCert))) Begin
Send CreateComObject of hoCert
End
Get ComLoadFromFile Of hoCert "qa_data/cert.pem" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoCert To sTemp1
Showln sTemp1
Procedure_Return
End
Get Create (RefClass(cComChilkatCertChain)) To hoChain
If (Not(IsComObjectCreated(hoChain))) Begin
Send CreateComObject of hoChain
End
Get pvComObject of hoChain to vChain
Get ComBuildCertChain Of hoCert vChain To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoCert To sTemp1
Showln sTemp1
Procedure_Return
End
// Add the private key and its certificate chain to the PFX.
Get pvComObject of hoPrivKey to vPrivKey
Get pvComObject of hoChain to vChain
Get ComAddPrivateKey Of hoPfx vPrivKey vChain To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPfx To sTemp1
Showln sTemp1
Procedure_Return
End
Showln "Private key and certificate chain added to the PFX."
End_Procedure