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