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.util.CharsetUtil;
20 import io.netty.util.internal.PlatformDependent;
21 import io.netty.util.internal.SuppressJava6Requirement;
22
23 import java.io.ByteArrayInputStream;
24 import java.io.IOException;
25 import java.security.InvalidAlgorithmParameterException;
26 import java.security.KeyException;
27 import java.security.KeyStore;
28 import java.security.KeyStoreException;
29 import java.security.NoSuchAlgorithmException;
30 import java.security.Provider;
31 import javax.crypto.NoSuchPaddingException;
32 import javax.net.ssl.KeyManager;
33
34 import javax.net.ssl.KeyManagerFactory;
35 import javax.net.ssl.SSLContext;
36 import javax.net.ssl.SSLException;
37 import javax.net.ssl.SSLSessionContext;
38 import javax.net.ssl.TrustManager;
39 import javax.net.ssl.TrustManagerFactory;
40 import javax.net.ssl.X509ExtendedTrustManager;
41 import java.io.File;
42 import java.security.PrivateKey;
43 import java.security.UnrecoverableKeyException;
44 import java.security.cert.CertificateException;
45 import java.security.cert.X509Certificate;
46 import java.security.spec.InvalidKeySpecException;
47
48 import static io.netty.handler.ssl.SslUtils.PROBING_CERT;
49 import static io.netty.handler.ssl.SslUtils.PROBING_KEY;
50
51
52
53
54
55
56
57 @Deprecated
58 public final class JdkSslServerContext extends JdkSslContext {
59
60 private static final boolean WRAP_TRUST_MANAGER;
61 static {
62 boolean wrapTrustManager = false;
63 if (PlatformDependent.javaVersion() >= 7) {
64 try {
65 checkIfWrappingTrustManagerIsSupported();
66 wrapTrustManager = true;
67 } catch (Throwable ignore) {
68
69
70 }
71 }
72 WRAP_TRUST_MANAGER = wrapTrustManager;
73 }
74
75
76 @SuppressJava6Requirement(reason = "Guarded by java version check")
77 static void checkIfWrappingTrustManagerIsSupported() throws CertificateException,
78 InvalidAlgorithmParameterException, NoSuchPaddingException, NoSuchAlgorithmException,
79 InvalidKeySpecException, IOException, KeyException, KeyStoreException, UnrecoverableKeyException {
80 X509Certificate[] certs = toX509Certificates(
81 new ByteArrayInputStream(PROBING_CERT.getBytes(CharsetUtil.US_ASCII)));
82 PrivateKey privateKey = toPrivateKey(new ByteArrayInputStream(
83 PROBING_KEY.getBytes(CharsetUtil.UTF_8)), null);
84 char[] keyStorePassword = keyStorePassword(null);
85 KeyStore ks = buildKeyStore(certs, privateKey, keyStorePassword, null);
86 KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
87 kmf.init(ks, keyStorePassword);
88
89 SSLContext ctx = SSLContext.getInstance(PROTOCOL);
90 TrustManagerFactory tm = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
91 tm.init((KeyStore) null);
92 TrustManager[] managers = tm.getTrustManagers();
93
94 ctx.init(kmf.getKeyManagers(), wrapTrustManagerIfNeeded(managers), null);
95 }
96
97
98
99
100
101
102
103
104 @Deprecated
105 public JdkSslServerContext(File certChainFile, File keyFile) throws SSLException {
106 this(null, certChainFile, keyFile, null, null, IdentityCipherSuiteFilter.INSTANCE,
107 JdkDefaultApplicationProtocolNegotiator.INSTANCE, 0, 0, null);
108 }
109
110
111
112
113
114
115
116
117
118
119 @Deprecated
120 public JdkSslServerContext(File certChainFile, File keyFile, String keyPassword) throws SSLException {
121 this(certChainFile, keyFile, keyPassword, null, IdentityCipherSuiteFilter.INSTANCE,
122 JdkDefaultApplicationProtocolNegotiator.INSTANCE, 0, 0);
123 }
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142 @Deprecated
143 public JdkSslServerContext(
144 File certChainFile, File keyFile, String keyPassword,
145 Iterable<String> ciphers, Iterable<String> nextProtocols,
146 long sessionCacheSize, long sessionTimeout) throws SSLException {
147 this(null, certChainFile, keyFile, keyPassword, ciphers, IdentityCipherSuiteFilter.INSTANCE,
148 toNegotiator(toApplicationProtocolConfig(nextProtocols), true), sessionCacheSize,
149 sessionTimeout, KeyStore.getDefaultType());
150 }
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169 @Deprecated
170 public JdkSslServerContext(
171 File certChainFile, File keyFile, String keyPassword,
172 Iterable<String> ciphers, CipherSuiteFilter cipherFilter, ApplicationProtocolConfig apn,
173 long sessionCacheSize, long sessionTimeout) throws SSLException {
174 this(null, certChainFile, keyFile, keyPassword, ciphers, cipherFilter,
175 toNegotiator(apn, true), sessionCacheSize, sessionTimeout, KeyStore.getDefaultType());
176 }
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195 @Deprecated
196 public JdkSslServerContext(
197 File certChainFile, File keyFile, String keyPassword,
198 Iterable<String> ciphers, CipherSuiteFilter cipherFilter, JdkApplicationProtocolNegotiator apn,
199 long sessionCacheSize, long sessionTimeout) throws SSLException {
200 this(null, certChainFile, keyFile, keyPassword, ciphers, cipherFilter, apn,
201 sessionCacheSize, sessionTimeout, KeyStore.getDefaultType());
202 }
203
204 JdkSslServerContext(Provider provider,
205 File certChainFile, File keyFile, String keyPassword,
206 Iterable<String> ciphers, CipherSuiteFilter cipherFilter, JdkApplicationProtocolNegotiator apn,
207 long sessionCacheSize, long sessionTimeout, String keyStore) throws SSLException {
208 super(newSSLContext(provider, null, null,
209 toX509CertificatesInternal(certChainFile), toPrivateKeyInternal(keyFile, keyPassword),
210 keyPassword, null, sessionCacheSize, sessionTimeout, keyStore), false,
211 ciphers, cipherFilter, apn, ClientAuth.NONE, null, false);
212 }
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242 @Deprecated
243 public JdkSslServerContext(File trustCertCollectionFile, TrustManagerFactory trustManagerFactory,
244 File keyCertChainFile, File keyFile, String keyPassword,
245 KeyManagerFactory keyManagerFactory,
246 Iterable<String> ciphers, CipherSuiteFilter cipherFilter, ApplicationProtocolConfig apn,
247 long sessionCacheSize, long sessionTimeout) throws SSLException {
248 super(newSSLContext(null, toX509CertificatesInternal(trustCertCollectionFile), trustManagerFactory,
249 toX509CertificatesInternal(keyCertChainFile), toPrivateKeyInternal(keyFile, keyPassword),
250 keyPassword, keyManagerFactory, sessionCacheSize, sessionTimeout, null), false,
251 ciphers, cipherFilter, apn, ClientAuth.NONE, null, false);
252 }
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282 @Deprecated
283 public JdkSslServerContext(File trustCertCollectionFile, TrustManagerFactory trustManagerFactory,
284 File keyCertChainFile, File keyFile, String keyPassword,
285 KeyManagerFactory keyManagerFactory,
286 Iterable<String> ciphers, CipherSuiteFilter cipherFilter,
287 JdkApplicationProtocolNegotiator apn,
288 long sessionCacheSize, long sessionTimeout) throws SSLException {
289 super(newSSLContext(null, toX509CertificatesInternal(trustCertCollectionFile), trustManagerFactory,
290 toX509CertificatesInternal(keyCertChainFile), toPrivateKeyInternal(keyFile, keyPassword),
291 keyPassword, keyManagerFactory, sessionCacheSize, sessionTimeout, KeyStore.getDefaultType()), false,
292 ciphers, cipherFilter, apn, ClientAuth.NONE, null, false);
293 }
294
295 JdkSslServerContext(Provider provider,
296 X509Certificate[] trustCertCollection, TrustManagerFactory trustManagerFactory,
297 X509Certificate[] keyCertChain, PrivateKey key, String keyPassword,
298 KeyManagerFactory keyManagerFactory, Iterable<String> ciphers, CipherSuiteFilter cipherFilter,
299 ApplicationProtocolConfig apn, long sessionCacheSize, long sessionTimeout,
300 ClientAuth clientAuth, String[] protocols, boolean startTls,
301 String keyStore) throws SSLException {
302 super(newSSLContext(provider, trustCertCollection, trustManagerFactory, keyCertChain, key,
303 keyPassword, keyManagerFactory, sessionCacheSize, sessionTimeout, keyStore), false,
304 ciphers, cipherFilter, toNegotiator(apn, true), clientAuth, protocols, startTls);
305 }
306
307 private static SSLContext newSSLContext(Provider sslContextProvider, X509Certificate[] trustCertCollection,
308 TrustManagerFactory trustManagerFactory, X509Certificate[] keyCertChain,
309 PrivateKey key, String keyPassword, KeyManagerFactory keyManagerFactory,
310 long sessionCacheSize, long sessionTimeout, String keyStore)
311 throws SSLException {
312 if (key == null && keyManagerFactory == null) {
313 throw new NullPointerException("key, keyManagerFactory");
314 }
315
316 try {
317 if (trustCertCollection != null) {
318 trustManagerFactory = buildTrustManagerFactory(trustCertCollection, trustManagerFactory, keyStore);
319 } else if (trustManagerFactory == null) {
320
321 trustManagerFactory = TrustManagerFactory.getInstance(
322 TrustManagerFactory.getDefaultAlgorithm());
323 trustManagerFactory.init((KeyStore) null);
324 }
325
326 if (key != null) {
327 keyManagerFactory = buildKeyManagerFactory(keyCertChain, null,
328 key, keyPassword, keyManagerFactory, null);
329 }
330
331
332 SSLContext ctx = sslContextProvider == null ? SSLContext.getInstance(PROTOCOL)
333 : SSLContext.getInstance(PROTOCOL, sslContextProvider);
334 ctx.init(keyManagerFactory.getKeyManagers(),
335 wrapTrustManagerIfNeeded(trustManagerFactory.getTrustManagers()),
336 null);
337
338 SSLSessionContext sessCtx = ctx.getServerSessionContext();
339 if (sessionCacheSize > 0) {
340 sessCtx.setSessionCacheSize((int) Math.min(sessionCacheSize, Integer.MAX_VALUE));
341 }
342 if (sessionTimeout > 0) {
343 sessCtx.setSessionTimeout((int) Math.min(sessionTimeout, Integer.MAX_VALUE));
344 }
345 return ctx;
346 } catch (Exception e) {
347 if (e instanceof SSLException) {
348 throw (SSLException) e;
349 }
350 throw new SSLException("failed to initialize the server-side SSL context", e);
351 }
352 }
353
354 @SuppressJava6Requirement(reason = "Guarded by java version check")
355 private static TrustManager[] wrapTrustManagerIfNeeded(TrustManager[] trustManagers) {
356 if (WRAP_TRUST_MANAGER && PlatformDependent.javaVersion() >= 7) {
357 for (int i = 0; i < trustManagers.length; i++) {
358 TrustManager tm = trustManagers[i];
359 if (tm instanceof X509ExtendedTrustManager) {
360
361
362 trustManagers[i] = new EnhancingX509ExtendedTrustManager((X509ExtendedTrustManager) tm);
363 }
364 }
365 }
366 return trustManagers;
367 }
368 }