Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android TLS connection and self signed certificate

Tags:

android

ssl

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.

like image 996
Robin Heggelund Hansen Avatar asked Aug 18 '12 12:08

Robin Heggelund Hansen


People also ask

Can you use a self signed certificate for TLS?

If you want to secure your website with an SSL/TLS certificate, you can use a free self-signed SSL/TLS certificate.

How do I trust a self signed certificate on Android?

Go to Settings / Security / Credential storage and select “Install from device storage”. The . crt file will be detected and you will be prompted to enter a certificate name. After importing the certificate, you will find it in Settings / Security / Credential storage / Trusted credentials / User.

How do I enable SSL on my Android?

From Credential Storage Tab, click on Install from Phone Storage/Install from SD Card. A new file storage manager will appear. Now find the SSL certificate from your device. If it asks to enter the PKCS#12 password, add that password which was generated during SSL download process.


2 Answers

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);
like image 131
Robin Heggelund Hansen Avatar answered Sep 18 '22 08:09

Robin Heggelund Hansen


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
?

like image 26
pawelzieba Avatar answered Sep 18 '22 08:09

pawelzieba