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.netty5.handler.ssl.JdkApplicationProtocolNegotiator.ProtocolSelectionListener;
19  import io.netty5.handler.ssl.JdkApplicationProtocolNegotiator.ProtocolSelector;
20  import org.eclipse.jetty.alpn.ALPN;
21  
22  import javax.net.ssl.SSLEngine;
23  import javax.net.ssl.SSLException;
24  import java.util.LinkedHashSet;
25  import java.util.List;
26  
27  import static io.netty5.handler.ssl.SslUtils.toSSLHandshakeException;
28  import static java.util.Objects.requireNonNull;
29  
30  @Deprecated(forRemoval = true)
31  abstract class JettyAlpnSslEngine extends JdkSslEngine {
32      private static final boolean available = initAvailable();
33  
34      static boolean isAvailable() {
35          return available;
36      }
37  
38      private static boolean initAvailable() {
39          return false;
40      }
41  
42      static JettyAlpnSslEngine newClientEngine(SSLEngine engine,
43              JdkApplicationProtocolNegotiator applicationNegotiator) {
44          return new ClientEngine(engine, applicationNegotiator);
45      }
46  
47      static JettyAlpnSslEngine newServerEngine(SSLEngine engine,
48              JdkApplicationProtocolNegotiator applicationNegotiator) {
49          return new ServerEngine(engine, applicationNegotiator);
50      }
51  
52      private JettyAlpnSslEngine(SSLEngine engine) {
53          super(engine);
54      }
55  
56      private static final class ClientEngine extends JettyAlpnSslEngine {
57          ClientEngine(SSLEngine engine, final JdkApplicationProtocolNegotiator applicationNegotiator) {
58              super(engine);
59              requireNonNull(applicationNegotiator, "applicationNegotiator");
60              final ProtocolSelectionListener protocolListener = requireNonNull(applicationNegotiator
61                              .protocolListenerFactory().newListener(this, applicationNegotiator.protocols()),
62                      "protocolListener");
63              ALPN.put(engine, new ALPN.ClientProvider() {
64                  @Override
65                  public List<String> protocols() {
66                      return applicationNegotiator.protocols();
67                  }
68  
69                  @Override
70                  public void selected(String protocol) throws SSLException {
71                      try {
72                          protocolListener.selected(protocol);
73                      } catch (Throwable t) {
74                          throw toSSLHandshakeException(t);
75                      }
76                  }
77  
78                  @Override
79                  public void unsupported() {
80                      protocolListener.unsupported();
81                  }
82              });
83          }
84  
85          @Override
86          public void closeInbound() throws SSLException {
87              try {
88                  ALPN.remove(getWrappedEngine());
89              } finally {
90                  super.closeInbound();
91              }
92          }
93  
94          @Override
95          public void closeOutbound() {
96              try {
97                  ALPN.remove(getWrappedEngine());
98              } finally {
99                  super.closeOutbound();
100             }
101         }
102     }
103 
104     private static final class ServerEngine extends JettyAlpnSslEngine {
105         ServerEngine(SSLEngine engine, final JdkApplicationProtocolNegotiator applicationNegotiator) {
106             super(engine);
107             requireNonNull(applicationNegotiator, "applicationNegotiator");
108             final ProtocolSelector protocolSelector = requireNonNull(applicationNegotiator.protocolSelectorFactory()
109                             .newSelector(this, new LinkedHashSet<>(applicationNegotiator.protocols())),
110                     "protocolSelector");
111             ALPN.put(engine, new ALPN.ServerProvider() {
112                 @Override
113                 public String select(List<String> protocols) throws SSLException {
114                     try {
115                         return protocolSelector.select(protocols);
116                     } catch (Throwable t) {
117                         throw toSSLHandshakeException(t);
118                     }
119                 }
120 
121                 @Override
122                 public void unsupported() {
123                     protocolSelector.unsupported();
124                 }
125             });
126         }
127 
128         @Override
129         public void closeInbound() throws SSLException {
130             try {
131                 ALPN.remove(getWrappedEngine());
132             } finally {
133                 super.closeInbound();
134             }
135         }
136 
137         @Override
138         public void closeOutbound() {
139             try {
140                 ALPN.remove(getWrappedEngine());
141             } finally {
142                 super.closeOutbound();
143             }
144         }
145     }
146 }