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 */
017 package org.apache.logging.log4j.core.net.ssl;
018
019 import java.io.FileInputStream;
020 import java.io.FileNotFoundException;
021 import java.io.IOException;
022 import java.security.KeyStore;
023 import java.security.KeyStoreException;
024 import java.security.NoSuchAlgorithmException;
025 import java.security.cert.CertificateException;
026
027 /**
028 * Configuration of the KeyStore
029 */
030 public 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 FileInputStream fin = null;
044
045 LOGGER.debug("Loading keystore from file with params(location={})", this.getLocation());
046 try {
047 if (this.getLocation() == null) {
048 throw new IOException("The location is null");
049 }
050 fin = new FileInputStream(this.getLocation());
051 final KeyStore ks = KeyStore.getInstance(this.keyStoreType);
052 ks.load(fin, this.getPasswordAsCharArray());
053 LOGGER.debug("Keystore successfully loaded with params(location={})", this.getLocation());
054 return ks;
055 } catch (final CertificateException e) {
056 LOGGER.error("No Provider supports a KeyStoreSpi implementation for the specified type {}", this.keyStoreType);
057 throw new StoreConfigurationException(e);
058 } catch (final NoSuchAlgorithmException e) {
059 LOGGER.error("The algorithm used to check the integrity of the keystore cannot be found");
060 throw new StoreConfigurationException(e);
061 } catch (final KeyStoreException e) {
062 LOGGER.error(e);
063 throw new StoreConfigurationException(e);
064 } catch (final FileNotFoundException e) {
065 LOGGER.error("The keystore file({}) is not found", this.getLocation());
066 throw new StoreConfigurationException(e);
067 } catch (final IOException e) {
068 LOGGER.error("Something is wrong with the format of the keystore or the given password");
069 throw new StoreConfigurationException(e);
070 } finally {
071 try {
072 if (fin != null) {
073 fin.close();
074 }
075 } catch (final IOException e) {
076 LOGGER.debug(e);
077 }
078 }
079 }
080
081 public KeyStore getKeyStore() {
082 return this.keyStore;
083 }
084
085 }