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.Core;
26  import org.apache.logging.log4j.core.config.plugins.Plugin;
27  import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
28  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
29  
30  /**
31   * Configuration of the KeyStore
32   */
33  @Plugin(name = "KeyStore", category = Core.CATEGORY_NAME, printObject = true)
34  public class KeyStoreConfiguration extends AbstractKeyStoreConfiguration {
35  
36      private final String keyManagerFactoryAlgorithm;
37  
38      /**
39       * 
40       * @throws StoreConfigurationException Thrown if this instance cannot load the KeyStore.
41       */
42      public KeyStoreConfiguration(final String location, final String password, final String keyStoreType,
43              final String keyManagerFactoryAlgorithm) throws StoreConfigurationException {
44          super(location, password, keyStoreType);
45          this.keyManagerFactoryAlgorithm = keyManagerFactoryAlgorithm == null ? KeyManagerFactory.getDefaultAlgorithm()
46                  : keyManagerFactoryAlgorithm;
47      }
48  
49      /**
50       * Creates a KeyStoreConfiguration.
51       * 
52       * @param location
53       *        The location of the KeyStore.
54       * @param password
55       *        The password to access the KeyStore.
56       * @param keyStoreType
57       *        The KeyStore type, null defaults to {@code "JKS"}.
58       * @param keyManagerFactoryAlgorithm
59       *         The standard name of the requested algorithm. See the Java Secure Socket Extension Reference Guide for information about these names.
60       * @return a new KeyStoreConfiguration
61       * @throws StoreConfigurationException Thrown if this call cannot load the KeyStore.
62       */
63      @PluginFactory
64      public static KeyStoreConfiguration createKeyStoreConfiguration(
65              // @formatter:off
66              @PluginAttribute("location") final String location,
67              @PluginAttribute(value = "password", sensitive = true) final String password,
68              @PluginAttribute("type") final String keyStoreType, 
69              @PluginAttribute("keyManagerFactoryAlgorithm") final String keyManagerFactoryAlgorithm) throws StoreConfigurationException {
70              // @formatter:on
71          return new KeyStoreConfiguration(location, password, keyStoreType, keyManagerFactoryAlgorithm);
72      }
73  
74      public KeyManagerFactory initKeyManagerFactory() throws NoSuchAlgorithmException, UnrecoverableKeyException,
75              KeyStoreException {
76          final KeyManagerFactory kmFactory = KeyManagerFactory.getInstance(this.keyManagerFactoryAlgorithm);
77          kmFactory.init(this.getKeyStore(), this.getPasswordAsCharArray());
78          return kmFactory;
79      }
80  
81      @Override
82      public int hashCode() {
83          final int prime = 31;
84          int result = super.hashCode();
85          result = prime * result + ((keyManagerFactoryAlgorithm == null) ? 0 : keyManagerFactoryAlgorithm.hashCode());
86          return result;
87      }
88  
89      @Override
90      public boolean equals(final Object obj) {
91          if (this == obj) {
92              return true;
93          }
94          if (!super.equals(obj)) {
95              return false;
96          }
97          if (getClass() != obj.getClass()) {
98              return false;
99          }
100         final KeyStoreConfiguration other = (KeyStoreConfiguration) obj;
101         if (keyManagerFactoryAlgorithm == null) {
102             if (other.keyManagerFactoryAlgorithm != null) {
103                 return false;
104             }
105         } else if (!keyManagerFactoryAlgorithm.equals(other.keyManagerFactoryAlgorithm)) {
106             return false;
107         }
108         return true;
109     }
110 }