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          LOGGER.debug("Loading keystore from file with params(location={})", this.getLocation());
44          try {
45              if (this.getLocation() == null) {
46                  throw new IOException("The location is null");
47              }
48              try (final FileInputStream fin = new FileInputStream(this.getLocation())) {
49                  final KeyStore ks = KeyStore.getInstance(this.keyStoreType);
50                  ks.load(fin, this.getPasswordAsCharArray());
51                  LOGGER.debug("Keystore successfully loaded with params(location={})", this.getLocation());
52                  return ks;
53              }
54          } catch (final CertificateException e) {
55              LOGGER.error("No Provider supports a KeyStoreSpi implementation for the specified type" + this.keyStoreType, e);
56              throw new StoreConfigurationException(e);
57          } catch (final NoSuchAlgorithmException e) {
58              LOGGER.error("The algorithm used to check the integrity of the keystore cannot be found", e);
59              throw new StoreConfigurationException(e);
60          } catch (final KeyStoreException e) {
61              LOGGER.error(e);
62              throw new StoreConfigurationException(e);
63          } catch (final FileNotFoundException e) {
64              LOGGER.error("The keystore file(" + this.getLocation() + ") is not found", e);
65              throw new StoreConfigurationException(e);
66          } catch (final IOException e) {
67              LOGGER.error("Something is wrong with the format of the keystore or the given password", e);
68              throw new StoreConfigurationException(e);
69          }
70      }
71  
72      public KeyStore getKeyStore() {
73          return this.keyStore;
74      }
75  
76  }