Daniel Kobe Daniel Kobe - 3 months ago 22
Android Question

Setting up an HTTPS connection on Android with a self signed certificate from an AWS server

Im working with a team on an Android app and was allocated with the task of setting up the user login and registration pages. After setting up my server class to post using the

HttpsURLConnection
class I got an
SSLHandshakeException
error.


HttpsURLConnection Code

HttpsURLConnection connection = (HttpsURLConnection) new URL(spec.toString()).openConnection();



After some research I realized this is because Android doesn't trust our server because it is self signed. I am trying to follow this documentation from the Android dev site but don't understand how to get the
crt
file. Im using an AWS server but I wasn't the person that set up the self signed certificate but I do have full access to the server.


From Android Docs (How do I get the load-der.crt file?)

// Load CAs from an InputStream
// (could be from a resource or ByteArrayInputStream or ...)
CertificateFactory cf = CertificateFactory.getInstance("X.509");
// From https://www.washington.edu/itconnect/security/ca/load-der.crt
InputStream caInput = new BufferedInputStream(new FileInputStream("load-der.crt"));
Certificate ca;

Answer

Heres how I got the certificate, although this answer may not be very helpful to others. I asked the server admin for it and he gave it to me. I believe he generated the certificate from some bash code while he was SSHed into the server. I then created an assets folder in main and drop the certificate file in there. I got the input stream with this line of code InputStream in = context.getAssets().open("my_cert.crt").