Skip to content

Commit 0e69cd5

Browse files
svkiransjzheaux
authored andcommitted
Changes to enable TLSv1.2 on Java 7
Issue: BAEL-2500
1 parent 0474389 commit 0e69cd5

1 file changed

Lines changed: 115 additions & 0 deletions

File tree

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package com.baeldung.ssl;
2+
3+
import java.io.BufferedReader;
4+
import java.io.BufferedWriter;
5+
import java.io.IOException;
6+
import java.io.InputStreamReader;
7+
import java.io.OutputStreamWriter;
8+
import java.io.PrintWriter;
9+
import java.net.URL;
10+
import java.net.UnknownHostException;
11+
import java.security.KeyManagementException;
12+
import java.security.NoSuchAlgorithmException;
13+
import java.security.SecureRandom;
14+
15+
import javax.net.ssl.HttpsURLConnection;
16+
import javax.net.ssl.SSLContext;
17+
import javax.net.ssl.SSLParameters;
18+
import javax.net.ssl.SSLSocket;
19+
import javax.net.ssl.SSLSocketFactory;
20+
21+
import org.slf4j.Logger;
22+
import org.slf4j.LoggerFactory;
23+
24+
public class EnableTLSv12 {
25+
26+
private final Logger logger = LoggerFactory.getLogger(EnableTLSv12.class);
27+
28+
public String url = "";
29+
public Integer port = null;
30+
31+
public EnableTLSv12() {
32+
}
33+
34+
public static void main(String[] args) throws IOException, KeyManagementException, NoSuchAlgorithmException {
35+
EnableTLSv12 enableTLSv12 = new EnableTLSv12();
36+
if (args.length != 2) {
37+
System.out.println("Provide the server url and the secure port:");
38+
System.exit(-1);
39+
}
40+
enableTLSv12.setHost(args);
41+
enableTLSv12.setPort(args);
42+
enableTLSv12.enableTLSv12UsingHttpConnection();
43+
enableTLSv12.enableTLSv12UsingProtocol();
44+
enableTLSv12.enableTLSv12UsingSSLContext();
45+
enableTLSv12.enableTLSv12UsingSSLParameters();
46+
}
47+
48+
private void setPort(String[] args) {
49+
url = args[0];
50+
}
51+
52+
private void setHost(String[] args) {
53+
String portNumber = args[1];
54+
port = Integer.parseInt(portNumber);
55+
}
56+
57+
private void handleCommunication(SSLSocket socket, String usedTLSProcess) throws IOException {
58+
logger.debug("Enabled TLS v1.2 on " + usedTLSProcess);
59+
try (PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()))); BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {
60+
out.println("GET / HTTP/1.0");
61+
out.println();
62+
out.flush();
63+
if (out.checkError()) {
64+
logger.error("SSLSocketClient: java.io.PrintWriter error");
65+
return;
66+
}
67+
68+
String inputLine;
69+
while ((inputLine = in.readLine()) != null)
70+
logger.info(inputLine);
71+
}
72+
}
73+
74+
public void enableTLSv12UsingSSLParameters() throws UnknownHostException, IOException {
75+
SSLSocketFactory socketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
76+
SSLSocket sslSocket = (SSLSocket) socketFactory.createSocket(url.trim(), port);
77+
SSLParameters params = new SSLParameters();
78+
params.setProtocols(new String[] { "TLSv1.2" });
79+
sslSocket.setSSLParameters(params);
80+
sslSocket.startHandshake();
81+
handleCommunication(sslSocket, "SSLSocketFactory-SSLParameters");
82+
}
83+
84+
public void enableTLSv12UsingProtocol() throws IOException {
85+
SSLSocketFactory socketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
86+
SSLSocket sslSocket = (SSLSocket) socketFactory.createSocket(url, port);
87+
sslSocket.setEnabledProtocols(new String[] { "TLSv1.2" });
88+
sslSocket.startHandshake();
89+
handleCommunication(sslSocket, "SSLSocketFactory-EnabledProtocols");
90+
}
91+
92+
public void enableTLSv12UsingHttpConnection() throws IOException, NoSuchAlgorithmException, KeyManagementException {
93+
URL urls = new URL("https://" + url + ":" + port);
94+
SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
95+
sslContext.init(null, null, new SecureRandom());
96+
HttpsURLConnection connection = (HttpsURLConnection) urls.openConnection();
97+
connection.setSSLSocketFactory(sslContext.getSocketFactory());
98+
try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
99+
String input;
100+
while ((input = br.readLine()) != null) {
101+
logger.info(input);
102+
}
103+
}
104+
logger.debug("Created TLSv1.2 connection on HttpsURLConnection");
105+
}
106+
107+
public void enableTLSv12UsingSSLContext() throws NoSuchAlgorithmException, KeyManagementException, UnknownHostException, IOException {
108+
SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
109+
sslContext.init(null, null, new SecureRandom());
110+
SSLSocketFactory socketFactory = sslContext.getSocketFactory();
111+
SSLSocket socket = (SSLSocket) socketFactory.createSocket(url, port);
112+
handleCommunication(socket, "SSLContext");
113+
}
114+
115+
}

0 commit comments

Comments
 (0)