How to use p12 certificates in Android (client certificates)

烈酒焚心 提交于 2019-11-30 07:46:25

The point where you have to change your code is when your new SSLSocketFactory instance:

return new SSLSocketFactory(truststore);

The SSLSocketFactory class has other constructors, one of them allows to specify a keystore, the keystore password and a truststure:

public SSLSocketFactory (KeyStore keystore, String keystorePassword, KeyStore truststore)(JavaDoc)

I am not sure if you can load your .P12 file under Android as KeyStore (on J2SE you can). If you can not you have to convert the .P12 file to a Bouncy castle keystore similar to the truststore you are already using. Use that key store to create your SSLSocketFactory instance and you should be able to use the client certificate.

Importing a P12 file into BKS via Portecle

Creating a BKS file and importing existing .key+.pem file is very simple using Portecle GUI (Java program). After starting Portecle select File -> New Keystore -> BKS. Afterwards you can execute Tools -> Import Key Pair and select the .P12 file. Finally save the key store protected with a password of your choice.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!