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 — Load BouncyCastle providers independently (#16710) · netty/netty@deeabbd

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • handler/src/main/java/io/netty/handler/ssl/util
    • BouncyCastleUtil.java
Expand file treeCollapse file tree
Open diff view settings
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
+155-103Lines changed: 155 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -41,78 +41,70 @@ public final class BouncyCastleUtil {
4141
private static final String BC_JSSE_SSLENGINE = "org.bouncycastle.jsse.BCSSLEngine";
4242
private static final String BC_JSSE_ALPN_SELECTOR = "org.bouncycastle.jsse.BCApplicationProtocolSelector";
4343

44-
private static volatile Throwable unavailabilityCauseBcProv;
45-
private static volatile Throwable unavailabilityCauseBcPkix;
46-
private static volatile Throwable unavailabilityCauseBcTls;
47-
private static volatile Provider bcProviderJce;
48-
private static volatile Provider bcProviderJsse;
49-
private static volatile Class<? extends SSLEngine> bcSSLEngineClass;
50-
private static volatile boolean attemptedLoading;
51-
5244
/**
5345
* Indicate whether the BouncyCastle Java Crypto Extensions provider is available.
5446
*/
5547
public static boolean isBcProvAvailable() {
56-
ensureLoaded();
57-
return unavailabilityCauseBcProv == null;
48+
BcProv.ensureLoaded();
49+
return BcProv.unavailabilityCauseBcProv == null;
5850
}
5951

6052
/**
6153
* Indicate whether the BouncyCastle Public-Key Infrastructure utilities are available.
6254
*/
6355
public static boolean isBcPkixAvailable() {
64-
ensureLoaded();
65-
return unavailabilityCauseBcPkix == null;
56+
BcPkix.ensureLoaded();
57+
return BcPkix.unavailabilityCauseBcPkix == null;
6658
}
6759

6860
/**
6961
* Indicate whether the BouncyCastle Java Secure Socket Extensions provider is available.
7062
*/
7163
public static boolean isBcTlsAvailable() {
72-
ensureLoaded();
73-
return unavailabilityCauseBcTls == null;
64+
BcTls.ensureLoaded();
65+
return BcTls.unavailabilityCauseBcTls == null;
7466
}
7567

7668
/**
7769
* @return the cause if unavailable. {@code null} if available.
7870
*/
7971
public static Throwable unavailabilityCauseBcProv() {
80-
ensureLoaded();
81-
return unavailabilityCauseBcProv;
72+
BcProv.ensureLoaded();
73+
return BcProv.unavailabilityCauseBcProv;
8274
}
8375

8476
/**
8577
* @return the cause if unavailable. {@code null} if available.
8678
*/
8779
public static Throwable unavailabilityCauseBcPkix() {
88-
ensureLoaded();
89-
return unavailabilityCauseBcPkix;
80+
BcPkix.ensureLoaded();
81+
return BcPkix.unavailabilityCauseBcPkix;
9082
}
9183

9284
/**
9385
* @return the cause if unavailable. {@code null} if available.
9486
*/
9587
public static Throwable unavailabilityCauseBcTls() {
96-
ensureLoaded();
97-
return unavailabilityCauseBcTls;
88+
BcTls.ensureLoaded();
89+
return BcTls.unavailabilityCauseBcTls;
9890
}
9991

10092
/**
10193
* Indicates whether the given SSLEngine is implemented by BouncyCastle.
10294
*/
10395
public static boolean isBcJsseInUse(SSLEngine engine) {
104-
ensureLoaded();
105-
Class<? extends SSLEngine> bcEngineClass = bcSSLEngineClass;
96+
BcTls.ensureLoaded();
97+
Class<? extends SSLEngine> bcEngineClass = BcTls.bcSSLEngineClass;
10698
return bcEngineClass != null && bcEngineClass.isInstance(engine);
10799
}
108100

109101
/**
110102
* Get the BouncyCastle Java Crypto Extensions provider, or throw an exception if it is unavailable.
111103
*/
112104
public static Provider getBcProviderJce() {
113-
ensureLoaded();
114-
Throwable cause = unavailabilityCauseBcProv;
115-
Provider provider = bcProviderJce;
105+
BcProv.ensureLoaded();
106+
Throwable cause = BcProv.unavailabilityCauseBcProv;
107+
Provider provider = BcProv.bcProviderJce;
116108
if (cause != null || provider == null) {
117109
throw new IllegalStateException(cause);
118110
}
@@ -123,9 +115,9 @@ public static Provider getBcProviderJce() {
123115
* Get the BouncyCastle Java Secure Socket Extensions provider, or throw an exception if it is unavailable.
124116
*/
125117
public static Provider getBcProviderJsse() {
126-
ensureLoaded();
127-
Throwable cause = unavailabilityCauseBcTls;
128-
Provider provider = bcProviderJsse;
118+
BcTls.ensureLoaded();
119+
Throwable cause = BcTls.unavailabilityCauseBcTls;
120+
Provider provider = BcTls.bcProviderJsse;
129121
if (cause != null || provider == null) {
130122
throw new IllegalStateException(cause);
131123
}
@@ -139,97 +131,157 @@ public static Provider getBcProviderJsse() {
139131
* @return engine class.
140132
*/
141133
public static Class<? extends SSLEngine> getBcSSLEngineClass() {
142-
ensureLoaded();
143-
return bcSSLEngineClass;
134+
BcTls.ensureLoaded();
135+
return BcTls.bcSSLEngineClass;
144136
}
145137

146138
/**
147139
* Reset the loaded providers. Useful for testing, to redo the loading under different conditions.
148140
*/
149141
static void reset() {
150-
attemptedLoading = false;
151-
unavailabilityCauseBcProv = null;
152-
unavailabilityCauseBcPkix = null;
153-
unavailabilityCauseBcTls = null;
154-
bcProviderJce = null;
155-
bcProviderJsse = null;
156-
bcSSLEngineClass = null;
157-
}
158-
159-
private static void ensureLoaded() {
160-
if (!attemptedLoading) {
161-
tryLoading();
162-
}
142+
BcProv.attemptedLoading = false;
143+
BcPkix.attemptedLoading = false;
144+
BcTls.attemptedLoading = false;
145+
BcProv.unavailabilityCauseBcProv = null;
146+
BcPkix.unavailabilityCauseBcPkix = null;
147+
BcTls.unavailabilityCauseBcTls = null;
148+
BcProv.bcProviderJce = null;
149+
BcTls.bcProviderJsse = null;
150+
BcTls.bcSSLEngineClass = null;
163151
}
164152

165-
@SuppressWarnings("unchecked")
166-
private static void tryLoading() {
167-
AccessController.doPrivileged((PrivilegedAction<?>) () -> {
168-
try {
169-
// Check for bcprov-jdk18on or bc-fips:
170-
Provider provider = Security.getProvider(BC_PROVIDER_NAME);
171-
if (provider == null) {
172-
provider = Security.getProvider(BC_FIPS_PROVIDER_NAME);
173-
}
174-
if (provider == null) {
175-
ClassLoader classLoader = BouncyCastleUtil.class.getClassLoader();
176-
Class<Provider> bcProviderClass;
153+
private static final class BcProv {
154+
static volatile Throwable unavailabilityCauseBcProv;
155+
static volatile Provider bcProviderJce;
156+
static volatile boolean attemptedLoading;
157+
158+
@SuppressWarnings("unchecked")
159+
private static void ensureLoaded() {
160+
if (!attemptedLoading) {
161+
AccessController.doPrivileged((PrivilegedAction<?>) () -> {
177162
try {
178-
bcProviderClass = (Class<Provider>) Class.forName(BC_PROVIDER, true, classLoader);
179-
} catch (ClassNotFoundException e) {
180-
try {
181-
bcProviderClass = (Class<Provider>) Class.forName(BC_FIPS_PROVIDER, true, classLoader);
182-
} catch (ClassNotFoundException ex) {
183-
ThrowableUtil.addSuppressed(e, ex);
184-
throw e;
163+
// Check for bcprov-jdk18on or bc-fips:
164+
Provider provider = Security.getProvider(BC_PROVIDER_NAME);
165+
if (provider == null) {
166+
provider = Security.getProvider(BC_FIPS_PROVIDER_NAME);
185167
}
168+
if (provider == null) {
169+
ClassLoader classLoader = BouncyCastleUtil.class.getClassLoader();
170+
Class<Provider> bcProviderClass;
171+
try {
172+
bcProviderClass = (Class<Provider>) Class.forName(
173+
BC_PROVIDER, true, classLoader);
174+
} catch (ClassNotFoundException e) {
175+
try {
176+
bcProviderClass = (Class<Provider>) Class.forName(
177+
BC_FIPS_PROVIDER, true, classLoader);
178+
} catch (ClassNotFoundException ex) {
179+
ThrowableUtil.addSuppressed(e, ex);
180+
throw e;
181+
}
182+
}
183+
provider = bcProviderClass.getConstructor().newInstance();
184+
}
185+
bcProviderJce = provider;
186+
logger.debug("Bouncy Castle provider available");
187+
} catch (Throwable e) {
188+
String msg = "Bouncy Castle provider (bcprov-jdk18on or bc-fips) was not loaded";
189+
if (includeLoadingErrorStackTrace(e)) {
190+
logger.debug(msg, e);
191+
} else {
192+
logger.debug(msg);
193+
}
194+
unavailabilityCauseBcProv = e;
186195
}
187-
provider = bcProviderClass.getConstructor().newInstance();
188-
}
189-
bcProviderJce = provider;
190-
logger.debug("Bouncy Castle provider available");
191-
} catch (Throwable e) {
192-
logger.debug("Cannot load Bouncy Castle provider", e);
193-
unavailabilityCauseBcProv = e;
196+
return null;
197+
});
198+
attemptedLoading = true;
194199
}
200+
}
201+
}
202+
203+
private static final class BcPkix {
204+
static volatile Throwable unavailabilityCauseBcPkix;
205+
static volatile boolean attemptedLoading;
195206

196-
try {
197-
// Check for bcpkix-jdk18on:
198-
ClassLoader classLoader = BouncyCastleUtil.class.getClassLoader();
199-
Provider provider = bcProviderJce;
200-
if (provider != null) {
201-
// Use provider class loader in case it was loaded by the system loader.
202-
classLoader = provider.getClass().getClassLoader();
203-
}
204-
Class.forName(BC_PEMPARSER, true, classLoader);
205-
logger.debug("Bouncy Castle PKIX available");
206-
} catch (Throwable e) {
207-
logger.debug("Cannot load Bouncy Castle PKIX", e);
208-
unavailabilityCauseBcPkix = e;
207+
private static void ensureLoaded() {
208+
if (!attemptedLoading) {
209+
BcProv.ensureLoaded(); // For BcProv.bcProviderJce
210+
AccessController.doPrivileged((PrivilegedAction<?>) () -> {
211+
try {
212+
// Check for bcpkix-jdk18on:
213+
ClassLoader classLoader = BouncyCastleUtil.class.getClassLoader();
214+
Provider provider = BcProv.bcProviderJce;
215+
if (provider != null) {
216+
// Use provider class loader in case it was loaded by the system loader.
217+
classLoader = provider.getClass().getClassLoader();
218+
}
219+
Class.forName(BC_PEMPARSER, true, classLoader);
220+
logger.debug("Bouncy Castle PKIX available");
221+
} catch (Throwable e) {
222+
String msg = "Bouncy Castle PKIX (bcpkix-jdk18on) was not loaded";
223+
if (includeLoadingErrorStackTrace(e)) {
224+
logger.debug(msg, e);
225+
} else {
226+
logger.debug(msg);
227+
}
228+
unavailabilityCauseBcPkix = e;
229+
}
230+
return null;
231+
});
232+
attemptedLoading = true;
209233
}
234+
}
235+
}
210236

211-
try {
212-
// Check for bctls-jdk18on:
213-
ClassLoader classLoader = BouncyCastleUtil.class.getClassLoader();
214-
Provider provider = Security.getProvider(BC_JSSE_PROVIDER_NAME);
215-
if (provider != null) {
216-
// Use provider class loader in case it was loaded by the system loader.
217-
classLoader = provider.getClass().getClassLoader();
218-
} else {
219-
Class<?> providerClass = Class.forName(BC_JSSE_PROVIDER, true, classLoader);
220-
provider = (Provider) providerClass.getConstructor().newInstance();
221-
}
222-
bcSSLEngineClass = (Class<? extends SSLEngine>) Class.forName(BC_JSSE_SSLENGINE, true, classLoader);
223-
Class.forName(BC_JSSE_ALPN_SELECTOR, true, classLoader);
224-
bcProviderJsse = provider;
225-
logger.debug("Bouncy Castle JSSE available");
226-
} catch (Throwable e) {
227-
logger.debug("Cannot load Bouncy Castle TLS", e);
228-
unavailabilityCauseBcTls = e;
237+
private static final class BcTls {
238+
static volatile Throwable unavailabilityCauseBcTls;
239+
static volatile Provider bcProviderJsse;
240+
static volatile Class<? extends SSLEngine> bcSSLEngineClass;
241+
static volatile boolean attemptedLoading;
242+
243+
@SuppressWarnings("unchecked")
244+
private static void ensureLoaded() {
245+
if (!attemptedLoading) {
246+
AccessController.doPrivileged((PrivilegedAction<?>) () -> {
247+
try {
248+
// Check for bctls-jdk18on:
249+
ClassLoader classLoader = BouncyCastleUtil.class.getClassLoader();
250+
Provider provider = Security.getProvider(BC_JSSE_PROVIDER_NAME);
251+
if (provider != null) {
252+
// Use provider class loader in case it was loaded by the system loader.
253+
classLoader = provider.getClass().getClassLoader();
254+
} else {
255+
Class<?> providerClass = Class.forName(BC_JSSE_PROVIDER, true, classLoader);
256+
provider = (Provider) providerClass.getConstructor().newInstance();
257+
}
258+
bcSSLEngineClass = (Class<? extends SSLEngine>) Class.forName(
259+
BC_JSSE_SSLENGINE, true, classLoader);
260+
Class.forName(BC_JSSE_ALPN_SELECTOR, true, classLoader);
261+
bcProviderJsse = provider;
262+
logger.debug("Bouncy Castle JSSE available");
263+
} catch (Throwable e) {
264+
String msg = "Bouncy Castle TLS (bctls-jdk18on) was not loaded";
265+
if (includeLoadingErrorStackTrace(e)) {
266+
logger.debug(msg, e);
267+
} else {
268+
logger.debug(msg);
269+
}
270+
unavailabilityCauseBcTls = e;
271+
}
272+
return null;
273+
});
274+
attemptedLoading = true;
229275
}
230-
attemptedLoading = true;
231-
return null;
232-
});
276+
}
277+
}
278+
279+
private static boolean includeLoadingErrorStackTrace(Throwable e) {
280+
// Only include the stack trace if we have TRACE logging enabled,
281+
// or if the exception is something other than ClassNotFoundException.
282+
// The ClassNotFoundException is what we would expect to see if the
283+
// BC JAR files are just not on the classpath.
284+
return logger.isTraceEnabled() || !(e instanceof ClassNotFoundException);
233285
}
234286

235287
private BouncyCastleUtil() {

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.