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.FileNotFoundException;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.security.KeyStore;
23 import java.security.KeyStoreException;
24 import java.security.NoSuchAlgorithmException;
25 import java.security.cert.CertificateException;
26
27 import org.apache.logging.log4j.core.config.ConfigurationSource;
28 import org.apache.logging.log4j.core.util.NetUtils;
29
30
31
32
33 public class AbstractKeyStoreConfiguration extends StoreConfiguration<KeyStore> {
34 private final KeyStore keyStore;
35 private final String keyStoreType;
36
37 public AbstractKeyStoreConfiguration(final String location, final char[] password, final String keyStoreType)
38 throws StoreConfigurationException {
39 super(location, password);
40 this.keyStoreType = keyStoreType == null ? SslConfigurationDefaults.KEYSTORE_TYPE : keyStoreType;
41 this.keyStore = this.load();
42 }
43
44
45
46
47
48
49 @Deprecated
50 public AbstractKeyStoreConfiguration(final String location, final String password, final String keyStoreType)
51 throws StoreConfigurationException {
52 super(location, password);
53 this.keyStoreType = keyStoreType == null ? SslConfigurationDefaults.KEYSTORE_TYPE : keyStoreType;
54 this.keyStore = this.load();
55 }
56
57 @Override
58 protected KeyStore load() throws StoreConfigurationException {
59 final String loadLocation = this.getLocation();
60 LOGGER.debug("Loading keystore from location {}", loadLocation);
61 try {
62 if (loadLocation == null) {
63 throw new IOException("The location is null");
64 }
65 try (final InputStream fin = openInputStream(loadLocation)) {
66 final KeyStore ks = KeyStore.getInstance(this.keyStoreType);
67 ks.load(fin, this.getPasswordAsCharArray());
68 LOGGER.debug("KeyStore successfully loaded from location {}", loadLocation);
69 return ks;
70 }
71 } catch (final CertificateException e) {
72 LOGGER.error("No Provider supports a KeyStoreSpi implementation for the specified type {} for location {}", this.keyStoreType, loadLocation, e);
73 throw new StoreConfigurationException(loadLocation, e);
74 } catch (final NoSuchAlgorithmException e) {
75 LOGGER.error("The algorithm used to check the integrity of the keystore cannot be found for location {}", loadLocation, e);
76 throw new StoreConfigurationException(loadLocation, e);
77 } catch (final KeyStoreException e) {
78 LOGGER.error("KeyStoreException for location {}", loadLocation, e);
79 throw new StoreConfigurationException(loadLocation, e);
80 } catch (final FileNotFoundException e) {
81 LOGGER.error("The keystore file {} is not found", loadLocation, e);
82 throw new StoreConfigurationException(loadLocation, e);
83 } catch (final IOException e) {
84 LOGGER.error("Something is wrong with the format of the keystore or the given password for location", loadLocation, e);
85 throw new StoreConfigurationException(loadLocation, e);
86 }
87 }
88
89 private InputStream openInputStream(final String filePathOrUri) {
90 return ConfigurationSource.fromUri(NetUtils.toURI(filePathOrUri)).getInputStream();
91 }
92
93 public KeyStore getKeyStore() {
94 return this.keyStore;
95 }
96
97 @Override
98 public int hashCode() {
99 final int prime = 31;
100 int result = super.hashCode();
101 result = prime * result + ((keyStore == null) ? 0 : keyStore.hashCode());
102 result = prime * result + ((keyStoreType == null) ? 0 : keyStoreType.hashCode());
103 return result;
104 }
105
106 @Override
107 public boolean equals(final Object obj) {
108 if (this == obj) {
109 return true;
110 }
111 if (!super.equals(obj)) {
112 return false;
113 }
114 if (getClass() != obj.getClass()) {
115 return false;
116 }
117 final AbstractKeyStoreConfiguration other = (AbstractKeyStoreConfiguration) obj;
118 if (keyStore == null) {
119 if (other.keyStore != null) {
120 return false;
121 }
122 } else if (!keyStore.equals(other.keyStore)) {
123 return false;
124 }
125 if (keyStoreType == null) {
126 if (other.keyStoreType != null) {
127 return false;
128 }
129 } else if (!keyStoreType.equals(other.keyStoreType)) {
130 return false;
131 }
132 return true;
133 }
134
135 public String getKeyStoreType() {
136 return keyStoreType;
137 }
138
139 }