Android - 如何以编程方式将证书存储在密钥库中?

8

我正在开发一个金融交易的安卓应用。它需要SSL认证,我已经成功地完成了Android和Tomcat之间的握手。我使用keytool和openSSL生成服务器和客户端证书。Tomcat证书格式是JKS,而Android证书格式是BKS。我将这个BKS文件存储在原始文件夹中,并按如下方式使用:

public class NetworkCallSecure extends AsyncTask<String, Void, String> {

ResponseListener responseListener;
Activity activity;
ResultCodes code;

public NetworkCallSecure(Activity activity, ResponseListener responseListener, ResultCodes code) {
    this.responseListener = responseListener;
    this.activity = activity;
    this.code = code;
}

@Override
protected String doInBackground(String... params) {

    try{

        System.setProperty("http.keepAlive", "false");
        HttpsURLConnection .setDefaultHostnameVerifier(new HostnameVerifier() {

                    public boolean verify(String hostname,
                                          SSLSession session) {
                        Log.d("HTTPS",hostname+":"+session);
                        return true;
                    }
                });

        char[] passwKey = "mypass".toCharArray();
        KeyStore ks = KeyStore.getInstance("BKS");
        InputStream in = activity.getResources().openRawResource(
                R.raw.client);
        InputStream is = activity.getResources().openRawResource(
                R.raw.client);
        ks.load(in, passwKey);
        KeyManagerFactory kmf = KeyManagerFactory.getInstance("X509");
        kmf.init(ks, passwKey);

        SSLContext context = SSLContext.getInstance("TLS");
        context.init(kmf.getKeyManagers(),
                new X509TrustManager[] { new MyX509TrustManager(is,
                        passwKey) }, new SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(context
                .getSocketFactory());

        URL url = new URL(params[0]);

        HttpsURLConnection connection = (HttpsURLConnection) url
                .openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setRequestProperty("Content-Length", "" + Integer.toString(params[1].getBytes().length));
        connection.setDoOutput(true);

        byte[] outputInBytes = params[1].getBytes("UTF-8");
        OutputStream os = connection.getOutputStream();
        os.write( outputInBytes );
        os.close();

        BufferedReader bin = new BufferedReader(new InputStreamReader(
                connection.getInputStream()));

        StringBuffer sb = new StringBuffer();
        String line;
        while ((line = bin.readLine()) != null) {
            sb.append(line);
        }
        in.close();
        is.close();
        return sb.toString();
    } catch (Exception e) { // should never happen
        e.printStackTrace();
        Log.d("Err", e.toString());
    }
    return "no result";
}

@Override
protected void onPostExecute(String result) {
    responseListener.getResponse(result,code);
}
}

我的Trustmanager类是:

public class MyX509TrustManager implements X509TrustManager {
X509TrustManager pkixTrustManager;

public MyX509TrustManager(InputStream trustStore, char[] password)
        throws Exception {
    // create a "default" JSSE X509TrustManager.

    KeyStore ks = KeyStore.getInstance("BKS");

    ks.load(trustStore, password);

    TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509");
    tmf.init(ks);

    TrustManager tms[] = tmf.getTrustManagers();

    /*
     * Iterate over the returned trustmanagers, look for an instance of
     * X509TrustManager. If found, use that as our "default" trust manager.
     */
    for (int i = 0; i < tms.length; i++) {
        if (tms[i] instanceof X509TrustManager) {
            pkixTrustManager = (X509TrustManager) tms[i];
            return;
        }
    }

    /*
     * Find some other way to initialize, or else we have to fail the
     * constructor.
     */
    throw new Exception("Couldn't initialize");
}

public void checkClientTrusted(X509Certificate[] arg0, String arg1)
        throws CertificateException {
    // TODO Auto-generated method stub
    try {
        pkixTrustManager.checkClientTrusted(arg0, arg1);
    } catch (CertificateException excep) {
        // do any special handling here, or rethrow exception.
    }

}

public void checkServerTrusted(X509Certificate[] arg0, String arg1)
        throws CertificateException {
    // TODO Auto-generated method stub
    try {
        pkixTrustManager.checkServerTrusted(arg0, arg1);
    } catch (CertificateException excep) {
        /*
         * Possibly pop up a dialog box asking whether to trust the cert
         * chain.
         */
    }
}

public X509Certificate[] getAcceptedIssuers() {
    // TODO Auto-generated method stub
    return pkixTrustManager.getAcceptedIssuers();
}
}

现在我想使用HTTPS连接注册用户。这个过程是从用户那里获取详细信息并将其发送到服务器。服务器将验证这些详细信息并将确认PIN发送到用户的手机(在用户详细信息中获取此MSISDN)。用户将输入此PIN,服务器将验证该PIN是否相同。在用户经过验证后,客户端应用程序(用户手机)将生成CSR并将其发送到服务器。服务器将使用此CSR生成证书并将其发送到客户端(移动应用程序)。 现在我的问题是我想将此证书存储在仅我的应用程序可以访问此证书的位置。 我正在尝试使用以下方式将其保存在raw文件夹中的BKS文件中:
private boolean storeCertInKeystore(byte[] cert) {
    try {
        InputStream is = getResources().openRawResource(
                R.raw.client);
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        InputStream certstream = new ByteArrayInputStream(cert);
        X509Certificate certificate = (X509Certificate) cf.generateCertificate(certstream);
        KeyStore keyStore = KeyStore.getInstance("BKS");
        keyStore.load(is, "mypass".toCharArray());
        keyStore.setCertificateEntry("mycert", certificate);


        Log.d("My App Cert: ", "true");
        return true;
    } catch(Exception e) {
            e.printStackTrace();
    }
    return false;
}

这段代码成功运行,但无法将证书存储到BKS文件中。我尝试了另一种方法,描述在这里,但是没有成功。(我想在我的应用程序中使用此证书进行客户端身份验证) 我的问题是Q.如何存储此证书,以便只能由我的应用程序访问?并且当用户注册过期时,我可以删除此证书。 请帮忙,谢谢。
1个回答

6
  • 你的问题不在于密钥库本身,而是你尝试存储新客户端证书的文件位置!
  • "RAW-folder"是你安装的应用程序包的一部分。所以你可以"虚拟地"访问它,并且只能读取,不能写入!
  • 如果你想让你的密钥库保持私有,最好的选择是你的应用程序沙盒私有文件夹(内部存储)。
    你无法在RAW文件夹中写入,但是你可以在应用程序的私有文件夹中写入。
  • 在你提供的链接中,存储/写入位置实际上是私有文件夹。所以它没有为你工作,因为你正在尝试“在原始文件夹中写入”
  • 你可能已经知道了,但是你可以将文件(R.raw.client)从“Raw-folder”复制到你的应用程序私有文件夹中。这样,你只需要使用一个密钥库文件 (可读可写)。

1
请问如何创建一个新的密钥库并将我的证书存储在其中以备将来使用,我将使用此证书进行SSL握手。 - IB's

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接