1 /*
2 * Copyright 2014 The Netty Project
3 *
4 * The Netty Project licenses this file to you under the Apache License,
5 * version 2.0 (the "License"); you may not use this file except in compliance
6 * with the License. You may obtain a copy of the License at:
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations
14 * under the License.
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.SecureRandom;
44 import java.security.UnrecoverableKeyException;
45 import java.security.cert.CertificateException;
46 import java.security.cert.X509Certificate;
47 import java.security.spec.InvalidKeySpecException;
48
49 import static io.netty.handler.ssl.SslUtils.PROBING_CERT;
50 import static io.netty.handler.ssl.SslUtils.PROBING_KEY;
51
52 /**
53 * A server-side {@link SslContext} which uses JDK's SSL/TLS implementation.
54 *
55 * @deprecated Use {@link SslContextBuilder} to create {@link JdkSslContext} instances and only
56 * use {@link JdkSslContext} in your code.
57 */
58 @Deprecated
59 public final class JdkSslServerContext extends JdkSslContext {
60
61 private static final boolean WRAP_TRUST_MANAGER;
62 static {
63 boolean wrapTrustManager = false;
64 if (PlatformDependent.javaVersion() >= 7) {
65 try {
66 checkIfWrappingTrustManagerIsSupported();
67 wrapTrustManager = true;
68 } catch (Throwable ignore) {
69 // Just don't wrap as we might not be able to do so because of FIPS:
70 // See https://github.com/netty/netty/issues/13840
71 }
72 }
73 WRAP_TRUST_MANAGER = wrapTrustManager;
74 }
75
76 // Package-private for testing.
77 @SuppressJava6Requirement(reason = "Guarded by java version check")
78 static void checkIfWrappingTrustManagerIsSupported() throws CertificateException,
79 InvalidAlgorithmParameterException, NoSuchPaddingException, NoSuchAlgorithmException,
80 InvalidKeySpecException, IOException, KeyException, KeyStoreException, UnrecoverableKeyException {
81 X509Certificate[] certs = toX509Certificates(
82 new ByteArrayInputStream(PROBING_CERT.getBytes(CharsetUtil.US_ASCII)));
83 PrivateKey privateKey = toPrivateKey(new ByteArrayInputStream(
84 PROBING_KEY.getBytes(CharsetUtil.UTF_8)), null);
85 char[] keyStorePassword = keyStorePassword(null);
86 KeyStore ks = buildKeyStore(certs, privateKey, keyStorePassword, null);
87 KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
88 kmf.init(ks, keyStorePassword);
89
90 SSLContext ctx = SSLContext.getInstance(PROTOCOL);
91 TrustManagerFactory tm = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
92 tm.init((KeyStore) null);
93 TrustManager[] managers = tm.getTrustManagers();
94
95 ctx.init(kmf.getKeyManagers(), wrapTrustManagerIfNeeded(managers, null), null);
96 }
97
98 /**
99 * Creates a new instance.
100 *
101 * @param certChainFile an X.509 certificate chain file in PEM format
102 * @param keyFile a PKCS#8 private key file in PEM format
103 * @deprecated use {@link SslContextBuilder}
104 */
105 @Deprecated
106 public JdkSslServerContext(File certChainFile, File keyFile) throws SSLException {
107 this(null, certChainFile, keyFile, null, null, IdentityCipherSuiteFilter.INSTANCE,
108 JdkDefaultApplicationProtocolNegotiator.INSTANCE, 0, 0, null);
109 }
110
111 /**
112 * Creates a new instance.
113 *
114 * @param certChainFile an X.509 certificate chain file in PEM format
115 * @param keyFile a PKCS#8 private key file in PEM format
116 * @param keyPassword the password of the {@code keyFile}.
117 * {@code null} if it's not password-protected.
118 * @deprecated use {@link SslContextBuilder}
119 */
120 @Deprecated
121 public JdkSslServerContext(File certChainFile, File keyFile, String keyPassword) throws SSLException {
122 this(certChainFile, keyFile, keyPassword, null, IdentityCipherSuiteFilter.INSTANCE,
123 JdkDefaultApplicationProtocolNegotiator.INSTANCE, 0, 0);
124 }
125
126 /**
127 * Creates a new instance.
128 *
129 * @param certChainFile an X.509 certificate chain file in PEM format
130 * @param keyFile a PKCS#8 private key file in PEM format
131 * @param keyPassword the password of the {@code keyFile}.
132 * {@code null} if it's not password-protected.
133 * @param ciphers the cipher suites to enable, in the order of preference.
134 * {@code null} to use the default cipher suites.
135 * @param nextProtocols the application layer protocols to accept, in the order of preference.
136 * {@code null} to disable TLS NPN/ALPN extension.
137 * @param sessionCacheSize the size of the cache used for storing SSL session objects.
138 * {@code 0} to use the default value.
139 * @param sessionTimeout the timeout for the cached SSL session objects, in seconds.
140 * {@code 0} to use the default value.
141 * @deprecated use {@link SslContextBuilder}
142 */
143 @Deprecated
144 public JdkSslServerContext(
145 File certChainFile, File keyFile, String keyPassword,
146 Iterable<String> ciphers, Iterable<String> nextProtocols,
147 long sessionCacheSize, long sessionTimeout) throws SSLException {
148 this(null, certChainFile, keyFile, keyPassword, ciphers, IdentityCipherSuiteFilter.INSTANCE,
149 toNegotiator(toApplicationProtocolConfig(nextProtocols), true), sessionCacheSize,
150 sessionTimeout, KeyStore.getDefaultType());
151 }
152
153 /**
154 * Creates a new instance.
155 *
156 * @param certChainFile an X.509 certificate chain file in PEM format
157 * @param keyFile a PKCS#8 private key file in PEM format
158 * @param keyPassword the password of the {@code keyFile}.
159 * {@code null} if it's not password-protected.
160 * @param ciphers the cipher suites to enable, in the order of preference.
161 * {@code null} to use the default cipher suites.
162 * @param cipherFilter a filter to apply over the supplied list of ciphers
163 * @param apn Provides a means to configure parameters related to application protocol negotiation.
164 * @param sessionCacheSize the size of the cache used for storing SSL session objects.
165 * {@code 0} to use the default value.
166 * @param sessionTimeout the timeout for the cached SSL session objects, in seconds.
167 * {@code 0} to use the default value.
168 * @deprecated use {@link SslContextBuilder}
169 */
170 @Deprecated
171 public JdkSslServerContext(
172 File certChainFile, File keyFile, String keyPassword,
173 Iterable<String> ciphers, CipherSuiteFilter cipherFilter, ApplicationProtocolConfig apn,
174 long sessionCacheSize, long sessionTimeout) throws SSLException {
175 this(null, certChainFile, keyFile, keyPassword, ciphers, cipherFilter,
176 toNegotiator(apn, true), sessionCacheSize, sessionTimeout, KeyStore.getDefaultType());
177 }
178
179 /**
180 * Creates a new instance.
181 *
182 * @param certChainFile an X.509 certificate chain file in PEM format
183 * @param keyFile a PKCS#8 private key file in PEM format
184 * @param keyPassword the password of the {@code keyFile}.
185 * {@code null} if it's not password-protected.
186 * @param ciphers the cipher suites to enable, in the order of preference.
187 * {@code null} to use the default cipher suites.
188 * @param cipherFilter a filter to apply over the supplied list of ciphers
189 * @param apn Application Protocol Negotiator object.
190 * @param sessionCacheSize the size of the cache used for storing SSL session objects.
191 * {@code 0} to use the default value.
192 * @param sessionTimeout the timeout for the cached SSL session objects, in seconds.
193 * {@code 0} to use the default value.
194 * @deprecated use {@link SslContextBuilder}
195 */
196 @Deprecated
197 public JdkSslServerContext(
198 File certChainFile, File keyFile, String keyPassword,
199 Iterable<String> ciphers, CipherSuiteFilter cipherFilter, JdkApplicationProtocolNegotiator apn,
200 long sessionCacheSize, long sessionTimeout) throws SSLException {
201 this(null, certChainFile, keyFile, keyPassword, ciphers, cipherFilter, apn,
202 sessionCacheSize, sessionTimeout, KeyStore.getDefaultType());
203 }
204
205 JdkSslServerContext(Provider provider,
206 File certChainFile, File keyFile, String keyPassword,
207 Iterable<String> ciphers, CipherSuiteFilter cipherFilter, JdkApplicationProtocolNegotiator apn,
208 long sessionCacheSize, long sessionTimeout, String keyStore) throws SSLException {
209 super(newSSLContext(provider, null, null,
210 toX509CertificatesInternal(certChainFile), toPrivateKeyInternal(keyFile, keyPassword),
211 keyPassword, null, sessionCacheSize, sessionTimeout, null, keyStore, null), false,
212 ciphers, cipherFilter, apn, ClientAuth.NONE, null, false);
213 }
214
215 /**
216 * Creates a new instance.
217 * @param trustCertCollectionFile an X.509 certificate collection file in PEM format.
218 * This provides the certificate collection used for mutual authentication.
219 * {@code null} to use the system default
220 * @param trustManagerFactory the {@link TrustManagerFactory} that provides the {@link TrustManager}s
221 * that verifies the certificates sent from clients.
222 * {@code null} to use the default or the results of parsing
223 * {@code trustCertCollectionFile}.
224 * @param keyCertChainFile an X.509 certificate chain file in PEM format
225 * @param keyFile a PKCS#8 private key file in PEM format
226 * @param keyPassword the password of the {@code keyFile}.
227 * {@code null} if it's not password-protected.
228 * @param keyManagerFactory the {@link KeyManagerFactory} that provides the {@link KeyManager}s
229 * that is used to encrypt data being sent to clients.
230 * {@code null} to use the default or the results of parsing
231 * {@code keyCertChainFile} and {@code keyFile}.
232 * @param ciphers the cipher suites to enable, in the order of preference.
233 * {@code null} to use the default cipher suites.
234 * @param cipherFilter a filter to apply over the supplied list of ciphers
235 * Only required if {@code provider} is {@link SslProvider#JDK}
236 * @param apn Provides a means to configure parameters related to application protocol negotiation.
237 * @param sessionCacheSize the size of the cache used for storing SSL session objects.
238 * {@code 0} to use the default value.
239 * @param sessionTimeout the timeout for the cached SSL session objects, in seconds.
240 * {@code 0} to use the default value.
241 * @deprecated use {@link SslContextBuilder}
242 */
243 @Deprecated
244 public JdkSslServerContext(File trustCertCollectionFile, TrustManagerFactory trustManagerFactory,
245 File keyCertChainFile, File keyFile, String keyPassword,
246 KeyManagerFactory keyManagerFactory,
247 Iterable<String> ciphers, CipherSuiteFilter cipherFilter, ApplicationProtocolConfig apn,
248 long sessionCacheSize, long sessionTimeout) throws SSLException {
249 super(newSSLContext(null, toX509CertificatesInternal(trustCertCollectionFile), trustManagerFactory,
250 toX509CertificatesInternal(keyCertChainFile), toPrivateKeyInternal(keyFile, keyPassword),
251 keyPassword, keyManagerFactory, sessionCacheSize, sessionTimeout, null, null, null), false,
252 ciphers, cipherFilter, apn, ClientAuth.NONE, null, false);
253 }
254
255 /**
256 * Creates a new instance.
257 * @param trustCertCollectionFile an X.509 certificate collection file in PEM format.
258 * This provides the certificate collection used for mutual authentication.
259 * {@code null} to use the system default
260 * @param trustManagerFactory the {@link TrustManagerFactory} that provides the {@link TrustManager}s
261 * that verifies the certificates sent from clients.
262 * {@code null} to use the default or the results of parsing
263 * {@code trustCertCollectionFile}
264 * @param keyCertChainFile an X.509 certificate chain file in PEM format
265 * @param keyFile a PKCS#8 private key file in PEM format
266 * @param keyPassword the password of the {@code keyFile}.
267 * {@code null} if it's not password-protected.
268 * @param keyManagerFactory the {@link KeyManagerFactory} that provides the {@link KeyManager}s
269 * that is used to encrypt data being sent to clients.
270 * {@code null} to use the default or the results of parsing
271 * {@code keyCertChainFile} and {@code keyFile}.
272 * @param ciphers the cipher suites to enable, in the order of preference.
273 * {@code null} to use the default cipher suites.
274 * @param cipherFilter a filter to apply over the supplied list of ciphers
275 * Only required if {@code provider} is {@link SslProvider#JDK}
276 * @param apn Application Protocol Negotiator object.
277 * @param sessionCacheSize the size of the cache used for storing SSL session objects.
278 * {@code 0} to use the default value.
279 * @param sessionTimeout the timeout for the cached SSL session objects, in seconds.
280 * {@code 0} to use the default value
281 * @deprecated use {@link SslContextBuilder}
282 */
283 @Deprecated
284 public JdkSslServerContext(File trustCertCollectionFile, TrustManagerFactory trustManagerFactory,
285 File keyCertChainFile, File keyFile, String keyPassword,
286 KeyManagerFactory keyManagerFactory,
287 Iterable<String> ciphers, CipherSuiteFilter cipherFilter,
288 JdkApplicationProtocolNegotiator apn,
289 long sessionCacheSize, long sessionTimeout) throws SSLException {
290 super(newSSLContext(null, toX509CertificatesInternal(trustCertCollectionFile), trustManagerFactory,
291 toX509CertificatesInternal(keyCertChainFile), toPrivateKeyInternal(keyFile, keyPassword),
292 keyPassword, keyManagerFactory, sessionCacheSize, sessionTimeout,
293 null, KeyStore.getDefaultType(), null), false,
294 ciphers, cipherFilter, apn, ClientAuth.NONE, null, false);
295 }
296
297 JdkSslServerContext(Provider provider,
298 X509Certificate[] trustCertCollection, TrustManagerFactory trustManagerFactory,
299 X509Certificate[] keyCertChain, PrivateKey key, String keyPassword,
300 KeyManagerFactory keyManagerFactory, Iterable<String> ciphers, CipherSuiteFilter cipherFilter,
301 ApplicationProtocolConfig apn, long sessionCacheSize, long sessionTimeout,
302 ClientAuth clientAuth, String[] protocols, boolean startTls,
303 SecureRandom secureRandom, String keyStore, ResumptionController resumptionController)
304 throws SSLException {
305 super(newSSLContext(provider, trustCertCollection, trustManagerFactory, keyCertChain, key,
306 keyPassword, keyManagerFactory, sessionCacheSize, sessionTimeout, secureRandom, keyStore,
307 resumptionController),
308 false, ciphers, cipherFilter, toNegotiator(apn, true), clientAuth, protocols, startTls, null,
309 resumptionController);
310 }
311
312 private static SSLContext newSSLContext(Provider sslContextProvider, X509Certificate[] trustCertCollection,
313 TrustManagerFactory trustManagerFactory, X509Certificate[] keyCertChain,
314 PrivateKey key, String keyPassword, KeyManagerFactory keyManagerFactory,
315 long sessionCacheSize, long sessionTimeout, SecureRandom secureRandom,
316 String keyStore, ResumptionController resumptionController)
317 throws SSLException {
318 if (key == null && keyManagerFactory == null) {
319 throw new NullPointerException("key, keyManagerFactory");
320 }
321
322 try {
323 if (trustCertCollection != null) {
324 trustManagerFactory = buildTrustManagerFactory(trustCertCollection, trustManagerFactory, keyStore);
325 } else if (trustManagerFactory == null) {
326 // Mimic the way SSLContext.getInstance(KeyManager[], null, null) works
327 trustManagerFactory = TrustManagerFactory.getInstance(
328 TrustManagerFactory.getDefaultAlgorithm());
329 trustManagerFactory.init((KeyStore) null);
330 }
331
332 if (key != null) {
333 keyManagerFactory = buildKeyManagerFactory(keyCertChain, null,
334 key, keyPassword, keyManagerFactory, null);
335 }
336
337 // Initialize the SSLContext to work with our key managers.
338 SSLContext ctx = sslContextProvider == null ? SSLContext.getInstance(PROTOCOL)
339 : SSLContext.getInstance(PROTOCOL, sslContextProvider);
340 ctx.init(keyManagerFactory.getKeyManagers(),
341 wrapTrustManagerIfNeeded(trustManagerFactory.getTrustManagers(), resumptionController),
342 secureRandom);
343
344 SSLSessionContext sessCtx = ctx.getServerSessionContext();
345 if (sessionCacheSize > 0) {
346 sessCtx.setSessionCacheSize((int) Math.min(sessionCacheSize, Integer.MAX_VALUE));
347 }
348 if (sessionTimeout > 0) {
349 sessCtx.setSessionTimeout((int) Math.min(sessionTimeout, Integer.MAX_VALUE));
350 }
351 return ctx;
352 } catch (Exception e) {
353 if (e instanceof SSLException) {
354 throw (SSLException) e;
355 }
356 throw new SSLException("failed to initialize the server-side SSL context", e);
357 }
358 }
359
360 @SuppressJava6Requirement(reason = "Guarded by java version check")
361 private static TrustManager[] wrapTrustManagerIfNeeded(
362 TrustManager[] trustManagers, ResumptionController resumptionController) {
363 if (WRAP_TRUST_MANAGER && PlatformDependent.javaVersion() >= 7) {
364 for (int i = 0; i < trustManagers.length; i++) {
365 TrustManager tm = trustManagers[i];
366 if (resumptionController != null) {
367 tm = resumptionController.wrapIfNeeded(tm);
368 }
369 if (tm instanceof X509ExtendedTrustManager) {
370 // Wrap the TrustManager to provide a better exception message for users to debug hostname
371 // validation failures.
372 trustManagers[i] = new EnhancingX509ExtendedTrustManager((X509ExtendedTrustManager) tm);
373 }
374 }
375 }
376 return trustManagers;
377 }
378 }