0

I am using Apache HttpClient(4.0) to communicate with an HTTP server(SSL configured).

The server is using a self-signed certificate. Ideally I would love to have HttpClient library behave the same way as browsers, but after doing some google I understand that I will have to import the certificate to JRE keystore(cacert). Inspite of installing the certificate in JRE keystore, I am stil getting "SSLHandshake exception".

Here is a SSL log, I am not an expert in this area, so need help to analyse the contents of the log.

trigger seeding of SecureRandom
done seeding SecureRandom
log4j:WARN No appenders could be found for logger (org.apache.http.impl.conn.SingleClientConnManager).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
main, setSoTimeout(0) called
main, setSoTimeout(0) called
%% No cached client session
*** ClientHello, TLSv1
RandomCookie:  GMT: 1328639128 bytes = { 1, 30, 97, 51, 147, 14, 89, 41, 43, 120, 56,    192, 4, 5, 113, 203, 165, 199, 153, 199, 137, 199, 98, 242, 166, 81, 208, 129 }
Session ID:  {}
Cipher Suites: [SSL_RSA_WITH_RC4_128_MD5, SSL_RSA_WITH_RC4_128_SHA,   TLS_RSA_WITH_AES_128_CBC_SHA, TLS_ECDH_ECDSA_WITH_RC4_128_SHA, TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA, TLS_ECDH_RSA_WITH_RC4_128_SHA, TLS_ECDH_RSA_WITH_AES_128_CBC_SHA, TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, TLS_ECDHE_RSA_WITH_RC4_128_SHA, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, TLS_DHE_RSA_WITH_AES_128_CBC_SHA, TLS_DHE_DSS_WITH_AES_128_CBC_SHA, SSL_RSA_WITH_3DES_EDE_CBC_SHA, TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA, TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA, TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA, TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA, SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA, SSL_RSA_WITH_DES_CBC_SHA, SSL_DHE_RSA_WITH_DES_CBC_SHA, SSL_DHE_DSS_WITH_DES_CBC_SHA, SSL_RSA_EXPORT_WITH_RC4_40_MD5, SSL_RSA_EXPORT_WITH_DES40_CBC_SHA, SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA, SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA]
Compression Methods:  { 0 }
Extension elliptic_curves, curve names: {secp256r1, sect163k1, sect163r2, secp192r1,  secp224r1, sect233k1, sect233r1, sect283k1, sect283r1, secp384r1, sect409k1, sect409r1, secp521r1, sect571k1, sect571r1, secp160k1, secp160r1, secp160r2, sect163r1, secp192k1, sect193r1, sect193r2, secp224k1, sect239k1, secp256k1}
Extension ec_point_formats, formats: [uncompressed]
***
main, WRITE: TLSv1 Handshake, length = 161
main, WRITE: SSLv2 client hello message, length = 146
main, READ: TLSv1 Alert, length = 2
main, RECV TLSv1 ALERT:  fatal, handshake_failure
main, called closeSocket()
main, handling exception: javax.net.ssl.SSLHandshakeException: Received fatal alert:   handshake_failure
main, called close()
main, called closeInternal(true)
main, called close()
main, called closeInternal(true)
main, called close()
main, called closeInternal(true)

Here is the code snippet I am using.

DefaultHttpClient httpClient = new DefaultHttpClient();

X509HostnameVerifier verifier = new X509HostnameVerifier() {
    @Override
    public boolean verify(String hostname, SSLSession session) {
        return true;
    }

    @Override
    public void verify(String arg0, String[] arg1, String[] arg2) throws SSLException {
    }

    @Override
    public void verify(String arg0, X509Certificate arg1) throws SSLException {
    }

    @Override
    public void verify(String arg0, SSLSocket arg1) throws IOException {
    }
};

private static class DefaultTrustManager implements X509TrustManager {

    @Override
    public void checkClientTrusted(java.security.cert.X509Certificate[] arg0, String arg1) throws java.security.cert.CertificateException {
    }

    @Override
    public void checkServerTrusted(java.security.cert.X509Certificate[] arg0, String arg1) throws java.security.cert.CertificateException {
    }

    @Override
    public java.security.cert.X509Certificate[] getAcceptedIssuers() {
        return null;
    }

}

SSLContext sslContext = SSLContext.getInstance("TLSv1");
sslContext.init(null, new TrustManager[] {new DefaultTrustManager()}, new SecureRandom());
SSLContext.setDefault(sslContext);

SSLSocketFactory sf = new SSLSocketFactory(sslContext,verifier);
ClientConnectionManager ccm = httpClient.getConnectionManager();
SchemeRegistry sr = ccm.getSchemeRegistry();
sr.register(new Scheme("https", sf, 9002));

HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
HttpParams params = new BasicHttpParams();
httpClient = new DefaultHttpClient(ccm, params);
Mandar K
  • 333
  • 4
  • 15
  • Why not checking this well documented question? http://stackoverflow.com/questions/2159586/jsse-handshake-failure-on-public-https-web-site – Alfabravo Feb 08 '12 at 05:54
  • Or this one: http://stackoverflow.com/questions/2642777/trusting-all-certificates-using-httpclient-over-https – DagR Feb 08 '12 at 06:25

1 Answers1

-1

not a lot of useful info in the log. however why don't you try to use a different SSL algorithm like say "SSLv2" instead of TLSv1 ?

dbrin
  • 15,525
  • 4
  • 56
  • 83