Android TLS connection and self signed certificate
Asked Answered
D

2

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.

Darendaresay answered 18/8, 2012 at 12:45 Comment(0)
D
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);
Darendaresay answered 27/8, 2012 at 23:26 Comment(0)
U
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
?

Unreal answered 22/8, 2012 at 15:2 Comment(3)
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.Pithecanthropus
Sorry, I've been terribly busy lately. I'll look over this tonight before the bounty expires :)Darendaresay
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. :)Darendaresay

© 2022 - 2024 — McMap. All rights reserved.