install X509 certificate programmatically in my case
Asked Answered
F

1

7

I am developing an Android project. I have a PEM certificate string:

-----BEGIN CERTIFICATE-----
MIIEczCCA1ugAwIBAgIBADANBgkqhkiG9w0BAQQFAD..AkGA1UEBhMCR0Ix
EzARBgNVBAgTClNvbWUtU3RhdGUxFDASBgNVBAoTC0..0EgTHRkMTcwNQYD
VQQLEy5DbGFzcyAxIFB1YmxpYyBQcmltYXJ5IENlcn..XRpb24gQXV0aG9y
...MANY LINES...
It8una2gY4l2O//on88r5IWJlm1L0oA8e4fR2yrBHX..adsGeFKkyNrwGi/
7vQMfXdGsRrXNGRGnX+vWDZ3/zWI0joDtCkNnqEpVn..HoX
-----END CERTIFICATE-----

(I assigned above certificate string to a variable named CERT_STR)

I convert above PEM string to X509Certificate by:

byte[] certBytes = CERT_STR.getBytes();
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
InputStream certIs = new ByteArrayInputStream(certBytes); 
// now I get the X509 certificate from the PEM string
X509Certificate certificate = (X509Certificate) certificateFactory.generateCertificate(certIs);

Then, I try to install the certificate programmatically by:

Intent intent = KeyChain.createInstallIntent();
// because my PEM only contains a certificate, no private key, so I use EXTRA_CERTIFICATE
intent.putExtra(KeyChain.EXTRA_CERTIFICATE, certificate.getEncoded());
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);

When I run my app, I see system dialog pops up saying "Extracting..." , I know system is extracting my certificate, but that dialog is showing there all the time saying "Extracting...".

Why? Where am I wrong in my code to install the certificate?

Frangipani answered 7/11, 2016 at 12:7 Comment(3)
The code you have requires a a binary certificate, not a base64 encoded one.Estefanaestel
@Robert, I don't quite understand your words, which part of my code uses base64 encoding?Frangipani
The part between BEGIN and END CERTIFICATE is base64 encoded certificate. The x.509 CertificateFactory requires AFAIR a binary certificate.Estefanaestel
B
4

You are probably not using properly created X509 certificate. Following worked on my end and I did not see any "Extracting..." dialog (Nexus 5X, Android 7.0):

String x509cert = "-----BEGIN CERTIFICATE-----\n" +
        "MIICrjCCAhegAwIBAgIJAO9T3E+oW38mMA0GCSqGSIb3DQEBCwUAMHAxCzAJBgNV\n" +
        "BAYTAlVaMREwDwYDVQQHDAhUYXNoa2VudDENMAsGA1UECgwERWZpcjEQMA4GA1UE\n" +
        "CwwHSVQgZGVwdDEQMA4GA1UEAwwHZWZpci51ejEbMBkGCSqGSIb3DQEJARYMaG9z\n" +
        "dEBlZmlyLnV6MB4XDTE2MTExMDA4MjIzMFoXDTE2MTIxMDA4MjIzMFowcDELMAkG\n" +
        "A1UEBhMCVVoxETAPBgNVBAcMCFRhc2hrZW50MQ0wCwYDVQQKDARFZmlyMRAwDgYD\n" +
        "VQQLDAdJVCBkZXB0MRAwDgYDVQQDDAdlZmlyLnV6MRswGQYJKoZIhvcNAQkBFgxo\n" +
        "b3N0QGVmaXIudXowgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAL60mG0Gpl7s\n" +
        "3qMnZcURB1xk5Qen6FN0+AJB5Z/WHA50n1MUkXNY28rkEYupkxpfEqR+/gXgBUAm\n" +
        "FACA3GSdoHMMY1kdeAzxsYbBEbtGKHICF/QFGTqScWmI6uBUwzsLDLv1ELef/zEY\n" +
        "Ru/krXtNh8ZNYyfwVKyZaB9+3M2yOqATAgMBAAGjUDBOMB0GA1UdDgQWBBS1nH3O\n" +
        "ecLDrIZLZ/f1WsNL/xtuEzAfBgNVHSMEGDAWgBS1nH3OecLDrIZLZ/f1WsNL/xtu\n" +
        "EzAMBgNVHRMEBTADAQH/MA0GCSqGSIb3DQEBCwUAA4GBAGzjJnXODvF9UHBKHAUF\n" +
        "kzisr78Og5BrKyAgdnjH196Jg4MO7RNJdQAmuAIk9aBB/jvAiznhhbcD3mYImH+h\n" +
        "F0Scewk5m736ydGhkcUpmxA5ye1hajjs9V7PQD2O4a8rNJSlJjiWRWSqxTfH79Ns\n" +
        "B7x2HND9LU/iz02ugGJ8vwg8\n" +
        "-----END CERTIFICATE-----\n";
Intent intent = KeyChain.createInstallIntent();
intent.putExtra(KeyChain.EXTRA_CERTIFICATE, x509cert.getBytes());
startActivity(intent);

To generate the above certificate, I used the following steps (based on Generating Keys and Certificates for SSO):

$ openssl genrsa -out rsaprivkey.pem 1024

$ openssl req -new -x509 -key rsaprivkey.pem -out rsacert.pem

$ ls
rsacert.pem rsaprivkey.pem

Then I simply copy/pasted the output from cat rsacert.pem to x509cert.

Hope this helps.

Bylaw answered 10/11, 2016 at 2:45 Comment(4)
@ozbek, have you tested in Android 7 Nougat device? I am having this issue in Android 7.Frangipani
@Leem.fin: yes, tested on Nexus 5X with Android 7.0Bylaw
@ozbek, I just realize my what I have is a certificate also contains private key. If you try that, you would encounter the same problem. Do you have any idea how to programmatically install certificate which contains private key in Android 7?Frangipani
I think you cannot install any CA cert programatically in android and that is intentionally done to avoid Man in the middle attack.Reflate

© 2022 - 2024 — McMap. All rights reserved.