19

I have a problem with reading certificate information. I want to read full information using java with bouncycastle library in Android programmatically. Now, i'm just using keytool command in console:

>keytool -list -keystore 1.p12 -storetype pkcs12 -v

Any suggestions?

2
  • 1
    And what have you tried? Commented Jun 7, 2013 at 8:40
  • 1
    I've found the solution (below), thanks for your attention. Commented Jun 7, 2013 at 9:06

1 Answer 1

54

I've found solution, the main idea is to cast certificate to x509, then get the SubjectDN and parse values.

public class TestClass {
    public static void main(String[] args) throws Exception {

        KeyStore p12 = KeyStore.getInstance("pkcs12");
        p12.load(new FileInputStream("pkcs.p12"), "password".toCharArray());
        Enumeration<String> e = p12.aliases();
        while (e.hasMoreElements()) {
            String alias = e.nextElement();
            X509Certificate c = (X509Certificate) p12.getCertificate(alias);
            Principal subject = c.getSubjectDN();
            String subjectArray[] = subject.toString().split(",");
            for (String s : subjectArray) {
                String[] str = s.trim().split("=");
                String key = str[0];
                String value = str[1];
                System.out.println(key + " - " + value);
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

Just remember to use java.security.* classes instead of javax.security.*
Hi @DiegoPlentz This code doesn't always work. On my machine, p12.getCertificate(alias) returns null. The pfx file was created by openssl as "openssl pkcs12 -export -out 1.pfx -in server.crt -inkey server.key". I tested your code with another pfx file, which was exported by windows certificate manager, it works fine.
This is work fine for me but any one know how to install .p12 file on device?
@ChangmingSun Maybe try passing the "-name" option. More info: stackoverflow.com/questions/21138420/…
a nice article with bit more details on the same.pixelstech.net/article/…
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.