This repository has been archived by the owner on Dec 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 166
/
TlsCheck.java
64 lines (56 loc) · 1.94 KB
/
TlsCheck.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLParameters;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.UnknownHostException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
public class TlsCheck {
/**
* Tests whether this client can make an HTTP connection with TLS 1.2.
*
* @return true if connection is successful. false otherwise.
*/
public static boolean isSuccessfulTLS12Connection() {
try {
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, null, null);
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
URL url = new URL("https://tlstest.paypal.com");
HttpsURLConnection httpsConnection = (HttpsURLConnection) url.openConnection();
httpsConnection.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(httpsConnection.getInputStream()));
StringBuilder body = new StringBuilder();
while (reader.ready()) {
body.append(reader.readLine());
}
httpsConnection.disconnect();
if (body.toString().equals("PayPal_Connection_OK")) {
return true;
}
} catch (NoSuchAlgorithmException e) {
} catch (UnknownHostException e) {
} catch (IOException e) {
} catch (KeyManagementException e) {
}
return false;
}
public static void main(String[] args) {
try {
SSLParameters sslParams = SSLContext.getDefault().getSupportedSSLParameters();
String[] protocols = sslParams.getProtocols();
System.out.println("Supported protocol versions: " + Arrays.asList(protocols));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
if (isSuccessfulTLS12Connection()) {
System.out.println("Successfully connected to TLS 1.2 endpoint.");
} else {
System.out.println("Failed to connect to TLS 1.2 endpoint.");
}
}
}