6

Here's a simplified version of the code I'm using to perform simple HTTPS requests:

// Assume the variables host, file and postData have valid String values

final URL url = new URL("https", host, file);
final HttpURLConnection connection = (HttpURLConnection) url.openConnection();

connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestProperty("Content-length", String.valueOf(postData.length()));

final DataOutputStream output = new DataOutputStream(connection.getOutputStream());
output.writeBytes(postData);
output.close();

final InputStream input = new DataInputStream(connection.getInputStream());

for (int c = input.read(); c != -1; c = input.read()) {
  System.out.print((char) c);
}

System.out.println();

input.close();

This used to work well for connecting to our server (and still does if I use http as the protocol) until recently when some security upgrades were done.

Now it's giving me the "Could not generate DH keypair" and "Prime size must be multiple of 64, and can only range from 512 to 1024 (inclusive)" errors mentioned in this question:

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

Turns out it's a known bug in Java and the recommendation is to use BouncyCastle's JCE implementation.

My question is... how do I use BouncyCastle for something like this? Or are there more alternatives?

Disclaimer: I have very little knowledge of and interest in cryptology and the underlying technology that makes HTTPS queries possible. Rather, I'd prefer to focus on my application logic and let various libraries take care of low level issues.

I checked out the BouncyCastle website and documentation and Googled to find out more about JCE etc, but all in all it's quite overwhelming and I haven't been able to find any simple code examples for doing something like the above code.

4 Answers 4

5

The following sample code uses jdk1.6.0_45 and bcprov-jdk15on-153.jar to perform simple https query:

import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Socket;

import org.bouncycastle.crypto.tls.CertificateRequest;
import org.bouncycastle.crypto.tls.DefaultTlsClient;
import org.bouncycastle.crypto.tls.TlsAuthentication;
import org.bouncycastle.crypto.tls.TlsClientProtocol;
import org.bouncycastle.crypto.tls.TlsCredentials;

public class TestHttpClient {
    // Reference: http://boredwookie.net/index.php/blog/how-to-use-bouncy-castle-lightweight-api-s-tlsclient/
    //            bcprov-jdk15on-153.tar\src\org\bouncycastle\crypto\tls\test\TlsClientTest.java
    public static void main(String[] args) throws Exception {
        java.security.SecureRandom secureRandom = new java.security.SecureRandom();
        Socket socket = new Socket(java.net.InetAddress.getByName("www.google.com"), 443);
        TlsClientProtocol protocol = new TlsClientProtocol(socket.getInputStream(), socket.getOutputStream(),secureRandom);
        DefaultTlsClient client = new DefaultTlsClient() {
            public TlsAuthentication getAuthentication() throws IOException {
                TlsAuthentication auth = new TlsAuthentication() {
                    // Capture the server certificate information!
                    public void notifyServerCertificate(org.bouncycastle.crypto.tls.Certificate serverCertificate) throws IOException {
                    }

                    public TlsCredentials getClientCredentials(CertificateRequest certificateRequest) throws IOException {
                        return null;
                    }
                };
                return auth;
            }
        };
        protocol.connect(client);

        java.io.OutputStream output = protocol.getOutputStream();
        output.write("GET / HTTP/1.1\r\n".getBytes("UTF-8"));
        output.write("Host: www.google.com\r\n".getBytes("UTF-8"));
        output.write("Connection: close\r\n".getBytes("UTF-8")); // So the server will close socket immediately.
        output.write("\r\n".getBytes("UTF-8")); // HTTP1.1 requirement: last line must be empty line.
        output.flush();

        java.io.InputStream input = protocol.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(input));
        String line;
        while ((line = reader.readLine()) != null)
        {
            System.out.println(line);
        }
    }
}
5
  • How do I use this with apache HTTP cleint 3.1
    – Arvind
    Jun 11, 2018 at 11:40
  • This answer is using plain JDK with Bouncy Castle. You may search "apache httpclient bouncy castle" for your question. In my opinion, as Httpclient and Bouncy Castle are advancing with different versions and JDK support, it may be challenging to find the perfect match.
    – oraclesoon
    Jun 12, 2018 at 9:29
  • 1
    Your code ends with "org.bouncycastle.crypto.tls.TlsNoCloseNotifyException: No close_notify alert received before connection closed" after printing out part of response from google. Jul 1, 2018 at 13:57
  • 1
    I recalled TlsNoCloseNotifyException has something to do with "Connection: close". If we didn't explicitly requests server to close connection, server is actually still maintaining an active connection with this Java Socket client, but this sample code has done a System.out.println and quitted. This has killed the active connection at TLS layer and caused TlsNoCloseNotifyException to throw.
    – oraclesoon
    Jul 10, 2018 at 20:10
  • @oraclesoon said it right, its caused only because the stream isnt closed; rest everything works perfect. So add line reader.close(); at the end and you should be good.
    – HarsH
    Dec 26, 2019 at 20:46
3

Here is a page on how to register bouncy castle JCE provider. If you choose the second option, make sure the value of N (preference order) is such that it precedes the Sun JCE provider (and accordingly adjust the preference order of the Sun JCE provider registration).

1
  • Looks like the most helpful part of that page was a note I originally missed - "Note: to make full use of the provider you must install the unlimited policy files in the JVM you are using - these can be downloaded from java.sun.com.". After I did that, I didn't even need BouncyCastle :D. I'll post that as a full answer.
    – mjomble
    Nov 17, 2011 at 20:52
1

I found a different solution (although via the link that srkavin posted) that doesn't even require using BouncyCastle:

After downloading "Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files" from the Java download site and replacing the files in my JRE, the above code started working without any modifications.

0

I have the same problem and resolve including this lines

Security.addProvider(new BouncyCastleProvider());
System.setProperty("https.protocols", "TLSv1");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.