17

I'm trying to connect to a node.js based TLS server from my Android app. Naturally it fails becouse I'm using a self-signed certificate.

Is there anyway I can just add the certificate to my app and have Android trust it somehow? Note, I'm not using HTTPS, this is a TLS over TCP connection.

Robin Heggelund Hansen
  • 4,906
  • 6
  • 37
  • 54

2 Answers2

17

After a lot of reading around, I came up with an answer.

A pretty good guide is here: http://nelenkov.blogspot.no/2011/12/using-custom-certificate-trust-store-on.html

Now, since I'm not using HTTPS, I had to come up with a slightly different approach for getting a clean SSL socket with the new keystore:

KeyStore store = KeyStore.getInstance("BKS");
InputStream truststore = mainActivity.getResources().openRawResource(R.raw.trust);
store.load(truststore, "PASSWORD".toCharArray());
TrustManagerFactory tmf = TrustManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
tmf.init(store);
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, tmf.getTrustManagers(), new SecureRandom());
Socket socket = context.getSocketFactory().createSocket(ip, port);
CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
Robin Heggelund Hansen
  • 4,906
  • 6
  • 37
  • 54
7

Adding certificate to your application isn't recommended. You'll have problems with updating the certificate.

Have you looked at:

Self-signed SSL acceptance on Android

HTTPS GET (SSL) with Android and self-signed server certificate
?

Community
  • 1
  • 1
pawelzieba
  • 16,082
  • 3
  • 46
  • 72
  • I think hardcoding a fingerprint into an application is a good idea. Updating an application to use a new fingerprint isn't hard, and it cuts out all the CA related crap. – CodesInChaos Aug 23 '12 at 16:42
  • Sorry, I've been terribly busy lately. I'll look over this tonight before the bounty expires :) – Robin Heggelund Hansen Aug 27 '12 at 12:27
  • I gave you a +1 for the links. Of course, the methods in those links makes the app trust EVERYONE, which certainly isn't what I was looking for. Still, might be interesting for people in a testing environment. :) – Robin Heggelund Hansen Aug 28 '12 at 10:42