Android 4.0 SSL身份验证

时间:2012-07-20 14:17:25

标签: android ssl ssl-certificate android-authenticator

我正在尝试构建一个需要进行客户端SSL身份验证的RSS提要阅读器。

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

这是我到目前为止所做的:

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;
}
}

任何有关如何做到这一点的帮助将非常感激,因为我之前从未做过任何身份验证,我发现很难在android 4.0上找到关于这个主题的任何内容,因为4.0似乎在实现方面与旧版本不同。

2 个答案:

答案 0 :(得分:2)

您应该能够检索证书链以及私钥并将其存储到临时的内存中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);

之后,您可以使用此密钥库来初始化SSLContext实例。

警告:请注意,示例代码包含不执行服务器证书验证的X509TrustManager实现。最好不要使用它。

答案 1 :(得分:0)

建议的解决方案不适用于4.1+。私钥不可导出,并且无法将其放入内存中的密钥存储区。 检查How to make client certificate authentication from Android 4.1 with Apache client and the KeyChain API 如何在4.1

中做到这一点