29
29
30
30
import javax .net .ssl .SSLException ;
31
31
import javax .net .ssl .TrustManagerFactory ;
32
+ import java .io .InputStream ;
32
33
import java .security .KeyStore ;
33
34
import java .util .List ;
34
35
import java .util .Objects ;
@@ -40,17 +41,19 @@ public final class SslContextFactory {
40
41
41
42
private static final Logger LOGGER = LogManager .getLogger (SslContextFactory .class );
42
43
43
- public static SslContext create (SslServerProperties serverProperties ) throws SSLException {
44
- Objects .requireNonNull (serverProperties , "serverProperties" );
44
+ private final SslProvider sslProvider ;
45
45
46
- SslProvider sslProvider = getSslProvider (serverProperties .getSslProviderType ());
46
+ public SslContextFactory (String providerType ) throws SSLException {
47
+ Objects .requireNonNull (providerType , "providerType" );
48
+ this .sslProvider = getSslProvider (providerType );
49
+ }
47
50
48
- SslContextBuilder sslContextBuilder ;
49
- try {
50
- Resource keyCertChainFileResource = serverProperties .getKeyCertChainResource ();
51
- Resource keyResource = serverProperties .getKeyResource ();
51
+ public SslContext forServer (InputStream keyCertChainInputStream , InputStream keyInputStream ) throws SSLException {
52
+ Objects .requireNonNull (keyCertChainInputStream , "keyCertChainInputStream" );
53
+ Objects .requireNonNull (keyInputStream , "keyInputStream" );
52
54
53
- sslContextBuilder = SslContextBuilder .forServer (keyCertChainFileResource .getInputStream (), keyResource .getInputStream ());
55
+ try {
56
+ SslContextBuilder sslContextBuilder = SslContextBuilder .forServer (keyCertChainInputStream , keyInputStream );
54
57
SslContext sslContext = createSslContext (sslContextBuilder , sslProvider );
55
58
56
59
assertValidCipherSuite (sslContext );
@@ -63,18 +66,15 @@ public static SslContext create(SslServerProperties serverProperties) throws SSL
63
66
}
64
67
}
65
68
66
- public static SslContext create (SslClientConfig clientConfig ) throws SSLException {
69
+ public SslContext forClient (SslClientConfig clientConfig ) throws SSLException {
67
70
Objects .requireNonNull (clientConfig , "clientConfig" );
68
71
69
72
if (!clientConfig .isEnable ()) {
70
73
throw new IllegalArgumentException ("sslConfig is disabled." );
71
74
}
72
75
73
- SslProvider sslProvider = getSslProvider (clientConfig .getSslProviderType ());
74
-
75
- SslContextBuilder sslContextBuilder = null ;
76
76
try {
77
- sslContextBuilder = SslContextBuilder .forClient ();
77
+ SslContextBuilder sslContextBuilder = SslContextBuilder .forClient ();
78
78
79
79
Resource trustCertResource = clientConfig .getTrustCertResource ();
80
80
if (trustCertResource != null ) {
@@ -85,7 +85,8 @@ public static SslContext create(SslClientConfig clientConfig) throws SSLExceptio
85
85
trustManagerFactory .init ((KeyStore )null );
86
86
sslContextBuilder .trustManager (trustManagerFactory );
87
87
}
88
-
88
+
89
+ SslProvider sslProvider = getSslProvider (clientConfig .getSslProviderType ());
89
90
SslContext sslContext = createSslContext (sslContextBuilder , sslProvider );
90
91
91
92
assertValidCipherSuite (sslContext );
@@ -98,7 +99,7 @@ public static SslContext create(SslClientConfig clientConfig) throws SSLExceptio
98
99
}
99
100
}
100
101
101
- private static SslContext createSslContext (SslContextBuilder sslContextBuilder , SslProvider sslProvider ) throws SSLException {
102
+ private SslContext createSslContext (SslContextBuilder sslContextBuilder , SslProvider sslProvider ) throws SSLException {
102
103
sslContextBuilder .sslProvider (sslProvider );
103
104
104
105
sslContextBuilder .protocols (SecurityConstants .DEFAULT_SUPPORT_PROTOCOLS .toArray (new String [0 ]));
@@ -108,7 +109,7 @@ private static SslContext createSslContext(SslContextBuilder sslContextBuilder,
108
109
return configure .build ();
109
110
}
110
111
111
- private static void assertValidCipherSuite (SslContext sslContext ) throws SSLException {
112
+ private void assertValidCipherSuite (SslContext sslContext ) throws SSLException {
112
113
Objects .requireNonNull (sslContext , "sslContext must not be null" );
113
114
114
115
List <String > supportedCipherSuiteList = sslContext .cipherSuites ();
@@ -125,7 +126,7 @@ private static void assertValidCipherSuite(SslContext sslContext) throws SSLExce
125
126
LOGGER .info ("Support cipher list : {} {}" , sslContext , supportedCipherSuiteList );
126
127
}
127
128
128
- static SslProvider getSslProvider (String providerType ) throws SSLException {
129
+ SslProvider getSslProvider (String providerType ) throws SSLException {
129
130
if (StringUtils .isEmpty (providerType )) {
130
131
return SslProvider .OPENSSL ;
131
132
}
0 commit comments