Skip to content

Instantly share code, notes, and snippets.

@luketn
Created November 9, 2020 01:42
Show Gist options
  • Select an option

  • Save luketn/4e7595cf39dab63fbcfdb62930fe8f4d to your computer and use it in GitHub Desktop.

Select an option

Save luketn/4e7595cf39dab63fbcfdb62930fe8f4d to your computer and use it in GitHub Desktop.
This code makes an SSL/TLS connection to a domain and writes the results to a file. Used to check issues with older TLS / SNI protocols by passing flags to java to control these settings (see class comments).
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import java.io.*;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
/**
* Must run with the Java options:
* -Djsse.enableSNIExtension=false -Djdk.tls.client.protocols=TLSv1 -Dhttps.protocols=TLSv1
* <p>
* And the hostname to test as an argument: domain.com
*/
public class TLSv1Test {
public static void main(String[] args) throws IOException {
String hostname = args[0];
InetSocketAddress address = new InetSocketAddress(hostname, 443);
try (FileOutputStream fileOutputStream = new FileOutputStream(new File(args[1]));
PrintStream output = new PrintStream(fileOutputStream)
) {
try {
SSLSocketFactory factory =
(SSLSocketFactory) SSLSocketFactory.getDefault();
logToOutput(output, "Connecting to " + hostname + " (" + address.getAddress() + ")...");
SSLSocket socket = (SSLSocket) factory.createSocket(address.getAddress(), 443);
socket.addHandshakeCompletedListener(event -> {
logToOutput(output, "SSL Connection Established:");
logToOutput(output, "Host: " + hostname);
logToOutput(output, "Cipher: " + event.getCipherSuite());
logToOutput(output, "Protocol: " + event.getSession().getProtocol());
logToOutput(output, "Peer Host: " + event.getSession().getPeerHost());
logToOutput(output, "");
});
socket.startHandshake();
PrintWriter out = new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
socket.getOutputStream())));
out.println("GET / HTTP/1.0");
out.println("Host: " + hostname);
out.println("User-Agent: java");
out.println("Accept: */*");
out.println();
out.flush();
/*
* Make sure there were no surprises
*/
if (out.checkError())
logToOutput(output, "SSLSocketClient: java.io.PrintWriter error");
/* read response */
BufferedReader in = new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
logToOutput(output, inputLine);
in.close();
out.close();
socket.close();
System.out.println("Success - "+ address.getAddress());
} catch (Exception e) {
System.out.println("Failed - "+ address.getAddress());
logToOutput(output, e);
}
}
}
private static void logToOutput(PrintStream output, String log) {
try {
writeLogTime(output);
output.write(log.getBytes(StandardCharsets.UTF_8));
writeNewline(output);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static void logToOutput(PrintStream output, Exception e) {
try {
writeLogTime(output);
output.write(("Exception occurred: " + e.getMessage()).getBytes(StandardCharsets.UTF_8));
writeNewline(output);
e.printStackTrace(output);
writeNewline(output);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
private static void writeLogTime(PrintStream output) throws IOException {
ZonedDateTime logTime = Instant.now().atZone(ZoneOffset.UTC);
output.write((logTime.toString() + ": ").getBytes(StandardCharsets.UTF_8));
}
private static void writeNewline(PrintStream output) {
output.write(10);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment