Sample code for 30+ languages & platforms
DataFlex

Load Java KeyStore and Access Contents

See more Java KeyStore (JKS) Examples

Loads a Java keystore file and iterates over the contents. A Java keystore (.jks) file can contain one or more trusted root certificate entries and/or one or more private key entries. Each private key entry includes an associated certificate chain.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoJks
    Integer iNumTrustedCerts
    Integer iNumPrivateKeys
    Variant vCert
    Handle hoCert
    String sAlias
    Integer i
    Variant vPrivKey
    Handle hoPrivKey
    Variant vCert
Chain    Handle hoCertChain
    String sTemp1
    String sTemp2

    Move False To iSuccess

    // This requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    Get Create (RefClass(cComChilkatJavaKeyStore)) To hoJks
    If (Not(IsComObjectCreated(hoJks))) Begin
        Send CreateComObject of hoJks
    End

    // Load the Java keystore from a file.  The JKS file password is used
    // to verify the keyed digest that is found at the very end of the keystore.
    // It verifies there has been no tampering with the file.
    Get ComLoadFile Of hoJks "jksFilePassword" "/someDir/keyStore.jks" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoJks To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Find out how many of each type of entry:
    Get ComNumTrustedCerts Of hoJks To iNumTrustedCerts
    Get ComNumPrivateKeys Of hoJks To iNumPrivateKeys

    Get Create (RefClass(cComChilkatCert)) To hoCert
    If (Not(IsComObjectCreated(hoCert))) Begin
        Send CreateComObject of hoCert
    End

    // For each trusted certificate, access it by getting
    // it as a cert object.  Also get the alias associated with the certificate.
    Showln "Trusted Certs:"
    Move 0 To i
    While (i < iNumTrustedCerts)
        Get pvComObject of hoCert to vCert
        Get ComTrustedCertAt Of hoJks i vCert To iSuccess
        Get ComGetTrustedCertAlias Of hoJks i To sTemp1
        Get ComSubjectDN Of hoCert To sTemp2
        Showln sTemp1 ": " sTemp2
        Move (i + 1) To i
    Loop

    Get Create (RefClass(cComChilkatPrivateKey)) To hoPrivKey
    If (Not(IsComObjectCreated(hoPrivKey))) Begin
        Send CreateComObject of hoPrivKey
    End
    Get Create (RefClass(cComChilkatCertChain)) To hoCertChain
    If (Not(IsComObjectCreated(hoCertChain))) Begin
        Send CreateComObject of hoCertChain
    End

    // For each private key entry, get the private key and
    // the associated certificate chain.
    // Each private key is password protected.  Usually it is the same
    // password as used for the keyed digest of the entire JKS.  
    // However, this does not have to be.  The password is passed
    // here to handle the possibility of each private key requiring
    // a different password.
    Showln "Private Keys:"
    Move 0 To i
    While (i < iNumPrivateKeys)
        Get pvComObject of hoPrivKey to vPrivKey
        Get ComPrivateKeyAt Of hoJks "jksFilePassword" i vPrivKey To iSuccess
        Get ComGetPrivateKeyAlias Of hoJks i To sTemp1
        Showln sTemp1
        Get pvComObject of hoCertChain to vCertChain
        Get ComCertChainAt Of hoJks i vCertChain To iSuccess

        // The 1st certificate in the chain is the one associated with the private key.
        Get pvComObject of hoCert to vCert
        Get ComCertAt Of hoCertChain 0 vCert To iSuccess
        Get ComSubjectDN Of hoCert To sTemp1
        Showln sTemp1

        Move (i + 1) To i
    Loop



End_Procedure