I'm having problems interacting with an HTTPS site via Java. My program uses one URL with an untrusted certificate each time the program runs. This program has to run on more than one system. Currently, I have the following:
public class A{
HostnameVerifier hv = new HostnameVerifier(){
public boolean verify(String urlHostName, SSLSession session){
return true;
}
};
HttpsURLConnection.setDefaultHostnameVerifier(hv);
javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[1];
javax.net.ssl.TrustManager tm = new miTM();
trustAllCerts[0] = tm;
javax.net.ssl.SSLContext sc = null;
try {
sc = javax.net.ssl.SSLContext.getInstance("SSL");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
try {
sc.init(null, trustAllCerts, null);
} catch (KeyManagementException e) {
e.printStackTrace();
}
javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
}
class miTM implements javax.net.ssl.TrustManager, javax.net.ssl.X509TrustManager{
public java.security.cert.X509Certificate[] getAcceptedIssuers(){
return null;
}
public boolean isServerTrusted(java.security.cert.X509Certificate[] certs){
return true;
}
public boolean isClientTrusted(java.security.cert.X509Certificate[] certs){
return true;
}
public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) throws java.security.cert.CertificateException{
return;
}
public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) throws java.security.cert.CertificateException{
return;
}
}
With this code, I can perform the following just fine:
URL url = new URL(urlString);
URLConnection cnx = url.openConnection();
cnx.connect();
InputStream ins = cnx.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(ins));
String curline;
while( (curline = in.readLine()) != null ) {
System.out.println(curline);
}
However, I cannot do the following:
httpClient = new HttpClient();
PostMethod postMethod = null;
int intResult = 0;
postMethod = new PostMethod(authURL);
Enumeration emParams = authParams.propertyNames();
while (emParams.hasMoreElements()) {
String paramName = (String) emParams.nextElement();
String paramValue = authParams.getProperty(paramName);
postMethod.addParameter(paramName, paramValue);
}
intResult = httpClient.executeMethod(postMethod);
postMethod.releaseConnection();
ins.close();
When executeMethod(postMethod) is executed, I get an SSLHandshakeException, CertPathBuilderException, and so on.
What can I do to remedy this? I'm thinking of either accepting the certificate or just bypassing all certificate validation (as the program runs internally within a private network).
Thanks