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.io.FileNotFoundException;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.security.KeyStore;
23  import java.security.KeyStoreException;
24  import java.security.NoSuchAlgorithmException;
25  import java.security.cert.CertificateException;
26  
27  import org.apache.logging.log4j.core.config.ConfigurationSource;
28  import org.apache.logging.log4j.core.util.NetUtils;
29  
30  /**
31   * Configuration of the KeyStore
32   */
33  public class AbstractKeyStoreConfiguration extends StoreConfiguration<KeyStore> {
34      private final KeyStore keyStore;
35      private final String keyStoreType;
36  
37      public AbstractKeyStoreConfiguration(final String location, final char[] password, final String keyStoreType)
38              throws StoreConfigurationException {
39          super(location, password);
40          this.keyStoreType = keyStoreType == null ? SslConfigurationDefaults.KEYSTORE_TYPE : keyStoreType;
41          this.keyStore = this.load();
42      }
43  
44      /*
45       * @deprecated Use
46       * org.apache.logging.log4j.core.net.ssl.AbstractKeyStoreConfiguration.AbstractKeyStoreConfiguration(String, char[],
47       * String)
48       */
49      @Deprecated
50      public AbstractKeyStoreConfiguration(final String location, final String password, final String keyStoreType)
51              throws StoreConfigurationException {
52          super(location, password);
53          this.keyStoreType = keyStoreType == null ? SslConfigurationDefaults.KEYSTORE_TYPE : keyStoreType;
54          this.keyStore = this.load();
55      }
56  
57      @Override
58      protected KeyStore load() throws StoreConfigurationException {
59          final String loadLocation = this.getLocation();
60          LOGGER.debug("Loading keystore from location {}", loadLocation);
61          try {
62              if (loadLocation == null) {
63                  throw new IOException("The location is null");
64              }
65              try (final InputStream fin = openInputStream(loadLocation)) {
66                  final KeyStore ks = KeyStore.getInstance(this.keyStoreType);
67                  ks.load(fin, this.getPasswordAsCharArray());
68                  LOGGER.debug("KeyStore successfully loaded from location {}", loadLocation);
69                  return ks;
70              }
71          } catch (final CertificateException e) {
72              LOGGER.error("No Provider supports a KeyStoreSpi implementation for the specified type {} for location {}", this.keyStoreType, loadLocation, e);
73              throw new StoreConfigurationException(loadLocation, e);
74          } catch (final NoSuchAlgorithmException e) {
75              LOGGER.error("The algorithm used to check the integrity of the keystore cannot be found for location {}", loadLocation, e);
76              throw new StoreConfigurationException(loadLocation, e);
77          } catch (final KeyStoreException e) {
78              LOGGER.error("KeyStoreException for location {}", loadLocation, e);
79              throw new StoreConfigurationException(loadLocation, e);
80          } catch (final FileNotFoundException e) {
81              LOGGER.error("The keystore file {} is not found", loadLocation, e);
82              throw new StoreConfigurationException(loadLocation, e);
83          } catch (final IOException e) {
84              LOGGER.error("Something is wrong with the format of the keystore or the given password for location", loadLocation, e);
85              throw new StoreConfigurationException(loadLocation, e);
86          }
87      }
88  
89      private InputStream openInputStream(final String filePathOrUri) {
90          return ConfigurationSource.fromUri(NetUtils.toURI(filePathOrUri)).getInputStream();
91      }
92  
93      public KeyStore getKeyStore() {
94          return this.keyStore;
95      }
96  
97      @Override
98      public int hashCode() {
99          final int prime = 31;
100         int result = super.hashCode();
101         result = prime * result + ((keyStore == null) ? 0 : keyStore.hashCode());
102         result = prime * result + ((keyStoreType == null) ? 0 : keyStoreType.hashCode());
103         return result;
104     }
105 
106     @Override
107     public boolean equals(final Object obj) {
108         if (this == obj) {
109             return true;
110         }
111         if (!super.equals(obj)) {
112             return false;
113         }
114         if (getClass() != obj.getClass()) {
115             return false;
116         }
117         final AbstractKeyStoreConfiguration other = (AbstractKeyStoreConfiguration) obj;
118         if (keyStore == null) {
119             if (other.keyStore != null) {
120                 return false;
121             }
122         } else if (!keyStore.equals(other.keyStore)) {
123             return false;
124         }
125         if (keyStoreType == null) {
126             if (other.keyStoreType != null) {
127                 return false;
128             }
129         } else if (!keyStoreType.equals(other.keyStoreType)) {
130             return false;
131         }
132         return true;
133     }
134 
135     public String getKeyStoreType() {
136         return keyStoreType;
137     }
138 
139 }