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.security.KeyStoreException;
020    import java.security.NoSuchAlgorithmException;
021    import java.security.UnrecoverableKeyException;
022    
023    import javax.net.ssl.KeyManagerFactory;
024    
025    import org.apache.logging.log4j.core.config.plugins.Plugin;
026    import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
027    import org.apache.logging.log4j.core.config.plugins.PluginFactory;
028    
029    /**
030     * Configuration of the KeyStore
031     */
032    @Plugin(name = "KeyStore", category = "Core", printObject = true)
033    public class KeyStoreConfiguration extends AbstractKeyStoreConfiguration {
034    
035        private final String keyManagerFactoryAlgorithm;
036    
037        public KeyStoreConfiguration(final String location, final String password, final String keyStoreType,
038                final String keyManagerFactoryAlgorithm) throws StoreConfigurationException {
039            super(location, password, keyStoreType);
040            this.keyManagerFactoryAlgorithm = keyManagerFactoryAlgorithm == null ? KeyManagerFactory.getDefaultAlgorithm()
041                    : keyManagerFactoryAlgorithm;
042        }
043    
044        /**
045         * Creates a KeyStoreConfiguration.
046         * 
047         * @param location
048         *        The location of the KeyStore.
049         * @param password
050         *        The password to access the KeyStore.
051         * @param keyStoreType
052         *        The KeyStore type, null defaults to {@code "JKS"}.
053         * @param keyManagerFactoryAlgorithm
054         *         The standard name of the requested algorithm. See the Java Secure Socket Extension Reference Guide for information about these names.
055         * @return a new KeyStoreConfiguration
056         * @throws StoreConfigurationException
057         */
058        @PluginFactory
059        public static KeyStoreConfiguration createKeyStoreConfiguration(
060                // @formatter:off
061                @PluginAttribute("location") final String location,
062                @PluginAttribute("password") final String password,
063                @PluginAttribute("type") final String keyStoreType, 
064                @PluginAttribute("keyManagerFactoryAlgorithm") final String keyManagerFactoryAlgorithm) throws StoreConfigurationException {
065                // @formatter:on
066            return new KeyStoreConfiguration(location, password, keyStoreType, keyManagerFactoryAlgorithm);
067        }
068    
069        public KeyManagerFactory initKeyManagerFactory() throws NoSuchAlgorithmException, UnrecoverableKeyException,
070                KeyStoreException {
071            final KeyManagerFactory kmFactory = KeyManagerFactory.getInstance(this.keyManagerFactoryAlgorithm);
072            kmFactory.init(this.getKeyStore(), this.getPasswordAsCharArray());
073            return kmFactory;
074        }
075    }