Sample code for 30+ languages & platforms
DataFlex

Add Trusted Certificate to JKS

See more Java KeyStore (JKS) Examples

Adds a trusted certificate to a Java keystore file.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoJks
    String sJksPassword
    String sJksPath
    Variant vCert
    Handle hoCert
    String sAlias
    String sTemp1

    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

    Move "secret" To sJksPassword
    Move "/myJksTrustedCerts/cacerts.jks" To sJksPath

    // Load the Java keystore from a file.
    Get ComLoadFile Of hoJks sJksPassword sJksPath To iSuccess
    If (iSuccess <> True) 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

    // The cert's LoadFrommFile method can load a certificate from
    // virtually any format.  It will automatically determine the format
    // and load appropriately.
    Get ComLoadFromFile Of hoCert "/certFiles/myNewTrustedCert.pem" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoCert To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // The alias can be anything.  It's basically just a label 
    // used within the JKS associated with the entry.  It should
    // be unique among aliases within the JKS file.
    Move "habanero" To sAlias

    Get pvComObject of hoCert to vCert
    Get ComAddTrustedCert Of hoJks vCert sAlias To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoJks To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Write the JKS containing the new certificate.
    Get ComToFile Of hoJks sJksPassword sJksPath To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoJks To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Added a trusted certificate to the JKS."


End_Procedure