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 org.apache.logging.log4j.core.config.plugins.Plugin;
20 import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
21 import org.apache.logging.log4j.core.config.plugins.PluginFactory;
22 import org.apache.logging.log4j.status.StatusLogger;
23
24 import java.io.*;
25 import java.security.KeyStore;
26 import java.security.KeyStoreException;
27 import java.security.NoSuchAlgorithmException;
28 import java.security.cert.CertificateException;
29
30
31
32
33 @Plugin(name = "trustStore", category = "Core", printObject = true)
34 public class TrustStoreConfiguration extends StoreConfiguration {
35 private KeyStore trustStore;
36 private String trustStoreType;
37
38 public TrustStoreConfiguration(String location, String password) {
39 super(location, password);
40 trustStoreType = SSLConfigurationDefaults.KEYSTORE_TYPE;
41 trustStore = null;
42 }
43
44 @Override
45 protected void load() throws StoreConfigurationException {
46 KeyStore ts = null;
47 InputStream in = null;
48
49 LOGGER.debug("Loading truststore from file with params(location={})", getLocation());
50 try {
51 if (getLocation() == null) {
52 throw new IOException("The location is null");
53 }
54 ts = KeyStore.getInstance(trustStoreType);
55 in = new FileInputStream(getLocation());
56 ts.load(in, getPasswordAsCharArray());
57 }
58 catch (CertificateException e) {
59 LOGGER.error("No Provider supports a KeyStoreSpi implementation for the specified type {}", trustStoreType);
60 throw new StoreConfigurationException(e);
61 } catch (NoSuchAlgorithmException e) {
62 LOGGER.error("The algorithm used to check the integrity of the keystore cannot be found");
63 throw new StoreConfigurationException(e);
64 } catch (KeyStoreException e) {
65 LOGGER.error(e);
66 throw new StoreConfigurationException(e);
67 } catch (FileNotFoundException e) {
68 LOGGER.error("The keystore file({}) is not found", getLocation());
69 throw new StoreConfigurationException(e);
70 } catch (IOException e) {
71 LOGGER.error("Something is wrong with the format of the truststore or the given password: {}", e.getMessage());
72 throw new StoreConfigurationException(e);
73 } finally {
74 try {
75 if (in != null) {
76 in.close();
77 }
78 }
79 catch (Exception e) {
80 LOGGER.warn("Error closing {}", getLocation(), e);
81 }
82 }
83 trustStore = ts;
84 LOGGER.debug("Truststore successfully loaded with params(location={})", getLocation());
85 }
86
87 public KeyStore getTrustStore() throws StoreConfigurationException {
88 if (trustStore == null) {
89 load();
90 }
91 return trustStore;
92 }
93
94
95
96
97
98
99
100 @PluginFactory
101 public static TrustStoreConfiguration createTrustStoreConfiguration(@PluginAttribute("location") String location,
102 @PluginAttribute("password") String password){
103 return new TrustStoreConfiguration(location, password);
104 }
105 }