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.FileInputStream;
20  import java.io.FileNotFoundException;
21  import java.io.IOException;
22  import java.security.KeyStore;
23  import java.security.KeyStoreException;
24  import java.security.NoSuchAlgorithmException;
25  import java.security.cert.CertificateException;
26  
27  /**
28   * Configuration of the KeyStore
29   */
30  public class AbstractKeyStoreConfiguration extends StoreConfiguration<KeyStore> {
31      private final KeyStore keyStore;
32      private final String keyStoreType;
33  
34      public AbstractKeyStoreConfiguration(final String location, final String password, final String keyStoreType)
35              throws StoreConfigurationException {
36          super(location, password);
37          this.keyStoreType = keyStoreType == null ? SslConfigurationDefaults.KEYSTORE_TYPE : keyStoreType;
38          this.keyStore = this.load();
39      }
40  
41      @Override
42      protected KeyStore load() throws StoreConfigurationException {
43          FileInputStream fin = null;
44  
45          LOGGER.debug("Loading keystore from file with params(location={})", this.getLocation());
46          try {
47              if (this.getLocation() == null) {
48                  throw new IOException("The location is null");
49              }
50              fin = new FileInputStream(this.getLocation());
51              final KeyStore ks = KeyStore.getInstance(this.keyStoreType);
52              ks.load(fin, this.getPasswordAsCharArray());
53              LOGGER.debug("Keystore successfully loaded with params(location={})", this.getLocation());
54              return ks;
55          } catch (final CertificateException e) {
56              LOGGER.error("No Provider supports a KeyStoreSpi implementation for the specified type {}", this.keyStoreType);
57              throw new StoreConfigurationException(e);
58          } catch (final NoSuchAlgorithmException e) {
59              LOGGER.error("The algorithm used to check the integrity of the keystore cannot be found");
60              throw new StoreConfigurationException(e);
61          } catch (final KeyStoreException e) {
62              LOGGER.error(e);
63              throw new StoreConfigurationException(e);
64          } catch (final FileNotFoundException e) {
65              LOGGER.error("The keystore file({}) is not found", this.getLocation());
66              throw new StoreConfigurationException(e);
67          } catch (final IOException e) {
68              LOGGER.error("Something is wrong with the format of the keystore or the given password");
69              throw new StoreConfigurationException(e);
70          } finally {
71              try {
72                  if (fin != null) {
73                      fin.close();
74                  }
75              } catch (final IOException e) {
76                  LOGGER.debug(e);
77              }
78          }
79      }
80  
81      public KeyStore getKeyStore() {
82          return this.keyStore;
83      }
84  
85  }