Ik probeer een verbinding tot stand te krijgen met een webservice die zijn eigen server certificaten heeft.
Het server certificaat heb ik na registratie ontvangen en kunnen importeren in mijn browser, als ik nu via mijn browser naar de url van de webservice ga dan krijg ik alles correct te zien.
Het certificaat heb ik vervolgens geconverteerd naar een ander formaat, zodat ik het via de code kan importeren in de keystore.
Echter nu ik het certificaat i.c.m. mijn java code wil gebruiken om een verbinding op te zetten krijg ik de volgende foutmelding:
main, SEND TLSv1 ALERT: fatal, description = certificate_unknown
main, WRITE: TLSv1 Alert, length = 2
main, called closeSocket()
javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated
main, handling 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
main, IOException in getSession(): 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
De volgende libraries worden gebruikt:
HttpClient 4.1.1
HttpCore 4.1
CommonsIO 2.0
Voor de zekerheid zet ik deze parameter in het begin:
Voor de connectie gebruik ik de volgende code:
En voor de CustomKeyStore het volgende:
Iemand een idee?
Het server certificaat heb ik na registratie ontvangen en kunnen importeren in mijn browser, als ik nu via mijn browser naar de url van de webservice ga dan krijg ik alles correct te zien.
Het certificaat heb ik vervolgens geconverteerd naar een ander formaat, zodat ik het via de code kan importeren in de keystore.
openssl pkcs12 -export -out certificate.p12 -inkey server.key -in uzi.pem
Echter nu ik het certificaat i.c.m. mijn java code wil gebruiken om een verbinding op te zetten krijg ik de volgende foutmelding:
main, SEND TLSv1 ALERT: fatal, description = certificate_unknown
main, WRITE: TLSv1 Alert, length = 2
main, called closeSocket()
javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated
main, handling 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
main, IOException in getSession(): 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
De volgende libraries worden gebruikt:
HttpClient 4.1.1
HttpCore 4.1
CommonsIO 2.0
Voor de zekerheid zet ik deze parameter in het begin:
Java:
1
| System.setProperty("sun.security.ssl.allowUnsafeRenegotiation", "true"); |
Voor de connectie gebruik ik de volgende code:
Java:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
| DefaultHttpClient httpclient = new DefaultHttpClient(); try { // File file = new File("...certificate.p12"); // load the key KeyStore trustStore = new CustomKeyStore(file, pass).getInstance(); // get the socket SSLSocketFactory socketFactory = new SSLSocketFactory("TLS", trustStore, new String(pass), null, new SecureRandom(), new TrustSelfSignedStrategy(), new AllowAllHostnameVerifier()); // create the scheme Scheme sch = new Scheme("https", 443, socketFactory); // register the scheme httpclient.getConnectionManager().getSchemeRegistry().register(sch); // set the GET/POST HttpGet http = new HttpGet(url); // get the reponse HttpResponse httpResponse = httpclient.execute(http); HttpEntity entity = httpResponse.getEntity(); // get the input stream, a copy as String response = IOUtils.toString(entity.getContent()); // clean up EntityUtils.consume(entity); } catch (Exception e) { e.printStackTrace(); } finally { // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); } |
En voor de CustomKeyStore het volgende:
Java:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
| // load the key KeyStore trustStore = KeyStore.getInstance("PKCS12"); FileInputStream fis = null; try { // get the file fis = FileUtils.openInputStream(file); // load trustStore.load(fis, pass); Enumeration<String> aliasEnum = trustStore.aliases(); while (aliasEnum.hasMoreElements()) { String alias = aliasEnum.nextElement(); System.out.println(alias); } } catch (Exception e) { e.printStackTrace(); } finally { IOUtils.closeQuietly(fis); } return trustStore; |
Iemand een idee?