1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty5.handler.ssl;
17
18 import io.netty5.buffer.api.BufferAllocator;
19
20 import javax.net.ssl.SSLEngine;
21
22
23
24
25
26
27 @Deprecated
28 public final class JdkAlpnApplicationProtocolNegotiator extends JdkBaseApplicationProtocolNegotiator {
29 private static final boolean AVAILABLE = Conscrypt.isAvailable() ||
30 JdkAlpnSslUtils.supportsAlpn() ||
31 JettyAlpnSslEngine.isAvailable() ||
32 BouncyCastle.isAvailable();
33
34 private static final SslEngineWrapperFactory ALPN_WRAPPER = AVAILABLE ? new AlpnWrapper() : new FailureWrapper();
35
36
37
38
39
40 public JdkAlpnApplicationProtocolNegotiator(Iterable<String> protocols) {
41 this(false, protocols);
42 }
43
44
45
46
47
48 public JdkAlpnApplicationProtocolNegotiator(String... protocols) {
49 this(false, protocols);
50 }
51
52
53
54
55
56
57 public JdkAlpnApplicationProtocolNegotiator(boolean failIfNoCommonProtocols, Iterable<String> protocols) {
58 this(failIfNoCommonProtocols, failIfNoCommonProtocols, protocols);
59 }
60
61
62
63
64
65
66 public JdkAlpnApplicationProtocolNegotiator(boolean failIfNoCommonProtocols, String... protocols) {
67 this(failIfNoCommonProtocols, failIfNoCommonProtocols, protocols);
68 }
69
70
71
72
73
74
75
76 public JdkAlpnApplicationProtocolNegotiator(boolean clientFailIfNoCommonProtocols,
77 boolean serverFailIfNoCommonProtocols, Iterable<String> protocols) {
78 this(serverFailIfNoCommonProtocols ? FAIL_SELECTOR_FACTORY : NO_FAIL_SELECTOR_FACTORY,
79 clientFailIfNoCommonProtocols ? FAIL_SELECTION_LISTENER_FACTORY : NO_FAIL_SELECTION_LISTENER_FACTORY,
80 protocols);
81 }
82
83
84
85
86
87
88
89 public JdkAlpnApplicationProtocolNegotiator(boolean clientFailIfNoCommonProtocols,
90 boolean serverFailIfNoCommonProtocols, String... protocols) {
91 this(serverFailIfNoCommonProtocols ? FAIL_SELECTOR_FACTORY : NO_FAIL_SELECTOR_FACTORY,
92 clientFailIfNoCommonProtocols ? FAIL_SELECTION_LISTENER_FACTORY : NO_FAIL_SELECTION_LISTENER_FACTORY,
93 protocols);
94 }
95
96
97
98
99
100
101
102 public JdkAlpnApplicationProtocolNegotiator(ProtocolSelectorFactory selectorFactory,
103 ProtocolSelectionListenerFactory listenerFactory, Iterable<String> protocols) {
104 super(ALPN_WRAPPER, selectorFactory, listenerFactory, protocols);
105 }
106
107
108
109
110
111
112
113 public JdkAlpnApplicationProtocolNegotiator(ProtocolSelectorFactory selectorFactory,
114 ProtocolSelectionListenerFactory listenerFactory, String... protocols) {
115 super(ALPN_WRAPPER, selectorFactory, listenerFactory, protocols);
116 }
117
118 private static final class FailureWrapper extends AllocatorAwareSslEngineWrapperFactory {
119 @Override
120 public SSLEngine wrapSslEngine(SSLEngine engine, BufferAllocator alloc,
121 JdkApplicationProtocolNegotiator applicationNegotiator, boolean isServer) {
122 throw new RuntimeException("ALPN unsupported. Is your classpath configured correctly?"
123 + " For Conscrypt, add the appropriate Conscrypt JAR to classpath and set the security provider."
124 + " For Jetty-ALPN, see "
125 + "https://www.eclipse.org/jetty/documentation/current/alpn-chapter.html#alpn-starting");
126 }
127 }
128
129 private static final class AlpnWrapper extends AllocatorAwareSslEngineWrapperFactory {
130 @Override
131 public SSLEngine wrapSslEngine(SSLEngine engine, BufferAllocator alloc,
132 JdkApplicationProtocolNegotiator applicationNegotiator, boolean isServer) {
133 if (Conscrypt.isEngineSupported(engine)) {
134 return isServer ? ConscryptAlpnSslEngine.newServerEngine(engine, alloc, applicationNegotiator)
135 : ConscryptAlpnSslEngine.newClientEngine(engine, alloc, applicationNegotiator);
136 }
137 if (BouncyCastle.isInUse(engine)) {
138 return new BouncyCastleAlpnSslEngine(engine, applicationNegotiator, isServer);
139 }
140
141
142
143
144 if (JdkAlpnSslUtils.supportsAlpn()) {
145 return new JdkAlpnSslEngine(engine, applicationNegotiator, isServer);
146 }
147 if (JettyAlpnSslEngine.isAvailable()) {
148 return isServer ? JettyAlpnSslEngine.newServerEngine(engine, applicationNegotiator)
149 : JettyAlpnSslEngine.newClientEngine(engine, applicationNegotiator);
150 }
151 throw new UnsupportedOperationException("ALPN not supported. Unable to wrap SSLEngine of type '"
152 + engine.getClass().getName() + "')");
153 }
154 }
155
156 static boolean isAlpnSupported() {
157 return AVAILABLE;
158 }
159 }