Sample code for 30+ languages & platforms
DataFlex

Get the Certificate with Private Key from a Java KeyStore

See more Java KeyStore (JKS) Examples

Load a Chilkat certificate object from a Java KeyStore.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoJks
    String sPassword
    Variant vChain
    Handle hoChain
    Variant vCert
    Handle hoCert
    Handle hoCrypt
    String sTemp1
    Integer iTemp1
    Boolean bTemp1

    Move False To iSuccess

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

    // Most of the time a .jks contains one certificate with it's associated private key.
    // (Similar to how a .pfx/.p12 usually contains a particular certificate with private key.)
    // This example demonstrates how to get the certificate with private key such that it can be used
    // by other Chilkat classes wherever a cert w/ private key is needed.
    Get Create (RefClass(cComChilkatJavaKeyStore)) To hoJks
    If (Not(IsComObjectCreated(hoJks))) Begin
        Send CreateComObject of hoJks
    End
    Move "secret" To sPassword
    Get ComLoadFile Of hoJks sPassword "qa_data/jks/test_secret.jks" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoJks To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Make sure we have a private key.
    Get ComNumPrivateKeys Of hoJks To iTemp1
    If (iTemp1 < 1) Begin
        Showln "No private key available."
        Procedure_Return
    End

    // -------------------------------------------------------------------------
    // Get the certificate chain associated with the 1st (and probably only) private key in the JKS.

    Get Create (RefClass(cComChilkatCertChain)) To hoChain
    If (Not(IsComObjectCreated(hoChain))) Begin
        Send CreateComObject of hoChain
    End
    Get pvComObject of hoChain to vChain
    Get ComCertChainAt Of hoJks 0 vChain To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoJks To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatCert)) To hoCert
    If (Not(IsComObjectCreated(hoCert))) Begin
        Send CreateComObject of hoCert
    End
    Get pvComObject of hoCert to vCert
    Get ComCertAt Of hoChain 0 vCert To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoChain To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Verify again that this cert has a private key.
    Get ComHasPrivateKey Of hoCert To bTemp1
    If (bTemp1 <> True) Begin
        Showln "Certificate has no associated private key."
        Procedure_Return
    End

    // We now have the cert object with it's associated private key, and it can be used in other Chilkat classes where needed.
    // For example..

    Get Create (RefClass(cComChilkatCrypt2)) To hoCrypt
    If (Not(IsComObjectCreated(hoCrypt))) Begin
        Send CreateComObject of hoCrypt
    End
    Get pvComObject of hoCert to vCert
    Get ComSetSigningCert Of hoCrypt vCert To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoCrypt To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // ...
    // ...


End_Procedure