Android 4.0 SSL Authentication

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-25 05:27:17

问题


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

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

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.


回答1:


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

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

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




回答2:


The proposed solution won't work in 4.1+. There the private key is not exportable and it is not possible to put it in the in-memory key store. Check 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



来源:https://stackoverflow.com/questions/11581208/android-4-0-ssl-authentication

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