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