Skip to content

Instantly share code, notes, and snippets.

@amarula-git
Forked from 4ndrej/SSLPoke.java
Created September 14, 2017 00:42
Show Gist options
  • Select an option

  • Save amarula-git/99136c1d455d60d52eb7833b0ece4a6f to your computer and use it in GitHub Desktop.

Select an option

Save amarula-git/99136c1d455d60d52eb7833b0ece4a6f to your computer and use it in GitHub Desktop.
Test of java SSL / keystore / cert setup. Check the commet #1 for howto.
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import java.io.*;
/** Establish a SSL connection to a host and port, writes a byte and
* prints the response. See
* http://confluence.atlassian.com/display/JIRA/Connecting+to+SSL+services
*/
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 {
SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
SSLSocket sslsocket = (SSLSocket) sslsocketfactory.createSocket(args[0], Integer.parseInt(args[1]));
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();
}
}
}
@amarula-git
Copy link
Author

amarula-git commented Sep 14, 2017

  1. Extract certificate from remote server
    openssl s_client -servername HOSTNAME -connect HOSTNAME:443 </dev/null 2>/dev/null | openssl x509 -text > HOSTNAME.crt

  2. make a copy of your truststore
    cp $JAVA_HOME/jre/lib/security/cacerts ./cacerts

  3. Import the certifcate
    $JAVA_HOME/bin/keytool -import -trustcacerts -alias HOSTNAME -keystore ./aacerts -file HOSTNAME.crt
    (default password is changeit)

  4. compile the downloaded SSLPoke
    $JAVA_HOME/bin/javac SSLPoke.java

  5. Test
    $JAVA_HOME/bin/java -Djavax.net.ssl.trustStore=./cacerts SSLPoke HOSTNAME 443

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