I need to setup a demo Android app that can help learn how ignoring certificate validation could lead to MITM.
At the moment, my test server has a self signed certificate, created as follows (see here) :
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/apache2/ssl/apache.key -out /etc/apache2/ssl/apache.crt
Now to test an Android app with this server (which has a self signed certificate), I made the code work by using this reference for setting up keystore and sending http post/get to my server.
A sample of my HTTP POST code (MyHttpClient is as per reference
) :
HttpClient httpclient = new MyHttpClient(getApplicationContext());
HttpPost httpPost = new HttpPost("https://www.testwebsite.com/api/rest/json/?");
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("method", "gettoken"));
nvps.add(new BasicNameValuePair("username",<Username> ));
nvps.add(new BasicNameValuePair("password", <Password>));
httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
HttpResponse response = httpclient.execute(httpPost);
If I understand correctly, the keystore step (in the reference), specifically :
final KeyStore ks = KeyStore.getInstance("BKS");
final InputStream inputStream = appContext.getResources().openRawResource(R.raw.certs);
ks.load(inputStream, appContext.getString(R.string.store_pass).toCharArray());
inputStream.close();
ret = new SSLSocketFactory(ks);
is trusting exactly the certificate I want. So here I don't have control over the CA of the certificate (correct me if I'm wrong).
To demo MITM, I need to have certificate signed by same CA, but for a different domain, and so without certificate validation, the app will talk to the wrong server.
I have a thought on how to correct my demo, and want to confirm whether it would be the right direction.
What I'm thinking is that on my Apache server, I will first create a CA, then create a certificate for the benign domain with that CA.
In my app, I'm thinking to replace the self-signed certificate file with the CA's file and change the code above to refer to the CA's certificate kept in app's raw resources (I presume the SSL handshake will still work - need some confirmation here .. ).
I think with this change, if I send down a certificate signed by the same CA but for a malicious domain, the app without any certificate validation, will accept the certificate and start communicating with that.
If any one could please comment on my thought, it would be appreciated.