1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.netty.handler.ssl;
18
19 import static io.netty.util.internal.ObjectUtil.checkNotNull;
20
21 import io.netty.util.internal.UnstableApi;
22
23 import java.security.Provider;
24 import javax.net.ssl.KeyManagerFactory;
25 import javax.net.ssl.SSLException;
26 import javax.net.ssl.TrustManagerFactory;
27
28 import java.io.File;
29 import java.io.InputStream;
30 import java.security.PrivateKey;
31 import java.security.cert.X509Certificate;
32 import javax.net.ssl.SSLEngine;
33
34
35
36
37 public final class SslContextBuilder {
38
39
40
41
42 public static SslContextBuilder forClient() {
43 return new SslContextBuilder(false);
44 }
45
46
47
48
49
50
51
52
53 public static SslContextBuilder forServer(File keyCertChainFile, File keyFile) {
54 return new SslContextBuilder(true).keyManager(keyCertChainFile, keyFile);
55 }
56
57
58
59
60
61
62
63
64 public static SslContextBuilder forServer(InputStream keyCertChainInputStream, InputStream keyInputStream) {
65 return new SslContextBuilder(true).keyManager(keyCertChainInputStream, keyInputStream);
66 }
67
68
69
70
71
72
73
74
75 public static SslContextBuilder forServer(PrivateKey key, X509Certificate... keyCertChain) {
76 return new SslContextBuilder(true).keyManager(key, keyCertChain);
77 }
78
79
80
81
82
83
84
85
86
87
88 public static SslContextBuilder forServer(
89 File keyCertChainFile, File keyFile, String keyPassword) {
90 return new SslContextBuilder(true).keyManager(keyCertChainFile, keyFile, keyPassword);
91 }
92
93
94
95
96
97
98
99
100
101
102 public static SslContextBuilder forServer(
103 InputStream keyCertChainInputStream, InputStream keyInputStream, String keyPassword) {
104 return new SslContextBuilder(true).keyManager(keyCertChainInputStream, keyInputStream, keyPassword);
105 }
106
107
108
109
110
111
112
113
114
115
116 public static SslContextBuilder forServer(
117 PrivateKey key, String keyPassword, X509Certificate... keyCertChain) {
118 return new SslContextBuilder(true).keyManager(key, keyPassword, keyCertChain);
119 }
120
121
122
123
124
125
126
127 public static SslContextBuilder forServer(KeyManagerFactory keyManagerFactory) {
128 return new SslContextBuilder(true).keyManager(keyManagerFactory);
129 }
130
131 private final boolean forServer;
132 private SslProvider provider;
133 private Provider sslContextProvider;
134 private X509Certificate[] trustCertCollection;
135 private TrustManagerFactory trustManagerFactory;
136 private X509Certificate[] keyCertChain;
137 private PrivateKey key;
138 private String keyPassword;
139 private KeyManagerFactory keyManagerFactory;
140 private Iterable<String> ciphers;
141 private CipherSuiteFilter cipherFilter = IdentityCipherSuiteFilter.INSTANCE;
142 private ApplicationProtocolConfig apn;
143 private long sessionCacheSize;
144 private long sessionTimeout;
145 private ClientAuth clientAuth = ClientAuth.NONE;
146 private String[] protocols;
147 private boolean startTls;
148 private boolean enableOcsp;
149
150 private SslContextBuilder(boolean forServer) {
151 this.forServer = forServer;
152 }
153
154
155
156
157 public SslContextBuilder sslProvider(SslProvider provider) {
158 this.provider = provider;
159 return this;
160 }
161
162
163
164
165
166 public SslContextBuilder sslContextProvider(Provider sslContextProvider) {
167 this.sslContextProvider = sslContextProvider;
168 return this;
169 }
170
171
172
173
174
175 public SslContextBuilder trustManager(File trustCertCollectionFile) {
176 try {
177 return trustManager(SslContext.toX509Certificates(trustCertCollectionFile));
178 } catch (Exception e) {
179 throw new IllegalArgumentException("File does not contain valid certificates: "
180 + trustCertCollectionFile, e);
181 }
182 }
183
184
185
186
187
188 public SslContextBuilder trustManager(InputStream trustCertCollectionInputStream) {
189 try {
190 return trustManager(SslContext.toX509Certificates(trustCertCollectionInputStream));
191 } catch (Exception e) {
192 throw new IllegalArgumentException("Input stream does not contain valid certificates.", e);
193 }
194 }
195
196
197
198
199 public SslContextBuilder trustManager(X509Certificate... trustCertCollection) {
200 this.trustCertCollection = trustCertCollection != null ? trustCertCollection.clone() : null;
201 trustManagerFactory = null;
202 return this;
203 }
204
205
206
207
208 public SslContextBuilder trustManager(TrustManagerFactory trustManagerFactory) {
209 trustCertCollection = null;
210 this.trustManagerFactory = trustManagerFactory;
211 return this;
212 }
213
214
215
216
217
218
219
220
221 public SslContextBuilder keyManager(File keyCertChainFile, File keyFile) {
222 return keyManager(keyCertChainFile, keyFile, null);
223 }
224
225
226
227
228
229
230
231
232 public SslContextBuilder keyManager(InputStream keyCertChainInputStream, InputStream keyInputStream) {
233 return keyManager(keyCertChainInputStream, keyInputStream, null);
234 }
235
236
237
238
239
240
241
242
243 public SslContextBuilder keyManager(PrivateKey key, X509Certificate... keyCertChain) {
244 return keyManager(key, null, keyCertChain);
245 }
246
247
248
249
250
251
252
253
254
255
256 public SslContextBuilder keyManager(File keyCertChainFile, File keyFile, String keyPassword) {
257 X509Certificate[] keyCertChain;
258 PrivateKey key;
259 try {
260 keyCertChain = SslContext.toX509Certificates(keyCertChainFile);
261 } catch (Exception e) {
262 throw new IllegalArgumentException("File does not contain valid certificates: " + keyCertChainFile, e);
263 }
264 try {
265 key = SslContext.toPrivateKey(keyFile, keyPassword);
266 } catch (Exception e) {
267 throw new IllegalArgumentException("File does not contain valid private key: " + keyFile, e);
268 }
269 return keyManager(key, keyPassword, keyCertChain);
270 }
271
272
273
274
275
276
277
278
279
280
281 public SslContextBuilder keyManager(InputStream keyCertChainInputStream, InputStream keyInputStream,
282 String keyPassword) {
283 X509Certificate[] keyCertChain;
284 PrivateKey key;
285 try {
286 keyCertChain = SslContext.toX509Certificates(keyCertChainInputStream);
287 } catch (Exception e) {
288 throw new IllegalArgumentException("Input stream not contain valid certificates.", e);
289 }
290 try {
291 key = SslContext.toPrivateKey(keyInputStream, keyPassword);
292 } catch (Exception e) {
293 throw new IllegalArgumentException("Input stream does not contain valid private key.", e);
294 }
295 return keyManager(key, keyPassword, keyCertChain);
296 }
297
298
299
300
301
302
303
304
305
306
307 public SslContextBuilder keyManager(PrivateKey key, String keyPassword, X509Certificate... keyCertChain) {
308 if (forServer) {
309 checkNotNull(keyCertChain, "keyCertChain required for servers");
310 if (keyCertChain.length == 0) {
311 throw new IllegalArgumentException("keyCertChain must be non-empty");
312 }
313 checkNotNull(key, "key required for servers");
314 }
315 if (keyCertChain == null || keyCertChain.length == 0) {
316 this.keyCertChain = null;
317 } else {
318 for (X509Certificate cert: keyCertChain) {
319 if (cert == null) {
320 throw new IllegalArgumentException("keyCertChain contains null entry");
321 }
322 }
323 this.keyCertChain = keyCertChain.clone();
324 }
325 this.key = key;
326 this.keyPassword = keyPassword;
327 keyManagerFactory = null;
328 return this;
329 }
330
331
332
333
334
335
336
337
338
339 public SslContextBuilder keyManager(KeyManagerFactory keyManagerFactory) {
340 if (forServer) {
341 checkNotNull(keyManagerFactory, "keyManagerFactory required for servers");
342 }
343 keyCertChain = null;
344 key = null;
345 keyPassword = null;
346 this.keyManagerFactory = keyManagerFactory;
347 return this;
348 }
349
350
351
352
353
354 public SslContextBuilder ciphers(Iterable<String> ciphers) {
355 return ciphers(ciphers, IdentityCipherSuiteFilter.INSTANCE);
356 }
357
358
359
360
361
362
363 public SslContextBuilder ciphers(Iterable<String> ciphers, CipherSuiteFilter cipherFilter) {
364 checkNotNull(cipherFilter, "cipherFilter");
365 this.ciphers = ciphers;
366 this.cipherFilter = cipherFilter;
367 return this;
368 }
369
370
371
372
373 public SslContextBuilder applicationProtocolConfig(ApplicationProtocolConfig apn) {
374 this.apn = apn;
375 return this;
376 }
377
378
379
380
381
382 public SslContextBuilder sessionCacheSize(long sessionCacheSize) {
383 this.sessionCacheSize = sessionCacheSize;
384 return this;
385 }
386
387
388
389
390
391 public SslContextBuilder sessionTimeout(long sessionTimeout) {
392 this.sessionTimeout = sessionTimeout;
393 return this;
394 }
395
396
397
398
399 public SslContextBuilder clientAuth(ClientAuth clientAuth) {
400 this.clientAuth = checkNotNull(clientAuth, "clientAuth");
401 return this;
402 }
403
404
405
406
407
408
409 public SslContextBuilder protocols(String... protocols) {
410 this.protocols = protocols == null ? null : protocols.clone();
411 return this;
412 }
413
414
415
416
417 public SslContextBuilder startTls(boolean startTls) {
418 this.startTls = startTls;
419 return this;
420 }
421
422
423
424
425
426
427
428 @UnstableApi
429 public SslContextBuilder enableOcsp(boolean enableOcsp) {
430 this.enableOcsp = enableOcsp;
431 return this;
432 }
433
434
435
436
437
438
439 public SslContext build() throws SSLException {
440 if (forServer) {
441 return SslContext.newServerContextInternal(provider, sslContextProvider, trustCertCollection,
442 trustManagerFactory, keyCertChain, key, keyPassword, keyManagerFactory,
443 ciphers, cipherFilter, apn, sessionCacheSize, sessionTimeout, clientAuth, protocols, startTls,
444 enableOcsp);
445 } else {
446 return SslContext.newClientContextInternal(provider, sslContextProvider, trustCertCollection,
447 trustManagerFactory, keyCertChain, key, keyPassword, keyManagerFactory,
448 ciphers, cipherFilter, apn, protocols, sessionCacheSize, sessionTimeout, enableOcsp);
449 }
450 }
451 }