if (certchain == null) { return null; }
    public static Certificate[] getCertChain(KeyStore keyStore, String privateKeyAlias)        throws KeyStoreException {        log.debug(">getCertChain: alias='" + privateKeyAlias + "'");        Certificate[] certchain = keyStore.getCertificateChain(privateKeyAlias);        log.debug("Certchain retrieved from alias '" + privateKeyAlias + "' has length " +            certchain.length);        if (certchain.length < 1) {            log.error("Cannot load certificate chain with alias '" + privateKeyAlias +                "' from keystore.");            log.debug("<getCertChain: alias='" + privateKeyAlias + "', retlength=" +                certchain.length);            return certchain;        } else if (certchain.length > 0) {            if (CertTools.isSelfSigned((X509Certificate) certchain[certchain.length - 1])) {                log.debug("Issuer='" +                    CertTools.getIssuerDN((X509Certificate) certchain[certchain.length - 1]) +                    "'.");                log.debug("Subject='" +                    CertTools.getSubjectDN((X509Certificate) certchain[certchain.length - 1]) +                    "'.");                log.debug("<getCertChain: alias='" + privateKeyAlias + "', retlength=" +                    certchain.length);                return certchain;            }        }        // If we came here, we have a cert which is not root cert in 'cert'        ArrayList array = new ArrayList();        for (int i = 0; i < certchain.length; i++) {            array.add(certchain[i]);        }        boolean stop = false;        while (!stop) {            X509Certificate cert = (X509Certificate) array.get(array.size() - 1);            String ialias = CertTools.getPartFromDN(CertTools.getIssuerDN(cert), "CN");            Certificate[] chain1 = keyStore.getCertificateChain(ialias);            if (chain1 == null) {                stop = true;            } else {                log.debug("Loaded certificate chain with length " + chain1.length +                    " with alias '" + ialias + "'.");                if (chain1.length == 0) {                    log.error("No RootCA certificate found!");                    stop = true;                }                for (int j = 0; j < chain1.length; j++) {                    array.add(chain1[j]);                    // If one cert is slefsigned, we have found a root certificate, we don't need to go on anymore                    if (CertTools.isSelfSigned((X509Certificate) chain1[j])) {                        stop = true;                    }                }            }        }        Certificate[] ret = new Certificate[array.size()];        for (int i = 0; i < ret.length; i++) {            ret[i] = (X509Certificate) array.get(i);            log.debug("Issuer='" + CertTools.getIssuerDN((X509Certificate) ret[i]) + "'.");            log.debug("Subject='" + CertTools.getSubjectDN((X509Certificate) ret[i]) + "'.");        }        log.debug("<getCertChain: alias='" + privateKeyAlias + "', retlength=" + ret.length);        return ret;    } // getCertChain