Apache HTTP Client javax.net.ssl.SSLPeerUnverified

2019-03-22 04:26发布

We are developing an application using tomcat and jersey.
Within this webapplication we need to connect to a https Website with a valid, not expired certificate. If I do connect to this website locally via my chrome browser, everything works fine! Unfortunately the tomcat server with our webapp throws an exception. We are using the Apache HttpClient (4.0) to connect to the https site:

javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated
at sun.security.ssl.SSLSessionImpl.getPeerCertificates(SSLSessionImpl.java:371)
at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:126)
at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:572)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:180)
at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:294)
at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:645)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:480)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:784)

The server certificate is absolutely valid and from thawte. Three different online tools validated the certificate successfully.
Openssl has an issue, too and showing me three certificates but throwing a simple error:

Verify return code: 20 (unable to get local issuer certificate)

The problem with openssl seems to be that it uses the wrong path /usr/lib/sslinstead of /etc/ssl/certs. If I use the CApath argument pointing to the proper path, openssl works fine so may this be an issue with the httpClient?

So our code for the default client is quite simple:

    client = new DefaultHttpClient();
    response = client.execute(url); //this throws the exception
    EntityUtils.consume(response.getEntity());

It's not an option to allow any certificates by implementing a custom TrustedManager! Futher I read, that some CA's are not part of the JDK/JRE and so it's certificates should be imported manually into the keystore or use a custom one, but thawte is a well known CA and shouldn't it work on default?

EDIT

I did set the javax.debug properties in catalina.sh so that I have further information about the problem:

http-bio-8080-exec-1, handling exception: javax.net.ssl.SSLHandshakeException: 
sun.security.validator.ValidatorException: PKIX path validation failed: 
java.security.cert.CertPathValidatorException: basic constraints check failed: 
pathLenConstraint violated - this cert must be the last cert in the certification path

I would appreciate any help! Thanks in advance!

2条回答
Bombasti
2楼-- · 2019-03-22 04:57

Okay, I got it working! Although thawte is a well known CA it seems that Java SSL did have some problems with it. After downloading the ssl Certificate via openssl:

echo |\
openssl s_client -connect ${REMHOST}:${REMPORT} 2>&1 |\
sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p'

and saving it into an pem file, I did the manual import into the java keystore:

keytool -import -alias myAlias -file theCert.pem -keystore lib/security/cacerts

I have no idea why java ssl was not able to validate the thawte certificate properly.

Listing the keystore showed me, that there are 7 thawte trusted certificates in the standard keystore but bizarrely it did not work until I manually imported the pem file

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-03-22 05:06

I am trying to understand your setup. You have a SSL certificate (issued by Thwate), installed in tomcat and you can access your site just fine over SSL using say IE or Firefox or Chrome.

But when you try to access it using HttpClient, you receive the above error ?

Is that correct ?

The error clearly indicates that your client does not trust the CA. But if the cert is signed by Thwate (and is installed correctly and is acessible via IE/Firefox etc), then it should work fine.

查看更多
登录 后发表回答