Last active
September 19, 2019 15:54
-
-
Save Hillrunner2008/f88c2b10b278fb5e58ee43b9a249b55b to your computer and use it in GitHub Desktop.
BetterSSLPoke
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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