user7374044
user7374044

Reputation: 167

OpenJDK 11 error "Can not initialize cryptographic mechanism"

I have an app written with openJDK (Amazon Correto 11) and openJFX.

When I run the app locally - everything works. But when I execute the app from network share there is an Exception:

java.lang.ExceptionInInitializerError
    at java.base/javax.crypto.JceSecurityManager.<clinit>(JceSecurityManager.java:65)
    at java.base/javax.crypto.Cipher.getConfiguredPermission(Cipher.java:2624)
    at java.base/javax.crypto.Cipher.getMaxAllowedKeyLength(Cipher.java:2646)
    at java.base/sun.security.ssl.SSLCipher.isUnlimited(SSLCipher.java:540)
    at java.base/sun.security.ssl.SSLCipher.<init>(SSLCipher.java:472)
    at java.base/sun.security.ssl.SSLCipher.<clinit>(SSLCipher.java:174)
    at java.base/sun.security.ssl.CipherSuite.<clinit>(CipherSuite.java:67)
    at java.base/sun.security.ssl.SSLContextImpl.getApplicableSupportedCipherSuites(SSLContextImpl.java:348)
    at java.base/sun.security.ssl.SSLContextImpl$AbstractTLSContext.<clinit>(SSLContextImpl.java:579)
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(Class.java:315)
    at java.base/java.security.Provider$Service.getImplClass(Provider.java:1848)
    at java.base/java.security.Provider$Service.newInstance(Provider.java:1824)
    at java.base/sun.security.jca.GetInstance.getInstance(GetInstance.java:236)
    at java.base/sun.security.jca.GetInstance.getInstance(GetInstance.java:164)
    at java.base/javax.net.ssl.SSLContext.getInstance(SSLContext.java:168)
    at org.apache.http.ssl.SSLContexts.createDefault(SSLContexts.java:51)
    at org.apache.http.conn.ssl.SSLConnectionSocketFactory.getSocketFactory(SSLConnectionSocketFactory.java:194)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.getDefaultRegistry(PoolingHttpClientConnectionManager.java:115)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:122)
    at com.mashape.unirest.http.options.Options.refresh(Options.java:72)
    at com.mashape.unirest.http.options.Options.<clinit>(Options.java:46)
    at com.mashape.unirest.http.HttpClientHelper.prepareRequest(HttpClientHelper.java:151)
    at com.mashape.unirest.http.HttpClientHelper.request(HttpClientHelper.java:131)
    at com.mashape.unirest.request.BaseRequest.asString(BaseRequest.java:56)
    at myapp.util.MyTaxNumberValidator$3.run(MyTaxNumberValidator.java:185)
    at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.SecurityException: Can not initialize cryptographic mechanism
    at java.base/javax.crypto.JceSecurity.<clinit>(JceSecurity.java:120)
    ... 27 more
Caused by: java.lang.SecurityException: Can't read cryptographic policy directory: unlimited
    at java.base/javax.crypto.JceSecurity.setupJurisdictionPolicies(JceSecurity.java:326)
    at java.base/javax.crypto.JceSecurity$1.run(JceSecurity.java:111)
    at java.base/javax.crypto.JceSecurity$1.run(JceSecurity.java:108)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.base/javax.crypto.JceSecurity.<clinit>(JceSecurity.java:107)
    ... 27 more

I run an app using command:

\\\some-network-share\app\jre\bin\javaw.exe -jar myapp.jar

because on this network share I've copied Amazon Correto 11 JDK files and other libs I use for the app.

I run get request in another thread and this thread throws an exception.

How can I overcome the problem?

Please help.

I've tried different libs: 1. OkHttpClient 2. Java Unirest

but in both the same error.


public class MyGetExecutor {


    public static void main(String[] args) {

        Thread getThread = new Thread( new Runnable() {

            @Override
            public void run() {

                try {
                    logMessage.add( "Before request" );
                    com.mashape.unirest.http.HttpResponse<String> response = Unirest.get("https://myappserver.domain.com/api/v1/users/1")
                    .header("User-Agent", "PostmanRuntime/7.17.1")
                    .header("Accept", "*/*")
                    .header("Cache-Control", "no-cache")
                    .header("Postman-Token", "abf4b45d-a2c5-4f53-b4e3-1fa29643d616,e59130fc-5ce0-4b70-af48-ab3808362e36")
                    .header("Host", "wf01.cdprojektred.com")
                    .header("Accept-Encoding", "gzip, deflate")
                    .header("Cookie", "JSESSIONID=2C71F035C2EDFF145FED5C03C4887EF1")
                    .header("Connection", "keep-alive")
                    .header("cache-control", "no-cache")
                    .asString();
                    logMessage.add( "After request" );
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    logMessage.add( "Exception in getThread: " + e.getMessage() );
                }


            }

        }, "Get-thread");
        getThread.setUncaughtExceptionHandler(
                new Thread.UncaughtExceptionHandler() {
                    @Override
                    public void uncaughtException(Thread t, Throwable e) {
                        logMessage.add( "Error in thread: " + e );
                        logMessage.add( "Details: " + e.getCause().getMessage() );
                    }
                }
            );
        getThread.start();

        while( getThread.isAlive() ) {

            try {
                Thread.sleep(1000);
                logMessage.add("Main thread sleeping...");
            } catch (Exception e) {
                logMessage.add("Exception in main thread...: " + e.getMessage());
            }



        }

    }
}

EDIT 1 I've added additional stacktrace - please check

Upvotes: 6

Views: 15563

Answers (2)

siddmuk2005
siddmuk2005

Reputation: 186

Check the conf folder exist or not? if conf folder is not there then this issue comes most of the time. as conf folder contain security related policy.

Upvotes: 0

diginoise
diginoise

Reputation: 7630

The clue is in last cause in your stack trace:
Caused by: java.lang.SecurityException: Can't read cryptographic policy directory: unlimited

down the rabit hole:

  • Verify where your security policies are coming from by inspecting java.security.properties from within running JVM:

    System.getProperty("java.security.properties");
    

    It should be present and pointing at a file. Inspect that file and make sure it has the following property crypto.policy=unlimited and the folder called unlimited is available from where the policy file is. The policy file itself should explain where to look for this folder.

    For Coretto it is <jre_home>/conf/security/policy/

  • You can modify the location of the security policy by starting your JVM and passing it the location of the policy file:
    java -Djava.security.properties==/conf/security/java.security
    OR
    java -Djava.security.properties=/conf/security/java.security

    Additional =sign in the first case will cause Java to use only properties in the file you pointing at.
    The second option will add / update the properties defined in the file.

  • Lastly you have to dig around your Correto 11 to find the policy file.
    The paths above are taken from amazon-corretto-11.0.3.7.1-linux-x64.tar.gz

Upvotes: 8

Related Questions