Skip to content
CertificateLoader.java 5.32 KiB
Newer Older
/*
 * The contents of this file are subject to the Mozilla Public
 * License Version 1.1 (the "License"); you may not use this
 * file except in compliance with the License. You may obtain
 * a copy of the License at http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an
 * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
 * or implied. See the License for the specific language governing
 * rights and limitations under the License.
 *
 *
 * The Original Code is Java RASP toolkit.
 *
 * The Initial Developer of the Original Code is Lenio. Portions
Peter Sone Koldkjær's avatar
Peter Sone Koldkjær committed
 * created by Lenio are Copyright (C) 2007 Danish National IT and
 * Telecom Agency (http://www.itst.dk). All Rights Reserved.
 */

package dk.gov.oiosi.security.lookup;

import dk.gov.oiosi.security.RootCertificateConfig;
import org.apache.commons.configuration.ConfigurationUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
dlk-truelink's avatar
dlk-truelink committed
import java.io.File;
import java.net.URISyntaxException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
Peter Sone Koldkjær's avatar
Peter Sone Koldkjær committed
import java.util.ArrayList;
Jacob Lund Mogensen's avatar
Jacob Lund Mogensen committed
import java.util.Enumeration;
import java.util.List;
public class CertificateLoader {
    private static final Log log = LogFactory.getLog(CertificateLoader.class);
Peter Sone Koldkjær's avatar
Peter Sone Koldkjær committed
     * Tries to load the root certificate by searching the user home directory, the current classpath and the system classpath.
     *
     */
    public static X509Certificate GetCertificateFromCertificateStoreInformation(RootCertificateConfig rootCertConfig) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException {
        X509Certificate cert = null;
        // define receiver certificate
        KeyStore ks = KeyStore.getInstance("JKS");
        URL rootCertLocation = ConfigurationUtils.locate(rootCertConfig.getKeyStoreLocation());
        if (rootCertLocation != null) {
            ks.load(rootCertLocation.openStream(), rootCertConfig.getKeyStorePassword().toCharArray());
            cert = (X509Certificate) ks.getCertificate(rootCertConfig.getKeyLabel());
dlk-truelink's avatar
dlk-truelink committed
        } else {
            if (log.isWarnEnabled()) {
                String fileCanonicalPath = rootCertConfig.getKeyStoreLocation();
                try {
                    File file = new File(rootCertConfig.getKeyStoreLocation());
                    fileCanonicalPath = file.getCanonicalPath();
                } catch (Exception e) {
                    // Hide exception
                }
                log.warn("CertificateLoader: cannot locate key store by location " + rootCertConfig.getKeyStoreLocation() + ", among others tried file path " + fileCanonicalPath);
            }
dlk-truelink's avatar
dlk-truelink committed
        }
        if (log.isWarnEnabled() && rootCertLocation != null && cert == null) {
            log.warn("CertificateLoader: No certificate with label '" + rootCertConfig.getKeyLabel() + "' is found at key store at " + rootCertLocation);
    public static List<X509Certificate> GetCertificateListFromCertificateStoreInformation(RootCertificateConfig rootCertConfig) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException {
        X509Certificate x509Certificate;
Jacob Lund Mogensen's avatar
Jacob Lund Mogensen committed
        String aliasLabelPrefix = rootCertConfig.getKeyLabel();
        String alias;
        List<X509Certificate> x509RootCertificateList = new ArrayList<>();
Peter Sone Koldkjær's avatar
Peter Sone Koldkjær committed
        // define receiver certificate.
Peter Sone Koldkjær's avatar
Peter Sone Koldkjær committed
        KeyStore keyStore = KeyStore.getInstance("JKS");
Jacob Lund Mogensen's avatar
Jacob Lund Mogensen committed
        URL rootCertLocation = ConfigurationUtils.locate(rootCertConfig.getKeyStoreLocation());
        if (rootCertLocation != null) {
            if (log.isInfoEnabled()) {
                try {
                    log.info("Loading root certs from JKS=" + new File(rootCertLocation.toURI()).getAbsolutePath());
                } catch (URISyntaxException e) {
                    log.warn("Exception for JKS=" + rootCertConfig.getKeyStoreLocation() + ": " + e.getMessage(), e);
                }
            }
Peter Sone Koldkjær's avatar
Peter Sone Koldkjær committed
            keyStore.load(rootCertLocation.openStream(), rootCertConfig.getKeyStorePassword().toCharArray());
            Enumeration<String> enumeration = keyStore.aliases();
            while (enumeration.hasMoreElements()) {
Jacob Lund Mogensen's avatar
Jacob Lund Mogensen committed
                alias = enumeration.nextElement();
                if (alias.startsWith(aliasLabelPrefix)) {
Peter Sone Koldkjær's avatar
Peter Sone Koldkjær committed
                    x509Certificate = (X509Certificate) keyStore.getCertificate(alias);
                    if (x509Certificate != null) {
                        x509RootCertificateList.add(x509Certificate);
//                } else {
Jacob Lund Mogensen's avatar
Jacob Lund Mogensen committed
                    // certificate alias does not start with the desired prefix
                }
            }
        } else {
            log.error("Keystore not found at location=" + rootCertConfig.getKeyStoreLocation());
        if (x509RootCertificateList.isEmpty()) {
Peter Sone Koldkjær's avatar
Peter Sone Koldkjær committed
            log.error("No certificates found based on: " + rootCertConfig);
        } else {
            log.info(x509RootCertificateList.size() + " certificates found based on: " + rootCertConfig);
Peter Sone Koldkjær's avatar
Peter Sone Koldkjær committed
        }
        return x509RootCertificateList;
Peter Sone Koldkjær's avatar
Peter Sone Koldkjær committed
}