Code example for X509Certificate

Methods: getSubjectAlternativeNames, getSubjectDN

0
     */ 
    public static List<String> getPeerIdentity(X509Certificate x509Certificate) {
        // Look the identity in the subjectAltName extension if available 
        List<String> names = getSubjectAlternativeNames(x509Certificate);
        if (names.isEmpty()) {
            String name = x509Certificate.getSubjectDN().getName();
            Matcher matcher = cnPattern.matcher(name);
            if (matcher.find()) {
                name = matcher.group(2);
            } 
            // Create an array with the unique identity 
            names = new ArrayList<String>();
            names.add(name);
        } 
        return names;
    } 
     
    
 
    /** 
     * Returns the JID representation of an XMPP entity contained as a SubjectAltName extension 
     * in the certificate. If none was found then return <tt>null</tt>. 
     * 
     * @param certificate the certificate presented by the remote entity. 
     * @return the JID representation of an XMPP entity contained as a SubjectAltName extension 
     *         in the certificate. If none was found then return <tt>null</tt>. 
     */ 
    private static List<String> getSubjectAlternativeNames(X509Certificate certificate) {
        List<String> identities = new ArrayList<String>();
        try { 
            Collection<List<?>> altNames = certificate.getSubjectAlternativeNames();
            // Check that the certificate includes the SubjectAltName extension 
            if (altNames == null) {
                return Collections.emptyList();
            } 
            // Use the type OtherName to search for the certified server name