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 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 }