Unable to Connect to SSL Services due to PKIX Path Building Failed sun.security.provider.certpath.SunCertPathBuilderException
Problem
Attempting to access URLs that are encrypted with SSL (for example HTTPS, LDAPS, IMAPS) throws an exception, below program will by pass the certificate and access the https data on the url.
Problem
Attempting to access URLs that are encrypted with SSL (for example HTTPS, LDAPS, IMAPS) throws an exception, below program will by pass the certificate and access the https data on the url.
package test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.commons.codec.binary.Base64;
public class HCSLDAPData {
private final String baseUrl;
private final String username;
private final String password;
private String hostName;
public HCSLDAPData(String baseUrl, String username, String password) {
this.baseUrl = baseUrl;
this.username = username;
this.password = password;
this.hostName = null;
}
public static void main(String args[]) {
String https_url = "https://198.18.133.254/api/v0/relation/HcsLdapServerREL/?policy_name=HcsLdapServerRelFDP&template_name=HcsLdapServerRelCFT&count=false&hierarchy=551cbc26acba0a1691197134&format=json";
new HCSLDAPData(https_url,"DCloudSPAdmin@dcloud.cisco.com", "C1sc0123").getHCSLdapData();
}
private TrustManager[ ] getTrustManager() {
TrustManager[ ] certs = new TrustManager[ ] {
new X509TrustManager() {
public X509Certificate[ ] getAcceptedIssuers() { return null; }
public void checkClientTrusted(X509Certificate[ ] certs, String t) { }
public void checkServerTrusted(X509Certificate[ ] certs, String t) { }
}
};
return certs;
}
private String getHCSLdapData() {
String https_url = this.baseUrl;
URL url;
try {
// Create a context that doesn't check certificates.
SSLContext ssl_ctx = SSLContext.getInstance("TLS");
TrustManager[ ] trust_mgr = getTrustManager();
ssl_ctx.init(null, // key manager
trust_mgr, // trust manager
new SecureRandom()); // random number generator
HttpsURLConnection.setDefaultSSLSocketFactory(ssl_ctx.getSocketFactory());
url = new URL(https_url);
this.hostName = url.getHost();
System.out.println("Host Name: " + url.getHost());
HttpsURLConnection con = (HttpsURLConnection)url.openConnection();
String authString = username + ":" + password;
String authStringEnc = new String(Base64.encodeBase64(authString.getBytes()));
con.setRequestProperty("Authorization", "Basic " + authStringEnc);
// Guard against "bad hostname" errors during handshake.
con.setHostnameVerifier(new HostnameVerifier() {
public boolean verify(String host, SSLSession sess) {
if(hostName != null) {
if (host.equals(hostName))
return true;
else
return false;
} else {
return false;
}
}
});
//dumpl all cert info
displayHttpsConnectionSettings(con);
//dump all the content
return getCustoerLDAPData(con);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}catch (KeyManagementException e) {
e.printStackTrace();
}
return null;
}
private void displayHttpsConnectionSettings(HttpsURLConnection con){
System.out.println("****** URL Specifications ********");
if(con!=null) {
try {
System.out.println("Response Code : " + con.getResponseCode());
System.out.println("Cipher Suite : " + con.getCipherSuite());
System.out.println("\n");
Certificate[] certs = con.getServerCertificates();
for(Certificate cert : certs){
System.out.println("Cert Type : " + cert.getType());
System.out.println("Cert Hash Code : " + cert.hashCode());
System.out.println("Cert Public Key Algorithm : " + cert.getPublicKey().getAlgorithm());
System.out.println("Cert Public Key Format : " + cert.getPublicKey().getFormat());
System.out.println("\n");
}
} catch (SSLPeerUnverifiedException e) {
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
}
System.out.println("****** URL Specifications ********");
}
private String getCustoerLDAPData(HttpsURLConnection con){
String ldapContent = null;
if(con!=null){
try {
System.out.println("****** Content of the URL ********");
BufferedReader br =
new BufferedReader(
new InputStreamReader(con.getInputStream()));
ldapContent = br.toString();
String input;
while ((input = br.readLine()) != null){
System.out.println(input);
}
System.out.println("****** Content of the URL ********");
br.close();
return ldapContent;
} catch (IOException e) {
e.printStackTrace();
}
}
return ldapContent;
}
}
No comments:
Post a Comment