1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.handler.ssl;
17
18 import io.netty.buffer.ByteBufAllocator;
19 import io.netty.util.internal.PlatformDependent;
20
21 import javax.net.ssl.SSLEngine;
22
23
24
25
26
27
28 @Deprecated
29 public final class JdkAlpnApplicationProtocolNegotiator extends JdkBaseApplicationProtocolNegotiator {
30 private static final boolean AVAILABLE = Conscrypt.isAvailable() ||
31 jdkAlpnSupported() ||
32 JettyAlpnSslEngine.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, ByteBufAllocator 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 + "http://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, ByteBufAllocator 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 (jdkAlpnSupported()) {
138 return new Java9SslEngine(engine, applicationNegotiator, isServer);
139 }
140 if (JettyAlpnSslEngine.isAvailable()) {
141 return isServer ? JettyAlpnSslEngine.newServerEngine(engine, applicationNegotiator)
142 : JettyAlpnSslEngine.newClientEngine(engine, applicationNegotiator);
143 }
144 throw new RuntimeException("Unable to wrap SSLEngine of type " + engine.getClass().getName());
145 }
146 }
147
148 static boolean jdkAlpnSupported() {
149 return PlatformDependent.javaVersion() >= 9 && Java9SslUtils.supportsAlpn();
150 }
151 }