I wanted to establish a HTTPS connection to this website particularly, https://elearning.utp.edu.my/
I have checked from SSL tool that the website used Entrust_L1K certificate, then I export the certificate file from Chrome browser.
I tried using the code provided by Android developer website.
try {
CertificateFactory cf = CertificateFactory.getInstance("X.509");
InputStream caInput = mContext.getResources().openRawResource(R.raw.entrust_l1k);
Certificate ca;
try {
ca = cf.generateCertificate(caInput);
System.out.println("ca = " + ((X509Certificate) ca).getSubjectDN());
} finally {
caInput.close();
}
// Create a KeyStore containing our trusted CAs
String keyStoreType = KeyStore.getDefaultType();
KeyStore keyStore = KeyStore.getInstance(keyStoreType);
keyStore.load(null, null);
keyStore.setCertificateEntry("ca", ca);
// Create a TrustManager that trusts the CAs in our KeyStore
String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
tmf.init(keyStore);
// Create an SSLContext that uses our TrustManager
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, tmf.getTrustManagers(), null);
HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
urlConnection.setConnectTimeout(7000);
urlConnection.setRequestMethod("GET");
urlConnection.setDoInput(true);
urlConnection.connect();
int responseCode = urlConnection.getResponseCode();
switch (responseCode) {
case HttpsURLConnection.HTTP_OK:
InputStream in = urlConnection.getInputStream();
Scanner scanner = new Scanner(in);
scanner.useDelimiter("\\A");
boolean hasInput = scanner.hasNext();
if (hasInput) {
return scanner.next();
} else {
return null;
}
default:
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
However I still get the following error code
W/System.err: javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
I have tried multiple solution from Stack Overflow.
I hope to learn the best practice to connect to HTTPS without trusting all certificates. Would appreciate if anyone can guide me.
EDIT 1: Apparently when I click "Log in" using Firefox, it prompts a warning shows that "elearning.utp.edu.my uses an invalid security certificate." However, I can established a "SECURE" connection to the website using Chrome. The website is as follow : https://elearning.utp.edu.my/login/index.php