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
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 BouncyCastle.isAvailable();
32
33 private static final SslEngineWrapperFactory ALPN_WRAPPER = AVAILABLE ? new AlpnWrapper() : new FailureWrapper();
34
35
36
37
38
39 public JdkAlpnApplicationProtocolNegotiator(Iterable<String> protocols) {
40 this(false, protocols);
41 }
42
43
44
45
46
47 public JdkAlpnApplicationProtocolNegotiator(String... protocols) {
48 this(false, protocols);
49 }
50
51
52
53
54
55
56 public JdkAlpnApplicationProtocolNegotiator(boolean failIfNoCommonProtocols, Iterable<String> protocols) {
57 this(failIfNoCommonProtocols, failIfNoCommonProtocols, protocols);
58 }
59
60
61
62
63
64
65 public JdkAlpnApplicationProtocolNegotiator(boolean failIfNoCommonProtocols, String... protocols) {
66 this(failIfNoCommonProtocols, failIfNoCommonProtocols, protocols);
67 }
68
69
70
71
72
73
74
75 public JdkAlpnApplicationProtocolNegotiator(boolean clientFailIfNoCommonProtocols,
76 boolean serverFailIfNoCommonProtocols, Iterable<String> protocols) {
77 this(serverFailIfNoCommonProtocols ? FAIL_SELECTOR_FACTORY : NO_FAIL_SELECTOR_FACTORY,
78 clientFailIfNoCommonProtocols ? FAIL_SELECTION_LISTENER_FACTORY : NO_FAIL_SELECTION_LISTENER_FACTORY,
79 protocols);
80 }
81
82
83
84
85
86
87
88 public JdkAlpnApplicationProtocolNegotiator(boolean clientFailIfNoCommonProtocols,
89 boolean serverFailIfNoCommonProtocols, String... protocols) {
90 this(serverFailIfNoCommonProtocols ? FAIL_SELECTOR_FACTORY : NO_FAIL_SELECTOR_FACTORY,
91 clientFailIfNoCommonProtocols ? FAIL_SELECTION_LISTENER_FACTORY : NO_FAIL_SELECTION_LISTENER_FACTORY,
92 protocols);
93 }
94
95
96
97
98
99
100
101 public JdkAlpnApplicationProtocolNegotiator(ProtocolSelectorFactory selectorFactory,
102 ProtocolSelectionListenerFactory listenerFactory, Iterable<String> protocols) {
103 super(ALPN_WRAPPER, selectorFactory, listenerFactory, protocols);
104 }
105
106
107
108
109
110
111
112 public JdkAlpnApplicationProtocolNegotiator(ProtocolSelectorFactory selectorFactory,
113 ProtocolSelectionListenerFactory listenerFactory, String... protocols) {
114 super(ALPN_WRAPPER, selectorFactory, listenerFactory, protocols);
115 }
116
117 private static final class FailureWrapper extends AllocatorAwareSslEngineWrapperFactory {
118 @Override
119 public SSLEngine wrapSslEngine(SSLEngine engine, ByteBufAllocator alloc,
120 JdkApplicationProtocolNegotiator applicationNegotiator, boolean isServer) {
121 throw new RuntimeException("ALPN unsupported. Does your JDK version support it?"
122 + " For Conscrypt, add the appropriate Conscrypt JAR to classpath and set the security provider.");
123 }
124 }
125
126 private static final class AlpnWrapper extends AllocatorAwareSslEngineWrapperFactory {
127 @Override
128 public SSLEngine wrapSslEngine(SSLEngine engine, ByteBufAllocator alloc,
129 JdkApplicationProtocolNegotiator applicationNegotiator, boolean isServer) {
130 if (Conscrypt.isEngineSupported(engine)) {
131 return isServer ? ConscryptAlpnSslEngine.newServerEngine(engine, alloc, applicationNegotiator)
132 : ConscryptAlpnSslEngine.newClientEngine(engine, alloc, applicationNegotiator);
133 }
134 if (BouncyCastle.isInUse(engine)) {
135 return new BouncyCastleAlpnSslEngine(engine, applicationNegotiator, isServer);
136 }
137
138
139
140
141 if (JdkAlpnSslUtils.supportsAlpn()) {
142 return new JdkAlpnSslEngine(engine, applicationNegotiator, isServer);
143 }
144 throw new UnsupportedOperationException("ALPN not supported. Unable to wrap SSLEngine of type '"
145 + engine.getClass().getName() + "')");
146 }
147 }
148
149 static boolean isAlpnSupported() {
150 return AVAILABLE;
151 }
152 }