0

I want to communicate with distant server and have got the following issue :

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

with the following code :

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.net.HttpURLConnection;

public class RequeteAuthentification {
    public static Reponse Execute(String siret, String nom, String prenom, String motdepasse) {
        Reponse reponse = new Reponse();
        
        try {
            
            URL urlAuthentification = new URL(Urls.getUrlAuthentification("test"));

            // xml
            String inData = "<identifiants><siret>" + siret + "</siret>";
            inData += "<nom>" + nom + "</nom>";
            inData += "<prenom>" + prenom + "</prenom>";
            inData += "<motdepasse>" + motdepasse + "</motdepasse>";
            inData += "<service>97</service></identifiants>";
        
            // creer la connexion
            HttpURLConnection connection = (HttpURLConnection) urlAuthentification.openConnection();

            // methode
            connection.setRequestMethod("POST");
            connection.setDoOutput(true);
            connection.setAllowUserInteraction(false);

            // headers
            connection.setRequestProperty("Content-Type", "application/xml");

            // data
            // error happens on the next line
            OutputStream outputStream = connection.getOutputStream();
            outputStream.write(inData.getBytes());

            // code http de retour
            reponse.setCode(String.valueOf(connection.getResponseCode()));
            System.out.println("Code retour : " + reponse.getCode());

            // r�sultat de la requ�te
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            
            reponse.setResultat(reader.readLine());
            reader.close();
        }
        catch (Exception e) {
            System.out.println("Exception: " + e);
            reponse.setErreur(e.toString());
        }
        return reponse;
    }
}

I have followed the answers on the related threads and I have taken the certificates with chrome with the URL I was trying to request :

C:\WINDOWS\system32>C:\\"Program Files"\Java\jdk1.8.0_271\bin\keytool  -noprompt -trustcacerts -import -file C:\Users\toto\Downloads\dsnrg.net-entreprises.fr.cer -alias dsnrg.net-entreprises.fr -keystore C:\\"Program Files"\Java\jdk1.8.0_271\jre\lib\security\cacerts -storepass changeit
Certificat ajouté au fichier de clés

C:\WINDOWS\system32>C:\\"Program Files"\Java\jdk1.8.0_271\bin\keytool  -noprompt -trustcacerts -import -file C:\Users\toto\Downloads\DigiCert.cer -alias dsnrg.net-entreprises.fr2 -keystore C:\\"Program Files"\Java\jdk1.8.0_271\jre\lib\security\cacerts -storepass changeit
Certificat ajouté au fichier de clés

C:\WINDOWS\system32>C:\\"Program Files"\Java\jdk1.8.0_271\bin\keytool  -noprompt -trustcacerts -import -file C:\Users\toto\Downloads\\"DigiCert Global Root CA".cer -alias dsnrg.net-entreprises.fr3 -keystore C:\\"Program Files"\Java\jdk1.8.0_271\jre\lib\security\cacerts -storepass changeit
Certificat ajouté au fichier de clés

and build run my project (on Idea Intellij) with the following VM options :

-Djavax.net.debug=all

In the debug logs I could see that the correct file cacerts was used by my program related to the JDK I was using and the certificates I have added was read.

But the exception I mentioned below is still thrown.

Any Idea ?

cladlink
  • 31
  • 4
  • Does the log show your trustmanager loading those certs _and_ the server sending (exactly) those certs in its Certificate message? – dave_thompson_085 Aug 18 '21 at 20:19
  • this problem is solved : the antivirus was bloking my certificates... (If it can help someone with the same issue). – cladlink Aug 26 '21 at 16:43

0 Answers0