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 io.netty.internal.tcnative.SSL;
19  
20  import javax.net.ssl.KeyManagerFactory;
21  import javax.net.ssl.SSLException;
22  import javax.net.ssl.TrustManagerFactory;
23  import java.security.PrivateKey;
24  import java.security.cert.X509Certificate;
25  import java.util.Map;
26  
27  import static io.netty5.handler.ssl.ReferenceCountedOpenSslServerContext.newSessionContext;
28  
29  /**
30   * A server-side {@link SslContext} which uses OpenSSL's SSL/TLS implementation.
31   * <p>This class will use a finalizer to ensure native resources are automatically cleaned up. To avoid finalizers
32   * and manually release the native memory see {@link ReferenceCountedOpenSslServerContext}.
33   */
34  final class OpenSslServerContext extends OpenSslContext {
35      private final OpenSslServerSessionContext sessionContext;
36  
37      OpenSslServerContext(
38              X509Certificate[] trustCertCollection, TrustManagerFactory trustManagerFactory,
39              X509Certificate[] keyCertChain, PrivateKey key, String keyPassword, KeyManagerFactory keyManagerFactory,
40              Iterable<String> ciphers, CipherSuiteFilter cipherFilter, ApplicationProtocolConfig apn,
41              long sessionCacheSize, long sessionTimeout, ClientAuth clientAuth, String[] protocols, boolean startTls,
42              boolean enableOcsp, String keyStore, Map.Entry<SslContextOption<?>, Object>... options)
43              throws SSLException {
44          this(trustCertCollection, trustManagerFactory, keyCertChain, key, keyPassword, keyManagerFactory, ciphers,
45                  cipherFilter, toNegotiator(apn), sessionCacheSize, sessionTimeout, clientAuth, protocols, startTls,
46                  enableOcsp, keyStore, options);
47      }
48  
49      @SuppressWarnings("deprecation")
50      private OpenSslServerContext(
51              X509Certificate[] trustCertCollection, TrustManagerFactory trustManagerFactory,
52              X509Certificate[] keyCertChain, PrivateKey key, String keyPassword, KeyManagerFactory keyManagerFactory,
53              Iterable<String> ciphers, CipherSuiteFilter cipherFilter, OpenSslApplicationProtocolNegotiator apn,
54              long sessionCacheSize, long sessionTimeout, ClientAuth clientAuth, String[] protocols, boolean startTls,
55              boolean enableOcsp, String keyStore, Map.Entry<SslContextOption<?>, Object>... options)
56              throws SSLException {
57          super(ciphers, cipherFilter, apn, SSL.SSL_MODE_SERVER, keyCertChain,
58                  clientAuth, protocols, startTls, enableOcsp, options);
59          // Create a new SSL_CTX and configure it.
60          boolean success = false;
61          try {
62              OpenSslKeyMaterialProvider.validateKeyMaterialSupported(keyCertChain, key, keyPassword);
63              sessionContext = newSessionContext(this, ctx, engineMap, trustCertCollection, trustManagerFactory,
64                                                 keyCertChain, key, keyPassword, keyManagerFactory, keyStore,
65                                                 sessionCacheSize, sessionTimeout);
66              success = true;
67          } finally {
68              if (!success) {
69                  release();
70              }
71          }
72      }
73  
74      @Override
75      public OpenSslServerSessionContext sessionContext() {
76          return sessionContext;
77      }
78  }