001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.core.net.ssl;
018
019import java.io.FileInputStream;
020import java.io.FileNotFoundException;
021import java.io.IOException;
022import java.security.KeyStore;
023import java.security.KeyStoreException;
024import java.security.NoSuchAlgorithmException;
025import java.security.cert.CertificateException;
026
027/**
028 * Configuration of the KeyStore
029 */
030public class AbstractKeyStoreConfiguration extends StoreConfiguration<KeyStore> {
031    private final KeyStore keyStore;
032    private final String keyStoreType;
033
034    public AbstractKeyStoreConfiguration(final String location, final String password, final String keyStoreType)
035            throws StoreConfigurationException {
036        super(location, password);
037        this.keyStoreType = keyStoreType == null ? SslConfigurationDefaults.KEYSTORE_TYPE : keyStoreType;
038        this.keyStore = this.load();
039    }
040
041    @Override
042    protected KeyStore load() throws StoreConfigurationException {
043        LOGGER.debug("Loading keystore from file with params(location={})", this.getLocation());
044        try {
045            if (this.getLocation() == null) {
046                throw new IOException("The location is null");
047            }
048            try (final FileInputStream fin = new FileInputStream(this.getLocation())) {
049                final KeyStore ks = KeyStore.getInstance(this.keyStoreType);
050                ks.load(fin, this.getPasswordAsCharArray());
051                LOGGER.debug("Keystore successfully loaded with params(location={})", this.getLocation());
052                return ks;
053            }
054        } catch (final CertificateException e) {
055            LOGGER.error("No Provider supports a KeyStoreSpi implementation for the specified type" + this.keyStoreType, e);
056            throw new StoreConfigurationException(e);
057        } catch (final NoSuchAlgorithmException e) {
058            LOGGER.error("The algorithm used to check the integrity of the keystore cannot be found", e);
059            throw new StoreConfigurationException(e);
060        } catch (final KeyStoreException e) {
061            LOGGER.error(e);
062            throw new StoreConfigurationException(e);
063        } catch (final FileNotFoundException e) {
064            LOGGER.error("The keystore file(" + this.getLocation() + ") is not found", e);
065            throw new StoreConfigurationException(e);
066        } catch (final IOException e) {
067            LOGGER.error("Something is wrong with the format of the keystore or the given password", e);
068            throw new StoreConfigurationException(e);
069        }
070    }
071
072    public KeyStore getKeyStore() {
073        return this.keyStore;
074    }
075
076}