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.buffer.ByteBuf;
20 import io.netty.buffer.ByteBufAllocator;
21 import io.netty.buffer.ByteBufInputStream;
22 import io.netty.channel.ChannelInitializer;
23 import io.netty.channel.ChannelPipeline;
24 import io.netty.handler.ssl.ApplicationProtocolConfig.Protocol;
25 import io.netty.handler.ssl.ApplicationProtocolConfig.SelectedListenerFailureBehavior;
26 import io.netty.handler.ssl.ApplicationProtocolConfig.SelectorFailureBehavior;
27 import io.netty.util.AttributeMap;
28 import io.netty.util.DefaultAttributeMap;
29 import io.netty.util.concurrent.ImmediateExecutor;
30 import io.netty.util.internal.EmptyArrays;
31
32 import java.io.BufferedInputStream;
33 import java.io.File;
34 import java.io.IOException;
35 import java.io.InputStream;
36 import java.security.AlgorithmParameters;
37 import java.security.InvalidAlgorithmParameterException;
38 import java.security.InvalidKeyException;
39 import java.security.KeyException;
40 import java.security.KeyFactory;
41 import java.security.KeyStore;
42 import java.security.KeyStoreException;
43 import java.security.NoSuchAlgorithmException;
44 import java.security.PrivateKey;
45 import java.security.Provider;
46 import java.security.SecureRandom;
47 import java.security.UnrecoverableKeyException;
48 import java.security.cert.CertificateException;
49 import java.security.cert.CertificateFactory;
50 import java.security.cert.X509Certificate;
51 import java.security.spec.InvalidKeySpecException;
52 import java.security.spec.PKCS8EncodedKeySpec;
53 import java.util.Collections;
54 import java.util.List;
55 import java.util.Map;
56 import java.util.concurrent.Executor;
57 import javax.crypto.Cipher;
58 import javax.crypto.EncryptedPrivateKeyInfo;
59 import javax.crypto.NoSuchPaddingException;
60 import javax.crypto.SecretKey;
61 import javax.crypto.SecretKeyFactory;
62 import javax.crypto.spec.PBEKeySpec;
63 import javax.net.ssl.KeyManager;
64 import javax.net.ssl.KeyManagerFactory;
65 import javax.net.ssl.SNIServerName;
66 import javax.net.ssl.SSLContext;
67 import javax.net.ssl.SSLEngine;
68 import javax.net.ssl.SSLException;
69 import javax.net.ssl.SSLSessionContext;
70 import javax.net.ssl.TrustManager;
71 import javax.net.ssl.TrustManagerFactory;
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95 public abstract class SslContext {
96 static final String ALIAS = "key";
97
98 static final CertificateFactory X509_CERT_FACTORY;
99 static {
100 try {
101 X509_CERT_FACTORY = CertificateFactory.getInstance("X.509");
102 } catch (CertificateException e) {
103 throw new IllegalStateException("unable to instance X.509 CertificateFactory", e);
104 }
105 }
106
107 private final boolean startTls;
108 private final AttributeMap attributes = new DefaultAttributeMap();
109 final ResumptionController resumptionController;
110 private static final String OID_PKCS5_PBES2 = "1.2.840.113549.1.5.13";
111 private static final String PBES2 = "PBES2";
112
113
114
115
116
117
118 public static SslProvider defaultServerProvider() {
119 return defaultProvider();
120 }
121
122
123
124
125
126
127 public static SslProvider defaultClientProvider() {
128 return defaultProvider();
129 }
130
131 private static SslProvider defaultProvider() {
132 if (OpenSsl.isAvailable()) {
133 return SslProvider.OPENSSL;
134 } else {
135 return SslProvider.JDK;
136 }
137 }
138
139
140
141
142
143
144
145
146
147 @Deprecated
148 public static SslContext newServerContext(File certChainFile, File keyFile) throws SSLException {
149 return newServerContext(certChainFile, keyFile, null);
150 }
151
152
153
154
155
156
157
158
159
160
161
162 @Deprecated
163 public static SslContext newServerContext(
164 File certChainFile, File keyFile, String keyPassword) throws SSLException {
165 return newServerContext(null, certChainFile, keyFile, keyPassword);
166 }
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186 @Deprecated
187 public static SslContext newServerContext(
188 File certChainFile, File keyFile, String keyPassword,
189 Iterable<String> ciphers, Iterable<String> nextProtocols,
190 long sessionCacheSize, long sessionTimeout) throws SSLException {
191
192 return newServerContext(
193 null, certChainFile, keyFile, keyPassword,
194 ciphers, nextProtocols, sessionCacheSize, sessionTimeout);
195 }
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215 @Deprecated
216 public static SslContext newServerContext(
217 File certChainFile, File keyFile, String keyPassword,
218 Iterable<String> ciphers, CipherSuiteFilter cipherFilter, ApplicationProtocolConfig apn,
219 long sessionCacheSize, long sessionTimeout) throws SSLException {
220 return newServerContext(
221 null, certChainFile, keyFile, keyPassword,
222 ciphers, cipherFilter, apn, sessionCacheSize, sessionTimeout);
223 }
224
225
226
227
228
229
230
231
232
233
234
235 @Deprecated
236 public static SslContext newServerContext(
237 SslProvider provider, File certChainFile, File keyFile) throws SSLException {
238 return newServerContext(provider, certChainFile, keyFile, null);
239 }
240
241
242
243
244
245
246
247
248
249
250
251
252
253 @Deprecated
254 public static SslContext newServerContext(
255 SslProvider provider, File certChainFile, File keyFile, String keyPassword) throws SSLException {
256 return newServerContext(provider, certChainFile, keyFile, keyPassword, null, IdentityCipherSuiteFilter.INSTANCE,
257 null, 0, 0);
258 }
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280 @Deprecated
281 public static SslContext newServerContext(
282 SslProvider provider,
283 File certChainFile, File keyFile, String keyPassword,
284 Iterable<String> ciphers, Iterable<String> nextProtocols,
285 long sessionCacheSize, long sessionTimeout) throws SSLException {
286 return newServerContext(provider, certChainFile, keyFile, keyPassword,
287 ciphers, IdentityCipherSuiteFilter.INSTANCE,
288 toApplicationProtocolConfig(nextProtocols), sessionCacheSize, sessionTimeout);
289 }
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314 @Deprecated
315 public static SslContext newServerContext(
316 SslProvider provider,
317 File certChainFile, File keyFile, String keyPassword, TrustManagerFactory trustManagerFactory,
318 Iterable<String> ciphers, Iterable<String> nextProtocols,
319 long sessionCacheSize, long sessionTimeout) throws SSLException {
320
321 return newServerContext(
322 provider, null, trustManagerFactory, certChainFile, keyFile, keyPassword,
323 null, ciphers, IdentityCipherSuiteFilter.INSTANCE,
324 toApplicationProtocolConfig(nextProtocols), sessionCacheSize, sessionTimeout);
325 }
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348 @Deprecated
349 public static SslContext newServerContext(SslProvider provider,
350 File certChainFile, File keyFile, String keyPassword,
351 Iterable<String> ciphers, CipherSuiteFilter cipherFilter, ApplicationProtocolConfig apn,
352 long sessionCacheSize, long sessionTimeout) throws SSLException {
353 return newServerContext(provider, null, null, certChainFile, keyFile, keyPassword, null,
354 ciphers, cipherFilter, apn, sessionCacheSize, sessionTimeout, KeyStore.getDefaultType());
355 }
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390 @Deprecated
391 public static SslContext newServerContext(
392 SslProvider provider,
393 File trustCertCollectionFile, TrustManagerFactory trustManagerFactory,
394 File keyCertChainFile, File keyFile, String keyPassword, KeyManagerFactory keyManagerFactory,
395 Iterable<String> ciphers, CipherSuiteFilter cipherFilter, ApplicationProtocolConfig apn,
396 long sessionCacheSize, long sessionTimeout) throws SSLException {
397 return newServerContext(provider, trustCertCollectionFile, trustManagerFactory, keyCertChainFile,
398 keyFile, keyPassword, keyManagerFactory, ciphers, cipherFilter, apn,
399 sessionCacheSize, sessionTimeout, KeyStore.getDefaultType());
400 }
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435 static SslContext newServerContext(
436 SslProvider provider,
437 File trustCertCollectionFile, TrustManagerFactory trustManagerFactory,
438 File keyCertChainFile, File keyFile, String keyPassword, KeyManagerFactory keyManagerFactory,
439 Iterable<String> ciphers, CipherSuiteFilter cipherFilter, ApplicationProtocolConfig apn,
440 long sessionCacheSize, long sessionTimeout, String keyStore) throws SSLException {
441 try {
442 return newServerContextInternal(provider, null, toX509Certificates(trustCertCollectionFile),
443 trustManagerFactory, toX509Certificates(keyCertChainFile),
444 toPrivateKey(keyFile, keyPassword),
445 keyPassword, keyManagerFactory, ciphers, cipherFilter, apn,
446 sessionCacheSize, sessionTimeout, ClientAuth.NONE, null,
447 false, false, null, keyStore);
448 } catch (Exception e) {
449 if (e instanceof SSLException) {
450 throw (SSLException) e;
451 }
452 throw new SSLException("failed to initialize the server-side SSL context", e);
453 }
454 }
455
456 static SslContext newServerContextInternal(
457 SslProvider provider,
458 Provider sslContextProvider,
459 X509Certificate[] trustCertCollection, TrustManagerFactory trustManagerFactory,
460 X509Certificate[] keyCertChain, PrivateKey key, String keyPassword, KeyManagerFactory keyManagerFactory,
461 Iterable<String> ciphers, CipherSuiteFilter cipherFilter, ApplicationProtocolConfig apn,
462 long sessionCacheSize, long sessionTimeout, ClientAuth clientAuth, String[] protocols, boolean startTls,
463 boolean enableOcsp, SecureRandom secureRandom, String keyStoreType,
464 Map.Entry<SslContextOption<?>, Object>... ctxOptions)
465 throws SSLException {
466
467 if (provider == null) {
468 provider = defaultServerProvider();
469 }
470
471 ResumptionController resumptionController = new ResumptionController();
472
473 switch (provider) {
474 case JDK:
475 if (enableOcsp) {
476 throw new IllegalArgumentException("OCSP is not supported with this SslProvider: " + provider);
477 }
478 return new JdkSslServerContext(sslContextProvider,
479 trustCertCollection, trustManagerFactory, keyCertChain, key, keyPassword,
480 keyManagerFactory, ciphers, cipherFilter, apn, sessionCacheSize, sessionTimeout,
481 clientAuth, protocols, startTls, secureRandom, keyStoreType, resumptionController);
482 case OPENSSL:
483 verifyNullSslContextProvider(provider, sslContextProvider);
484 return new OpenSslServerContext(
485 trustCertCollection, trustManagerFactory, keyCertChain, key, keyPassword,
486 keyManagerFactory, ciphers, cipherFilter, apn, sessionCacheSize, sessionTimeout,
487 clientAuth, protocols, startTls, enableOcsp, keyStoreType, resumptionController, ctxOptions);
488 case OPENSSL_REFCNT:
489 verifyNullSslContextProvider(provider, sslContextProvider);
490 return new ReferenceCountedOpenSslServerContext(
491 trustCertCollection, trustManagerFactory, keyCertChain, key, keyPassword,
492 keyManagerFactory, ciphers, cipherFilter, apn, sessionCacheSize, sessionTimeout,
493 clientAuth, protocols, startTls, enableOcsp, keyStoreType, resumptionController, ctxOptions);
494 default:
495 throw new Error(provider.toString());
496 }
497 }
498
499 private static void verifyNullSslContextProvider(SslProvider provider, Provider sslContextProvider) {
500 if (sslContextProvider != null) {
501 throw new IllegalArgumentException("Java Security Provider unsupported for SslProvider: " + provider);
502 }
503 }
504
505
506
507
508
509
510
511 @Deprecated
512 public static SslContext newClientContext() throws SSLException {
513 return newClientContext(null, null, null);
514 }
515
516
517
518
519
520
521
522
523
524 @Deprecated
525 public static SslContext newClientContext(File certChainFile) throws SSLException {
526 return newClientContext(null, certChainFile);
527 }
528
529
530
531
532
533
534
535
536
537
538
539 @Deprecated
540 public static SslContext newClientContext(TrustManagerFactory trustManagerFactory) throws SSLException {
541 return newClientContext(null, null, trustManagerFactory);
542 }
543
544
545
546
547
548
549
550
551
552
553
554
555
556 @Deprecated
557 public static SslContext newClientContext(
558 File certChainFile, TrustManagerFactory trustManagerFactory) throws SSLException {
559 return newClientContext(null, certChainFile, trustManagerFactory);
560 }
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582 @Deprecated
583 public static SslContext newClientContext(
584 File certChainFile, TrustManagerFactory trustManagerFactory,
585 Iterable<String> ciphers, Iterable<String> nextProtocols,
586 long sessionCacheSize, long sessionTimeout) throws SSLException {
587 return newClientContext(
588 null, certChainFile, trustManagerFactory,
589 ciphers, nextProtocols, sessionCacheSize, sessionTimeout);
590 }
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612 @Deprecated
613 public static SslContext newClientContext(
614 File certChainFile, TrustManagerFactory trustManagerFactory,
615 Iterable<String> ciphers, CipherSuiteFilter cipherFilter, ApplicationProtocolConfig apn,
616 long sessionCacheSize, long sessionTimeout) throws SSLException {
617 return newClientContext(
618 null, certChainFile, trustManagerFactory,
619 ciphers, cipherFilter, apn, sessionCacheSize, sessionTimeout);
620 }
621
622
623
624
625
626
627
628
629
630
631 @Deprecated
632 public static SslContext newClientContext(SslProvider provider) throws SSLException {
633 return newClientContext(provider, null, null);
634 }
635
636
637
638
639
640
641
642
643
644
645
646
647 @Deprecated
648 public static SslContext newClientContext(SslProvider provider, File certChainFile) throws SSLException {
649 return newClientContext(provider, certChainFile, null);
650 }
651
652
653
654
655
656
657
658
659
660
661
662
663
664 @Deprecated
665 public static SslContext newClientContext(
666 SslProvider provider, TrustManagerFactory trustManagerFactory) throws SSLException {
667 return newClientContext(provider, null, trustManagerFactory);
668 }
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684 @Deprecated
685 public static SslContext newClientContext(
686 SslProvider provider, File certChainFile, TrustManagerFactory trustManagerFactory) throws SSLException {
687 return newClientContext(provider, certChainFile, trustManagerFactory, null, IdentityCipherSuiteFilter.INSTANCE,
688 null, 0, 0);
689 }
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713 @Deprecated
714 public static SslContext newClientContext(
715 SslProvider provider,
716 File certChainFile, TrustManagerFactory trustManagerFactory,
717 Iterable<String> ciphers, Iterable<String> nextProtocols,
718 long sessionCacheSize, long sessionTimeout) throws SSLException {
719 return newClientContext(
720 provider, certChainFile, trustManagerFactory, null, null, null, null,
721 ciphers, IdentityCipherSuiteFilter.INSTANCE,
722 toApplicationProtocolConfig(nextProtocols), sessionCacheSize, sessionTimeout);
723 }
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747 @Deprecated
748 public static SslContext newClientContext(
749 SslProvider provider,
750 File certChainFile, TrustManagerFactory trustManagerFactory,
751 Iterable<String> ciphers, CipherSuiteFilter cipherFilter, ApplicationProtocolConfig apn,
752 long sessionCacheSize, long sessionTimeout) throws SSLException {
753
754 return newClientContext(
755 provider, certChainFile, trustManagerFactory, null, null, null, null,
756 ciphers, cipherFilter, apn, sessionCacheSize, sessionTimeout);
757 }
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796 @Deprecated
797 public static SslContext newClientContext(
798 SslProvider provider,
799 File trustCertCollectionFile, TrustManagerFactory trustManagerFactory,
800 File keyCertChainFile, File keyFile, String keyPassword,
801 KeyManagerFactory keyManagerFactory,
802 Iterable<String> ciphers, CipherSuiteFilter cipherFilter, ApplicationProtocolConfig apn,
803 long sessionCacheSize, long sessionTimeout) throws SSLException {
804 try {
805 return newClientContextInternal(provider, null,
806 toX509Certificates(trustCertCollectionFile), trustManagerFactory,
807 toX509Certificates(keyCertChainFile), toPrivateKey(keyFile, keyPassword),
808 keyPassword, keyManagerFactory, ciphers, cipherFilter,
809 apn, null, sessionCacheSize, sessionTimeout, false,
810 null, KeyStore.getDefaultType(),
811 SslUtils.defaultEndpointVerificationAlgorithm,
812 Collections.emptyList());
813 } catch (Exception e) {
814 if (e instanceof SSLException) {
815 throw (SSLException) e;
816 }
817 throw new SSLException("failed to initialize the client-side SSL context", e);
818 }
819 }
820
821 static SslContext newClientContextInternal(
822 SslProvider provider,
823 Provider sslContextProvider,
824 X509Certificate[] trustCert, TrustManagerFactory trustManagerFactory,
825 X509Certificate[] keyCertChain, PrivateKey key, String keyPassword, KeyManagerFactory keyManagerFactory,
826 Iterable<String> ciphers, CipherSuiteFilter cipherFilter, ApplicationProtocolConfig apn, String[] protocols,
827 long sessionCacheSize, long sessionTimeout, boolean enableOcsp,
828 SecureRandom secureRandom, String keyStoreType, String endpointIdentificationAlgorithm,
829 List<SNIServerName> serverNames,
830 Map.Entry<SslContextOption<?>, Object>... options) throws SSLException {
831 if (provider == null) {
832 provider = defaultClientProvider();
833 }
834
835 ResumptionController resumptionController = new ResumptionController();
836
837 switch (provider) {
838 case JDK:
839 if (enableOcsp) {
840 throw new IllegalArgumentException("OCSP is not supported with this SslProvider: " + provider);
841 }
842 return new JdkSslClientContext(sslContextProvider,
843 trustCert, trustManagerFactory, keyCertChain, key, keyPassword,
844 keyManagerFactory, ciphers, cipherFilter, apn, protocols, sessionCacheSize,
845 sessionTimeout, secureRandom, keyStoreType, endpointIdentificationAlgorithm,
846 serverNames, resumptionController);
847 case OPENSSL:
848 verifyNullSslContextProvider(provider, sslContextProvider);
849 OpenSsl.ensureAvailability();
850 return new OpenSslClientContext(
851 trustCert, trustManagerFactory, keyCertChain, key, keyPassword,
852 keyManagerFactory, ciphers, cipherFilter, apn, protocols, sessionCacheSize, sessionTimeout,
853 enableOcsp, keyStoreType, endpointIdentificationAlgorithm, serverNames, resumptionController,
854 options);
855 case OPENSSL_REFCNT:
856 verifyNullSslContextProvider(provider, sslContextProvider);
857 OpenSsl.ensureAvailability();
858 return new ReferenceCountedOpenSslClientContext(
859 trustCert, trustManagerFactory, keyCertChain, key, keyPassword,
860 keyManagerFactory, ciphers, cipherFilter, apn, protocols, sessionCacheSize, sessionTimeout,
861 enableOcsp, keyStoreType, endpointIdentificationAlgorithm, serverNames, resumptionController,
862 options);
863 default:
864 throw new Error(provider.toString());
865 }
866 }
867
868 static ApplicationProtocolConfig toApplicationProtocolConfig(Iterable<String> nextProtocols) {
869 ApplicationProtocolConfig apn;
870 if (nextProtocols == null) {
871 apn = ApplicationProtocolConfig.DISABLED;
872 } else {
873 apn = new ApplicationProtocolConfig(
874 Protocol.NPN_AND_ALPN, SelectorFailureBehavior.CHOOSE_MY_LAST_PROTOCOL,
875 SelectedListenerFailureBehavior.ACCEPT, nextProtocols);
876 }
877 return apn;
878 }
879
880
881
882
883 protected SslContext() {
884 this(false);
885 }
886
887
888
889
890 protected SslContext(boolean startTls) {
891 this(startTls, null);
892 }
893
894 SslContext(boolean startTls, ResumptionController resumptionController) {
895 this.startTls = startTls;
896 this.resumptionController = resumptionController;
897 }
898
899
900
901
902 public final AttributeMap attributes() {
903 return attributes;
904 }
905
906
907
908
909 public final boolean isServer() {
910 return !isClient();
911 }
912
913
914
915
916 public abstract boolean isClient();
917
918
919
920
921 public abstract List<String> cipherSuites();
922
923
924
925
926 public long sessionCacheSize() {
927 return sessionContext().getSessionCacheSize();
928 }
929
930
931
932
933 public long sessionTimeout() {
934 return sessionContext().getSessionTimeout();
935 }
936
937
938
939
940 @Deprecated
941 public final List<String> nextProtocols() {
942 return applicationProtocolNegotiator().protocols();
943 }
944
945
946
947
948 public abstract ApplicationProtocolNegotiator applicationProtocolNegotiator();
949
950
951
952
953
954
955
956 public abstract SSLEngine newEngine(ByteBufAllocator alloc);
957
958
959
960
961
962
963
964
965
966
967
968 public abstract SSLEngine newEngine(ByteBufAllocator alloc, String peerHost, int peerPort);
969
970
971
972
973 public abstract SSLSessionContext sessionContext();
974
975
976
977
978
979 public final SslHandler newHandler(ByteBufAllocator alloc) {
980 return newHandler(alloc, startTls);
981 }
982
983
984
985
986
987 protected SslHandler newHandler(ByteBufAllocator alloc, boolean startTls) {
988 return new SslHandler(newEngine(alloc), startTls, ImmediateExecutor.INSTANCE, resumptionController);
989 }
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016 public SslHandler newHandler(ByteBufAllocator alloc, Executor delegatedTaskExecutor) {
1017 return newHandler(alloc, startTls, delegatedTaskExecutor);
1018 }
1019
1020
1021
1022
1023
1024 protected SslHandler newHandler(ByteBufAllocator alloc, boolean startTls, Executor executor) {
1025 return new SslHandler(newEngine(alloc), startTls, executor, resumptionController);
1026 }
1027
1028
1029
1030
1031
1032
1033 public final SslHandler newHandler(ByteBufAllocator alloc, String peerHost, int peerPort) {
1034 return newHandler(alloc, peerHost, peerPort, startTls);
1035 }
1036
1037
1038
1039
1040
1041 protected SslHandler newHandler(ByteBufAllocator alloc, String peerHost, int peerPort, boolean startTls) {
1042 return new SslHandler(newEngine(alloc, peerHost, peerPort), startTls, ImmediateExecutor.INSTANCE,
1043 resumptionController);
1044 }
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074 public SslHandler newHandler(ByteBufAllocator alloc, String peerHost, int peerPort,
1075 Executor delegatedTaskExecutor) {
1076 return newHandler(alloc, peerHost, peerPort, startTls, delegatedTaskExecutor);
1077 }
1078
1079 protected SslHandler newHandler(ByteBufAllocator alloc, String peerHost, int peerPort, boolean startTls,
1080 Executor delegatedTaskExecutor) {
1081 return new SslHandler(newEngine(alloc, peerHost, peerPort), startTls, delegatedTaskExecutor,
1082 resumptionController);
1083 }
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101 @Deprecated
1102 protected static PKCS8EncodedKeySpec generateKeySpec(char[] password, byte[] key)
1103 throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeySpecException,
1104 InvalidKeyException, InvalidAlgorithmParameterException {
1105
1106 if (password == null) {
1107 return new PKCS8EncodedKeySpec(key);
1108 }
1109
1110 EncryptedPrivateKeyInfo encryptedPrivateKeyInfo = new EncryptedPrivateKeyInfo(key);
1111 String pbeAlgorithm = getPBEAlgorithm(encryptedPrivateKeyInfo);
1112 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(pbeAlgorithm);
1113 PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
1114 SecretKey pbeKey = keyFactory.generateSecret(pbeKeySpec);
1115
1116 Cipher cipher = Cipher.getInstance(pbeAlgorithm);
1117 cipher.init(Cipher.DECRYPT_MODE, pbeKey, encryptedPrivateKeyInfo.getAlgParameters());
1118
1119 return encryptedPrivateKeyInfo.getKeySpec(cipher);
1120 }
1121
1122 private static String getPBEAlgorithm(EncryptedPrivateKeyInfo encryptedPrivateKeyInfo) {
1123 AlgorithmParameters parameters = encryptedPrivateKeyInfo.getAlgParameters();
1124 String algName = encryptedPrivateKeyInfo.getAlgName();
1125
1126
1127 if (parameters != null && (OID_PKCS5_PBES2.equals(algName) || PBES2.equals(algName))) {
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138 return parameters.toString();
1139 }
1140 return encryptedPrivateKeyInfo.getAlgName();
1141 }
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153 protected static KeyStore buildKeyStore(X509Certificate[] certChain, PrivateKey key,
1154 char[] keyPasswordChars, String keyStoreType)
1155 throws KeyStoreException, NoSuchAlgorithmException,
1156 CertificateException, IOException {
1157 if (keyStoreType == null) {
1158 keyStoreType = KeyStore.getDefaultType();
1159 }
1160 KeyStore ks = KeyStore.getInstance(keyStoreType);
1161 ks.load(null, null);
1162 ks.setKeyEntry(ALIAS, key, keyPasswordChars, certChain);
1163 return ks;
1164 }
1165
1166 protected static PrivateKey toPrivateKey(File keyFile, String keyPassword) throws NoSuchAlgorithmException,
1167 NoSuchPaddingException, InvalidKeySpecException,
1168 InvalidAlgorithmParameterException,
1169 KeyException, IOException {
1170 return toPrivateKey(keyFile, keyPassword, true);
1171 }
1172
1173 static PrivateKey toPrivateKey(File keyFile, String keyPassword, boolean tryBouncyCastle)
1174 throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeySpecException,
1175 InvalidAlgorithmParameterException,
1176 KeyException, IOException {
1177 if (keyFile == null) {
1178 return null;
1179 }
1180
1181
1182 if (tryBouncyCastle && BouncyCastlePemReader.isAvailable()) {
1183 PrivateKey pk = BouncyCastlePemReader.getPrivateKey(keyFile, keyPassword);
1184 if (pk != null) {
1185 return pk;
1186 }
1187 }
1188
1189 return getPrivateKeyFromByteBuffer(PemReader.readPrivateKey(keyFile), keyPassword);
1190 }
1191
1192 protected static PrivateKey toPrivateKey(InputStream keyInputStream, String keyPassword)
1193 throws NoSuchAlgorithmException,
1194 NoSuchPaddingException, InvalidKeySpecException,
1195 InvalidAlgorithmParameterException,
1196 KeyException, IOException {
1197 if (keyInputStream == null) {
1198 return null;
1199 }
1200
1201
1202 if (BouncyCastlePemReader.isAvailable()) {
1203 if (!keyInputStream.markSupported()) {
1204
1205 keyInputStream = new BufferedInputStream(keyInputStream);
1206 }
1207 keyInputStream.mark(1048576);
1208 PrivateKey pk = BouncyCastlePemReader.getPrivateKey(keyInputStream, keyPassword);
1209 if (pk != null) {
1210 return pk;
1211 }
1212
1213 keyInputStream.reset();
1214 }
1215
1216 return getPrivateKeyFromByteBuffer(PemReader.readPrivateKey(keyInputStream), keyPassword);
1217 }
1218
1219 private static PrivateKey getPrivateKeyFromByteBuffer(ByteBuf encodedKeyBuf, String keyPassword)
1220 throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeySpecException,
1221 InvalidAlgorithmParameterException, KeyException, IOException {
1222
1223 byte[] encodedKey = new byte[encodedKeyBuf.readableBytes()];
1224 encodedKeyBuf.readBytes(encodedKey).release();
1225
1226 PKCS8EncodedKeySpec encodedKeySpec = generateKeySpec(
1227 keyPassword == null ? null : keyPassword.toCharArray(), encodedKey);
1228 try {
1229 return KeyFactory.getInstance("RSA").generatePrivate(encodedKeySpec);
1230 } catch (InvalidKeySpecException ignore) {
1231 try {
1232 return KeyFactory.getInstance("DSA").generatePrivate(encodedKeySpec);
1233 } catch (InvalidKeySpecException ignore2) {
1234 try {
1235 return KeyFactory.getInstance("EC").generatePrivate(encodedKeySpec);
1236 } catch (InvalidKeySpecException e) {
1237 throw new InvalidKeySpecException("Neither RSA, DSA nor EC worked", e);
1238 }
1239 }
1240 }
1241 }
1242
1243
1244
1245
1246
1247
1248
1249 @Deprecated
1250 protected static TrustManagerFactory buildTrustManagerFactory(
1251 File certChainFile, TrustManagerFactory trustManagerFactory)
1252 throws NoSuchAlgorithmException, CertificateException, KeyStoreException, IOException {
1253 return buildTrustManagerFactory(certChainFile, trustManagerFactory, null);
1254 }
1255
1256
1257
1258
1259
1260
1261
1262
1263 protected static TrustManagerFactory buildTrustManagerFactory(
1264 File certChainFile, TrustManagerFactory trustManagerFactory, String keyType)
1265 throws NoSuchAlgorithmException, CertificateException, KeyStoreException, IOException {
1266 X509Certificate[] x509Certs = toX509Certificates(certChainFile);
1267
1268 return buildTrustManagerFactory(x509Certs, trustManagerFactory, keyType);
1269 }
1270
1271 protected static X509Certificate[] toX509Certificates(File file) throws CertificateException {
1272 if (file == null) {
1273 return null;
1274 }
1275 return getCertificatesFromBuffers(PemReader.readCertificates(file));
1276 }
1277
1278 protected static X509Certificate[] toX509Certificates(InputStream in) throws CertificateException {
1279 if (in == null) {
1280 return null;
1281 }
1282 return getCertificatesFromBuffers(PemReader.readCertificates(in));
1283 }
1284
1285 private static X509Certificate[] getCertificatesFromBuffers(ByteBuf[] certs) throws CertificateException {
1286 CertificateFactory cf = CertificateFactory.getInstance("X.509");
1287 X509Certificate[] x509Certs = new X509Certificate[certs.length];
1288
1289 try {
1290 for (int i = 0; i < certs.length; i++) {
1291 InputStream is = new ByteBufInputStream(certs[i], false);
1292 try {
1293 x509Certs[i] = (X509Certificate) cf.generateCertificate(is);
1294 } finally {
1295 try {
1296 is.close();
1297 } catch (IOException e) {
1298
1299 throw new RuntimeException(e);
1300 }
1301 }
1302 }
1303 } finally {
1304 for (ByteBuf buf: certs) {
1305 buf.release();
1306 }
1307 }
1308 return x509Certs;
1309 }
1310
1311 protected static TrustManagerFactory buildTrustManagerFactory(
1312 X509Certificate[] certCollection, TrustManagerFactory trustManagerFactory, String keyStoreType)
1313 throws NoSuchAlgorithmException, CertificateException, KeyStoreException, IOException {
1314 if (keyStoreType == null) {
1315 keyStoreType = KeyStore.getDefaultType();
1316 }
1317 final KeyStore ks = KeyStore.getInstance(keyStoreType);
1318 ks.load(null, null);
1319
1320 int i = 1;
1321 for (X509Certificate cert: certCollection) {
1322 String alias = Integer.toString(i);
1323 ks.setCertificateEntry(alias, cert);
1324 i++;
1325 }
1326
1327
1328 if (trustManagerFactory == null) {
1329 trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
1330 }
1331 trustManagerFactory.init(ks);
1332
1333 return trustManagerFactory;
1334 }
1335
1336 static PrivateKey toPrivateKeyInternal(File keyFile, String keyPassword) throws SSLException {
1337 try {
1338 return toPrivateKey(keyFile, keyPassword);
1339 } catch (Exception e) {
1340 throw new SSLException(e);
1341 }
1342 }
1343
1344 static X509Certificate[] toX509CertificatesInternal(File file) throws SSLException {
1345 try {
1346 return toX509Certificates(file);
1347 } catch (CertificateException e) {
1348 throw new SSLException(e);
1349 }
1350 }
1351
1352 protected static KeyManagerFactory buildKeyManagerFactory(X509Certificate[] certChainFile,
1353 String keyAlgorithm, PrivateKey key,
1354 String keyPassword, KeyManagerFactory kmf,
1355 String keyStore)
1356 throws KeyStoreException, NoSuchAlgorithmException, IOException,
1357 CertificateException, UnrecoverableKeyException {
1358 if (keyAlgorithm == null) {
1359 keyAlgorithm = KeyManagerFactory.getDefaultAlgorithm();
1360 }
1361 char[] keyPasswordChars = keyStorePassword(keyPassword);
1362 KeyStore ks = buildKeyStore(certChainFile, key, keyPasswordChars, keyStore);
1363 return buildKeyManagerFactory(ks, keyAlgorithm, keyPasswordChars, kmf);
1364 }
1365
1366 static KeyManagerFactory buildKeyManagerFactory(KeyStore ks,
1367 String keyAlgorithm,
1368 char[] keyPasswordChars, KeyManagerFactory kmf)
1369 throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException {
1370
1371 if (kmf == null) {
1372 if (keyAlgorithm == null) {
1373 keyAlgorithm = KeyManagerFactory.getDefaultAlgorithm();
1374 }
1375 kmf = KeyManagerFactory.getInstance(keyAlgorithm);
1376 }
1377 kmf.init(ks, keyPasswordChars);
1378
1379 return kmf;
1380 }
1381
1382 static char[] keyStorePassword(String keyPassword) {
1383 return keyPassword == null ? EmptyArrays.EMPTY_CHARS : keyPassword.toCharArray();
1384 }
1385 }