0

我正在尝试构建一个需要进行一些客户端 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 在实现上似乎与旧版本不同。

4

2 回答 2

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不执行服务器证书验证的实现。最好不要使用它。

于 2012-07-23T12:54:30.297 回答
0

建议的解决方案不适用于 4.1+。那里的私钥不可导出,也无法将其放入内存密钥存储中。检查How to make client certificate authentication from Android 4.1 with Apache client and the KeyChain API on how to do it in 4.1

于 2012-11-28T13:54:57.033 回答