I try to make an https connection via java build-in functionality for that (HttpURLConnection
). But I get this exception:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:174)
...
...
My Certificate chain is:
Root certificate -> Intermediate certificate -> Web server certificate
The used certificate is correct by the meaning of "Path discovery". The trust anchor is the Root certificate, which is imported in java's keystore on my system. The intermediate certificate is not... BUT
- Intermediate certificate is signed by the root, who I trust - so I trust on the intermediate too.
- Web server certificate is signed with Intermediate certificate, which I trust (point 1)
So the validation has to pass successfully? Do I getting something wrong?
Somewhere i read this:
Browsers can do auto-discovery, server to server doesn't.
but absence of this functionality is very basic. Are there clear way of doing this auto-discovery?
** Update
Yes, this was the issue, GPI. I was confused, because browsers could validate the server certificate but java application could not. The cause of that behaviour was:
- the server sends only final certificate, not the whole certificate chain;
- the certificate was recently bought and it was signed with relatively new intermediate certificate;
- the browsers have relatively up-to-date list of certificates including the intermediate certificate;
- java have relative not up-to-date list of certificates, and intermediate certificate was not inside.
- browsers validate the final certificate over the intermediate certificate java could not check the certificate chain because: 1. the chain was not sent; 2. the signer of final certificate (the intermediate one) was not a trust anchor.
The solutions could be:
- server to return the whole certificate chain
- the intermediate certificate to be added in java trust store