Sample code for 30+ languages & platforms
DataFlex

Get Certificates from .p12 / .pfx

See more PFX/P12 Examples

A PKCS12 (.p12 / .pfx) is a container for holding a certificate, its private key, and the certs in the chain of authentication up to and possibly including the root CA cert. A .p12 is not required to contain certain things. It will contain whatever the creator of the .p12 decided to include. It's possible to contain just a private key, just a cert, many certs without private keys, or many certs with many private keys. Usually, a .p12 contains one certificate, its associated private key, and certificates in the chain of authentication.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoPfx
    Variant vCert
    Handle hoCert
    Integer iNumCerts
    Integer i
    Variant vIssuer
    Handle hoIssuer
    String sTemp1
    Boolean bTemp1

    Move False To iSuccess

    Get Create (RefClass(cComChilkatPfx)) To hoPfx
    If (Not(IsComObjectCreated(hoPfx))) Begin
        Send CreateComObject of hoPfx
    End

    Get ComLoadPfxFile Of hoPfx "qa_data/pfx/test.pfx" "pfx_password" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPfx To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Iterate over the certs contained in the PFX
    Get Create (RefClass(cComChilkatCert)) To hoCert
    If (Not(IsComObjectCreated(hoCert))) Begin
        Send CreateComObject of hoCert
    End
    Get ComNumCerts Of hoPfx To iNumCerts
    Move 0 To i
    While (i < iNumCerts)

        Get pvComObject of hoCert to vCert
        Get ComCertAt Of hoPfx i vCert To iSuccess

        Showln "--- " i " ---"
        Get ComSubjectDN Of hoCert To sTemp1
        Showln sTemp1
        // Is this a root cert, or self-signed?
        Get ComIsRoot Of hoCert To bTemp1
        Showln "Root: " bTemp1
        Get ComSelfSigned Of hoCert To bTemp1
        Showln "Self-Signed: " bTemp1

        // If this certificate is not the root (self-signed), then get the issuer.
        // If the issuing certificate is contained in the PFX, then it will be found here..
        Get ComSelfSigned Of hoCert To bTemp1
        If (bTemp1 <> True) Begin
            Get ComFindIssuer Of hoCert To vIssuer
            If (IsComObject(vIssuer)) Begin
                Get Create (RefClass(cComChilkatCert)) To hoIssuer
                Set pvComObject Of hoIssuer To vIssuer
            End
            Get ComLastMethodSuccess Of hoCert To bTemp1
            If (bTemp1 = False) Begin
                Showln "Issuer not found."
            End
            Else Begin
                Get ComSubjectDN Of hoIssuer To sTemp1
                Showln "Issuer: " sTemp1
                Send Destroy of hoIssuer
            End

        End

        Move (i + 1) To i
    Loop

    // Usually, the user certificate is at index 0, its issuer is at index 1, etc. until we get to the root certificate.


End_Procedure