开发者

SSL client authentication in Android

开发者 https://www.devze.com 2023-03-01 16:32 出处:网络
I need to write Android app that will be communicate with .Net service. I have to make server/cl开发者_C百科ient authentication. I found some useful topics (this blog and this blog) , but they both sh

I need to write Android app that will be communicate with .Net service. I have to make server/cl开发者_C百科ient authentication. I found some useful topics (this blog and this blog) , but they both show how to made server authentication. How can I made client authentication? I found a useful discussion, but there author uses Sockets, but i need to make it via HttpClient.


the following allows me to use my own rootca and client+server certificates. ie, security without paying anyone money :-)

create your rootca, and client and server keys and certs using openssl (many tutorials for this on the web)

create rootcacert.bks using keytool with bouncycastle as provider and -importcert

create clientcertandkey.p12 using openssl pkcs12 -export ...

HttpClient httpClient = null;
try {
    HttpParams httpParameters = new BasicHttpParams();
    KeyStore rootca = KeyStore.getInstance("BKS");
    rootca.load(getResources().openRawResource(R.raw.rootcacert),"bkskeystorepass".toCharArray());
    KeyStore mycert = KeyStore.getInstance("pkcs12");
    mycert.load(getResources().openRawResource(R.raw.clientcertandkey),"pkcs12storepass".toCharArray());
    SSLSocketFactory sockfact = new SSLSocketFactory(mycert,null,rootca);
    SchemeRegistry registry = new SchemeRegistry();
    registry.register(new Scheme("https",sockfact , 443));
    httpClient = new DefaultHttpClient(new ThreadSafeClientConnManager(httpParameters, registry), httpParameters);
} catch (Exception e) {
    e.printStackTrace();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消