I am having problems interacting with an HTTPS site through Java. My program uses one URL with an untrusted certificate every time the program starts. This program should work on several systems. I currently 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 do the following simply:
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, etc.
What can I do to fix this? I am thinking about accepting a certificate or just bypassing the entire certificate verification (since the program runs inside a private network).
thanks
source share