keychain - A proper way to install PEM certifiate in Android -
i developing android project.
i have pem certificate string:
-----begin certificate----- miieczcca1ugawibagibadanbgkqhkig9w0baqqfad..akga1uebhmcr0ix ezarbgnvbagtclnvbwutu3rhdguxfdasbgnvbaotc0..0egthrkmtcwnqyd vqqley5dbgfzcyaxifb1ymxpyybqcmltyxj5ienlcn..xrpb24gqxv0ag9y ...many lines... it8una2gy4l2o//on88r5iwjlm1l0oa8e4fr2yrbhx..adsgefkkynrwgi/ 7vqmfxdgsrrxngrgnx+vwdz3/zwi0jodtcknnqepvn..hox -----end certificate-----
(assigned above certificate string variable named cert_str
)
i decode above pem string byte array:
byte[] pembytes = base64.decode( cert_str.replaceall("-----(begin|end) certificate-----", "") .replaceall("\n", "") .getbytes("utf-8"), base64.default );
i try programmatically install pem certificate android phone following code:
intent intent = keychain.createinstallintent(); // because pem contains certificate, no private key, use extra_certificate intent.putextra(keychain.extra_certificate, pembytes);// above pem bytes intent.addflags(intent.flag_activity_new_task); context.startactivity(intent);
when run code (in android 7 device), android system certificate installer app pops window, when press "ok" button of window, got following log:
java.io.ioexception: stream not represent pkcs12 key store @ com.android.org.bouncycastle.jcajce.provider.keystore.pkcs12.pkcs12keystorespi.engineload(pkcs12keystorespi.java:793) @ java.security.keystore.load(keystore.java:1247) @ com.android.certinstaller.credentialhelper.loadpkcs12internal(credentialhelper.java:396) @ com.android.certinstaller.credentialhelper.extractpkcs12internal(credentialhelper.java:364) @ com.android.certinstaller.credentialhelper.extractpkcs12(credentialhelper.java:354) @ com.android.certinstaller.certinstaller$1.doinbackground(certinstaller.java:328) @ com.android.certinstaller.certinstaller$1.doinbackground(certinstaller.java:327)
my questions:
i have used extra_certificate & set
intent
, not using extra_pkcs12, log, android system thinks installing pkcs#12 keystore. why?what correct way programmatically install pem certificate in android?
your code should work, said @sergey nikitin. starred example @ github using similar code
i have reviewed android 7.1 source code of credentialhelper , certinstaller trace exception log. unique reachable path execute pkcs12
loader at
com.android.certinstaller.credentialhelper.extractpkcs12(credentialhelper.java:354)
is method onscreenlockok
private void onscreenlockok() { if (mcredentials.haspkcs12keystore()) { if (mcredentials.haspassword()) { showdialog(pkcs12_password_dialog); } else { new pkcs12extractaction("").run(this); }
which protected credentialhelper.haspkcs12keystore()
boolean haspkcs12keystore() { return mbundle.containskey(keychain.extra_pkcs12); }
i have not found default assigned values or alternative paths, deduce keychain.extra_pkcs12
being used in way. weird behaviour, may have clean&rebuild issue?
i suggest debug code including android certinstaller class ensure values of extras , ensure executed code expected
Comments
Post a Comment