1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.core.net.ssl;
15
16 import java.security.KeyManagementException;
17 import java.security.KeyStore;
18 import java.security.KeyStoreException;
19 import java.security.NoSuchAlgorithmException;
20 import java.security.NoSuchProviderException;
21 import java.security.SecureRandom;
22 import java.security.UnrecoverableKeyException;
23 import java.security.cert.CertificateException;
24
25 import javax.net.ssl.KeyManager;
26 import javax.net.ssl.KeyManagerFactory;
27 import javax.net.ssl.SSLContext;
28 import javax.net.ssl.TrustManager;
29 import javax.net.ssl.TrustManagerFactory;
30
31 import ch.qos.logback.core.spi.ContextAware;
32
33
34
35
36
37
38
39
40
41 public class SSLContextFactoryBean {
42
43 private static final String JSSE_KEY_STORE_PROPERTY = "javax.net.ssl.keyStore";
44 private static final String JSSE_TRUST_STORE_PROPERTY = "javax.net.ssl.trustStore";
45
46 private KeyStoreFactoryBean keyStore;
47 private KeyStoreFactoryBean trustStore;
48 private SecureRandomFactoryBean secureRandom;
49 private KeyManagerFactoryFactoryBean keyManagerFactory;
50 private TrustManagerFactoryFactoryBean trustManagerFactory;
51 private String protocol;
52 private String provider;
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 public SSLContext createContext(ContextAware context) throws NoSuchProviderException, NoSuchAlgorithmException,
78 KeyManagementException, UnrecoverableKeyException, KeyStoreException, CertificateException {
79
80 SSLContext sslContext = getProvider() != null ? SSLContext.getInstance(getProtocol(), getProvider())
81 : SSLContext.getInstance(getProtocol());
82
83 context.addInfo("SSL protocol '" + sslContext.getProtocol() + "' provider '" + sslContext.getProvider() + "'");
84
85 KeyManager[] keyManagers = createKeyManagers(context);
86 TrustManager[] trustManagers = createTrustManagers(context);
87 SecureRandom secureRandom = createSecureRandom(context);
88 sslContext.init(keyManagers, trustManagers, secureRandom);
89 return sslContext;
90 }
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106 private KeyManager[] createKeyManagers(ContextAware context)
107 throws NoSuchProviderException, NoSuchAlgorithmException, UnrecoverableKeyException, KeyStoreException {
108
109 if (getKeyStore() == null)
110 return null;
111
112 KeyStore keyStore = getKeyStore().createKeyStore();
113 context.addInfo("key store of type '" + keyStore.getType() + "' provider '" + keyStore.getProvider() + "': "
114 + getKeyStore().getLocation());
115
116 KeyManagerFactory kmf = getKeyManagerFactory().createKeyManagerFactory();
117 context.addInfo("key manager algorithm '" + kmf.getAlgorithm() + "' provider '" + kmf.getProvider() + "'");
118
119 char[] passphrase = getKeyStore().getPassword().toCharArray();
120 kmf.init(keyStore, passphrase);
121 return kmf.getKeyManagers();
122 }
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139 private TrustManager[] createTrustManagers(ContextAware context)
140 throws NoSuchProviderException, NoSuchAlgorithmException, KeyStoreException {
141
142 if (getTrustStore() == null)
143 return null;
144
145 KeyStore trustStore = getTrustStore().createKeyStore();
146 context.addInfo("trust store of type '" + trustStore.getType() + "' provider '" + trustStore.getProvider()
147 + "': " + getTrustStore().getLocation());
148
149 TrustManagerFactory tmf = getTrustManagerFactory().createTrustManagerFactory();
150 context.addInfo("trust manager algorithm '" + tmf.getAlgorithm() + "' provider '" + tmf.getProvider() + "'");
151
152 tmf.init(trustStore);
153 return tmf.getTrustManagers();
154 }
155
156 private SecureRandom createSecureRandom(ContextAware context)
157 throws NoSuchProviderException, NoSuchAlgorithmException {
158
159 SecureRandom secureRandom = getSecureRandom().createSecureRandom();
160 context.addInfo("secure random algorithm '" + secureRandom.getAlgorithm() + "' provider '"
161 + secureRandom.getProvider() + "'");
162
163 return secureRandom;
164 }
165
166
167
168
169
170
171
172 public KeyStoreFactoryBean getKeyStore() {
173 if (keyStore == null) {
174 keyStore = keyStoreFromSystemProperties(JSSE_KEY_STORE_PROPERTY);
175 }
176 return keyStore;
177 }
178
179
180
181
182
183
184 public void setKeyStore(KeyStoreFactoryBean keyStore) {
185 this.keyStore = keyStore;
186 }
187
188
189
190
191
192
193
194 public KeyStoreFactoryBean getTrustStore() {
195 if (trustStore == null) {
196 trustStore = keyStoreFromSystemProperties(JSSE_TRUST_STORE_PROPERTY);
197 }
198 return trustStore;
199 }
200
201
202
203
204
205
206 public void setTrustStore(KeyStoreFactoryBean trustStore) {
207 this.trustStore = trustStore;
208 }
209
210
211
212
213
214
215
216
217 private KeyStoreFactoryBean keyStoreFromSystemProperties(String property) {
218 if (System.getProperty(property) == null)
219 return null;
220 KeyStoreFactoryBean keyStore = new KeyStoreFactoryBean();
221 keyStore.setLocation(locationFromSystemProperty(property));
222 keyStore.setProvider(System.getProperty(property + "Provider"));
223 keyStore.setPassword(System.getProperty(property + "Password"));
224 keyStore.setType(System.getProperty(property + "Type"));
225 return keyStore;
226 }
227
228
229
230
231
232
233
234
235 private String locationFromSystemProperty(String name) {
236 String location = System.getProperty(name);
237 if (location != null && !location.startsWith("file:")) {
238 location = "file:" + location;
239 }
240 return location;
241 }
242
243
244
245
246
247
248
249 public SecureRandomFactoryBean getSecureRandom() {
250 if (secureRandom == null) {
251 return new SecureRandomFactoryBean();
252 }
253 return secureRandom;
254 }
255
256
257
258
259
260
261 public void setSecureRandom(SecureRandomFactoryBean secureRandom) {
262 this.secureRandom = secureRandom;
263 }
264
265
266
267
268
269
270
271 public KeyManagerFactoryFactoryBean getKeyManagerFactory() {
272 if (keyManagerFactory == null) {
273 return new KeyManagerFactoryFactoryBean();
274 }
275 return keyManagerFactory;
276 }
277
278
279
280
281
282
283 public void setKeyManagerFactory(KeyManagerFactoryFactoryBean keyManagerFactory) {
284 this.keyManagerFactory = keyManagerFactory;
285 }
286
287
288
289
290
291
292
293 public TrustManagerFactoryFactoryBean getTrustManagerFactory() {
294 if (trustManagerFactory == null) {
295 return new TrustManagerFactoryFactoryBean();
296 }
297 return trustManagerFactory;
298 }
299
300
301
302
303
304
305 public void setTrustManagerFactory(TrustManagerFactoryFactoryBean trustManagerFactory) {
306 this.trustManagerFactory = trustManagerFactory;
307 }
308
309
310
311
312
313
314
315
316 public String getProtocol() {
317 if (protocol == null) {
318 return SSL.DEFAULT_PROTOCOL;
319 }
320 return protocol;
321 }
322
323
324
325
326
327
328
329
330 public void setProtocol(String protocol) {
331 this.protocol = protocol;
332 }
333
334
335
336
337
338
339 public String getProvider() {
340 return provider;
341 }
342
343
344
345
346
347
348 public void setProvider(String provider) {
349 this.provider = provider;
350 }
351
352 }