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.handler.ssl.util.BouncyCastleUtil;
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 JdkAlpnSslUtils.supportsAlpn() ||
32 (BouncyCastleUtil.isBcTlsAvailable() && BouncyCastleAlpnSslUtils.isAlpnSupported());
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. Does your JDK version support it?"
123 + " For Conscrypt, add the appropriate Conscrypt JAR to classpath and set the security provider.");
124 }
125 }
126
127 private static final class AlpnWrapper extends AllocatorAwareSslEngineWrapperFactory {
128 @Override
129 public SSLEngine wrapSslEngine(SSLEngine engine, ByteBufAllocator alloc,
130 JdkApplicationProtocolNegotiator applicationNegotiator, boolean isServer) {
131 if (Conscrypt.isEngineSupported(engine)) {
132 return isServer ? ConscryptAlpnSslEngine.newServerEngine(engine, alloc, applicationNegotiator)
133 : ConscryptAlpnSslEngine.newClientEngine(engine, alloc, applicationNegotiator);
134 }
135 if (BouncyCastleUtil.isBcJsseInUse(engine) && BouncyCastleAlpnSslUtils.isAlpnSupported()) {
136 return new BouncyCastleAlpnSslEngine(engine, applicationNegotiator, isServer);
137 }
138
139
140
141
142 if (JdkAlpnSslUtils.supportsAlpn()) {
143 return new JdkAlpnSslEngine(engine, applicationNegotiator, isServer);
144 }
145 throw new UnsupportedOperationException("ALPN not supported. Unable to wrap SSLEngine of type '"
146 + engine.getClass().getName() + "')");
147 }
148 }
149
150 static boolean isAlpnSupported() {
151 return AVAILABLE;
152 }
153 }