get X509Certificate Owner - Java Security

Java examples for Security:Certificate

Description

get X509Certificate Owner

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 getOwner(X509Certificate cert) {
        try {/*from w  w  w  . ja v a  2 s  .c  om*/

            String dn = cert.getSubjectX500Principal().getName();
            String ownerName = null;

            LdapName ln = new LdapName(dn);

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

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

        return null;
    }
}

Related Tutorials