PureBasic
PureBasic
Load PKCS12 / PFX and Access Contents
See more PFX/P12 Examples
Loads a PKCS12 / PFX file and iterates over the contents which include private keys and certificates.Chilkat PureBasic Downloads
IncludeFile "CkCert.pb"
IncludeFile "CkPrivateKey.pb"
IncludeFile "CkPfx.pb"
Procedure ChilkatExample()
success.i = 0
pfx.i = CkPfx::ckCreate()
If pfx.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; Load the PKCS12 from a file
success = CkPfx::ckLoadPfxFile(pfx,"/someDir/my.p12","pfxFilePassword")
If success = 0
Debug CkPfx::ckLastErrorText(pfx)
CkPfx::ckDispose(pfx)
ProcedureReturn
EndIf
numPrivateKeys.i = CkPfx::ckNumPrivateKeys(pfx)
privKey.i = CkPrivateKey::ckCreate()
If privKey.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
Debug "Private Keys:"
i.i = 0
While i < numPrivateKeys
CkPfx::ckPrivateKeyAt(pfx,i,privKey)
; Do something with the private key ...
i = i + 1
Wend
cert.i = CkCert::ckCreate()
If cert.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
numCerts.i = CkPfx::ckNumCerts(pfx)
Debug "Certs:"
i = 0
While i < numCerts
CkPfx::ckCertAt(pfx,i,cert)
Debug CkCert::ckSubjectDN(cert)
; If the certificate has a private key (one of the private keys within the PFX)
; then it can also be obtained via the certificate object:
If CkCert::ckHasPrivateKey(cert) = 1
Debug "Has private key!"
success = CkCert::ckGetPrivateKey(cert,privKey)
; ...
EndIf
i = i + 1
Wend
CkPfx::ckDispose(pfx)
CkPrivateKey::ckDispose(privKey)
CkCert::ckDispose(cert)
ProcedureReturn
EndProcedure