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 org.apache.logging.log4j.core.config.plugins.Plugin; 020import org.apache.logging.log4j.core.config.plugins.PluginAttribute; 021import org.apache.logging.log4j.core.config.plugins.PluginFactory; 022import java.io.FileInputStream; 023import java.io.FileNotFoundException; 024import java.io.IOException; 025import java.security.KeyStore; 026import java.security.KeyStoreException; 027import java.security.NoSuchAlgorithmException; 028import java.security.cert.CertificateException; 029 030/** 031 * Configuration of the KeyStore 032 */ 033@Plugin(name = "keyStore", category = "Core", printObject = true) 034public class KeyStoreConfiguration extends StoreConfiguration { 035 private KeyStore keyStore; 036 private String keyStoreType; 037 038 039 public KeyStoreConfiguration(String location, String password) { 040 super(location, password); 041 this.keyStoreType = SSLConfigurationDefaults.KEYSTORE_TYPE; 042 this.keyStore = null; 043 } 044 045 @Override 046 protected void load() throws StoreConfigurationException { 047 FileInputStream fin = null; 048 049 LOGGER.debug("Loading keystore from file with params(location={})", getLocation()); 050 try { 051 if (getLocation() == null) { 052 throw new IOException("The location is null"); 053 } 054 fin = new FileInputStream(getLocation()); 055 KeyStore ks = KeyStore.getInstance(keyStoreType); 056 ks.load(fin, getPasswordAsCharArray()); 057 keyStore = ks; 058 } 059 catch (CertificateException e) { 060 LOGGER.error("No Provider supports a KeyStoreSpi implementation for the specified type {}", keyStoreType); 061 throw new StoreConfigurationException(e); 062 } catch (NoSuchAlgorithmException e) { 063 LOGGER.error("The algorithm used to check the integrity of the keystore cannot be found"); 064 throw new StoreConfigurationException(e); 065 } catch (KeyStoreException e) { 066 LOGGER.error(e); 067 throw new StoreConfigurationException(e); 068 } catch (FileNotFoundException e) { 069 LOGGER.error("The keystore file({}) is not found", getLocation()); 070 throw new StoreConfigurationException(e); 071 } catch (IOException e) { 072 LOGGER.error("Something is wrong with the format of the keystore or the given password"); 073 throw new StoreConfigurationException(e); 074 } 075 finally { 076 try { 077 if (fin != null) { 078 fin.close(); 079 } 080 } catch (IOException e) { 081 } 082 } 083 LOGGER.debug("Keystore successfully loaded with params(location={})", getLocation()); 084 } 085 086 public KeyStore getKeyStore() throws StoreConfigurationException { 087 if (keyStore == null) { 088 load(); 089 } 090 return keyStore; 091 } 092 093 /** 094 * Create a KeyStoreConfiguration. 095 * @param location The location of the KeyStore. 096 * @param password The password to access the KeyStore. 097 * @return 098 */ 099 @PluginFactory 100 public static KeyStoreConfiguration createKeyStoreConfiguration( 101 @PluginAttribute("location") String location, 102 @PluginAttribute("password") String password) { 103 return new KeyStoreConfiguration(location, password); 104 } 105}