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.util.internal.EmptyArrays;
19 import io.netty.util.internal.PlatformDependent;
20 import io.netty.util.internal.logging.InternalLogger;
21 import io.netty.util.internal.logging.InternalLoggerFactory;
22
23 import java.lang.reflect.Method;
24 import java.security.AccessController;
25 import java.security.PrivilegedExceptionAction;
26 import java.util.List;
27 import java.util.function.BiFunction;
28 import javax.net.ssl.SSLContext;
29 import javax.net.ssl.SSLEngine;
30 import javax.net.ssl.SSLParameters;
31
32 final class JdkAlpnSslUtils {
33 private static final InternalLogger logger = InternalLoggerFactory.getInstance(JdkAlpnSslUtils.class);
34 private static final Method SET_APPLICATION_PROTOCOLS;
35 private static final Method GET_APPLICATION_PROTOCOL;
36 private static final Method GET_HANDSHAKE_APPLICATION_PROTOCOL;
37 private static final Method SET_HANDSHAKE_APPLICATION_PROTOCOL_SELECTOR;
38 private static final Method GET_HANDSHAKE_APPLICATION_PROTOCOL_SELECTOR;
39
40 static {
41 Method getHandshakeApplicationProtocol;
42 Method getApplicationProtocol;
43 Method setApplicationProtocols;
44 Method setHandshakeApplicationProtocolSelector;
45 Method getHandshakeApplicationProtocolSelector;
46
47 try {
48 SSLContext context = SSLContext.getInstance(JdkSslContext.PROTOCOL);
49 context.init(null, null, null);
50 SSLEngine engine = context.createSSLEngine();
51 getHandshakeApplicationProtocol = AccessController.doPrivileged(new PrivilegedExceptionAction<Method>() {
52 @Override
53 public Method run() throws Exception {
54 return SSLEngine.class.getMethod("getHandshakeApplicationProtocol");
55 }
56 });
57 getHandshakeApplicationProtocol.invoke(engine);
58 getApplicationProtocol = AccessController.doPrivileged(new PrivilegedExceptionAction<Method>() {
59 @Override
60 public Method run() throws Exception {
61 return SSLEngine.class.getMethod("getApplicationProtocol");
62 }
63 });
64 getApplicationProtocol.invoke(engine);
65
66 setApplicationProtocols = AccessController.doPrivileged(new PrivilegedExceptionAction<Method>() {
67 @Override
68 public Method run() throws Exception {
69 return SSLParameters.class.getMethod("setApplicationProtocols", String[].class);
70 }
71 });
72 setApplicationProtocols.invoke(engine.getSSLParameters(), new Object[]{EmptyArrays.EMPTY_STRINGS});
73
74 setHandshakeApplicationProtocolSelector =
75 AccessController.doPrivileged(new PrivilegedExceptionAction<Method>() {
76 @Override
77 public Method run() throws Exception {
78 return SSLEngine.class.getMethod("setHandshakeApplicationProtocolSelector", BiFunction.class);
79 }
80 });
81 setHandshakeApplicationProtocolSelector.invoke(engine, new BiFunction<SSLEngine, List<String>, String>() {
82 @Override
83 public String apply(SSLEngine sslEngine, List<String> strings) {
84 return null;
85 }
86 });
87
88 getHandshakeApplicationProtocolSelector =
89 AccessController.doPrivileged(new PrivilegedExceptionAction<Method>() {
90 @Override
91 public Method run() throws Exception {
92 return SSLEngine.class.getMethod("getHandshakeApplicationProtocolSelector");
93 }
94 });
95 getHandshakeApplicationProtocolSelector.invoke(engine);
96 } catch (Throwable t) {
97 int version = PlatformDependent.javaVersion();
98 if (version >= 9) {
99
100 logger.error("Unable to initialize JdkAlpnSslUtils, but the detected java version was: {}", version, t);
101 }
102 getHandshakeApplicationProtocol = null;
103 getApplicationProtocol = null;
104 setApplicationProtocols = null;
105 setHandshakeApplicationProtocolSelector = null;
106 getHandshakeApplicationProtocolSelector = null;
107 }
108 GET_HANDSHAKE_APPLICATION_PROTOCOL = getHandshakeApplicationProtocol;
109 GET_APPLICATION_PROTOCOL = getApplicationProtocol;
110 SET_APPLICATION_PROTOCOLS = setApplicationProtocols;
111 SET_HANDSHAKE_APPLICATION_PROTOCOL_SELECTOR = setHandshakeApplicationProtocolSelector;
112 GET_HANDSHAKE_APPLICATION_PROTOCOL_SELECTOR = getHandshakeApplicationProtocolSelector;
113 }
114
115 private JdkAlpnSslUtils() {
116 }
117
118 static boolean supportsAlpn() {
119 return GET_APPLICATION_PROTOCOL != null;
120 }
121
122 static String getApplicationProtocol(SSLEngine sslEngine) {
123 try {
124 return (String) GET_APPLICATION_PROTOCOL.invoke(sslEngine);
125 } catch (UnsupportedOperationException ex) {
126 throw ex;
127 } catch (Exception ex) {
128 throw new IllegalStateException(ex);
129 }
130 }
131
132 static String getHandshakeApplicationProtocol(SSLEngine sslEngine) {
133 try {
134 return (String) GET_HANDSHAKE_APPLICATION_PROTOCOL.invoke(sslEngine);
135 } catch (UnsupportedOperationException ex) {
136 throw ex;
137 } catch (Exception ex) {
138 throw new IllegalStateException(ex);
139 }
140 }
141
142 static void setApplicationProtocols(SSLEngine engine, List<String> supportedProtocols) {
143 SSLParameters parameters = engine.getSSLParameters();
144
145 String[] protocolArray = supportedProtocols.toArray(EmptyArrays.EMPTY_STRINGS);
146 try {
147 SET_APPLICATION_PROTOCOLS.invoke(parameters, new Object[]{protocolArray});
148 } catch (UnsupportedOperationException ex) {
149 throw ex;
150 } catch (Exception ex) {
151 throw new IllegalStateException(ex);
152 }
153 engine.setSSLParameters(parameters);
154 }
155
156 static void setHandshakeApplicationProtocolSelector(
157 SSLEngine engine, BiFunction<SSLEngine, List<String>, String> selector) {
158 try {
159 SET_HANDSHAKE_APPLICATION_PROTOCOL_SELECTOR.invoke(engine, selector);
160 } catch (UnsupportedOperationException ex) {
161 throw ex;
162 } catch (Exception ex) {
163 throw new IllegalStateException(ex);
164 }
165 }
166
167 @SuppressWarnings("unchecked")
168 static BiFunction<SSLEngine, List<String>, String> getHandshakeApplicationProtocolSelector(SSLEngine engine) {
169 try {
170 return (BiFunction<SSLEngine, List<String>, String>)
171 GET_HANDSHAKE_APPLICATION_PROTOCOL_SELECTOR.invoke(engine);
172 } catch (UnsupportedOperationException ex) {
173 throw ex;
174 } catch (Exception ex) {
175 throw new IllegalStateException(ex);
176 }
177 }
178 }