Android:以编程方式将.p12证书安装到设备中

时间:2015-04-24 09:06:55

标签: java android openssl certificate ssl-certificate

我正在尝试以编程方式安装.p12证书,该证书是为使用ARC GRID网络而创建的。

当我尝试通过SETTINGS>安装此证书时,它有效。安全>安装证书。当然我被要求输入我的密码。所以主要的问题是如何以最简单的方式以编程方式安装证书。

我在某个示例中看到的功能并尝试使用:

public void installCertificate(String myCert) {

    try {
        FileInputStream fis = new FileInputStream(myCert);
        byte[] keychain = new byte[fis.available()];
        fis.read(keychain);

        Intent installIntent = KeyChain.createInstallIntent();
        X509Certificate x509 = X509Certificate.getInstance(keychain);
        installIntent.putExtra(KeyChain.EXTRA_CERTIFICATE, x509.getEncoded());
        installIntent.putExtra(KeyChain.EXTRA_NAME, myCert);
        startActivityForResult(installIntent, 1);
    }
    catch (IOException e) {
        e.printStackTrace();
        Toast.makeText(this, "IOException: " + e.getMessage(), Toast.LENGTH_SHORT).show();
    } catch (CertificateEncodingException e) {
        e.printStackTrace();
        Toast.makeText(this, "CertificateEncodingException: " + e.getMessage(), Toast.LENGTH_SHORT).show();
    } catch (javax.security.cert.CertificateException e) {
        e.printStackTrace();
        Toast.makeText(this, "javax.security.cert.CertificateException: " + e.getMessage(), Toast.LENGTH_SHORT).show();
    }
}

onActivityResult:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if ((requestCode == FILE_CHOOSER) && (resultCode == RESULT_OK)) {
        String fileSelected = data.getStringExtra(com.orleonsoft.android.simplefilechooser.Constants.KEY_FILE_SELECTED);
        installCertificate(fileSelected);
        pathToFile.setText(fileSelected);
        Toast.makeText(this, R.string.pasirinktasFailas + fileSelected, Toast.LENGTH_SHORT).show();
    }
}

还有logcat,没有红色错误,但有些蓝色:

04-24 08:45:25.490    2654-2654/? I/FILE CHOOSER﹕ result ok
04-24 08:45:25.546    2654-2654/? W/System.err﹕ javax.security.cert.CertificateException: com.android.org.conscrypt.OpenSSLX509CertificateFactory$ParsingException: com.android.org.conscrypt.OpenSSLX509CertificateFactory$ParsingException: java.lang.RuntimeException: error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag
04-24 08:45:25.546    2654-2654/? W/System.err﹕ at javax.security.cert.X509Certificate.getInstance(X509Certificate.java:94)
04-24 08:45:25.546    2654-2654/? W/System.err﹕ at javax.security.cert.X509Certificate.getInstance(X509Certificate.java:213)
04-24 08:45:25.546    2654-2654/? W/System.err﹕ at com.example.dev.nordugrid.EsamoRegistravimas.installCertificate(EsamoRegistravimas.java:69)
04-24 08:45:25.546    2654-2654/? W/System.err﹕ at com.example.dev.nordugrid.EsamoRegistravimas.onActivityResult(EsamoRegistravimas.java:90)
04-24 08:45:25.546    2654-2654/? W/System.err﹕ at android.app.Activity.dispatchActivityResult(Activity.java:6139)
04-24 08:45:25.546    2654-2654/? W/System.err﹕ at android.app.ActivityGroup.dispatchActivityResult(ActivityGroup.java:126)
04-24 08:45:25.546    2654-2654/? W/System.err﹕ at android.app.ActivityThread.deliverResults(ActivityThread.java:3535)
04-24 08:45:25.546    2654-2654/? W/System.err﹕ at android.app.ActivityThread.handleSendResult(ActivityThread.java:3582)
04-24 08:45:25.546    2654-2654/? W/System.err﹕ at android.app.ActivityThread.access$1300(ActivityThread.java:144)
04-24 08:45:25.546    2654-2654/? W/System.err﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1327)
04-24 08:45:25.546    2654-2654/? W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:102)
04-24 08:45:25.546    2654-2654/? W/System.err﹕ at android.os.Looper.loop(Looper.java:135)
04-24 08:45:25.546    2654-2654/? W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5221)
04-24 08:45:25.546    2654-2654/? W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
04-24 08:45:25.546    2654-2654/? W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:372)
04-24 08:45:25.547    2654-2654/? W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
04-24 08:45:25.547    2654-2654/? W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
04-24 08:45:25.592    2654-2669/? W/EGL_emulation﹕ eglSurfaceAttrib not implemented
04-24 08:45:25.593    2654-2669/? W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa5b7fba0, error=EGL_SUCCESS
04-24 08:45:25.716    2654-2669/? W/EGL_emulation﹕ eglSurfaceAttrib not implemented
04-24 08:45:25.716    2654-2669/? W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa5b7fb60, error=EGL_SUCCESS
04-24 08:45:25.860    2654-2669/? D/OpenGLRenderer﹕ endAllStagingAnimators on 0xa6e85300 (ListView) with handle 0xa6067f60
04-24 08:45:25.973      933-933/? W/SurfaceFlinger﹕ couldn't log to binary event log: overflow.
04-24 08:45:27.639    2654-2669/? W/EGL_emulation﹕ eglSurfaceAttrib not implemented
04-24 08:45:27.639    2654-2669/? W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa5b7fb60, error=EGL_SUCCESS
04-24 08:52:00.012    1234-1252/? I/ProcessStatsService﹕ Prepared write state in 1ms
04-24 08:53:47.520    1234-1251/? I/UsageStatsService﹕ User[0] Flushing usage stats to disk
04-24 08:54:28.775    1234-1401/? I/ActivityManager﹕ Delay finish: com.google.android.gms/.checkin.EventLogService$Receiver
04-24 08:54:28.786    1833-2687/? I/EventLogService﹕ Aggregate from 1429863868353 (log), 1429863868353 (data)
04-24 08:54:28.817    1676-2035/? D/GCM﹕ Message class com.google.f.a.a.i
04-24 08:54:29.025    1234-1508/? I/ActivityManager﹕ Resuming delayed broadcast

也许这个java函数代码不适合.p12证书?

0 个答案:

没有答案