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.

4

2 回答 2

17

经过大量阅读后,我想出了一个答案。

一个很好的指南在这里: http: //nelenkov.blogspot.no/2011/12/using-custom-certificate-trust-store-on.html

现在,由于我没有使用 HTTPS,我不得不想出一个稍微不同的方法来使用新的密钥库获得一个干净的 SSL 套接字:

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);
于 2012-08-27T23:26:55.980 回答
7

不建议向您的应用程序添加证书。您将在更新证书时遇到问题。

你看过:

Android 上的自签名 SSL 接受

带有 Android 和自签名服务器证书的 HTTPS GET (SSL)

于 2012-08-22T15:02:16.907 回答