|
import java.io.InputStreamReader; |
|
import java.io.Reader; |
|
import java.io.BufferedReader; |
|
import java.io.InputStream; |
|
import java.net.URL; |
|
import java.net.URLConnection; |
|
|
|
import javax.net.ssl.HostnameVerifier; |
|
import javax.net.ssl.HttpsURLConnection; |
|
import javax.net.ssl.SSLContext; |
|
import javax.net.ssl.SSLSession; |
|
import javax.net.ssl.SSLSocketFactory; |
|
import javax.net.ssl.TrustManager; |
|
import javax.net.ssl.TrustManagerFactory; |
|
import javax.net.ssl.X509TrustManager; |
|
|
|
import java.security.KeyStore; |
|
import java.security.cert.X509Certificate; |
|
|
|
public class TestSkipValidation |
|
{ |
|
private static SSLSocketFactory default_factory = null; |
|
|
|
private static HostnameVerifier default_hostname_verifier; |
|
|
|
private static class DummyTrustManager implements X509TrustManager |
|
{ |
|
public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; } |
|
public void checkClientTrusted(X509Certificate[] certs, String authType) {} |
|
public void checkServerTrusted(X509Certificate[] certs, String authType) {} |
|
} |
|
|
|
private static class DummyHostnameVerifier implements HostnameVerifier |
|
{ |
|
public boolean verify(String hostname, SSLSession session) { |
|
return true; |
|
} |
|
} |
|
|
|
public static void main(String[] args) throws Exception |
|
{ |
|
String request_url; |
|
if (args.length > 0) { |
|
request_url = args[0]; |
|
} else { |
|
System.out.println("Usage: java TestSkipValidation <https-url>"); |
|
return; |
|
} |
|
System.out.println("Testing on: "+request_url); |
|
|
|
default_factory = HttpsURLConnection.getDefaultSSLSocketFactory(); |
|
default_hostname_verifier = HttpsURLConnection.getDefaultHostnameVerifier(); |
|
|
|
System.out.println(" --- Req #1 (without validation) ---"); |
|
requestWithoutValidation(request_url); |
|
|
|
System.out.println(" --- Req #2 (with validation) ---"); |
|
requestWithValidation(request_url); |
|
|
|
return; |
|
} |
|
|
|
public static void requestWithValidation(String request_url) throws Exception |
|
{ |
|
// Restore default factories |
|
HttpsURLConnection.setDefaultSSLSocketFactory(default_factory); |
|
HttpsURLConnection.setDefaultHostnameVerifier(default_hostname_verifier); |
|
|
|
URL url = new URL(request_url); |
|
URLConnection con = url.openConnection(); |
|
|
|
consumeInput(con); |
|
} |
|
|
|
public static void requestWithoutValidation(String request_url) throws Exception |
|
{ |
|
// Create a dummy all=trusting trust manager |
|
TrustManager[] dummy_manager = new TrustManager[] { |
|
new DummyTrustManager() |
|
}; |
|
|
|
// Install the all-trusting trust manager |
|
SSLContext ssl_context = SSLContext.getInstance("SSL"); |
|
ssl_context.init(null, dummy_manager, new java.security.SecureRandom()); |
|
HttpsURLConnection.setDefaultSSLSocketFactory(ssl_context.getSocketFactory()); |
|
|
|
// Create and install all-trusting host name verifier |
|
HostnameVerifier dummy_hostname_verifier = new DummyHostnameVerifier(); |
|
HttpsURLConnection.setDefaultHostnameVerifier(dummy_hostname_verifier); |
|
|
|
URL url = new URL(request_url); |
|
URLConnection con = url.openConnection(); |
|
|
|
consumeInput(con); |
|
} |
|
|
|
private static void consumeInput(URLConnection con) throws Exception |
|
{ |
|
InputStream istr = con.getInputStream(); |
|
BufferedReader inp1 = new BufferedReader(new InputStreamReader(istr)); |
|
String line; |
|
System.out.println(" -- Start Response (10 lines) --"); |
|
int nl = 0; |
|
while ((nl < 10) && (line = inp1.readLine()) != null) { |
|
System.out.println(line); |
|
nl++; |
|
} |
|
System.out.println(" -- End Response --"); |
|
inp1.close(); |
|
} |
|
|
|
} |