I'm just about done working on an app for a local gym, and as my testing is nearly complete, and a version 1 is nearly finished, I'm starting to think about securing the app against any MITM type attacks. While I know the chances are next to zero of someone even wanting to MITM this app (as opposed to say, a banking app), I would still like to be a little proactive in security.
While the app sends/receives no user information (data sent back and forth is stuff like weight, reps, time, the name of the class the user checks in to, etc.), I am transmitting the names of all active gym members (to be used for an auto complete text box). I would like to encrypt the names, but I've been finding it difficult to change my code from HTTP to HTTPS. I've got HTTPS and a self-signed cert on my server, but can't seem to get the android side to work (keep getting no peer cert errors in eclipse). As a work around, I've thought about using AES128 to encrypt/hash each name, then decrypt it on the phone, and then likewise do the same when sending data back through PHP to the database.
Is this a sufficient alternative to encrypting the entire session? Call it "Lazy SSL", as if someone were to get the key, they would be able to decrypt the data, but again, we are only transmitting names, no other user information.
Here is the unencrypted code I'm using (I left out unnecessary stuff to make this block smalller):
public JSONObject makeHttpRequest(String url, String method, List<NameValuePair> params) {
if (method == "POST") {
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
This is in a larger class used for parsing Json: My entire JSONParser class
I'm calling this class in places I need to pull or send data to the server, such as the following:
final JSONParser jsonParser = new JSONParser();
final List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", Globals.TAG_GETMEMBERS));
params.add(new BasicNameValuePair("LastRow", lastRow));
// getting JSON string from URL
final JSONObject json = jsonParser.makeHttpRequest(
Globals.WEBSERVICE_URL, "POST", params);
using various resources:
How to enable a self-signed certificate for SSL sockets on Android?
http://randomizedsort.blogspot.com/2010/09/step-to-step-guide-to-programming.html
I was able to get something useful, I originally tried doing the "trust all certs" method, but since that is MITM prone, I would rather not use it (plus it wasn't working. Using the 2nd link I've gotten so far as re-generating the cert, I've downloaded the bouncy castle jar (
I also used the following commands to generate a keystore, and import it into my project:
keytool -genkey -dname "cn = smashwebserver, ou=Development Team, o=Smash Gyms, L=Sunnyvale, s=California, c=US" -alias ssltest -keypass ssltest -keystore c:\dell\ssltest.keystore -storepass ssltest -validity 180
keytool -export -alias ssltest -keystore c:\dell\ssltest.keystore -file c:\dell\ssltest.cer -storepass ssltest -keypass ssltest
keytool -import -alias ssltestcert -file C:\dell\ssltest.cer -keypass ssltestcert -keystore "C:\Users\Evan Richardson\workspace\SmashGyms\res\raw\ssltestcert" -storetype BKS -storepass ssltestcert -providerClass org.bouncycastle.jce.provider.BouncyCastleProvider -providerpath "C:\Users\Evan Richardson\workspace\SmashGyms\libs\bcprov-jdk15on-147.jar"
The resulting JSONParser class block looks like this:
if (method == "POST") {
// Load the self-signed server certificate
char[] passphrase = "ssltest".toCharArray();
KeyStore ksTrust = KeyStore.getInstance("BKS");
ksTrust.load(context.getResources().openRawResource(
R.raw.ssltestcert), passphrase);
TrustManagerFactory tmf = TrustManagerFactory
.getInstance(KeyManagerFactory.getDefaultAlgorithm());
tmf.init(ksTrust);
// Create a SSLContext with the certificate
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, tmf.getTrustManagers(),
new SecureRandom());
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
however now I get the following error:
10-29 11:55:28.470: W/System.err(9561): java.io.IOException: Wrong version of key store.
I looked that error up, and a possible solution was found here:Android bouncy castle: IOException
I've downloaded the 145 version of bouncycastles Jar, and used that. This fixes the ioexception error, but now I get the following:
10-29 12:21:57.536: W/System.err(12506): Catch exception while startHandshake: javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x10b9a10: Failure in SSL library, usually a protocol error
10-29 12:21:57.536: W/System.err(12506): error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol (external/openssl/ssl/s23_clnt.c:683 0x4026dced:0x00000000)
10-29 12:21:57.536: W/System.err(12506): return an invalid session with invalid cipher suite of SSL_NULL_WITH_NULL_NULL
10-29 12:21:57.586: W/System.err(12506): javax.net.ssl.SSLPeerUnverifiedException: No peer certificate
Strangely enough, if I change my url to "https://google.com", I don't get any errors, just the following:
10-29 14:03:50.198: V/httpresponsetag:(17810): <!DOCTYPE html>
10-29 14:03:50.198: V/httpresponsetag:(17810): <html lang=en>
10-29 14:03:50.198: V/httpresponsetag:(17810): <meta charset=utf-8>
10-29 14:03:50.198: V/httpresponsetag:(17810): <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
10-29 14:03:50.198: V/httpresponsetag:(17810): <title>Error 405 (Method Not Allowed)!!1</title>
10-29 14:03:50.198: V/httpresponsetag:(17810): <style>
10-29 14:03:50.198: V/httpresponsetag:(17810): *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}
10-29 14:03:50.198: V/httpresponsetag:(17810): </style>
10-29 14:03:50.198: V/httpresponsetag:(17810): <a href=//www.google.com/><img src=//www.google.com/images/errors/logo_sm.gif alt=Google></a>
10-29 14:03:50.198: V/httpresponsetag:(17810): <p><b>405.</b> <ins>That’s an error.</ins>
10-29 14:03:50.198: V/httpresponsetag:(17810): <p>The request method <code>POST</code> is inappropriate for the URL <code>/</code>. <ins>That’s all we know.</ins>
This may indicate it's in fact my self signed cert, but if i open up https:servername, it works (of course with the default warning)
EDIT:
I was getting the same errors even with accepting all certs, so i went and looked in my browser with the hostname I'm using, same error. I then looked at my NAT settings on my router...I was forwarding to port 80, instead of 443. FAIL. changed to 443, now it looks like it's working, at least with accepting all certs and the following code:
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) throws NoSuchAlgorithmException,
CertificateException, NotFoundException, KeyStoreException,
KeyManagementException {
// Making HTTP request
try {
// check for request method
if (method == "POST") {
// request method is POST
// defaultHttpClient
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs,
String authType) {
}
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs,
String authType) {
}
} };
// Install the all-trusting trust manager
try {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts,
new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc
.getSocketFactory());
} catch (Exception e) {
}
// Now you can access an https URL without having the certificate in the truststore
HttpClient client = new DefaultHttpClient();
client = this.sslClient(client);
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
// Log.v(TAG, EntityUtils.toString(result.getEntity()));
HttpResponse httpResponse = client.execute(httpPost);
// Log.v("httpresponsetag:", EntityUtils.toString(httpResponse
// .getEntity()));
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}