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  
22  import javax.net.ssl.TrustManagerFactory;
23  
24  import org.apache.logging.log4j.core.Core;
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 TrustStore
31   */
32  @Plugin(name = "TrustStore", category = Core.CATEGORY_NAME, printObject = true)
33  public class TrustStoreConfiguration extends AbstractKeyStoreConfiguration {
34  
35      private final String trustManagerFactoryAlgorithm;
36  
37      public TrustStoreConfiguration(final String location, final char[] password, final String keyStoreType,
38              final String trustManagerFactoryAlgorithm) throws StoreConfigurationException {
39          super(location, password, keyStoreType);
40          this.trustManagerFactoryAlgorithm = trustManagerFactoryAlgorithm == null ? TrustManagerFactory
41                  .getDefaultAlgorithm() : trustManagerFactoryAlgorithm;
42      }
43  
44      /**
45       * @deprecated Use TrustStoreConfiguration(String, char[], String, String)
46       */
47      @Deprecated
48      public TrustStoreConfiguration(final String location, final String password, final String keyStoreType,
49              final String trustManagerFactoryAlgorithm) throws StoreConfigurationException {
50          super(location, password, keyStoreType);
51          this.trustManagerFactoryAlgorithm = trustManagerFactoryAlgorithm == null ? TrustManagerFactory
52                  .getDefaultAlgorithm() : trustManagerFactoryAlgorithm;
53      }
54  
55      /**
56       * Creates a KeyStoreConfiguration.
57       *
58       * @param location
59       *        The location of the KeyStore, a file path, URL or resource.
60       * @param password
61       *        The password to access the KeyStore.
62       * @param keyStoreType
63       *        The KeyStore type, null defaults to {@code "JKS"}.
64       * @param trustManagerFactoryAlgorithm
65       *        The standard name of the requested trust management algorithm. See the Java Secure Socket Extension Reference Guide for information these names.
66       * @return a new TrustStoreConfiguration
67       * @throws StoreConfigurationException Thrown if this instance cannot load the KeyStore.
68       */
69      @PluginFactory
70      public static TrustStoreConfiguration createKeyStoreConfiguration(
71              // @formatter:off
72              @PluginAttribute("location") final String location,
73              @PluginAttribute(value = "password", sensitive = true) final char[] password,
74              @PluginAttribute("type") final String keyStoreType,
75              @PluginAttribute("trustManagerFactoryAlgorithm") final String trustManagerFactoryAlgorithm) throws StoreConfigurationException {
76              // @formatter:on
77          return new TrustStoreConfiguration(location, password, keyStoreType, trustManagerFactoryAlgorithm);
78      }
79  
80      /**
81       * Creates a KeyStoreConfiguration.
82       *
83       * @param location
84       *        The location of the KeyStore, a file path, URL or resource.
85       * @param password
86       *        The password to access the KeyStore.
87       * @param keyStoreType
88       *        The KeyStore type, null defaults to {@code "JKS"}.
89       * @param trustManagerFactoryAlgorithm
90       *        The standard name of the requested trust management algorithm. See the Java Secure Socket Extension Reference Guide for information these names.
91       * @return a new TrustStoreConfiguration
92       * @throws StoreConfigurationException Thrown if this instance cannot load the KeyStore.
93       * @deprecated Use createKeyStoreConfiguration(String, char[], String, String)
94       */
95      @Deprecated
96      public static TrustStoreConfiguration createKeyStoreConfiguration(
97              // @formatter:off
98              final String location,
99              final String password,
100             final String keyStoreType,
101             final String trustManagerFactoryAlgorithm) throws StoreConfigurationException {
102             // @formatter:on
103         return new TrustStoreConfiguration(location, password, keyStoreType, trustManagerFactoryAlgorithm);
104     }
105 
106     public TrustManagerFactory initTrustManagerFactory() throws NoSuchAlgorithmException, KeyStoreException {
107         final TrustManagerFactory tmFactory = TrustManagerFactory.getInstance(this.trustManagerFactoryAlgorithm);
108         tmFactory.init(this.getKeyStore());
109         return tmFactory;
110     }
111 
112     @Override
113     public int hashCode() {
114         final int prime = 31;
115         int result = super.hashCode();
116         result = prime * result
117                 + ((trustManagerFactoryAlgorithm == null) ? 0 : trustManagerFactoryAlgorithm.hashCode());
118         return result;
119     }
120 
121     @Override
122     public boolean equals(final Object obj) {
123         if (this == obj) {
124             return true;
125         }
126         if (!super.equals(obj)) {
127             return false;
128         }
129         if (getClass() != obj.getClass()) {
130             return false;
131         }
132         final TrustStoreConfiguration other = (TrustStoreConfiguration) obj;
133         if (trustManagerFactoryAlgorithm == null) {
134             if (other.trustManagerFactoryAlgorithm != null) {
135                 return false;
136             }
137         } else if (!trustManagerFactoryAlgorithm.equals(other.trustManagerFactoryAlgorithm)) {
138             return false;
139         }
140         return true;
141     }
142 
143     public String getTrustManagerFactoryAlgorithm() {
144         return trustManagerFactoryAlgorithm;
145     }
146 }