Example usage for org.apache.commons.ssl KeyMaterial getAssociatedCertificateChains

List of usage examples for org.apache.commons.ssl KeyMaterial getAssociatedCertificateChains

Introduction

In this page you can find the example usage for org.apache.commons.ssl KeyMaterial getAssociatedCertificateChains.

Prototype

public List getAssociatedCertificateChains() 

Source Link

Usage

From source file:org.fedoraproject.eclipse.packager.FedoraSSL.java

/**
 * Determine FAS username from fedora cert file.
 * //from w w w.  j av  a2s.c  om
 * @return Username if retrieval is successful.
 *         {@link FedoraSSL#UNKNOWN_USER} otherwise.
 */
public String getUsernameFromCert() {
    if (fedoraCert.exists()) {
        KeyMaterial kmat;
        try {
            kmat = new KeyMaterial(fedoraCert, fedoraCert, new char[0]);
            List<?> chains = kmat.getAssociatedCertificateChains();
            Iterator<?> it = chains.iterator();
            ArrayList<String> cns = new ArrayList<String>();
            while (it.hasNext()) {
                X509Certificate[] certs = (X509Certificate[]) it.next();
                if (certs != null) {
                    for (int i = 0; i < certs.length; i++) {
                        cns.add(Certificates.getCN(certs[i]));
                    }
                }
            }
            return cns.get(0);
        } catch (GeneralSecurityException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return UNKNOWN_USER;
}

From source file:org.fedoraproject.eclipse.packager.FedoraSSL.java

/**
 * Determine if FAS certificate (~/.fedora.cert) is valid.
 * /*  w ww .  j  a  v a  2  s .c om*/
 * @return {@code true} if certificate exist and is valid. {@code false}
 *         otherwise.
 */
public boolean isFedoraCertValid() {
    if (fedoraCert.exists()) {
        KeyMaterial kmat;
        try {
            kmat = new KeyMaterial(fedoraCert, fedoraCert, new char[0]);
            List<?> chains = kmat.getAssociatedCertificateChains();
            Iterator<?> it = chains.iterator();
            while (it.hasNext()) {
                X509Certificate[] certs = (X509Certificate[]) it.next();
                if (certs != null) {
                    if (certs.length == 1) {
                        try {
                            certs[0].checkValidity();
                            return true;
                        } catch (CertificateExpiredException e) {
                            return false;
                        } catch (CertificateNotYetValidException e) {
                            return false;
                        }
                    }
                }
            }
        } catch (GeneralSecurityException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return false;
}