I'm writing a desktop app which needs to download a few config files from my HTTPS only server which runs a valid Let's Encrypt certificate which is trusted in Chrome and Firefox, and Java 8. I want the app to be as compatible as possible so I am targeting Java 7 as a minimum. In Java 7 the app cannot connect with the error Caused by: 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
I've tried many solutions and this appears to be the closest to my problem:
"PKIX path building failed" despite valid Verisign certificate
Unfortunately nothing appears wrong with my server and https://www.ssllabs.com/ssltest/analyze.html?d=baldeonline.comshows that Java 7 SHOULD connect.
How would I use a different (or system) certificate store programmatically? Obviously it's not user friendly if the user has to dig around in their java installation folder so I'd want to make any changes with the program itself.
The function which raises the error:
try { URL obj = new URL(urlPointer); HttpsURLConnection con = (HttpsURLConnection) obj.openConnection(); SSLContext sslContext = SSLContext.getInstance("TLSv1.2");//I have also tries TLSv1 but no difference sslContext.init(null, null, new SecureRandom()); con.setSSLSocketFactory(sslContext.getSocketFactory()); con.setRequestMethod("GET"); con.setRequestProperty("User-Agent", USER_AGENT); int responseCode = 0; try { responseCode = con.getResponseCode(); } catch (IOException e) { } System.out.println("POST Response Code : "+ responseCode); if (responseCode >= 400) { BufferedReader in = new BufferedReader(new InputStreamReader( con.getErrorStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); return response.toString(); } else { BufferedReader in = new BufferedReader(new InputStreamReader( con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); return response.toString(); } } catch (IOException e) { e.printStackTrace(); return ""; } catch (NoSuchAlgorithmException e1) { e1.printStackTrace(); return ""; } catch (KeyManagementException e1) { e1.printStackTrace(); return ""; } }```