1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty5.handler.ssl;
17
18 import javax.net.ssl.KeyManagerFactory;
19 import javax.net.ssl.SSLContext;
20 import javax.net.ssl.SSLException;
21 import javax.net.ssl.SSLSessionContext;
22 import javax.net.ssl.TrustManagerFactory;
23 import java.io.File;
24 import java.security.KeyStore;
25 import java.security.PrivateKey;
26 import java.security.Provider;
27 import java.security.cert.X509Certificate;
28
29
30
31
32 final class JdkSslServerContext extends JdkSslContext {
33
34
35 JdkSslServerContext(Provider provider,
36 File certChainFile,
37 File keyFile,
38 String keyPassword,
39 Iterable<String> ciphers,
40 CipherSuiteFilter cipherFilter,
41 JdkApplicationProtocolNegotiator apn,
42 long sessionCacheSize,
43 long sessionTimeout)
44 throws Exception {
45 super(newSSLContext(provider, null, null,
46 toX509CertificatesInternal(certChainFile), toPrivateKeyInternal(keyFile, keyPassword),
47 keyPassword, null, sessionCacheSize, sessionTimeout, KeyStore.getDefaultType()), false,
48 ciphers, cipherFilter, apn, ClientAuth.NONE, null, false);
49 }
50
51 JdkSslServerContext(Provider provider,
52 X509Certificate[] trustCertCollection,
53 TrustManagerFactory trustManagerFactory,
54 X509Certificate[] keyCertChain,
55 PrivateKey key,
56 String keyPassword,
57 KeyManagerFactory keyManagerFactory,
58 Iterable<String> ciphers,
59 CipherSuiteFilter cipherFilter,
60 ApplicationProtocolConfig apn,
61 long sessionCacheSize,
62 long sessionTimeout,
63 ClientAuth clientAuth,
64 String[] protocols,
65 boolean startTls,
66 String keyStore)
67 throws Exception {
68 super(newSSLContext(provider, trustCertCollection, trustManagerFactory, keyCertChain, key,
69 keyPassword, keyManagerFactory, sessionCacheSize, sessionTimeout, keyStore), false,
70 ciphers, cipherFilter, toNegotiator(apn, true), clientAuth, protocols, startTls);
71 }
72
73 private static SSLContext newSSLContext(Provider sslContextProvider, X509Certificate[] trustCertCollection,
74 TrustManagerFactory trustManagerFactory, X509Certificate[] keyCertChain,
75 PrivateKey key, String keyPassword, KeyManagerFactory keyManagerFactory,
76 long sessionCacheSize, long sessionTimeout, String keyStore)
77 throws SSLException {
78 if (key == null && keyManagerFactory == null) {
79 throw new NullPointerException("key, keyManagerFactory");
80 }
81
82 try {
83 if (trustCertCollection != null) {
84 trustManagerFactory = buildTrustManagerFactory(trustCertCollection, trustManagerFactory, keyStore);
85 }
86 if (key != null) {
87 keyManagerFactory = buildKeyManagerFactory(keyCertChain, null,
88 key, keyPassword, keyManagerFactory, null);
89 }
90
91
92 SSLContext ctx = sslContextProvider == null ? SSLContext.getInstance(PROTOCOL)
93 : SSLContext.getInstance(PROTOCOL, sslContextProvider);
94 ctx.init(keyManagerFactory.getKeyManagers(),
95 trustManagerFactory == null ? null : trustManagerFactory.getTrustManagers(),
96 null);
97
98 SSLSessionContext sessCtx = ctx.getServerSessionContext();
99 if (sessionCacheSize > 0) {
100 sessCtx.setSessionCacheSize((int) Math.min(sessionCacheSize, Integer.MAX_VALUE));
101 }
102 if (sessionTimeout > 0) {
103 sessCtx.setSessionTimeout((int) Math.min(sessionTimeout, Integer.MAX_VALUE));
104 }
105 return ctx;
106 } catch (Exception e) {
107 if (e instanceof SSLException) {
108 throw (SSLException) e;
109 }
110 throw new SSLException("failed to initialize the server-side SSL context", e);
111 }
112 }
113 }