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