Skip to content

Use initialized BouncyCastle providers when available (#14855)#14862

Merged
normanmaurer merged 1 commit intonetty:4.2from
chrisvest:4.2-bc-provider
Feb 25, 2025
Merged

Use initialized BouncyCastle providers when available (#14855)#14862
normanmaurer merged 1 commit intonetty:4.2from
chrisvest:4.2-bc-provider

Conversation

@chrisvest
Copy link
Copy Markdown
Member

Motivation:

Before this change, the 'io.netty.handler.ssl.BouncyCastlePemReader' class used by the SSL context always instantiated a new BouncyCastle provider when one of supported providers were on the classpath. When Netty is used in a native image created with GraalVM, the moment of initialization matters. Providers instantiated when the native image is built will see different classes than providers instantiated during runtime unless every single class the BouncyCastle loads using the reflection API is registered for a reflection. Some frameworks like Quarkus makes sure that BouncyCastle providers are created for users and registered with the Java Security API. However in this case, the providers prepared by Quarkus are ignored and users are left to a difficult task to find all required classes loaded by the BouncyCastle for their use case and register them. This commit prefers the providers registered against the Java Security API and fallbacks to manual class instantiation, so that user experience improves when using native executables.

Modifications:

I have modified
'src/main/java/io/netty/handler/ssl/BouncyCastlePemReader.java' to load the 2 supported BouncyCastle providers (BC/BCFIPS) from the Java Security API first and fallback to previous behavior when the providers are not available. I didn't find any test class explicitly testing this 'BouncyCastlePemReader', therefore I added a new test class, 'src/test/java/io/netty/handler/ssl/BouncyCastlePemReaderTest.java', that assures providers are loaded from the Java Security API, but when not available, Netty creates a new providers if respective classes are available on the classpath.

Result:

It will be easier to use SSL context with BouncyCastle support in native images.

Fixes #14826.

Motivation:

Before this change, the 'io.netty.handler.ssl.BouncyCastlePemReader'
class used by the SSL context always instantiated a new BouncyCastle
provider when one of supported providers were on the classpath. When
Netty is used in a native image created with GraalVM, the moment of
initialization matters. Providers instantiated when the native image is
built will see different classes than providers instantiated during
runtime unless every single class the BouncyCastle loads using the
reflection API is registered for a reflection. Some frameworks like
Quarkus makes sure that BouncyCastle providers are created for users and
registered with the Java Security API. However in this case, the
providers prepared by Quarkus are ignored and users are left to a
difficult task to find all required classes loaded by the BouncyCastle
for their use case and register them. This commit prefers the providers
registered against the Java Security API and fallbacks to manual class
instantiation, so that user experience improves when using native
executables.

Modifications:

I have modified
'src/main/java/io/netty/handler/ssl/BouncyCastlePemReader.java' to load
the 2 supported BouncyCastle providers (BC/BCFIPS) from the Java
Security API first and fallback to previous behavior when the providers
are not available. I didn't find any test class explicitly testing this
'BouncyCastlePemReader', therefore I added a new test class,
'src/test/java/io/netty/handler/ssl/BouncyCastlePemReaderTest.java',
that assures providers are loaded from the Java Security API, but when
not available, Netty creates a new providers if respective classes are
available on the classpath.

Result:

It will be easier to use SSL context with BouncyCastle support in native
images.

Fixes netty#14826.
@normanmaurer normanmaurer added this to the 4.2.0.RC4 milestone Feb 25, 2025
@normanmaurer normanmaurer merged commit 51fb006 into netty:4.2 Feb 25, 2025
14 of 15 checks passed
@chrisvest chrisvest deleted the 4.2-bc-provider branch February 25, 2025 19:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants