Skip to content

Instantly share code, notes, and snippets.

@powellnathanj
Forked from 4ndrej/SSLPoke.java
Last active August 26, 2022 20:07
Show Gist options
  • Save powellnathanj/3cf20be23f20a5961b80e48aae6c0ceb to your computer and use it in GitHub Desktop.
Save powellnathanj/3cf20be23f20a5961b80e48aae6c0ceb to your computer and use it in GitHub Desktop.
Test of java SSL / keystore / cert setup. Check the comment #1 for howto.
import javax.net.ssl.SSLParameters;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import java.io.*;
public class SSLPoke {
public static void main(String[] args) {
if (args.length != 2) {
System.out.println("Usage: "+SSLPoke.class.getName()+" <host> <port>");
System.exit(1);
}
try {
System.setProperty("javax.net.ssl.trustStore", "/etc/pki/java/cacerts");
SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
SSLSocket sslsocket = (SSLSocket) sslsocketfactory.createSocket(args[0], Integer.parseInt(args[1]));
SSLParameters sslparams = new SSLParameters();
sslparams.setEndpointIdentificationAlgorithm("HTTPS");
sslparams.setProtocols(new String[] { "TLSv1.2" });
//sslparams.setCipherSuites(new String[] { "TLS_DHE_RSA_WITH_AES_128_CBC_SHA" });
sslsocket.setSSLParameters(sslparams);
InputStream in = sslsocket.getInputStream();
OutputStream out = sslsocket.getOutputStream();
// Write a test byte to get a reaction :)
out.write(1);
while (in.available() > 0) {
System.out.print(in.read());
}
System.out.println("Successfully connected");
} catch (Exception exception) {
exception.printStackTrace();
System.exit(1);
}
}
}
@powellnathanj
Copy link
Author

Added a line to test a specific keystore

@powellnathanj
Copy link
Author

Added params to set tls version and cipher suite(s)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment