Code example for X509Certificate

Methods: getSubjectAlternativeNames, getSubjectDN

0
            for (int i = 0; i < nSize; i++) {
                try { 
                    x509Certificates[i].checkValidity(date);
                } 
                catch (GeneralSecurityException generalsecurityexception) {
                    throw new CertificateException("invalid date of " + server);
                } 
            } 
        } 
 
    } 
 
    /** 
     * Returns the identity of the remote server as defined in the specified certificate. The 
     * identity is defined in the subjectDN of the certificate and it can also be defined in 
     * the subjectAltName extension of type "xmpp". When the extension is being used then the 
     * identity defined in the extension in going to be returned. Otherwise, the value stored in 
     * the subjectDN is returned. 
     * 
     * @param x509Certificate the certificate the holds the identity of the remote server. 
     * @return the identity of the remote server as defined in the specified certificate. 
     */ 
    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);