Java SSL DH Keypair Generation - Prime Size Error

前端 未结 4 569
忘掉有多难
忘掉有多难 2021-01-02 23:30

I\'m currently implementing Reddit OAuth2 login into my web app. The handshake and token exchange work fine when testing locally but when running on the server (hosted on \'

相关标签:
4条回答
  • 2021-01-03 00:10

    I solved the problem on oracle java 8 by switching to bouncycastle provider for ssl/tls:

    1. Added bouncycastle to my project

      <dependency>
          <groupId>org.bouncycastle</groupId>
          <artifactId>bcprov-jdk15on</artifactId>
          <version>1.54</version>
      </dependency>
      
    2. Before I do any SSL stuff, I add the BouncyCastle provider as 1st provider to the list:

      Security.insertProviderAt(new BouncyCastleProvider(),1);
      

    That's all. Now my connections to sites with 4096 bit DH parameters works as expected (I'm using Apache HTTP Client). This should also work with jdk 7.

    0 讨论(0)
  • 2021-01-03 00:14

    First, "Unlimited Strength" is irrelevant here. That would fix the entirely different problem that you can't use cipher suites using AES-256 (and if the peer insists on them can't handshake at all). Also bitsize of the JVM doesn't matter; this (not really justified) restriction on DH is in the "run-everywhere" bytecode in SunJCE.

    You can use BouncyCastle as a crypto provider without changing the code that does SSL connections (in your case Scribe), but from what I've read making BC the preferred provider causes other problems. If you want to try anyway, either put bcprov-version.jar in JRE/lib/exit and edit JRE/lib/security/java.security; or put it anywhere in your classpath and have your init code call java.security.Security.insertProviderAt (new org.bouncycastle.jce.provider.BouncyCastleProvider(), position);

    I suggest starting from why your local system DOES work. When I try ssl.reddit.com with openssl, it supports both ECDHE-RSA (with P-256) and DHE-RSA with dh 2048 bits. Suncle Java 7 does support and prefer ECDHE, and I would expect OpenJDK also does but maybe not or maybe sometimes not; I know RedHat until recently nobbled ECC in its rpms of openssl, and it wouldn't astonish me if they did so in openjdk also. If you compile and run the following (with ssl.reddit.com 443) it will tell what suite gets negotiated on your system, using all default SSL settings of your JRE (which I expect/hope Scribe is also using):

    //nopackage DThompson 2012.08.13b
    import java.net.InetSocketAddress;
    import java.net.Socket;
    import javax.net.ssl.SSLSocket;
    import javax.net.ssl.SSLSocketFactory;
    
    public class JustBConnectSSL {
    
        /* (optionally bind and) just make SSL connection, for testing reach and trust
         * uses default providers, truststore (normally JRE/lib/security/[jsse]cacerts), 
         * and keystore (normally none), override with -Djavax.net.ssl.{trust,key}Store* 
         */
        public static void main (String[] args) throws Exception {
            if( args.length < 2 ){ System.out.println ("Usage: tohost port [fromaddr [fromport]]"); return; }
            Socket sock = SSLSocketFactory.getDefault().createSocket();
            if( args.length > 2 )
                sock.bind (new InetSocketAddress (args[2], args.length>3? Integer.parseInt(args[3]): 0));
            sock.connect (new InetSocketAddress (args[0], Integer.parseInt(args[1])));
            System.out.println (sock.getInetAddress().getHostName() + " = " + sock.getInetAddress().getHostAddress());
            ((SSLSocket)sock).startHandshake();
            System.out.println ("connect okay " + ((SSLSocket)sock).getSession().getCipherSuite());
        }
    }
    

    If test gets _DHE_RSA_something, the crypto providers in your JRE must be different from the Suncle ones, either changed by Ubuntu or some customization or patch on your system. If test gets _ECDHE_RSA_something but OpenShift doesn't, they may have disabled ECC/ECDHE somehow. If they can enable that's best (ECDHE-P-256 is at least as secure and probably more efficient than DH-2048). Otherwise until Oracle fixes this (apparently in 8) the only way I think can be relied on is to disable DHE suites (and drop back to plain RSA, which may not be safe against NSA); that is simplest in the code that actually creates the SSLSocket, but if Scribe (like most java web clients) uses URL -> HttpsUrlConnection with its default SSLSocketFactory you can substitute a tweaked factory that changes the EnabledCiphers list along the lines of question #6851461 (although for a host with a good public certificate you don't need the custom-trustmanager parts of that solution).

    0 讨论(0)
  • 2021-01-03 00:26

    Not sure if it matters, but the version of OpenJDK running on OpenShift is 32 bit, you can view this by using the System.getProperty("sun.arch.data.model") in your code.

    I wrote a quick class to just output the bit-ness and compiled it on the OpenShift gear and ran it and got 32

    class Test {
    
    public static void main(String[] args) {
    
    System.out.println(System.getProperty("sun.arch.data.model"));
    
    }
    
    }
    
    0 讨论(0)
  • 2021-01-03 00:28

    I know I am very late to answer this but I was struggling with the similar problem and then solved it. My solution is for MAC-OS.

    1. Install Bouncy Castle jar from http://www.bouncycastle.org/latest_releases.html, in /Library/Java/JavaVirtualMachines//Contents/Home/jre/lib/ext
    2. edit java.security file add the below line security.provider.2=org.bouncycastle.jce.provider.BouncyCastleProvider and then reorder all the other providers you may have.
    3. voila
    0 讨论(0)
提交回复
热议问题