|
1 |
+ |
/* |
|
2 |
+ |
* -------------------------------- MIT License -------------------------------- |
|
3 |
+ |
* |
|
4 |
+ |
* Copyright (c) 2021 SNF4J contributors |
|
5 |
+ |
* |
|
6 |
+ |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
7 |
+ |
* of this software and associated documentation files (the "Software"), to deal |
|
8 |
+ |
* in the Software without restriction, including without limitation the rights |
|
9 |
+ |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
10 |
+ |
* copies of the Software, and to permit persons to whom the Software is |
|
11 |
+ |
* furnished to do so, subject to the following conditions: |
|
12 |
+ |
* |
|
13 |
+ |
* The above copyright notice and this permission notice shall be included in all |
|
14 |
+ |
* copies or substantial portions of the Software. |
|
15 |
+ |
* |
|
16 |
+ |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
17 |
+ |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
18 |
+ |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
19 |
+ |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
20 |
+ |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
21 |
+ |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
22 |
+ |
* SOFTWARE. |
|
23 |
+ |
* |
|
24 |
+ |
* ----------------------------------------------------------------------------- |
|
25 |
+ |
*/ |
|
26 |
+ |
package org.snf4j.core.session.ssl; |
|
27 |
+ |
|
|
28 |
+ |
import java.io.ByteArrayInputStream; |
|
29 |
+ |
import java.io.Closeable; |
|
30 |
+ |
import java.io.File; |
|
31 |
+ |
import java.io.IOException; |
|
32 |
+ |
import java.io.InputStream; |
|
33 |
+ |
import java.security.KeyException; |
|
34 |
+ |
import java.security.KeyFactory; |
|
35 |
+ |
import java.security.KeyStore; |
|
36 |
+ |
import java.security.PrivateKey; |
|
37 |
+ |
import java.security.Provider; |
|
38 |
+ |
import java.security.SecureRandom; |
|
39 |
+ |
import java.security.cert.CertificateException; |
|
40 |
+ |
import java.security.cert.CertificateFactory; |
|
41 |
+ |
import java.security.cert.X509Certificate; |
|
42 |
+ |
import java.security.spec.PKCS8EncodedKeySpec; |
|
43 |
+ |
import java.util.Arrays; |
|
44 |
+ |
import java.util.List; |
|
45 |
+ |
|
|
46 |
+ |
import javax.crypto.Cipher; |
|
47 |
+ |
import javax.crypto.EncryptedPrivateKeyInfo; |
|
48 |
+ |
import javax.crypto.SecretKey; |
|
49 |
+ |
import javax.crypto.SecretKeyFactory; |
|
50 |
+ |
import javax.crypto.spec.PBEKeySpec; |
|
51 |
+ |
import javax.net.ssl.KeyManagerFactory; |
|
52 |
+ |
import javax.net.ssl.SSLContext; |
|
53 |
+ |
import javax.net.ssl.SSLEngine; |
|
54 |
+ |
import javax.net.ssl.SSLSessionContext; |
|
55 |
+ |
import javax.net.ssl.TrustManagerFactory; |
|
56 |
+ |
import javax.security.auth.DestroyFailedException; |
|
57 |
+ |
import javax.security.auth.Destroyable; |
|
58 |
+ |
|
|
59 |
+ |
import org.snf4j.core.util.PemUtil; |
|
60 |
+ |
import org.snf4j.core.util.PemUtil.Label; |
|
61 |
+ |
|
|
62 |
+ |
/** |
|
63 |
+ |
* A builder for the {@link SSLContext}. |
|
64 |
+ |
* |
|
65 |
+ |
* @author <a href="http://snf4j.org">SNF4J.ORG</a> |
|
66 |
+ |
*/ |
|
67 |
+ |
public class SSLContextBuilder implements Destroyable { |
|
68 |
+ |
|
|
69 |
+ |
private final static String[] KEY_ALGOS = new String[] {"RSA","DSA","EC"}; |
|
70 |
+ |
|
|
71 |
+ |
private final boolean forServer; |
|
72 |
+ |
|
|
73 |
+ |
private String protocol = ProtocolDefaults.TLS; |
|
74 |
+ |
|
|
75 |
+ |
private Provider provider; |
|
76 |
+ |
|
|
77 |
+ |
private String providerName; |
|
78 |
+ |
|
|
79 |
+ |
private int sessionCacheSize = -1; |
|
80 |
+ |
|
|
81 |
+ |
private int sessionTimeout = -1; |
|
82 |
+ |
|
|
83 |
+ |
private TrustManagerFactory trustManager; |
|
84 |
+ |
|
|
85 |
+ |
private X509Certificate[] trustCerts; |
|
86 |
+ |
|
|
87 |
+ |
private PrivateKey key; |
|
88 |
+ |
|
|
89 |
+ |
private char[] password; |
|
90 |
+ |
|
|
91 |
+ |
private KeyManagerFactory keyManager; |
|
92 |
+ |
|
|
93 |
+ |
private X509Certificate[] keyCerts; |
|
94 |
+ |
|
|
95 |
+ |
private SecureRandom secureRandom; |
|
96 |
+ |
|
|
97 |
+ |
//SSL engine default |
|
98 |
+ |
|
|
99 |
+ |
private String[] protocols; |
|
100 |
+ |
|
|
101 |
+ |
private ProtocolFilter protocolFilter; |
|
102 |
+ |
|
|
103 |
+ |
private String[] ciphers; |
|
104 |
+ |
|
|
105 |
+ |
private CipherFilter cipherFilter; |
|
106 |
+ |
|
|
107 |
+ |
private Boolean enableRetransmissions; //JDK9 |
|
108 |
+ |
|
|
109 |
+ |
private int maximumPacketSize = -1; //JDK9 |
|
110 |
+ |
|
|
111 |
+ |
private Boolean useCiphersOrder; |
|
112 |
+ |
|
|
113 |
+ |
private ClientAuth clientAuth = ClientAuth.NONE; |
|
114 |
+ |
|
|
115 |
+ |
private SSLContextBuilder(boolean forServer) { |
|
116 |
+ |
this.forServer = forServer; |
|
117 |
+ |
} |
|
118 |
+ |
|
|
119 |
+ |
private static SSLContextBuilder forServer() { |
|
120 |
+ |
return new SSLContextBuilder(true); |
|
121 |
+ |
} |
|
122 |
+ |
|
|
123 |
+ |
/** |
|
124 |
+ |
* Creates a builder for a client-side {@link SSLContext}. |
|
125 |
+ |
* |
|
126 |
+ |
* @return a builder for a client-side {@link SSLContext} |
|
127 |
+ |
*/ |
|
128 |
+ |
public static SSLContextBuilder forClient() { |
|
129 |
+ |
return new SSLContextBuilder(false); |
|
130 |
+ |
} |
|
131 |
+ |
|
|
132 |
+ |
/** |
|
133 |
+ |
* Creates a builder for a server-side {@link SSLContext}. |
|
134 |
+ |
* |
|
135 |
+ |
* @param keyFile a file for a PKCS#8 private key in the PEM encoding |
|
136 |
+ |
* @param keyCertsFile a file for an X.509 certificate chain in the PEM encoding |
|
137 |
+ |
* @return a builder for a server-side {@link SSLContext} |
|
138 |
+ |
* @throws IOException if a failure occurred while reading the files |
|
139 |
+ |
* @throws KeyException if a failure occurred while creating the key |
|
140 |
+ |
* @throws CertificateException if a failure occurred while creating the |
|
141 |
+ |
* certificates |
|
142 |
+ |
*/ |
|
143 |
+ |
public static SSLContextBuilder forServer(File keyFile, File keyCertsFile) throws IOException, KeyException, CertificateException { |
|
144 |
+ |
return forServer().keyManager(keyFile, keyCertsFile); |
|
145 |
+ |
} |
|
146 |
+ |
|
|
147 |
+ |
/** |
|
148 |
+ |
* Creates a builder for a server-side {@link SSLContext}. |
|
149 |
+ |
* |
|
150 |
+ |
* @param keyFile a file for a PKCS#8 private key in the PEM encoding |
|
151 |
+ |
* @param password the password protecting the private key, or {@code null} |
|
152 |
+ |
* if the key is not password-protected |
|
153 |
+ |
* @param keyCertsFile a file for an X.509 certificate chain in the PEM encoding |
|
154 |
+ |
* @return a builder for a server-side {@link SSLContext} |
|
155 |
+ |
* @throws IOException if a failure occurred while reading the files |
|
156 |
+ |
* @throws KeyException if a failure occurred while creating the key |
|
157 |
+ |
* @throws CertificateException if a failure occurred while creating the |
|
158 |
+ |
* certificates |
|
159 |
+ |
*/ |
|
160 |
+ |
public static SSLContextBuilder forServer(File keyFile, char[] password, File keyCertsFile) throws IOException, KeyException, CertificateException { |
|
161 |
+ |
return forServer().keyManager(keyFile, password, keyCertsFile); |
|
162 |
+ |
} |
|
163 |
+ |
|
|
164 |
+ |
/** |
|
165 |
+ |
* Creates a builder for a server-side {@link SSLContext}. |
|
166 |
+ |
* |
|
167 |
+ |
* @param keyIn an input stream for a PKCS#8 private key in the PEM |
|
168 |
+ |
* encoding |
|
169 |
+ |
* @param keyCertsIn an input stream for an X.509 certificate chain in the PEM |
|
170 |
+ |
* encoding |
|
171 |
+ |
* @return a builder for a server-side {@link SSLContext} |
|
172 |
+ |
* @throws IOException if a failure occurred while reading from the |
|
173 |
+ |
* input streams |
|
174 |
+ |
* @throws KeyException if a failure occurred while creating the key |
|
175 |
+ |
* @throws CertificateException if a failure occurred while creating the |
|
176 |
+ |
* certificates |
|
177 |
+ |
*/ |
|
178 |
+ |
public static SSLContextBuilder forServer(InputStream keyIn, InputStream keyCertsIn) throws IOException, KeyException, CertificateException { |
|
179 |
+ |
return forServer().keyManager(keyIn, keyCertsIn); |
|
180 |
+ |
} |
|
181 |
+ |
|
|
182 |
+ |
/** |
|
183 |
+ |
* Creates a builder for a server-side {@link SSLContext}. |
|
184 |
+ |
* |
|
185 |
+ |
* @param keyIn an input stream for a PKCS#8 private key in the PEM encoding |
|
186 |
+ |
* @param password the password protecting the private key, or {@code null} if |
|
187 |
+ |
* the key is not password-protected |
|
188 |
+ |
* @param keyCertsIn an input stream for an X.509 certificate chain in the PEM |
|
189 |
+ |
* encoding |
|
190 |
+ |
* @return a builder for a server-side {@link SSLContext} |
|
191 |
+ |
* @throws IOException if a failure occurred while reading from the |
|
192 |
+ |
* input streams |
|
193 |
+ |
* @throws KeyException if a failure occurred while creating the key |
|
194 |
+ |
* @throws CertificateException if a failure occurred while creating the |
|
195 |
+ |
* certificates |
|
196 |
+ |
*/ |
|
197 |
+ |
public static SSLContextBuilder forServer(InputStream keyIn, char[] password, InputStream keyCertsIn) throws IOException, KeyException, CertificateException { |
|
198 |
+ |
return forServer().keyManager(keyIn, password, keyCertsIn); |
|
199 |
+ |
} |
|
200 |
+ |
|
|
201 |
+ |
/** |
|
202 |
+ |
* Creates a builder for a server-side {@link SSLContext}. |
|
203 |
+ |
* |
|
204 |
+ |
* @param key a PKCS#8 private key |
|
205 |
+ |
* @param keyCerts an X.509 certificate chain |
|
206 |
+ |
* @return a builder for a server-side {@link SSLContext} |
|
207 |
+ |
*/ |
|
208 |
+ |
public static SSLContextBuilder forServer(PrivateKey key, X509Certificate... keyCerts) { |
|
209 |
+ |
return forServer().keyManager(key, keyCerts); |
|
210 |
+ |
} |
|
211 |
+ |
|
|
212 |
+ |
/** |
|
213 |
+ |
* Creates a builder for a server-side {@link SSLContext}. |
|
214 |
+ |
* |
|
215 |
+ |
* @param key a PKCS#8 private key |
|
216 |
+ |
* @param password the password protecting the private key, or {@code null} if |
|
217 |
+ |
* the key is not password-protected |
|
218 |
+ |
* @param keyCerts an X.509 certificate chain |
|
219 |
+ |
* @return a builder for a server-side {@link SSLContext} |
|
220 |
+ |
*/ |
|
221 |
+ |
public static SSLContextBuilder forServer(PrivateKey key, char[] password, X509Certificate... keyCerts) { |
|
222 |
+ |
return forServer().keyManager(key, password, keyCerts); |
|
223 |
+ |
} |
|
224 |
+ |
|
|
225 |
+ |
/** |
|
226 |
+ |
* Creates a builder for a server-side {@link SSLContext}. |
|
227 |
+ |
* |
|
228 |
+ |
* @param keyFactory a factory for a private key |
|
229 |
+ |
* @return a builder for a server-side {@link SSLContext} |
|
230 |
+ |
*/ |
|
231 |
+ |
public static SSLContextBuilder forServer(KeyManagerFactory keyFactory) { |
|
232 |
+ |
return forServer().keyManager(keyFactory); |
|
233 |
+ |
} |
|
234 |
+ |
|
|
235 |
+ |
/** |
|
236 |
+ |
* Tells if the builder if for a server-side {@link SSLContext}. |
|
237 |
+ |
* |
|
238 |
+ |
* @return {@code true} if the builder if for a server-side {@link SSLContext} |
|
239 |
+ |
*/ |
|
240 |
+ |
public boolean isForServer() { |
|
241 |
+ |
return forServer; |
|
242 |
+ |
} |
|
243 |
+ |
|
|
244 |
+ |
/** |
|
245 |
+ |
* Tells if the builder if for a client-side {@link SSLContext}. |
|
246 |
+ |
* |
|
247 |
+ |
* @return {@code true} if the builder if for a client-side {@link SSLContext} |
|
248 |
+ |
*/ |
|
249 |
+ |
public boolean isForClient() { |
|
250 |
+ |
return !forServer; |
|
251 |
+ |
} |
|
252 |
+ |
|
|
253 |
+ |
/** |
|
254 |
+ |
* Configures the protocol name of the {@link SSLContext} to be created by this |
|
255 |
+ |
* builder. |
|
256 |
+ |
* |
|
257 |
+ |
* @param protocol the protocol name |
|
258 |
+ |
* @return this builder |
|
259 |
+ |
*/ |
|
260 |
+ |
public SSLContextBuilder protocol(String protocol) { |
|
261 |
+ |
this.protocol = protocol; |
|
262 |
+ |
return this; |
|
263 |
+ |
} |
|
264 |
+ |
|
|
265 |
+ |
/** |
|
266 |
+ |
* Configures protocol versions to enable, or {@code null} to enable the |
|
267 |
+ |
* recommended protocol versions. |
|
268 |
+ |
* <p> |
|
269 |
+ |
* This configuration is used to pre-configure the {@link SSLEngineBuilder} |
|
270 |
+ |
* returned by the {@link #engineBuilder()} method. |
|
271 |
+ |
* |
|
272 |
+ |
* @param protocols the protocol versions |
|
273 |
+ |
* @return this builder |
|
274 |
+ |
*/ |
|
275 |
+ |
public SSLContextBuilder protocols(String... protocols) { |
|
276 |
+ |
this.protocols = protocols == null ? null : protocols.clone(); |
|
277 |
+ |
return this; |
|
278 |
+ |
} |
|
279 |
+ |
|
|
280 |
+ |
/** |
|
281 |
+ |
* Configures a filter for protocol versions to enable, or {@code null} to use |
|
282 |
+ |
* the default filter. |
|
283 |
+ |
* <p> |
|
284 |
+ |
* This configuration is used to pre-configure the {@link SSLEngineBuilder} |
|
285 |
+ |
* returned by the {@link #engineBuilder()} method. |
|
286 |
+ |
* |
|
287 |
+ |
* @param filter the protocol filter |
|
288 |
+ |
* @return this builder |
|
289 |
+ |
*/ |
|
290 |
+ |
public SSLContextBuilder protocolFilter(ProtocolFilter filter) { |
|
291 |
+ |
protocolFilter = filter; |
|
292 |
+ |
return this; |
|
293 |
+ |
} |
|
294 |
+ |
|
|
295 |
+ |
/** |
|
296 |
+ |
* Configures cipher suites to enable, or {@code null} to enable the |
|
297 |
+ |
* recommended cipher suites. |
|
298 |
+ |
* <p> |
|
299 |
+ |
* This configuration is used to pre-configure the {@link SSLEngineBuilder} |
|
300 |
+ |
* returned by the {@link #engineBuilder()} method. |
|
301 |
+ |
* |
|
302 |
+ |
* @param ciphers the cipher suites |
|
303 |
+ |
* @return this builder |
|
304 |
+ |
*/ |
|
305 |
+ |
public SSLContextBuilder ciphers(String... ciphers) { |
|
306 |
+ |
this.ciphers = ciphers == null ? null : ciphers.clone(); |
|
307 |
+ |
return this; |
|
308 |
+ |
} |
|
309 |
+ |
|
|
310 |
+ |
/** |
|
311 |
+ |
* Configures a filter for cipher suites to enable, or {@code null} to use |
|
312 |
+ |
* the default filter. |
|
313 |
+ |
* <p> |
|
314 |
+ |
* This configuration is used to pre-configure the {@link SSLEngineBuilder} |
|
315 |
+ |
* returned by the {@link #engineBuilder()} method. |
|
316 |
+ |
* |
|
317 |
+ |
* @param filter the cipher filter |
|
318 |
+ |
* @return this builder |
|
319 |
+ |
*/ |
|
320 |
+ |
public SSLContextBuilder cipherFilter(CipherFilter filter) { |
|
321 |
+ |
cipherFilter = filter; |
|
322 |
+ |
return this; |
|
323 |
+ |
} |
|
324 |
+ |
|
|
325 |
+ |
/** |
|
326 |
+ |
* Configures if DTLS handshake retransmissions should be enabled. |
|
327 |
+ |
* <p> |
|
328 |
+ |
* This configuration is used to pre-configure the {@link SSLEngineBuilder} |
|
329 |
+ |
* returned by the {@link #engineBuilder()} method. |
|
330 |
+ |
* <p> |
|
331 |
+ |
* NOTE: It requires Java 9 or newer. |
|
332 |
+ |
* |
|
333 |
+ |
* @param enable {@code true} to enable DTLS handshake retransmissions. |
|
334 |
+ |
* @return this builder |
|
335 |
+ |
*/ |
|
336 |
+ |
public SSLContextBuilder enableRetransmissions(boolean enable) { |
|
337 |
+ |
enableRetransmissions = enable ? Boolean.TRUE : Boolean.FALSE; |
|
338 |
+ |
return this; |
|
339 |
+ |
} |
|
340 |
+ |
|
|
341 |
+ |
/** |
|
342 |
+ |
* Configures the maximum expected network packet size. |
|
343 |
+ |
* <p> |
|
344 |
+ |
* This configuration is used to pre-configure the {@link SSLEngineBuilder} |
|
345 |
+ |
* returned by the {@link #engineBuilder()} method. |
|
346 |
+ |
* <p> |
|
347 |
+ |
* NOTE: It requires Java 9 or newer. |
|
348 |
+ |
* |
|
349 |
+ |
* @param maxSize the maximum expected network packet size in bytes, or 0 to use |
|
350 |
+ |
* the default value that is specified by the underlying |
|
351 |
+ |
* implementation. |
|
352 |
+ |
* @return this builder |
|
353 |
+ |
*/ |
|
354 |
+ |
public SSLContextBuilder maximumPacketSize(int maxSize) { |
|
355 |
+ |
maximumPacketSize = maxSize; |
|
356 |
+ |
return this; |
|
357 |
+ |
} |
|
358 |
+ |
|
|
359 |
+ |
/** |
|
360 |
+ |
* Configures if the local cipher suites preferences should be honored during |
|
361 |
+ |
* SSL/TLS/DTLS handshaking |
|
362 |
+ |
* <p> |
|
363 |
+ |
* This configuration is used to pre-configure the {@link SSLEngineBuilder} |
|
364 |
+ |
* returned by the {@link #engineBuilder()} method. |
|
365 |
+ |
* |
|
366 |
+ |
* @param useOrder {@code true} to honor the local cipher suites preferences |
|
367 |
+ |
* @return this builder |
|
368 |
+ |
*/ |
|
369 |
+ |
public SSLContextBuilder useCiphersOrder(boolean useOrder) { |
|
370 |
+ |
useCiphersOrder = useOrder ? Boolean.TRUE : Boolean.FALSE; |
|
371 |
+ |
return this; |
|
372 |
+ |
} |
|
373 |
+ |
|
|
374 |
+ |
/** |
|
375 |
+ |
* Configures the client authentication mode for a server-side |
|
376 |
+ |
* {@link SSLEngine}. |
|
377 |
+ |
* <p> |
|
378 |
+ |
* This configuration is used to pre-configure the {@link SSLEngineBuilder} |
|
379 |
+ |
* returned by the {@link #engineBuilder()} method. |
|
380 |
+ |
* |
|
381 |
+ |
* @param clientAuth the client authentication mode. |
|
382 |
+ |
* @return this builder |
|
383 |
+ |
*/ |
|
384 |
+ |
public SSLContextBuilder clientAuth(ClientAuth clientAuth) { |
|
385 |
+ |
this.clientAuth = clientAuth; |
|
386 |
+ |
return this; |
|
387 |
+ |
} |
|
388 |
+ |
|
|
389 |
+ |
/** |
|
390 |
+ |
* Configures the provide of the {@link SSLContext} to be created by this |
|
391 |
+ |
* builder. |
|
392 |
+ |
* |
|
393 |
+ |
* @param provider the provider |
|
394 |
+ |
* @return this builder |
|
395 |
+ |
*/ |
|
396 |
+ |
public SSLContextBuilder provider(Provider provider) { |
|
397 |
+ |
this.provider = provider; |
|
398 |
+ |
providerName = null; |
|
399 |
+ |
return this; |
|
400 |
+ |
} |
|
401 |
+ |
|
|
402 |
+ |
/** |
|
403 |
+ |
* Configures the provider name of the {@link SSLContext} to be created by this |
|
404 |
+ |
* builder. |
|
405 |
+ |
* |
|
406 |
+ |
* @param provider the provider name |
|
407 |
+ |
* @return this builder |
|
408 |
+ |
*/ |
|
409 |
+ |
public SSLContextBuilder providerName(String provider) { |
|
410 |
+ |
this.providerName = provider; |
|
411 |
+ |
provider = null; |
|
412 |
+ |
return this; |
|
413 |
+ |
} |
|
414 |
+ |
|
|
415 |
+ |
/** |
|
416 |
+ |
* Configures the timeout limit for the cached SSL session objects. |
|
417 |
+ |
* |
|
418 |
+ |
* @param timeout the timeout limit in seconds, or 0 to set no limit. |
|
419 |
+ |
* @return this builder |
|
420 |
+ |
*/ |
|
421 |
+ |
public SSLContextBuilder sessionTimeout(int timeout) { |
|
422 |
+ |
sessionTimeout = timeout; |
|
423 |
+ |
return this; |
|
424 |
+ |
} |
|
425 |
+ |
|
|
426 |
+ |
/** |
|
427 |
+ |
* Configures the size of the cache used for storing the SSL session objects. |
|
428 |
+ |
* |
|
429 |
+ |
* @param size the cache size limit, or 0 to set no limit. |
|
430 |
+ |
* @return this builder |
|
431 |
+ |
*/ |
|
432 |
+ |
public SSLContextBuilder sessionCacheSize(int size) { |
|
433 |
+ |
sessionCacheSize = size; |
|
434 |
+ |
return this; |
|
435 |
+ |
} |
|
436 |
+ |
|
|
437 |
+ |
private X509Certificate[] createCerts(List<byte[]> certs) throws CertificateException { |
|
438 |
+ |
if (certs == null) { |
|
439 |
+ |
throw new CertificateException("Invalid certificate PEM format"); |
|
440 |
+ |
} |
|
441 |
+ |
if (certs.isEmpty()) { |
|
442 |
+ |
throw new CertificateException("No certificate found"); |
|
443 |
+ |
} |
|
444 |
+ |
|
|
445 |
+ |
CertificateFactory cf = CertificateFactory.getInstance("X.509"); |
|
446 |
+ |
int len = certs.size(); |
|
447 |
+ |
X509Certificate[] out = new X509Certificate[len]; |
|
448 |
+ |
|
|
449 |
+ |
for (int i=0; i<len; ++i) { |
|
450 |
+ |
InputStream in = new ByteArrayInputStream(certs.get(i)); |
|
451 |
+ |
|
|
452 |
+ |
try { |
|
453 |
+ |
out[i] = (X509Certificate) cf.generateCertificate(in); |
|
454 |
+ |
} |
|
455 |
+ |
finally { |
|
456 |
+ |
silentClose(in); |
|
457 |
+ |
} |
|
458 |
+ |
} |
|
459 |
+ |
return out; |
|
460 |
+ |
} |
|
461 |
+ |
|
|
462 |
+ |
static void silentClose(Closeable stream) { |
|
463 |
+ |
try { |
|
464 |
+ |
stream.close(); |
|
465 |
+ |
} |
|
466 |
+ |
catch (IOException e) { |
|
467 |
+ |
//Ignore |
|
468 |
+ |
} |
|
469 |
+ |
} |
|
470 |
+ |
|
|
471 |
+ |
private X509Certificate[] readCerts(File file) throws IOException, CertificateException { |
|
472 |
+ |
return createCerts(PemUtil.read(Label.CERTIFICATE, file)); |
|
473 |
+ |
} |
|
474 |
+ |
|
|
475 |
+ |
private X509Certificate[] readCerts(InputStream in) throws IOException, CertificateException { |
|
476 |
+ |
return createCerts(PemUtil.read(Label.CERTIFICATE, in)); |
|
477 |
+ |
} |
|
478 |
+ |
|
|
479 |
+ |
/** |
|
480 |
+ |
* Configures trusted certificates for remote hosts verification. |
|
481 |
+ |
* |
|
482 |
+ |
* @param trustCertsFile a file for X.509 certificates in the PEM encoding |
|
483 |
+ |
* @return this builder |
|
484 |
+ |
* @throws IOException if a failure occurred while reading the files |
|
485 |
+ |
* @throws CertificateException if a failure occurred while creating the |
|
486 |
+ |
*/ |
|
487 |
+ |
public SSLContextBuilder trustManager(File trustCertsFile) throws IOException, CertificateException { |
|
488 |
+ |
return trustManager(readCerts(trustCertsFile)); |
|
489 |
+ |
} |
|
490 |
+ |
|
|
491 |
+ |
/** |
|
492 |
+ |
* Configures trusted certificates for remote hosts verification. |
|
493 |
+ |
* |
|
494 |
+ |
* @param trustCertsIn an input stream for X.509 certificates in the PEM encoding |
|
495 |
+ |
* @return this builder |
|
496 |
+ |
* @throws IOException if a failure occurred while reading the files |
|
497 |
+ |
* @throws CertificateException if a failure occurred while creating the |
|
498 |
+ |
*/ |
|
499 |
+ |
public SSLContextBuilder trustManager(InputStream trustCertsIn) throws IOException, CertificateException { |
|
500 |
+ |
return trustManager(readCerts(trustCertsIn)); |
|
501 |
+ |
} |
|
502 |
+ |
|
|
503 |
+ |
/** |
|
504 |
+ |
* Configures trusted certificates for remote hosts verification. |
|
505 |
+ |
* |
|
506 |
+ |
* @param trustCerts X.509 certificates |
|
507 |
+ |
* @return this builder |
|
508 |
+ |
*/ |
|
509 |
+ |
public SSLContextBuilder trustManager(X509Certificate... trustCerts) { |
|
510 |
+ |
this.trustCerts = certs(trustCerts, true, "trustCerts"); |
|
511 |
+ |
trustManager = null; |
|
512 |
+ |
return this; |
|
513 |
+ |
} |
|
514 |
+ |
|
|
515 |
+ |
/** |
|
516 |
+ |
* Configures trusted certificates for remote hosts verification. |
|
517 |
+ |
* |
|
518 |
+ |
* @param trustFactory a factory for trusted certificates |
|
519 |
+ |
* @return this builder |
|
520 |
+ |
*/ |
|
521 |
+ |
public SSLContextBuilder trustManager(TrustManagerFactory trustFactory) { |
|
522 |
+ |
this.trustManager = trustFactory; |
|
523 |
+ |
trustCerts = null; |
|
524 |
+ |
return this; |
|
525 |
+ |
} |
|
526 |
+ |
|
|
527 |
+ |
private PrivateKey createKey(List<byte[]> keys, char[] password) throws KeyException { |
|
528 |
+ |
if (keys == null) { |
|
529 |
+ |
throw new KeyException("Invalid private key PEM format"); |
|
530 |
+ |
} |
|
531 |
+ |
if (keys.isEmpty()) { |
|
532 |
+ |
throw new KeyException("No private key found"); |
|
533 |
+ |
} |
|
534 |
+ |
|
|
535 |
+ |
PKCS8EncodedKeySpec keySpec = null; |
|
536 |
+ |
byte[] key = keys.get(0); |
|
537 |
+ |
|
|
538 |
+ |
if (password == null) { |
|
539 |
+ |
keySpec = new PKCS8EncodedKeySpec(keys.get(0)); |
|
540 |
+ |
} |
|
541 |
+ |
else { |
|
542 |
+ |
try { |
|
543 |
+ |
EncryptedPrivateKeyInfo encryptedPrivateKeyInfo = new EncryptedPrivateKeyInfo(key); |
|
544 |
+ |
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(encryptedPrivateKeyInfo.getAlgName()); |
|
545 |
+ |
PBEKeySpec pbeKeySpec = new PBEKeySpec(password); |
|
546 |
+ |
SecretKey pbeKey; |
|
547 |
+ |
Cipher cipher; |
|
548 |
+ |
|
|
549 |
+ |
try { |
|
550 |
+ |
pbeKey = keyFactory.generateSecret(pbeKeySpec); |
|
551 |
+ |
try { |
|
552 |
+ |
cipher = Cipher.getInstance(encryptedPrivateKeyInfo.getAlgName()); |
|
553 |
+ |
cipher.init(Cipher.DECRYPT_MODE, pbeKey, encryptedPrivateKeyInfo.getAlgParameters()); |
|
554 |
+ |
keySpec = encryptedPrivateKeyInfo.getKeySpec(cipher); |
|
555 |
+ |
} |
|
556 |
+ |
finally { |
|
557 |
+ |
pbeKey.destroy(); |
|
558 |
+ |
} |
|
559 |
+ |
} |
|
560 |
+ |
finally { |
|
561 |
+ |
pbeKeySpec.clearPassword(); |
|
562 |
+ |
} |
|
563 |
+ |
} |
|
564 |
+ |
catch (DestroyFailedException e) { |
|
565 |
+ |
//Ignore |
|
566 |
+ |
} |
|
567 |
+ |
catch (Exception e) { |
|
568 |
+ |
throw new KeyException("Invalid PKCS8 encoding of password-protected private key", e); |
|
569 |
+ |
} |
|
570 |
+ |
} |
|
571 |
+ |
|
|
572 |
+ |
Exception exception = null; |
|
573 |
+ |
|
|
574 |
+ |
for (int i=0; i < KEY_ALGOS.length; ++i) { |
|
575 |
+ |
try { |
|
576 |
+ |
return KeyFactory.getInstance(KEY_ALGOS[i]).generatePrivate(keySpec); |
|
577 |
+ |
} |
|
578 |
+ |
catch (Exception e) { |
|
579 |
+ |
exception = e; |
|
580 |
+ |
} |
|
581 |
+ |
} |
|
582 |
+ |
throw new KeyException("Generation of private key failed: none of " + Arrays.toString(KEY_ALGOS) + " worked", exception); |
|
583 |
+ |
} |
|
584 |
+ |
|
|
585 |
+ |
private PrivateKey readKey(File file, char[] password) throws IOException, KeyException { |
|
586 |
+ |
return createKey(PemUtil.read(password == null ? Label.PRIVATE_KEY : Label.ENCRYPTED_PRIVATE_KEY, file), |
|
587 |
+ |
password); |
|
588 |
+ |
} |
|
589 |
+ |
|
|
590 |
+ |
private PrivateKey readKey(InputStream in, char[] password) throws IOException, KeyException { |
|
591 |
+ |
return createKey(PemUtil.read(password == null ? Label.PRIVATE_KEY : Label.ENCRYPTED_PRIVATE_KEY, in), |
|
592 |
+ |
password); |
|
593 |
+ |
} |
|
594 |
+ |
|
|
595 |
+ |
/** |
|
596 |
+ |
* Configures a private key with certificate chain for host identification. |
|
597 |
+ |
* |
|
598 |
+ |
* @param keyFile a file for a PKCS#8 private key in the PEM encoding |
|
599 |
+ |
* @param keyCertsFile a file for an X.509 certificate chain in the PEM encoding |
|
600 |
+ |
* @throws IOException if a failure occurred while reading the files |
|
601 |
+ |
* @throws KeyException if a failure occurred while creating the key |
|
602 |
+ |
* @throws CertificateException if a failure occurred while creating the |
|
603 |
+ |
* certificates |
|
604 |
+ |
* @return this builder |
|
605 |
+ |
*/ |
|
606 |
+ |
public SSLContextBuilder keyManager(File keyFile, File keyCertsFile) throws IOException, KeyException, CertificateException { |
|
607 |
+ |
return keyManager(keyFile, null, keyCertsFile); |
|
608 |
+ |
} |
|
609 |
+ |
|
|
610 |
+ |
/** |
|
611 |
+ |
* Configures a private key with certificate chain for host identification. |
|
612 |
+ |
* |
|
613 |
+ |
* @param keyFile a file for a PKCS#8 private key in the PEM encoding |
|
614 |
+ |
* @param password the password protecting the private key, or {@code null} |
|
615 |
+ |
* if the key is not password-protected |
|
616 |
+ |
* @param keyCertsFile a file for an X.509 certificate chain in the PEM encoding |
|
617 |
+ |
* @throws IOException if a failure occurred while reading the files |
|
618 |
+ |
* @throws KeyException if a failure occurred while creating the key |
|
619 |
+ |
* @throws CertificateException if a failure occurred while creating the |
|
620 |
+ |
* certificates |
|
621 |
+ |
* @return this builder |
|
622 |
+ |
*/ |
|
623 |
+ |
public SSLContextBuilder keyManager(File keyFile, char[] password, File keyCertsFile) throws IOException, KeyException, CertificateException { |
|
624 |
+ |
return keyManager(readKey(keyFile, password), password, readCerts(keyCertsFile)); |
|
625 |
+ |
} |
|
626 |
+ |
|
|
627 |
+ |
/** |
|
628 |
+ |
* Configures a private key with certificate chain for host identification. |
|
629 |
+ |
* |
|
630 |
+ |
* @param keyIn an input stream for a PKCS#8 private key in the PEM |
|
631 |
+ |
* encoding |
|
632 |
+ |
* @param keyCertsIn an input stream for an X.509 certificate chain in the PEM |
|
633 |
+ |
* encoding |
|
634 |
+ |
* @throws IOException if a failure occurred while reading from the |
|
635 |
+ |
* input streams |
|
636 |
+ |
* @throws KeyException if a failure occurred while creating the key |
|
637 |
+ |
* @throws CertificateException if a failure occurred while creating the |
|
638 |
+ |
* certificates |
|
639 |
+ |
* @return this builder |
|
640 |
+ |
*/ |
|
641 |
+ |
public SSLContextBuilder keyManager(InputStream keyIn, InputStream keyCertsIn) throws IOException, KeyException, CertificateException { |
|
642 |
+ |
return keyManager(keyIn, null, keyCertsIn); |
|
643 |
+ |
} |
|
644 |
+ |
|
|
645 |
+ |
/** |
|
646 |
+ |
* Configures a private key with certificate chain for host identification. |
|
647 |
+ |
* |
|
648 |
+ |
* @param keyIn an input stream for a PKCS#8 private key in the PEM |
|
649 |
+ |
* encoding |
|
650 |
+ |
* @param password the password protecting the private key, or {@code null} if |
|
651 |
+ |
* the key is not password-protected |
|
652 |
+ |
* @param keyCertsIn an input stream for an X.509 certificate chain in the PEM |
|
653 |
+ |
* encoding |
|
654 |
+ |
* @throws IOException if a failure occurred while reading from the |
|
655 |
+ |
* input streams |
|
656 |
+ |
* @throws KeyException if a failure occurred while creating the key |
|
657 |
+ |
* @throws CertificateException if a failure occurred while creating the |
|
658 |
+ |
* certificates |
|
659 |
+ |
* @return this builder |
|
660 |
+ |
*/ |
|
661 |
+ |
public SSLContextBuilder keyManager(InputStream keyIn, char[] password, InputStream keyCertsIn) throws IOException, KeyException, CertificateException { |
|
662 |
+ |
return keyManager(readKey(keyIn, password), password, readCerts(keyCertsIn)); |
|
663 |
+ |
} |
|
664 |
+ |
|
|
665 |
+ |
private static X509Certificate[] certs(X509Certificate[] certs, boolean allowEmpty, String name) { |
|
666 |
+ |
if (!allowEmpty) { |
|
667 |
+ |
if (certs == null) { |
|
668 |
+ |
throw new IllegalArgumentException(name + " is null"); |
|
669 |
+ |
} |
|
670 |
+ |
if (certs.length == 0) { |
|
671 |
+ |
throw new IllegalArgumentException(name + " is empty"); |
|
672 |
+ |
} |
|
673 |
+ |
} |
|
674 |
+ |
if (certs != null) { |
|
675 |
+ |
for (X509Certificate cert: certs) { |
|
676 |
+ |
if (cert == null) { |
|
677 |
+ |
throw new IllegalArgumentException(name + " contains null entry"); |
|
678 |
+ |
} |
|
679 |
+ |
} |
|
680 |
+ |
certs = certs.clone(); |
|
681 |
+ |
} |
|
682 |
+ |
return certs; |
|
683 |
+ |
} |
|
684 |
+ |
|
|
685 |
+ |
/** |
|
686 |
+ |
* Configures a private key with certificate chain for host identification. |
|
687 |
+ |
* |
|
688 |
+ |
* @param key a PKCS#8 private key |
|
689 |
+ |
* @param keyCerts an X.509 certificate chain |
|
690 |
+ |
* @return this builder |
|
691 |
+ |
*/ |
|
692 |
+ |
public SSLContextBuilder keyManager(PrivateKey key, X509Certificate... keyCerts) { |
|
693 |
+ |
return keyManager(key, null, keyCerts); |
|
694 |
+ |
} |
|
695 |
+ |
|
|
696 |
+ |
/** |
|
697 |
+ |
* Configures a private key with certificate chain for host identification. |
|
698 |
+ |
* |
|
699 |
+ |
* @param key a PKCS#8 private key |
|
700 |
+ |
* @param password the password protecting the private key, or {@code null} if |
|
701 |
+ |
* the key is not password-protected |
|
702 |
+ |
* @param keyCerts an X.509 certificate chain |
|
703 |
+ |
* @return this builder |
|
704 |
+ |
*/ |
|
705 |
+ |
public SSLContextBuilder keyManager(PrivateKey key, char[] password, X509Certificate... keyCerts) { |
|
706 |
+ |
keyCerts = certs(keyCerts, false, "keyCerts"); |
|
707 |
+ |
if (key == null) { |
|
708 |
+ |
throw new IllegalArgumentException("key is null"); |
|
709 |
+ |
} |
|
710 |
+ |
this.key = key; |
|
711 |
+ |
this.password = password == null ? null : password.clone(); |
|
712 |
+ |
this.keyCerts = keyCerts; |
|
713 |
+ |
this.keyManager = null; |
|
714 |
+ |
return this; |
|
715 |
+ |
} |
|
716 |
+ |
|
|
717 |
+ |
/** |
|
718 |
+ |
* Configures a private key with certificate chain for host identification. |
|
719 |
+ |
* |
|
720 |
+ |
* @param keyFactory a factory for a private key |
|
721 |
+ |
* @return this builder |
|
722 |
+ |
*/ |
|
723 |
+ |
public SSLContextBuilder keyManager(KeyManagerFactory keyFactory) { |
|
724 |
+ |
this.keyManager = keyFactory; |
|
725 |
+ |
silentDestroy(); |
|
726 |
+ |
return this; |
|
727 |
+ |
} |
|
728 |
+ |
|
|
729 |
+ |
/** |
|
730 |
+ |
* Configures a secure source of randomness. |
|
731 |
+ |
* |
|
732 |
+ |
* @param random the source of randomness, or {@code null} to use the default |
|
733 |
+ |
* source. |
|
734 |
+ |
* @return this builder |
|
735 |
+ |
*/ |
|
736 |
+ |
public SSLContextBuilder secureRandom(SecureRandom random) { |
|
737 |
+ |
this.secureRandom = random; |
|
738 |
+ |
return this; |
|
739 |
+ |
} |
|
740 |
+ |
|
|
741 |
+ |
private TrustManagerFactory buildTrustManager() throws Exception { |
|
742 |
+ |
if (trustCerts == null) { |
|
743 |
+ |
return null; |
|
744 |
+ |
} |
|
745 |
+ |
|
|
746 |
+ |
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); |
|
747 |
+ |
|
|
748 |
+ |
ks.load(null, null); |
|
749 |
+ |
for (int i=0; i<trustCerts.length; ++i) { |
|
750 |
+ |
ks.setCertificateEntry(Integer.toString(i+1), trustCerts[i]); |
|
751 |
+ |
} |
|
752 |
+ |
|
|
753 |
+ |
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); |
|
754 |
+ |
|
|
755 |
+ |
tmf.init(ks); |
|
756 |
+ |
return tmf; |
|
757 |
+ |
} |
|
758 |
+ |
|
|
759 |
+ |
private KeyManagerFactory buildKeyManager() throws Exception { |
|
760 |
+ |
if (key == null) { |
|
761 |
+ |
return null; |
|
762 |
+ |
} |
|
763 |
+ |
|
|
764 |
+ |
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); |
|
765 |
+ |
char[] password = this.password == null ? new char[0] : this.password; |
|
766 |
+ |
|
|
767 |
+ |
ks.load(null, null); |
|
768 |
+ |
ks.setKeyEntry("key", key, password, keyCerts); |
|
769 |
+ |
|
|
770 |
+ |
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); |
|
771 |
+ |
|
|
772 |
+ |
kmf.init(ks, password); |
|
773 |
+ |
return kmf; |
|
774 |
+ |
} |
|
775 |
+ |
|
|
776 |
+ |
/** |
|
777 |
+ |
* Creates a new {@link SSLEngine} builder pre-configured with the current |
|
778 |
+ |
* configuration settings. The returned builder is constructed with a new |
|
779 |
+ |
* {@link SSLContext} created by calling the {@link #build()} method. |
|
780 |
+ |
* |
|
781 |
+ |
* @return the new {@link SSLEngine} builder |
|
782 |
+ |
* @throws SSLContextCreateException if a failure occurred while building the |
|
783 |
+ |
* {@link SSLContext} instance used to |
|
784 |
+ |
* construct the new {@link SSLEngine} builder |
|
785 |
+ |
*/ |
|
786 |
+ |
public SSLEngineBuilder engineBuilder() throws SSLContextCreateException { |
|
787 |
+ |
SSLEngineBuilder builder = new SSLEngineBuilder(build(), forServer); |
|
788 |
+ |
Boolean value; |
|
789 |
+ |
|
|
790 |
+ |
builder.protocols(protocols); |
|
791 |
+ |
builder.ciphers(ciphers); |
|
792 |
+ |
builder.clientAuth(clientAuth); |
|
793 |
+ |
builder.cipherFilter(cipherFilter); |
|
794 |
+ |
builder.protocolFilter(protocolFilter); |
|
795 |
+ |
builder.maximumPacketSize(maximumPacketSize); |
|
796 |
+ |
value = enableRetransmissions; |
|
797 |
+ |
if (value != null) { |
|
798 |
+ |
builder.enableRetransmissions(value.booleanValue()); |
|
799 |
+ |
} |
|
800 |
+ |
value = useCiphersOrder; |
|
801 |
+ |
if (value != null) { |
|
802 |
+ |
builder.useCiphersOrder(value.booleanValue()); |
|
803 |
+ |
} |
|
804 |
+ |
return builder; |
|
805 |
+ |
} |
|
806 |
+ |
|
|
807 |
+ |
private enum Phase { |
|
808 |
+ |
|
|
809 |
+ |
NONE(null), |
|
810 |
+ |
GET_DEFAULT_CTX("Getting of the default SSL context failed"), |
|
811 |
+ |
BUILD_TRUST_MGR_FACTORY("Building of the trust manager factory failed"), |
|
812 |
+ |
BUILD_KEY_MGR_FACTORY("Building of the key manager factory failed"), |
|
813 |
+ |
BUILD_CTX("Building of the SSL context failed"); |
|
814 |
+ |
|
|
815 |
+ |
String msg; |
|
816 |
+ |
|
|
817 |
+ |
private Phase(String msg) { |
|
818 |
+ |
this.msg = msg; |
|
819 |
+ |
} |
|
820 |
+ |
|
|
821 |
+ |
private String exceptionMessage() { |
|
822 |
+ |
return msg; |
|
823 |
+ |
} |
|
824 |
+ |
} |
|
825 |
+ |
|
|
826 |
+ |
/** |
|
827 |
+ |
* Builds a new {@link SSLContext} instance based on the current configuration |
|
828 |
+ |
* settings. |
|
829 |
+ |
* |
|
830 |
+ |
* @return the new {@link SSLContext} instance. |
|
831 |
+ |
* @throws SSLContextCreateException if a failure occurred while building the |
|
832 |
+ |
* {@link SSLContext} instance |
|
833 |
+ |
*/ |
|
834 |
+ |
public SSLContext build() throws SSLContextCreateException { |
|
835 |
+ |
String protocol = this.protocol; |
|
836 |
+ |
Phase phase = Phase.NONE; |
|
837 |
+ |
|
|
838 |
+ |
try { |
|
839 |
+ |
if (protocol == null) { |
|
840 |
+ |
phase = Phase.GET_DEFAULT_CTX; |
|
841 |
+ |
return SSLContext.getDefault(); |
|
842 |
+ |
} |
|
843 |
+ |
|
|
844 |
+ |
TrustManagerFactory tmf; |
|
845 |
+ |
KeyManagerFactory kmf; |
|
846 |
+ |
|
|
847 |
+ |
phase = Phase.BUILD_TRUST_MGR_FACTORY; |
|
848 |
+ |
tmf = trustManager != null ? trustManager : buildTrustManager(); |
|
849 |
+ |
|
|
850 |
+ |
phase = Phase.BUILD_KEY_MGR_FACTORY; |
|
851 |
+ |
kmf = keyManager != null ? keyManager : buildKeyManager(); |
|
852 |
+ |
|
|
853 |
+ |
String providerName = this.providerName; |
|
854 |
+ |
Provider provider = this.provider; |
|
855 |
+ |
SSLContext context; |
|
856 |
+ |
SSLSessionContext sessionContext; |
|
857 |
+ |
|
|
858 |
+ |
phase = Phase.BUILD_CTX; |
|
859 |
+ |
if (provider != null) { |
|
860 |
+ |
context = SSLContext.getInstance(protocol, provider); |
|
861 |
+ |
} |
|
862 |
+ |
else if (providerName != null) { |
|
863 |
+ |
context = SSLContext.getInstance(protocol, providerName); |
|
864 |
+ |
} |
|
865 |
+ |
else { |
|
866 |
+ |
context = SSLContext.getInstance(protocol); |
|
867 |
+ |
} |
|
868 |
+ |
|
|
869 |
+ |
context.init( |
|
870 |
+ |
kmf == null ? null : kmf.getKeyManagers(), |
|
871 |
+ |
tmf == null ? null : tmf.getTrustManagers(), |
|
872 |
+ |
secureRandom); |
|
873 |
+ |
sessionContext = forServer ? context.getServerSessionContext() : context.getClientSessionContext(); |
|
874 |
+ |
|
|
875 |
+ |
if (sessionCacheSize >= 0) { |
|
876 |
+ |
sessionContext.setSessionCacheSize(sessionCacheSize); |
|
877 |
+ |
} |
|
878 |
+ |
if (sessionTimeout >= 0) { |
|
879 |
+ |
sessionContext.setSessionTimeout(sessionTimeout); |
|
880 |
+ |
} |
|
881 |
+ |
|
|
882 |
+ |
return context; |
|
883 |
+ |
} |
|
884 |
+ |
catch (Exception e) { |
|
885 |
+ |
throw new SSLContextCreateException(phase.exceptionMessage(), e); |
|
886 |
+ |
} |
|
887 |
+ |
} |
|
888 |
+ |
|
|
889 |
+ |
private void silentDestroy() { |
|
890 |
+ |
try { |
|
891 |
+ |
destroy(); |
|
892 |
+ |
} |
|
893 |
+ |
catch (DestroyFailedException e) { |
|
894 |
+ |
//Ignore |
|
895 |
+ |
} |
|
896 |
+ |
} |
|
897 |
+ |
|
|
898 |
+ |
/** |
|
899 |
+ |
* Destroys sensitive information associated with this builder (i.e. password |
|
900 |
+ |
* and private key). |
|
901 |
+ |
* |
|
902 |
+ |
* @throws DestroyFailedException if the destroy operation failed |
|
903 |
+ |
*/ |
|
904 |
+ |
@Override |
|
905 |
+ |
public void destroy() throws DestroyFailedException { |
|
906 |
+ |
if (password != null) { |
|
907 |
+ |
Arrays.fill(password, (char)0); |
|
908 |
+ |
password = null; |
|
909 |
+ |
} |
|
910 |
+ |
try { |
|
911 |
+ |
if (key != null) { |
|
912 |
+ |
key.destroy(); |
|
913 |
+ |
} |
|
914 |
+ |
} |
|
915 |
+ |
finally { |
|
916 |
+ |
key = null; |
|
917 |
+ |
keyCerts = null; |
|
918 |
+ |
} |
|
919 |
+ |
} |
|
920 |
+ |
|
|
921 |
+ |
/** |
|
922 |
+ |
* Tells if sensitive information associated with this builder is destroyed |
|
923 |
+ |
* |
|
924 |
+ |
* @return {@code true} if the sensitive information is destroyed |
|
925 |
+ |
*/ |
|
926 |
+ |
@Override |
|
927 |
+ |
public boolean isDestroyed() { |
|
928 |
+ |
return key == null; |
|
929 |
+ |
} |
|
930 |
+ |
} |