View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.net.ssl;
18  
19  import java.security.KeyStoreException;
20  import java.security.NoSuchAlgorithmException;
21  import java.security.UnrecoverableKeyException;
22  
23  import javax.net.ssl.KeyManagerFactory;
24  
25  import org.apache.logging.log4j.core.config.plugins.Plugin;
26  import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
27  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
28  
29  /**
30   * Configuration of the KeyStore
31   */
32  @Plugin(name = "KeyStore", category = "Core", printObject = true)
33  public class KeyStoreConfiguration extends AbstractKeyStoreConfiguration {
34  
35      private final String keyManagerFactoryAlgorithm;
36  
37      public KeyStoreConfiguration(final String location, final String password, final String keyStoreType,
38              final String keyManagerFactoryAlgorithm) throws StoreConfigurationException {
39          super(location, password, keyStoreType);
40          this.keyManagerFactoryAlgorithm = keyManagerFactoryAlgorithm == null ? KeyManagerFactory.getDefaultAlgorithm()
41                  : keyManagerFactoryAlgorithm;
42      }
43  
44      /**
45       * Creates a KeyStoreConfiguration.
46       * 
47       * @param location
48       *        The location of the KeyStore.
49       * @param password
50       *        The password to access the KeyStore.
51       * @param keyStoreType
52       *        The KeyStore type, null defaults to {@code "JKS"}.
53       * @param keyManagerFactoryAlgorithm
54       *         The standard name of the requested algorithm. See the Java Secure Socket Extension Reference Guide for information about these names.
55       * @return a new KeyStoreConfiguration
56       * @throws StoreConfigurationException
57       */
58      @PluginFactory
59      public static KeyStoreConfiguration createKeyStoreConfiguration(
60              // @formatter:off
61              @PluginAttribute("location") final String location,
62              @PluginAttribute("password") final String password,
63              @PluginAttribute("type") final String keyStoreType, 
64              @PluginAttribute("keyManagerFactoryAlgorithm") final String keyManagerFactoryAlgorithm) throws StoreConfigurationException {
65              // @formatter:on
66          return new KeyStoreConfiguration(location, password, keyStoreType, keyManagerFactoryAlgorithm);
67      }
68  
69      public KeyManagerFactory initKeyManagerFactory() throws NoSuchAlgorithmException, UnrecoverableKeyException,
70              KeyStoreException {
71          final KeyManagerFactory kmFactory = KeyManagerFactory.getInstance(this.keyManagerFactoryAlgorithm);
72          kmFactory.init(this.getKeyStore(), this.getPasswordAsCharArray());
73          return kmFactory;
74      }
75  }