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