View Javadoc
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  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   * A server-side {@link SslContext} which uses JDK's SSL/TLS implementation.
31   */
32  final class JdkSslServerContext extends JdkSslContext {
33  
34      // FIXME test only
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              // Initialize the SSLContext to work with our key managers.
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 }