Question

Okay this is probably a very stupid question but I'm using bouncycastle to parse issuer RDNS from a X509Certificate in the following way:

X500Name x500name = new JcaX509CertificateHolder(certificate).getIssuer();
RDN[] rdns = x500name.getRDNs();
for (int i = 0; i < rdns.length; ++i)
    String readableString = IETFUtils.valueToString(rdns[i].getFirst().getType())
    ...

... but all I get is some ASN1 OIDs.

So is there a way to convert ASN1ObjectIdentifier to a readable String like "CN", "OU" etc... instead of OIDs?

Thanks!

Was it helpful?

Solution

Take a look at org.bouncycastle.asn1.x500.X500NameStyle and its implementations.

X500NameStyle x500NameStyle = RFC4519Style.INSTANCE;

X500Name x500name = new JcaX509CertificateHolder( certificate ).getIssuer();
RDN[] rdns = x500name.getRDNs();
for ( RDN rdn : rdns ) {
    for ( AttributeTypeAndValue attribute : rdn.getTypesAndValues() ) {
        System.out.printf( "%s (%s) = %s%n",
                x500NameStyle.oidToDisplayName( attribute.getType() ),
                attribute.getType(),
                attribute.getValue()
        );
    }
}

OTHER TIPS

Using

X500Name x500name = new JcaX509CertificateHolder(certificate).getSubject();

will give you more detailed output than getIssuer().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top