Sample code for 30+ languages & platforms
DataFlex

Enumerate Certificates in a PFX

See more PFX/P12 Examples

Demonstrates Pfx.CertAt, which copies the certificate at a zero-based index into a Cert object. The example enumerates all certificates in the 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.

Background. Valid indexes are less than NumCerts. Each retrieved certificate is an independent copy that can be inspected or used separately.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoPfx
    String sPassword
    Integer iNumCerts
    Variant vCert
    Handle hoCert
    Integer i
    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.
    //  The PFX password should come from a secure source rather than being hard-coded.
    Move "myPfxPassword" To sPassword
    Get ComLoadPfxFile Of hoPfx "qa_data/identity.pfx" sPassword To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPfx To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Enumerate the certificates in the PFX by zero-based index.  Valid indexes are less than NumCerts.
    //  The certificate at index 0 is typically the certificate that has a private key, and the remaining
    //  certificates are the others in its certificate chain.
    Get ComNumCerts Of hoPfx To iNumCerts
    Get Create (RefClass(cComChilkatCert)) To hoCert
    If (Not(IsComObjectCreated(hoCert))) Begin
        Send CreateComObject of hoCert
    End

    For i From 0 To (iNumCerts - 1)
        Get pvComObject of hoCert to vCert
        Get ComCertAt Of hoPfx i vCert To iSuccess
        If (iSuccess = False) Begin
            Get ComLastErrorText Of hoPfx To sTemp1
            Showln sTemp1
            Procedure_Return
        End

        Get ComSubjectCN Of hoCert To sTemp1
        Showln "Certificate " i ": " sTemp1
    Loop



End_Procedure