Why does SSL handshake give 'Could not generate DH keypair' exception?

前端 未结 21 779
梦如初夏
梦如初夏 2020-11-22 07:46

When I make an SSL connection with some IRC servers (but not others - presumably due to the server\'s preferred encryption method) I get the following exception:

<         


        
相关标签:
21条回答
  • 2020-11-22 08:41

    If you are still bitten by this issue AND you are using Apache httpd v> 2.4.7, try this: http://httpd.apache.org/docs/current/ssl/ssl_faq.html#javadh

    copied from the url:

    Beginning with version 2.4.7, mod_ssl will use DH parameters which include primes with lengths of more than 1024 bits. Java 7 and earlier limit their support for DH prime sizes to a maximum of 1024 bits, however.

    If your Java-based client aborts with exceptions such as java.lang.RuntimeException: Could not generate DH keypair and java.security.InvalidAlgorithmParameterException: Prime size must be multiple of 64, and can only range from 512 to 1024 (inclusive), and httpd logs tlsv1 alert internal error (SSL alert number 80) (at LogLevel info or higher), you can either rearrange mod_ssl's cipher list with SSLCipherSuite (possibly in conjunction with SSLHonorCipherOrder), or you can use custom DH parameters with a 1024-bit prime, which will always have precedence over any of the built-in DH parameters.

    To generate custom DH parameters, use the

    openssl dhparam 1024

    command. Alternatively, you can use the following standard 1024-bit DH parameters from RFC 2409, section 6.2:

    -----BEGIN DH PARAMETERS-----
    MIGHAoGBAP//////////yQ/aoiFowjTExmKLgNwc0SkCTgiKZ8x0Agu+pjsTmyJR
    Sgh5jjQE3e+VGbPNOkMbMCsKbfJfFDdP4TVtbVHCReSFtXZiXn7G9ExC6aY37WsL
    /1y29Aa37e44a/taiZ+lrp8kEXxLH+ZJKGZR7OZTgf//////////AgEC
    -----END DH PARAMETERS-----
    

    Add the custom parameters including the "BEGIN DH PARAMETERS" and "END DH PARAMETERS" lines to the end of the first certificate file you have configured using the SSLCertificateFile directive.


    I am using java 1.6 on client side, and it solved my issue. I didn't lowered the cipher suites or like, but added a custom generated DH param to the cert file..

    0 讨论(0)
  • 2020-11-22 08:42

    The problem is the prime size. The maximum-acceptable size that Java accepts is 1024 bits. This is a known issue (see JDK-6521495).

    The bug report that I linked to mentions a workaround using BouncyCastle's JCE implementation. Hopefully that should work for you.

    UPDATE

    This was reported as bug JDK-7044060 and fixed recently.

    Note, however, that the limit was only raised to 2048 bit. For sizes > 2048 bit, there is JDK-8072452 - Remove the maximum prime size of DH Keys; the fix appears to be for 9.

    0 讨论(0)
  • 2020-11-22 08:42

    For me, the following command line fixed the issue:

    java -jar -Dhttps.protocols=TLSv1.2 -Ddeployment.security.TLSv1.2=true -Djavax.net.debug=ssl:handshake XXXXX.jar

    I am using JDK 1.7.0_79

    0 讨论(0)
  • 2020-11-22 08:45

    I encountered the SSL error on a CentOS server running JDK 6.

    My plan was to install a higher JDK version (JDK 7) to co-exist with JDK 6 but it turns out that merely installing the newer JDK with rpm -i was not enough.

    The JDK 7 installation would only succeed with the rpm -U upgrade option as illustrated below.

    1. Download JDK 7

    wget -O /root/jdk-7u79-linux-x64.rpm --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; o raclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/7u79-b15/jdk-7u79-linux-x64.rpm"
    

    2. RPM installation fails

    rpm -ivh jdk-7u79-linux-x64.rpm
    Preparing...                ########################################### [100%]
            file /etc/init.d/jexec from install of jdk-2000:1.7.0_79-fcs.x86_64 conflicts with file from package jdk-2000:1.6.0_43-fcs.x86_64
    

    3. RPM upgrade succeeds

    rpm -Uvh jdk-7u79-linux-x64.rpm
    Preparing...                ########################################### [100%]
       1:jdk                    ########################################### [100%]
    Unpacking JAR files...
            rt.jar...
            jsse.jar...
            charsets.jar...
            tools.jar...
            localedata.jar...
            jfxrt.jar...
    

    4. Confirm the new version

    java -version
    java version "1.7.0_79"
    Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
    Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)
    
    0 讨论(0)
  • 2020-11-22 08:47

    Try downloading "Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files" from the Java download site and replacing the files in your JRE.

    This worked for me and I didn't even need to use BouncyCastle - the standard Sun JCE was able to connect to the server.

    PS. I got the same error (ArrayIndexOutOfBoundsException: 64) when I tried using BouncyCastle before changing the policy files, so it seems our situation is very similar.

    0 讨论(0)
  • 2020-11-22 08:51

    If you are using jdk1.7.0_04, upgrade to jdk1.7.0_21. The problem has been fixed in that update.

    0 讨论(0)
提交回复
热议问题