Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use TLSV1 or SSLV3 for first handshake(Client Hello) in Java?

When I execute the following code, why is the first handshake SSLv2, not TLSv1 or SSLv3?

How to use TLSV1 or SSLV3 for first handshake in Java?

String host = "www.google.com";
String url = "/adsense/?sourceid=aso&subid=ZH_CN-ET-AS-ADSBY6&medium=link&hl=zh_CN";
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(new KeyManager[0], new TrustManager[] {new DefaultTrustManager()}, new SecureRandom());
SSLContext.setDefault(ctx);
SSLSocketFactory factory = ctx.getSocketFactory();
Socket socket = factory.createSocket(host, 443);
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out.write("GET " + url + " HTTP/1.0");
out.flush();
out.close();
in.close();
like image 864
Steve Avatar asked Jun 17 '11 08:06

Steve


2 Answers

For TLSv1: Starting the client jvm with

-Dhttps.protocols="TLSv1" 

or using

System.setProperty("https.protocols", "TLSv1");

solved the problem for me.

like image 89
shampoo Avatar answered Nov 19 '22 05:11

shampoo


Use SSLSocket:

SSLSocket socket = factory.createSocket(host, 443);
String[] newProtocols = {"TLSv1"};
socket.setEnabledProtocols(newProtocols);
like image 8
Steve Avatar answered Nov 19 '22 03:11

Steve