001/**
002 * Copyright 2017 Emmanuel Bourg
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package net.jsign;
018
019import java.io.File;
020import java.io.FileInputStream;
021import java.security.KeyStore;
022import java.security.KeyStoreException;
023import java.security.Provider;
024
025/**
026 * Helper class for loading KeyStores (JKS, PKCS#12 or PKCS#11).
027 * 
028 * @author Emmanuel Bourg
029 * @since 2.0
030 */
031public class KeyStoreUtils {
032
033    private KeyStoreUtils() {
034    }
035
036    /**
037     * Load the keystore from the specified file.
038     * 
039     * @param keystore   the file containing the keystore
040     * @param storetype  the type of the keystore (either JKS, PKCS12 or PKCS11).
041     *                   If null the type is inferred from the extension of the file (.p12 or .pfx for PKCS#12 keystores)
042     * @param storepass  The password of the keystore
043     * @param provider   The security provider used to load the keystore (must be specified for PKCS#11 keystores)
044     * @throws KeyStoreException thrown if the keystore cannot be loaded
045     */
046    public static KeyStore load(File keystore, String storetype, String storepass, Provider provider) throws KeyStoreException {
047        if (keystore != null && storetype == null) {
048            // guess the type of the keystore from the extension of the file
049            String filename = keystore.getName().toLowerCase();
050            if (filename.endsWith(".p12") || filename.endsWith(".pfx")) {
051                storetype = "PKCS12";
052            } else {
053                storetype = "JKS";
054            }
055        }
056        
057        KeyStore ks;
058        try {
059            if ("PKCS11".equals(storetype)) {
060                ks = KeyStore.getInstance(storetype, provider);
061            } else {
062                ks = KeyStore.getInstance(storetype);
063            }
064        } catch (KeyStoreException e) {
065            throw new KeyStoreException("keystore type '" + storetype + "' is not supported", e);
066        }
067
068        if (keystore == null || !keystore.exists()) {
069            throw new KeyStoreException("The keystore " + keystore + " couldn't be found");
070        }
071        
072        try {
073            FileInputStream in = "PKCS11".equals(storetype) ? null : new FileInputStream(keystore);
074            try {
075                ks.load(in, storepass != null ? storepass.toCharArray() : null);
076            } finally {
077                if (in != null) {
078                    in.close();
079                }
080            }
081        } catch (Exception e) {
082            throw new KeyStoreException("Unable to load the keystore " + keystore, e);
083        }
084        
085        return ks;
086    }
087}