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 io.netty.handler.ssl.util.KeyManagerFactoryWrapper;
20 import io.netty.handler.ssl.util.TrustManagerFactoryWrapper;
21 import io.netty.util.internal.UnstableApi;
22
23 import javax.net.ssl.KeyManager;
24 import javax.net.ssl.KeyManagerFactory;
25 import javax.net.ssl.SNIHostName;
26 import javax.net.ssl.SNIServerName;
27 import javax.net.ssl.SSLEngine;
28 import javax.net.ssl.SSLException;
29 import javax.net.ssl.SSLParameters;
30 import javax.net.ssl.TrustManager;
31 import javax.net.ssl.TrustManagerFactory;
32 import java.io.File;
33 import java.io.InputStream;
34 import java.security.KeyStore;
35 import java.security.PrivateKey;
36 import java.security.Provider;
37 import java.security.SecureRandom;
38 import java.security.cert.X509Certificate;
39 import java.util.ArrayList;
40 import java.util.HashMap;
41 import java.util.List;
42 import java.util.Map;
43
44 import static io.netty.util.internal.EmptyArrays.EMPTY_STRINGS;
45 import static io.netty.util.internal.EmptyArrays.EMPTY_X509_CERTIFICATES;
46 import static io.netty.util.internal.ObjectUtil.checkNotNull;
47 import static io.netty.util.internal.ObjectUtil.checkNotNullWithIAE;
48 import static io.netty.util.internal.ObjectUtil.checkNonEmpty;
49
50
51
52
53 public final class SslContextBuilder {
54 @SuppressWarnings("rawtypes")
55 private static final Map.Entry[] EMPTY_ENTRIES = new Map.Entry[0];
56
57
58
59
60 public static SslContextBuilder forClient() {
61 return new SslContextBuilder(false);
62 }
63
64
65
66
67
68
69
70
71 public static SslContextBuilder forServer(File keyCertChainFile, File keyFile) {
72 return new SslContextBuilder(true).keyManager(keyCertChainFile, keyFile);
73 }
74
75
76
77
78
79
80
81
82
83
84
85
86
87 public static SslContextBuilder forServer(InputStream keyCertChainInputStream, InputStream keyInputStream) {
88 return new SslContextBuilder(true).keyManager(keyCertChainInputStream, keyInputStream);
89 }
90
91
92
93
94
95
96
97
98 public static SslContextBuilder forServer(PrivateKey key, X509Certificate... keyCertChain) {
99 return new SslContextBuilder(true).keyManager(key, keyCertChain);
100 }
101
102
103
104
105
106
107
108
109 public static SslContextBuilder forServer(PrivateKey key, Iterable<? extends X509Certificate> keyCertChain) {
110 return forServer(key, toArray(keyCertChain, EMPTY_X509_CERTIFICATES));
111 }
112
113
114
115
116
117
118
119
120
121
122 public static SslContextBuilder forServer(
123 File keyCertChainFile, File keyFile, String keyPassword) {
124 return new SslContextBuilder(true).keyManager(keyCertChainFile, keyFile, keyPassword);
125 }
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140 public static SslContextBuilder forServer(
141 InputStream keyCertChainInputStream, InputStream keyInputStream, String keyPassword) {
142 return new SslContextBuilder(true).keyManager(keyCertChainInputStream, keyInputStream, keyPassword);
143 }
144
145
146
147
148
149
150
151
152
153
154 public static SslContextBuilder forServer(
155 PrivateKey key, String keyPassword, X509Certificate... keyCertChain) {
156 return new SslContextBuilder(true).keyManager(key, keyPassword, keyCertChain);
157 }
158
159
160
161
162
163
164
165
166
167
168 public static SslContextBuilder forServer(
169 PrivateKey key, String keyPassword, Iterable<? extends X509Certificate> keyCertChain) {
170 return forServer(key, keyPassword, toArray(keyCertChain, EMPTY_X509_CERTIFICATES));
171 }
172
173
174
175
176
177
178
179
180
181
182 public static SslContextBuilder forServer(KeyManagerFactory keyManagerFactory) {
183 return new SslContextBuilder(true).keyManager(keyManagerFactory);
184 }
185
186
187
188
189
190
191 public static SslContextBuilder forServer(KeyManager keyManager) {
192 return new SslContextBuilder(true).keyManager(keyManager);
193 }
194
195 private final boolean forServer;
196 private SslProvider provider;
197 private Provider sslContextProvider;
198 private X509Certificate[] trustCertCollection;
199 private TrustManagerFactory trustManagerFactory;
200 private X509Certificate[] keyCertChain;
201 private PrivateKey key;
202 private String keyPassword;
203 private KeyManagerFactory keyManagerFactory;
204 private Iterable<String> ciphers;
205 private CipherSuiteFilter cipherFilter = IdentityCipherSuiteFilter.INSTANCE;
206 private ApplicationProtocolConfig apn;
207 private long sessionCacheSize;
208 private long sessionTimeout;
209 private ClientAuth clientAuth = ClientAuth.NONE;
210 private String[] protocols;
211 private boolean startTls;
212 private boolean enableOcsp;
213 private SecureRandom secureRandom;
214 private String keyStoreType = KeyStore.getDefaultType();
215 private String endpointIdentificationAlgorithm;
216 private final Map<SslContextOption<?>, Object> options = new HashMap<SslContextOption<?>, Object>();
217 private final List<SNIServerName> serverNames;
218
219 private SslContextBuilder(boolean forServer) {
220 this.forServer = forServer;
221 if (!forServer) {
222 endpointIdentificationAlgorithm = SslUtils.defaultEndpointVerificationAlgorithm;
223 }
224 serverNames = forServer ? null : new ArrayList<>(2);
225 }
226
227
228
229
230 public <T> SslContextBuilder option(SslContextOption<T> option, T value) {
231 if (value == null) {
232 options.remove(option);
233 } else {
234 options.put(option, value);
235 }
236 return this;
237 }
238
239
240
241
242 public SslContextBuilder sslProvider(SslProvider provider) {
243 this.provider = provider;
244 return this;
245 }
246
247
248
249
250 public SslContextBuilder keyStoreType(String keyStoreType) {
251 this.keyStoreType = keyStoreType;
252 return this;
253 }
254
255
256
257
258
259 public SslContextBuilder sslContextProvider(Provider sslContextProvider) {
260 this.sslContextProvider = sslContextProvider;
261 return this;
262 }
263
264
265
266
267
268 public SslContextBuilder trustManager(File trustCertCollectionFile) {
269 try {
270 return trustManager(SslContext.toX509Certificates(trustCertCollectionFile));
271 } catch (Exception e) {
272 throw new IllegalArgumentException("File does not contain valid certificates: "
273 + trustCertCollectionFile, e);
274 }
275 }
276
277
278
279
280
281
282
283 public SslContextBuilder trustManager(InputStream trustCertCollectionInputStream) {
284 try {
285 return trustManager(SslContext.toX509Certificates(trustCertCollectionInputStream));
286 } catch (Exception e) {
287 throw new IllegalArgumentException("Input stream does not contain valid certificates.", e);
288 }
289 }
290
291
292
293
294 public SslContextBuilder trustManager(X509Certificate... trustCertCollection) {
295 this.trustCertCollection = trustCertCollection != null ? trustCertCollection.clone() : null;
296 trustManagerFactory = null;
297 return this;
298 }
299
300
301
302
303 public SslContextBuilder trustManager(Iterable<? extends X509Certificate> trustCertCollection) {
304 return trustManager(toArray(trustCertCollection, EMPTY_X509_CERTIFICATES));
305 }
306
307
308
309
310 public SslContextBuilder trustManager(TrustManagerFactory trustManagerFactory) {
311 trustCertCollection = null;
312 this.trustManagerFactory = trustManagerFactory;
313 return this;
314 }
315
316
317
318
319
320
321
322
323 public SslContextBuilder trustManager(TrustManager trustManager) {
324 if (trustManager != null) {
325 trustManagerFactory = new TrustManagerFactoryWrapper(trustManager);
326 } else {
327 trustManagerFactory = null;
328 }
329 trustCertCollection = null;
330 return this;
331 }
332
333
334
335
336
337
338
339
340 public SslContextBuilder keyManager(File keyCertChainFile, File keyFile) {
341 return keyManager(keyCertChainFile, keyFile, null);
342 }
343
344
345
346
347
348
349
350
351
352
353
354
355 public SslContextBuilder keyManager(InputStream keyCertChainInputStream, InputStream keyInputStream) {
356 return keyManager(keyCertChainInputStream, keyInputStream, null);
357 }
358
359
360
361
362
363
364
365
366 public SslContextBuilder keyManager(PrivateKey key, X509Certificate... keyCertChain) {
367 return keyManager(key, null, keyCertChain);
368 }
369
370
371
372
373
374
375
376
377 public SslContextBuilder keyManager(PrivateKey key, Iterable<? extends X509Certificate> keyCertChain) {
378 return keyManager(key, toArray(keyCertChain, EMPTY_X509_CERTIFICATES));
379 }
380
381
382
383
384
385
386
387
388
389
390 public SslContextBuilder keyManager(File keyCertChainFile, File keyFile, String keyPassword) {
391 X509Certificate[] keyCertChain;
392 PrivateKey key;
393 try {
394 keyCertChain = SslContext.toX509Certificates(keyCertChainFile);
395 } catch (Exception e) {
396 throw new IllegalArgumentException("File does not contain valid certificates: " + keyCertChainFile, e);
397 }
398 try {
399 key = SslContext.toPrivateKey(keyFile, keyPassword);
400 } catch (Exception e) {
401 throw new IllegalArgumentException("File does not contain valid private key: " + keyFile, e);
402 }
403 return keyManager(key, keyPassword, keyCertChain);
404 }
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419 public SslContextBuilder keyManager(InputStream keyCertChainInputStream, InputStream keyInputStream,
420 String keyPassword) {
421 X509Certificate[] keyCertChain;
422 PrivateKey key;
423 try {
424 keyCertChain = SslContext.toX509Certificates(keyCertChainInputStream);
425 } catch (Exception e) {
426 throw new IllegalArgumentException("Input stream not contain valid certificates.", e);
427 }
428 try {
429 key = SslContext.toPrivateKey(keyInputStream, keyPassword);
430 } catch (Exception e) {
431 throw new IllegalArgumentException("Input stream does not contain valid private key.", e);
432 }
433 return keyManager(key, keyPassword, keyCertChain);
434 }
435
436
437
438
439
440
441
442
443
444
445 public SslContextBuilder keyManager(PrivateKey key, String keyPassword, X509Certificate... keyCertChain) {
446 if (forServer) {
447 checkNonEmpty(keyCertChain, "keyCertChain");
448 checkNotNull(key, "key required for servers");
449 }
450 if (keyCertChain == null || keyCertChain.length == 0) {
451 this.keyCertChain = null;
452 } else {
453 for (X509Certificate cert: keyCertChain) {
454 checkNotNullWithIAE(cert, "cert");
455 }
456 this.keyCertChain = keyCertChain.clone();
457 }
458 this.key = key;
459 this.keyPassword = keyPassword;
460 keyManagerFactory = null;
461 return this;
462 }
463
464
465
466
467
468
469
470
471
472
473 public SslContextBuilder keyManager(PrivateKey key, String keyPassword,
474 Iterable<? extends X509Certificate> keyCertChain) {
475 return keyManager(key, keyPassword, toArray(keyCertChain, EMPTY_X509_CERTIFICATES));
476 }
477
478
479
480
481
482
483
484
485
486
487
488
489 public SslContextBuilder keyManager(KeyManagerFactory keyManagerFactory) {
490 if (forServer) {
491 checkNotNull(keyManagerFactory, "keyManagerFactory required for servers");
492 }
493 keyCertChain = null;
494 key = null;
495 keyPassword = null;
496 this.keyManagerFactory = keyManagerFactory;
497 return this;
498 }
499
500
501
502
503
504
505
506
507 public SslContextBuilder keyManager(KeyManager keyManager) {
508 if (forServer) {
509 checkNotNull(keyManager, "keyManager required for servers");
510 }
511 if (keyManager != null) {
512 keyManagerFactory = new KeyManagerFactoryWrapper(keyManager);
513 } else {
514 keyManagerFactory = null;
515 }
516 keyCertChain = null;
517 key = null;
518 keyPassword = null;
519 return this;
520 }
521
522
523
524
525
526 public SslContextBuilder ciphers(Iterable<String> ciphers) {
527 return ciphers(ciphers, IdentityCipherSuiteFilter.INSTANCE);
528 }
529
530
531
532
533
534
535 public SslContextBuilder ciphers(Iterable<String> ciphers, CipherSuiteFilter cipherFilter) {
536 this.cipherFilter = checkNotNull(cipherFilter, "cipherFilter");
537 this.ciphers = ciphers;
538 return this;
539 }
540
541
542
543
544 public SslContextBuilder applicationProtocolConfig(ApplicationProtocolConfig apn) {
545 this.apn = apn;
546 return this;
547 }
548
549
550
551
552
553 public SslContextBuilder sessionCacheSize(long sessionCacheSize) {
554 this.sessionCacheSize = sessionCacheSize;
555 return this;
556 }
557
558
559
560
561
562 public SslContextBuilder sessionTimeout(long sessionTimeout) {
563 this.sessionTimeout = sessionTimeout;
564 return this;
565 }
566
567
568
569
570 public SslContextBuilder clientAuth(ClientAuth clientAuth) {
571 this.clientAuth = checkNotNull(clientAuth, "clientAuth");
572 return this;
573 }
574
575
576
577
578
579
580 public SslContextBuilder protocols(String... protocols) {
581 this.protocols = protocols == null ? null : protocols.clone();
582 return this;
583 }
584
585
586
587
588
589
590 public SslContextBuilder protocols(Iterable<String> protocols) {
591 return protocols(toArray(protocols, EMPTY_STRINGS));
592 }
593
594
595
596
597 public SslContextBuilder startTls(boolean startTls) {
598 this.startTls = startTls;
599 return this;
600 }
601
602
603
604
605
606
607
608 @UnstableApi
609 public SslContextBuilder enableOcsp(boolean enableOcsp) {
610 this.enableOcsp = enableOcsp;
611 return this;
612 }
613
614
615
616
617
618
619
620
621
622
623
624 public SslContextBuilder secureRandom(SecureRandom secureRandom) {
625 this.secureRandom = secureRandom;
626 return this;
627 }
628
629
630
631
632
633
634
635
636
637
638
639 public SslContextBuilder endpointIdentificationAlgorithm(String algorithm) {
640 endpointIdentificationAlgorithm = algorithm;
641 return this;
642 }
643
644
645
646
647
648
649
650
651
652
653 public SslContextBuilder serverName(SNIServerName serverName) {
654 if (forServer) {
655 throw new UnsupportedOperationException("Cannot add Server Name Indication extension, " +
656 "because this is a server context builder.");
657 }
658 checkNotNull(serverName, "serverName");
659 if (!(serverName instanceof SNIHostName)) {
660 throw new IllegalArgumentException("Only SNIHostName is supported. The given SNIServerName type was " +
661 serverName.getClass().getName());
662 }
663 serverNames.add(serverName);
664 return this;
665 }
666
667
668
669
670
671
672 public SslContext build() throws SSLException {
673 if (forServer) {
674 return SslContext.newServerContextInternal(provider, sslContextProvider, trustCertCollection,
675 trustManagerFactory, keyCertChain, key, keyPassword, keyManagerFactory,
676 ciphers, cipherFilter, apn, sessionCacheSize, sessionTimeout, clientAuth, protocols, startTls,
677 enableOcsp, secureRandom, keyStoreType, toArray(options.entrySet(), EMPTY_ENTRIES));
678 } else {
679 return SslContext.newClientContextInternal(provider, sslContextProvider, trustCertCollection,
680 trustManagerFactory, keyCertChain, key, keyPassword, keyManagerFactory,
681 ciphers, cipherFilter, apn, protocols, sessionCacheSize,
682 sessionTimeout, enableOcsp, secureRandom, keyStoreType, endpointIdentificationAlgorithm,
683 serverNames, toArray(options.entrySet(), EMPTY_ENTRIES));
684 }
685 }
686
687 private static <T> T[] toArray(Iterable<? extends T> iterable, T[] prototype) {
688 if (iterable == null) {
689 return null;
690 }
691 final List<T> list = new ArrayList<T>();
692 for (T element : iterable) {
693 list.add(element);
694 }
695 return list.toArray(prototype);
696 }
697 }