package testssl;
import java.io.IOException;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
/*
* Test establishment of an SSL handshake multiple ways: jdk default, TLSv1, TLSv1.1, and TLSv1.2
*/
public class SSLSocketClient {
public static void main(String[] args) {
if (args.length < 2) {
printSyntax();
System.exit(1);
}
int port = 0;
try {
port = Integer.parseInt(args[1]);
} catch (NumberFormatException e) {
System.out.println("Invalid port number: " + args[1]);
printSyntax();
System.exit(1);
}
String[] tlsList = new String[]{
"",
"TLSv1",
"TLSv1.1",
"TLSv1.2",
"TLSv1.3"};
for (String tls : tlsList) {
doHandshake(args[0], port, tls);
}
}
private static void printSyntax() {
System.out.println("Attempts an SSL Handshake to the provided host and port");
System.out.println("Syntax: SSLSocketClient host port");
System.out.println(" sample: SSLSocketClient google.com 443");
System.out.println(" sample: SSLSocketClient p4poke 1667");
}
public static void doHandshake(String host, int port, String tls) {
SSLSocket socket = null;
System.out.println("------------");
System.out.println("Handshake to " + host + " port " + port);
try {
SSLSocketFactory factory;
factory = TrustAllTrustManager.getSSLSocketFactory();
socket = (SSLSocket)factory.createSocket(host, port);
if (tls.length() > 0) {
socket.setEnabledProtocols(new String[]{tls});
System.out.println("Setting protocol to " + tls);
} else {
System.out.println("Not setting protocol: using JVM default");
}
String[] enabledProtocols = socket.getEnabledProtocols();
System.out.println(" Supported Protocol(s): " + String.join(",", enabledProtocols));
socket.startHandshake();
String protocol = socket.getSession().getProtocol();
System.out.println(" --> success: " + "Handshake to " + host + " port " + port + "; using " + protocol);
} catch (IOException e) {
e.printStackTrace(System.out);
System.out.println(" --> fail: " + "Handshake to " + host + " port " + port);
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
// don't care.
}
}
}
System.out.println("-----------------");
}
}
# |
Change |
User |
Description |
Committed |
|
#1
|
28029 |
Joel Brown |
New: for testing SSL handshakes to p4d enabled servers. |
|
|