get X509Certificate Issuer - Java Security

Java examples for Security:Certificate

Description

get X509Certificate Issuer

Demo Code


//package com.java2s;
import java.security.cert.X509Certificate;
import javax.naming.InvalidNameException;
import javax.naming.ldap.LdapName;
import javax.naming.ldap.Rdn;

public class Main {
    public static String getIssuer(X509Certificate cert) {
        try {// w w  w.j av a  2 s.c o  m

            String dn = cert.getIssuerX500Principal().getName();
            String issuerName = null;

            LdapName ln = new LdapName(dn);

            for (Rdn rdn : ln.getRdns()) {
                if (rdn.getType().equalsIgnoreCase("CN")) {
                    issuerName = rdn.getValue().toString();
                    break;
                }
            }

            if (issuerName != null)
                return issuerName;
        } catch (InvalidNameException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }
}

Related Tutorials