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 char[] 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       *
51       * @throws StoreConfigurationException Thrown if this instance cannot load the KeyStore.
52       * @deprecated Use KeyStoreConfiguration(String, char[], String, String)
53       */
54      @Deprecated
55      public KeyStoreConfiguration(final String location, final String password, final String keyStoreType,
56              final String keyManagerFactoryAlgorithm) throws StoreConfigurationException {
57          super(location, password, keyStoreType);
58          this.keyManagerFactoryAlgorithm = keyManagerFactoryAlgorithm == null ? KeyManagerFactory.getDefaultAlgorithm()
59                  : keyManagerFactoryAlgorithm;
60      }
61  
62      /**
63       * Creates a KeyStoreConfiguration.
64       *
65       * @param location
66       *        The location of the KeyStore, a file path, URL or resource.
67       * @param password
68       *        The password to access the KeyStore.
69       * @param keyStoreType
70       *        The KeyStore type, null defaults to {@code "JKS"}.
71       * @param keyManagerFactoryAlgorithm
72       *         The standard name of the requested algorithm. See the Java Secure Socket Extension Reference Guide for information about these names.
73       * @return a new KeyStoreConfiguration
74       * @throws StoreConfigurationException Thrown if this call cannot load the KeyStore.
75       */
76      @PluginFactory
77      public static KeyStoreConfiguration createKeyStoreConfiguration(
78              // @formatter:off
79              @PluginAttribute("location") final String location,
80              @PluginAttribute(value = "password", sensitive = true) final char[] password,
81              @PluginAttribute("type") final String keyStoreType,
82              @PluginAttribute("keyManagerFactoryAlgorithm") final String keyManagerFactoryAlgorithm) throws StoreConfigurationException {
83              // @formatter:on
84          return new KeyStoreConfiguration(location, password, keyStoreType,
85                  keyManagerFactoryAlgorithm);
86      }
87  
88      /**
89       * Creates a KeyStoreConfiguration.
90       *
91       * @param location
92       *        The location of the KeyStore, a file path, URL or resource.
93       * @param password
94       *        The password to access the KeyStore.
95       * @param keyStoreType
96       *        The KeyStore type, null defaults to {@code "JKS"}.
97       * @param keyManagerFactoryAlgorithm
98       *         The standard name of the requested algorithm. See the Java Secure Socket Extension Reference Guide for information about these names.
99       * @return a new KeyStoreConfiguration
100      * @throws StoreConfigurationException Thrown if this call cannot load the KeyStore.
101      * @deprecated Use createKeyStoreConfiguration(String, char[], String, String)
102      */
103     @Deprecated
104     public static KeyStoreConfiguration createKeyStoreConfiguration(
105             // @formatter:off
106             final String location,
107             final String password,
108             final String keyStoreType,
109             final String keyManagerFactoryAlgorithm) throws StoreConfigurationException {
110             // @formatter:on
111         return new KeyStoreConfiguration(location, password == null ? null : password.toCharArray(), keyStoreType,
112                 keyManagerFactoryAlgorithm);
113     }
114 
115     public KeyManagerFactory initKeyManagerFactory() throws NoSuchAlgorithmException, UnrecoverableKeyException,
116             KeyStoreException {
117         final KeyManagerFactory kmFactory = KeyManagerFactory.getInstance(this.keyManagerFactoryAlgorithm);
118         kmFactory.init(this.getKeyStore(), this.getPasswordAsCharArray());
119         return kmFactory;
120     }
121 
122     @Override
123     public int hashCode() {
124         final int prime = 31;
125         int result = super.hashCode();
126         result = prime * result + ((keyManagerFactoryAlgorithm == null) ? 0 : keyManagerFactoryAlgorithm.hashCode());
127         return result;
128     }
129 
130     @Override
131     public boolean equals(final Object obj) {
132         if (this == obj) {
133             return true;
134         }
135         if (!super.equals(obj)) {
136             return false;
137         }
138         if (getClass() != obj.getClass()) {
139             return false;
140         }
141         final KeyStoreConfiguration other = (KeyStoreConfiguration) obj;
142         if (keyManagerFactoryAlgorithm == null) {
143             if (other.keyManagerFactoryAlgorithm != null) {
144                 return false;
145             }
146         } else if (!keyManagerFactoryAlgorithm.equals(other.keyManagerFactoryAlgorithm)) {
147             return false;
148         }
149         return true;
150     }
151 
152     public String getKeyManagerFactoryAlgorithm() {
153         return keyManagerFactoryAlgorithm;
154     }
155 }