Skip to content

Instantly share code, notes, and snippets.

@Hillrunner2008
Last active September 19, 2019 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hillrunner2008/f88c2b10b278fb5e58ee43b9a249b55b to your computer and use it in GitHub Desktop.
Save Hillrunner2008/f88c2b10b278fb5e58ee43b9a249b55b to your computer and use it in GitHub Desktop.
BetterSSLPoke
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.net.ssl.SSLParameters;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
/**
* SSLPoke with hostname validation check for java 7+
*
* @author dcnorris
*/
public class BetterSSLPoke {
public static void main(String[] args) {
if (args.length != 2) {
System.err.println("Usage: ");
System.err.println(" java " + BetterSSLPoke.class.getName() + " <host> <port>");
System.err.println("or for more debugging:");
System.err.println(" java -Djavax.net.debug=ssl " + BetterSSLPoke.class.getName() + " <host> <port>");
System.exit(1);
}
final String hostname = args[0];
final int port = Integer.parseInt(args[1]);
try {
SSLSocketFactory sSLSocketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
SSLSocket sslSocket = (SSLSocket) sSLSocketFactory.createSocket(hostname, port);
SSLParameters sslParams = new SSLParameters();
sslParams.setEndpointIdentificationAlgorithm("HTTPS");
sslSocket.setSSLParameters(sslParams);
InputStream inputStream = sslSocket.getInputStream();
OutputStream outputStream = sslSocket.getOutputStream();
outputStream.write(1);
while (inputStream.available() > 0) {
System.out.print(inputStream.read());
}
System.out.println("Successfully connected");
System.exit(0);
} catch (IOException ex) {
if (ex.getCause() != null) {
if (ex.getCause() instanceof java.security.cert.CertificateException) {
if (ex.getCause().getMessage().contains("No subject alternative DNS name matching") || ex.getCause().getMessage().contains("No name matching")) {
System.out.println("hostname validation failed");
}
}
ex.getCause().printStackTrace();
} else {
ex.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
System.exit(1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment