Android 4.0的SSL认证 [英] Android 4.0 SSL Authentication

查看:220
本文介绍了Android 4.0的SSL认证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图构建需要做一些客户端SSL验证的RSS阅读器。

I'm trying to build an RSS feed reader that needs to do some client side SSL authentication.

我已经得到了,或者至少认为我有,证书。但是我现在无法弄清楚如何设置一个SSL隧道将证书发送到服务器进行身份验证。

I've got, or at least think I have, the certificate. However I now cannot figure out how to setup a ssl tunnel to send the certificate to the server to authenticate.

下面是我到目前为止有:

Here is what I have so far:

public class Authenticator extends Activity {

PrivateKey privateKey = null;
String SavedAlias = "";
private static final String TAG = "AUTHENTICATOR.CLASS";
final HttpParams httpParams = new BasicHttpParams();
private KeyStore mKeyStore = KeyStore.getInstance();

public Handler mHandler = new Handler(Looper.getMainLooper());

public void run()
{
   mHandler.post(new Runnable() {
      public void run() {
          new AliasLoader().execute();
      }
   });
}


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getCertificates("TEST");
}

public class AliasLoader extends AsyncTask<Void, Void, X509Certificate[]> 
{
    X509Certificate[] chain = null;

    @Override protected X509Certificate[] doInBackground(Void... params) {
        android.os.Debug.waitForDebugger();

        if(!SavedAlias.isEmpty())
        {
                try {
                    chain = KeyChain.getCertificateChain(getApplicationContext(),SavedAlias);
                } catch (Exception e) {
                    Log.e(TAG, e.getMessage());
                }
        }
        else
        {
            this.cancel(true);
        }

        return chain;
    }

    @Override 
    protected void onPostExecute(X509Certificate[] chain) 
    {

        if(chain != null)
        {
            Toast.makeText(getApplicationContext(), "YAY, Certificate is not empty", Toast.LENGTH_LONG).show();
        }
        else
        {
            Toast.makeText(getApplicationContext(), "Certificate is Empty", Toast.LENGTH_LONG).show();
        }

        /*
        if (privateKey != null) {
            Signature signature = null;
            try {
                signature = Signature.getInstance("SHA1withRSA");
            } catch (NoSuchAlgorithmException e) {
                Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
            }
            try {
                signature.initSign(privateKey);
            } catch (InvalidKeyException e) {
                Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
            }
        }
        */
    }
}

public void getCertificates(String Host)
{
    KeyChainAliasCallback callBack = new KeyChainAliasCallback() {

        @Override
        public void alias(String alias) {               
            if (alias != null) 
            {
                Looper.prepare();
                saveAlias(alias);
                run();
                Looper.loop();
            }
        }
    };

    KeyChain.choosePrivateKeyAlias(this, callBack,
    new String[] {"RSA", "DSA"}, // List of acceptable key types. null for any
    null,                        // issuer, null for any
    null,      // host name of server requesting the cert, null if unavailable
    443,                         // port of server requesting the cert, -1 if unavailable
    null);                       // alias to preselect, null if unavailable
}

public void saveAlias(String alias)
{
    SavedAlias = alias;
}
}

这是如何做到这将是极大的AP preciated正如我以前从来没有做过任何身份验证,我发现很难找到关于这个主题为Android 4.0作为4.0什么任何帮助似乎是在执行不同的话旧版本。

Any help on how to do this would be greatly appreciated as i have never done any authentication before and i have found it difficult to find anything on this topic for android 4.0 as 4.0 seems to be different in implementation then the older versions.

推荐答案

您应该能够检索证书链,以及私有密钥并将其存储到临时内存密钥库

You should be able to retrieve the certificate chain as well as the private key and store it into a temporary in-memory KeyStore:

String alias = "test";
KeyStore memoryKeyStore = KeyStore.getInstance("BKS");
memoryKeyStore.load(null);
X509Certificate[] chain = KeyChain.getCertificateChain(getApplicationContext(),alias);
PrivateKey key = KeyChain.getPrivateKey(getApplicationContext(),alias);
memoryKeyStore.setKeyEntry(alias, key.getEncoded(), chain);

之后,您可以使用该密钥存储用于初始化SSL连接实例。

Afterwards you can use this key store for initializing the SSLContext instance.

警告:注意样品code包含不执行服务器证书验证的 X509TrustManager 的实施。最好不要使用它。

Warning: Note the sample code contains an X509TrustManager implementation that does not perform server certificate validation. Better not use it.

这篇关于Android 4.0的SSL认证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆