views:

82

answers:

2

I am trying to make a test SSL connection using the following Java code:

String httpsURL = "https://www.somehost.com";
URL myurl = new URL(httpsURL);
HttpsURLConnection con = (HttpsURLConnection)myurl.openConnection();

InputStream ins = con.getInputStream();
InputStreamReader isr=new InputStreamReader(ins);
BufferedReader in =new BufferedReader(isr);

String inputLine;

while ((inputLine = in.readLine()) != null)
    System.out.println(inputLine);

in.close();

When I connect to Host A everything works fine - the connection is made and the response is received.

However when I connect to Host B, which is secured by a certificate that is issued by the same authority as Host A's, I receive the following exception:

sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

Everything that I have read so far suggests that I need to install the certificates in my keystore, however if that were the solution then why does Host A work whilst Host B doesn't?

As a probably unhelpful aside - if I write a similar piece of C# code then the connection is successfully negotiated for both Hosts A and B - the same applies for navigating to the URL in the browser.

A: 

Can you explain what is "host A" and "host B" ? are they different URLs? You stated

the same applies for navigating to the URL in the browser

Did you ever add the certificate in your browser's trusted store? If so, your browser will never show an error.

ring bearer
Yes, they are different URLs - Host A: test-syd.company.com.au Host B: test-uk.company.co.ukI haven't added the certificates to the trust store. However, the intermediate CA, that issued both certificates, is in my certificate store and if I remove it it is re-added when I browse to either URL.
Big Fat Noodle
+1  A: 

Most likely causes are,

  1. The Host B uses a self-signed certificate.
  2. The certificate is signed by CA which is not in your trust store.
  3. The cert is signed with an intermediate cert but Host B is misconfigured so it doesn't send the server cert with intermediate cert.

For #1, #2, you need to import the cert or the CA cert into your trust store.

For #3, tell host B to send the intermediate cert.

ZZ Coder
nos
I am not sure that's a good advice to give ...
ZZ Coder
Host B's cert is signed with the same intermediate cert as Host A. So, option #3 is the most likely possibility.I don't have immediate access to Host B - is there anyway that I could prove this was the case in the meantime? I tried removing the intermediate cert from my browser's certificate store and then browsing to the url. The certificate is presented as valid and the intermediate cert appears in the certificate store again. Does this prove that the intermediate cert is sent from the server, or would/could the browser request it separately for elsewhere?
Big Fat Noodle