1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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 }