|
30 | 30 | import java.lang.reflect.Proxy; |
31 | 31 | import java.security.AccessController; |
32 | 32 | import java.security.PrivilegedExceptionAction; |
| 33 | +import java.security.SecureRandom; |
33 | 34 | import java.util.List; |
34 | 35 | import java.util.function.BiFunction; |
35 | 36 |
|
@@ -45,6 +46,7 @@ final class BouncyCastleAlpnSslUtils { |
45 | 46 | private static final Method GET_HANDSHAKE_APPLICATION_PROTOCOL_SELECTOR; |
46 | 47 | private static final Class<?> BC_APPLICATION_PROTOCOL_SELECTOR; |
47 | 48 | private static final Method BC_APPLICATION_PROTOCOL_SELECTOR_SELECT; |
| 49 | + private static final boolean SUPPORTED; |
48 | 50 |
|
49 | 51 | static { |
50 | 52 | Method setApplicationProtocols; |
@@ -54,18 +56,26 @@ final class BouncyCastleAlpnSslUtils { |
54 | 56 | Method getHandshakeApplicationProtocolSelector; |
55 | 57 | Method bcApplicationProtocolSelectorSelect; |
56 | 58 | Class<?> bcApplicationProtocolSelector; |
| 59 | + boolean supported; |
57 | 60 |
|
58 | 61 | try { |
59 | 62 | if (!BouncyCastleUtil.isBcTlsAvailable()) { |
60 | 63 | throw new IllegalStateException(BouncyCastleUtil.unavailabilityCauseBcTls()); |
61 | 64 | } |
62 | | - SSLContext context = getSSLContext(BouncyCastleUtil.getBcProviderJsse()); |
| 65 | + SSLContext context = getSSLContext(BouncyCastleUtil.getBcProviderJsse(), new SecureRandom()); |
63 | 66 | SSLEngine engine = context.createSSLEngine(); |
64 | | - final Class<? extends SSLEngine> engineClass = engine.getClass(); |
| 67 | + Class<? extends SSLEngine> engineClass = engine.getClass(); |
| 68 | + // We need to use the class returned by BounceCastleUtil below to access the methods as the engine |
| 69 | + // returned by createSSLEngine might be package-private and so would not allow us to access the methods |
| 70 | + // even thought the interface itself that it implements is public and so the methods are public. |
| 71 | + // See https://github.com/netty/netty/issues/15627 |
| 72 | + final Class<? extends SSLEngine> bcEngineClass = BouncyCastleUtil.getBcSSLEngineClass(); |
| 73 | + if (bcEngineClass == null || !bcEngineClass.isAssignableFrom(engineClass)) { |
| 74 | + throw new IllegalStateException("Unexpected engine class: " + engineClass); |
| 75 | + } |
65 | 76 |
|
66 | 77 | final SSLParameters bcSslParameters = engine.getSSLParameters(); |
67 | 78 | final Class<?> bCSslParametersClass = bcSslParameters.getClass(); |
68 | | - |
69 | 79 | setApplicationProtocols = AccessController.doPrivileged(new PrivilegedExceptionAction<Method>() { |
70 | 80 | @Override |
71 | 81 | public Method run() throws Exception { |
@@ -77,15 +87,15 @@ public Method run() throws Exception { |
77 | 87 | getApplicationProtocol = AccessController.doPrivileged(new PrivilegedExceptionAction<Method>() { |
78 | 88 | @Override |
79 | 89 | public Method run() throws Exception { |
80 | | - return engineClass.getMethod("getApplicationProtocol"); |
| 90 | + return bcEngineClass.getMethod("getApplicationProtocol"); |
81 | 91 | } |
82 | 92 | }); |
83 | 93 | getApplicationProtocol.invoke(engine); |
84 | 94 |
|
85 | 95 | getHandshakeApplicationProtocol = AccessController.doPrivileged(new PrivilegedExceptionAction<Method>() { |
86 | 96 | @Override |
87 | 97 | public Method run() throws Exception { |
88 | | - return engineClass.getMethod("getHandshakeApplicationProtocol"); |
| 98 | + return bcEngineClass.getMethod("getHandshakeApplicationProtocol"); |
89 | 99 | } |
90 | 100 | }); |
91 | 101 | getHandshakeApplicationProtocol.invoke(engine); |
@@ -106,7 +116,7 @@ public Method run() throws Exception { |
106 | 116 | AccessController.doPrivileged(new PrivilegedExceptionAction<Method>() { |
107 | 117 | @Override |
108 | 118 | public Method run() throws Exception { |
109 | | - return engineClass.getMethod("setBCHandshakeApplicationProtocolSelector", |
| 119 | + return bcEngineClass.getMethod("setBCHandshakeApplicationProtocolSelector", |
110 | 120 | testBCApplicationProtocolSelector); |
111 | 121 | } |
112 | 122 | }); |
@@ -115,10 +125,11 @@ public Method run() throws Exception { |
115 | 125 | AccessController.doPrivileged(new PrivilegedExceptionAction<Method>() { |
116 | 126 | @Override |
117 | 127 | public Method run() throws Exception { |
118 | | - return engineClass.getMethod("getBCHandshakeApplicationProtocolSelector"); |
| 128 | + return bcEngineClass.getMethod("getBCHandshakeApplicationProtocolSelector"); |
119 | 129 | } |
120 | 130 | }); |
121 | 131 | getHandshakeApplicationProtocolSelector.invoke(engine); |
| 132 | + supported = true; |
122 | 133 | } catch (Throwable t) { |
123 | 134 | logger.error("Unable to initialize BouncyCastleAlpnSslUtils.", t); |
124 | 135 | setApplicationProtocols = null; |
@@ -128,6 +139,7 @@ public Method run() throws Exception { |
128 | 139 | getHandshakeApplicationProtocolSelector = null; |
129 | 140 | bcApplicationProtocolSelectorSelect = null; |
130 | 141 | bcApplicationProtocolSelector = null; |
| 142 | + supported = false; |
131 | 143 | } |
132 | 144 | SET_APPLICATION_PROTOCOLS = setApplicationProtocols; |
133 | 145 | GET_APPLICATION_PROTOCOL = getApplicationProtocol; |
@@ -136,6 +148,7 @@ public Method run() throws Exception { |
136 | 148 | GET_HANDSHAKE_APPLICATION_PROTOCOL_SELECTOR = getHandshakeApplicationProtocolSelector; |
137 | 149 | BC_APPLICATION_PROTOCOL_SELECTOR_SELECT = bcApplicationProtocolSelectorSelect; |
138 | 150 | BC_APPLICATION_PROTOCOL_SELECTOR = bcApplicationProtocolSelector; |
| 151 | + SUPPORTED = supported; |
139 | 152 | } |
140 | 153 |
|
141 | 154 | private BouncyCastleAlpnSslUtils() { |
@@ -227,4 +240,8 @@ public String apply(SSLEngine sslEngine, List<String> strings) { |
227 | 240 | throw new IllegalStateException(ex); |
228 | 241 | } |
229 | 242 | } |
| 243 | + |
| 244 | + static boolean isAlpnSupported() { |
| 245 | + return SUPPORTED; |
| 246 | + } |
230 | 247 | } |