GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — BouncyCastleAlpnSslUtils needs to use the correct SSLEngine class as … · netty/netty@1a4c690

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • handler/src
    • main/java/io/netty/handler/ssl
      • BouncyCastleAlpnSslUtils.java
      • JdkAlpnApplicationProtocolNegotiator.java
      • SslUtils.java
      • util
        • BouncyCastleUtil.java
    • test/java/io/netty/handler/ssl
      • BouncyCastleEngineAlpnTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎handler/src/main/java/io/netty/handler/ssl/BouncyCastleAlpnSslUtils.java‎

Copy file name to clipboardExpand all lines: handler/src/main/java/io/netty/handler/ssl/BouncyCastleAlpnSslUtils.java
+24-7Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.lang.reflect.Proxy;
3131
import java.security.AccessController;
3232
import java.security.PrivilegedExceptionAction;
33+
import java.security.SecureRandom;
3334
import java.util.List;
3435
import java.util.function.BiFunction;
3536

@@ -45,6 +46,7 @@ final class BouncyCastleAlpnSslUtils {
4546
private static final Method GET_HANDSHAKE_APPLICATION_PROTOCOL_SELECTOR;
4647
private static final Class<?> BC_APPLICATION_PROTOCOL_SELECTOR;
4748
private static final Method BC_APPLICATION_PROTOCOL_SELECTOR_SELECT;
49+
private static final boolean SUPPORTED;
4850

4951
static {
5052
Method setApplicationProtocols;
@@ -54,18 +56,26 @@ final class BouncyCastleAlpnSslUtils {
5456
Method getHandshakeApplicationProtocolSelector;
5557
Method bcApplicationProtocolSelectorSelect;
5658
Class<?> bcApplicationProtocolSelector;
59+
boolean supported;
5760

5861
try {
5962
if (!BouncyCastleUtil.isBcTlsAvailable()) {
6063
throw new IllegalStateException(BouncyCastleUtil.unavailabilityCauseBcTls());
6164
}
62-
SSLContext context = getSSLContext(BouncyCastleUtil.getBcProviderJsse());
65+
SSLContext context = getSSLContext(BouncyCastleUtil.getBcProviderJsse(), new SecureRandom());
6366
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+
}
6576

6677
final SSLParameters bcSslParameters = engine.getSSLParameters();
6778
final Class<?> bCSslParametersClass = bcSslParameters.getClass();
68-
6979
setApplicationProtocols = AccessController.doPrivileged(new PrivilegedExceptionAction<Method>() {
7080
@Override
7181
public Method run() throws Exception {
@@ -77,15 +87,15 @@ public Method run() throws Exception {
7787
getApplicationProtocol = AccessController.doPrivileged(new PrivilegedExceptionAction<Method>() {
7888
@Override
7989
public Method run() throws Exception {
80-
return engineClass.getMethod("getApplicationProtocol");
90+
return bcEngineClass.getMethod("getApplicationProtocol");
8191
}
8292
});
8393
getApplicationProtocol.invoke(engine);
8494

8595
getHandshakeApplicationProtocol = AccessController.doPrivileged(new PrivilegedExceptionAction<Method>() {
8696
@Override
8797
public Method run() throws Exception {
88-
return engineClass.getMethod("getHandshakeApplicationProtocol");
98+
return bcEngineClass.getMethod("getHandshakeApplicationProtocol");
8999
}
90100
});
91101
getHandshakeApplicationProtocol.invoke(engine);
@@ -106,7 +116,7 @@ public Method run() throws Exception {
106116
AccessController.doPrivileged(new PrivilegedExceptionAction<Method>() {
107117
@Override
108118
public Method run() throws Exception {
109-
return engineClass.getMethod("setBCHandshakeApplicationProtocolSelector",
119+
return bcEngineClass.getMethod("setBCHandshakeApplicationProtocolSelector",
110120
testBCApplicationProtocolSelector);
111121
}
112122
});
@@ -115,10 +125,11 @@ public Method run() throws Exception {
115125
AccessController.doPrivileged(new PrivilegedExceptionAction<Method>() {
116126
@Override
117127
public Method run() throws Exception {
118-
return engineClass.getMethod("getBCHandshakeApplicationProtocolSelector");
128+
return bcEngineClass.getMethod("getBCHandshakeApplicationProtocolSelector");
119129
}
120130
});
121131
getHandshakeApplicationProtocolSelector.invoke(engine);
132+
supported = true;
122133
} catch (Throwable t) {
123134
logger.error("Unable to initialize BouncyCastleAlpnSslUtils.", t);
124135
setApplicationProtocols = null;
@@ -128,6 +139,7 @@ public Method run() throws Exception {
128139
getHandshakeApplicationProtocolSelector = null;
129140
bcApplicationProtocolSelectorSelect = null;
130141
bcApplicationProtocolSelector = null;
142+
supported = false;
131143
}
132144
SET_APPLICATION_PROTOCOLS = setApplicationProtocols;
133145
GET_APPLICATION_PROTOCOL = getApplicationProtocol;
@@ -136,6 +148,7 @@ public Method run() throws Exception {
136148
GET_HANDSHAKE_APPLICATION_PROTOCOL_SELECTOR = getHandshakeApplicationProtocolSelector;
137149
BC_APPLICATION_PROTOCOL_SELECTOR_SELECT = bcApplicationProtocolSelectorSelect;
138150
BC_APPLICATION_PROTOCOL_SELECTOR = bcApplicationProtocolSelector;
151+
SUPPORTED = supported;
139152
}
140153

141154
private BouncyCastleAlpnSslUtils() {
@@ -227,4 +240,8 @@ public String apply(SSLEngine sslEngine, List<String> strings) {
227240
throw new IllegalStateException(ex);
228241
}
229242
}
243+
244+
static boolean isAlpnSupported() {
245+
return SUPPORTED;
246+
}
230247
}
Collapse file

‎handler/src/main/java/io/netty/handler/ssl/JdkAlpnApplicationProtocolNegotiator.java‎

Copy file name to clipboardExpand all lines: handler/src/main/java/io/netty/handler/ssl/JdkAlpnApplicationProtocolNegotiator.java
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public final class JdkAlpnApplicationProtocolNegotiator extends JdkBaseApplicati
3030
private static final boolean AVAILABLE = Conscrypt.isAvailable() ||
3131
JdkAlpnSslUtils.supportsAlpn() ||
3232
JettyAlpnSslEngine.isAvailable() ||
33-
BouncyCastleUtil.isBcTlsAvailable();
33+
(BouncyCastleUtil.isBcTlsAvailable() && BouncyCastleAlpnSslUtils.isAlpnSupported());
3434

3535
private static final SslEngineWrapperFactory ALPN_WRAPPER = AVAILABLE ? new AlpnWrapper() : new FailureWrapper();
3636

@@ -135,7 +135,7 @@ public SSLEngine wrapSslEngine(SSLEngine engine, ByteBufAllocator alloc,
135135
return isServer ? ConscryptAlpnSslEngine.newServerEngine(engine, alloc, applicationNegotiator)
136136
: ConscryptAlpnSslEngine.newClientEngine(engine, alloc, applicationNegotiator);
137137
}
138-
if (BouncyCastleUtil.isBcJsseInUse(engine)) {
138+
if (BouncyCastleUtil.isBcJsseInUse(engine) && BouncyCastleAlpnSslUtils.isAlpnSupported()) {
139139
return new BouncyCastleAlpnSslEngine(engine, applicationNegotiator, isServer);
140140
}
141141
// ALPN support was recently backported to Java8 as
Collapse file

‎handler/src/main/java/io/netty/handler/ssl/SslUtils.java‎

Copy file name to clipboardExpand all lines: handler/src/main/java/io/netty/handler/ssl/SslUtils.java
+7-1Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.security.NoSuchAlgorithmException;
3434
import java.security.NoSuchProviderException;
3535
import java.security.Provider;
36+
import java.security.SecureRandom;
3637
import java.util.Collections;
3738
import java.util.LinkedHashSet;
3839
import java.util.List;
@@ -241,13 +242,18 @@ private static SSLContext newInitContext(Provider provider)
241242

242243
static SSLContext getSSLContext(Provider provider)
243244
throws NoSuchAlgorithmException, KeyManagementException, NoSuchProviderException {
245+
return getSSLContext(provider, null);
246+
}
247+
248+
static SSLContext getSSLContext(Provider provider, SecureRandom secureRandom)
249+
throws NoSuchAlgorithmException, KeyManagementException, NoSuchProviderException {
244250
final SSLContext context;
245251
if (provider == null) {
246252
context = SSLContext.getInstance(getTlsVersion());
247253
} else {
248254
context = SSLContext.getInstance(getTlsVersion(), provider);
249255
}
250-
context.init(null, new TrustManager[0], null);
256+
context.init(null, new TrustManager[0], secureRandom);
251257
return context;
252258
}
253259

Collapse file

‎handler/src/main/java/io/netty/handler/ssl/util/BouncyCastleUtil.java‎

Copy file name to clipboardExpand all lines: handler/src/main/java/io/netty/handler/ssl/util/BouncyCastleUtil.java
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,17 @@ public static Provider getBcProviderJsse() {
132132
return provider;
133133
}
134134

135+
/**
136+
* Returns the public {@link SSLEngine} sub-class that is used by bouncy-castle or {@code null} if
137+
* it can't be loaded.
138+
*
139+
* @return engine class.
140+
*/
141+
public static Class<? extends SSLEngine> getBcSSLEngineClass() {
142+
ensureLoaded();
143+
return bcSSLEngineClass;
144+
}
145+
135146
/**
136147
* Reset the loaded providers. Useful for testing, to redo the loading under different conditions.
137148
*/
Collapse file

‎handler/src/test/java/io/netty/handler/ssl/BouncyCastleEngineAlpnTest.java‎

Copy file name to clipboard
+55Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2025 The Netty Project
3+
*
4+
* The Netty Project licenses this file to you under the Apache License,
5+
* version 2.0 (the "License"); you may not use this file except in compliance
6+
* with the License. You may obtain a copy of the License at:
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
package io.netty.handler.ssl;
17+
18+
import io.netty.handler.ssl.util.BouncyCastleUtil;
19+
import org.bouncycastle.jsse.provider.BouncyCastleJsseProvider;
20+
import org.junit.jupiter.api.Test;
21+
22+
import javax.net.ssl.SSLContext;
23+
import javax.net.ssl.SSLEngine;
24+
import java.security.Provider;
25+
import java.security.SecureRandom;
26+
import java.util.List;
27+
import java.util.function.BiFunction;
28+
import static org.junit.jupiter.api.Assertions.assertTrue;
29+
30+
public class BouncyCastleEngineAlpnTest {
31+
32+
@Test
33+
public void testBouncyCastleSSLEngineSupportsAlpn() throws Exception {
34+
Provider bouncyCastleProvider = new BouncyCastleJsseProvider();
35+
SSLContext context = SslUtils.getSSLContext(bouncyCastleProvider, new SecureRandom());
36+
SSLEngine engine = context.createSSLEngine();
37+
assertTrue(BouncyCastleUtil.isBcJsseInUse(engine));
38+
assertTrue(BouncyCastleAlpnSslUtils.isAlpnSupported());
39+
40+
BouncyCastleAlpnSslEngine alpnSslEngine = new BouncyCastleAlpnSslEngine(
41+
engine, new JdkAlpnApplicationProtocolNegotiator("fake"), true);
42+
43+
// Call methods to ensure these not throw.
44+
alpnSslEngine.setHandshakeApplicationProtocolSelector(new BiFunction<SSLEngine, List<String>, String>() {
45+
@Override
46+
public String apply(SSLEngine sslEngine, List<String> strings) {
47+
return "fake";
48+
}
49+
});
50+
// Check that none of the methods will throw.
51+
alpnSslEngine.getHandshakeApplicationProtocolSelector();
52+
alpnSslEngine.setNegotiatedApplicationProtocol("fake");
53+
alpnSslEngine.getNegotiatedApplicationProtocol();
54+
}
55+
}

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.