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 — Avoid NPE in JdkSslServerContext when TrustManagerFactory returns nul… · netty/netty@0fc37d3

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • handler/src
    • main/java/io/netty/handler/ssl
      • JdkSslServerContext.java
    • test/java/io/netty/handler/ssl
      • JdkSslServerContextTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: handler/src/main/java/io/netty/handler/ssl/JdkSslServerContext.java
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,10 @@ private static SSLContext newSSLContext(Provider sslContextProvider, X509Certifi
353353

354354
private static TrustManager[] wrapTrustManagerIfNeeded(
355355
TrustManager[] trustManagers, ResumptionController resumptionController) {
356+
357+
if (trustManagers == null) {
358+
return null;
359+
}
356360
if (WRAP_TRUST_MANAGER) {
357361
for (int i = 0; i < trustManagers.length; i++) {
358362
TrustManager tm = trustManagers[i];
Collapse file

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

Copy file name to clipboardExpand all lines: handler/src/test/java/io/netty/handler/ssl/JdkSslServerContextTest.java
+63Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,21 @@
1515
*/
1616
package io.netty.handler.ssl;
1717

18+
import io.netty.handler.ssl.util.SelfSignedCertificate;
1819
import org.junit.jupiter.api.Assertions;
1920
import org.junit.jupiter.api.Test;
2021
import org.junit.jupiter.api.function.Executable;
2122

23+
import javax.net.ssl.ManagerFactoryParameters;
2224
import javax.net.ssl.SSLException;
25+
import javax.net.ssl.TrustManager;
26+
import javax.net.ssl.TrustManagerFactory;
27+
import javax.net.ssl.TrustManagerFactorySpi;
2328
import java.io.File;
29+
import java.security.KeyStore;
30+
import java.security.Provider;
31+
32+
import static org.junit.jupiter.api.Assertions.assertNotNull;
2433

2534
public class JdkSslServerContextTest extends SslContextTest {
2635

@@ -38,4 +47,58 @@ public void execute() throws Throwable {
3847
}
3948
});
4049
}
50+
51+
// A TrustManagerFactory whose getTrustManagers() legitimately returns null
52+
// (e.g., asking SSLContext.init to fall back to the JDK default trust store)
53+
// previously NPE'd inside wrapTrustManagerIfNeeded. Verify the server context
54+
// now builds without throwing.
55+
@Test
56+
void testTrustManagerFactoryReturningNullDoesNotThrowNpe() throws Exception {
57+
SelfSignedCertificate ssc = new SelfSignedCertificate();
58+
try {
59+
TrustManagerFactory tmf = new TrustManagerFactory(
60+
new NullReturningTrustManagerFactorySpi(),
61+
NullReturningTrustManagerFactorySpi.PROVIDER, "null") {
62+
};
63+
// TrustManagerFactory must be initialized before SslContextBuilder will accept it;
64+
// without this call the builder throws before reaching the code path under test.
65+
tmf.init((KeyStore) null);
66+
67+
SslContext ctx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey())
68+
.sslProvider(SslProvider.JDK)
69+
.trustManager(tmf)
70+
.build();
71+
// Success is "did not throw". Before the fix this call produced
72+
// SSLException("failed to initialize the server-side SSL context")
73+
// caused by NullPointerException from wrapTrustManagerIfNeeded.
74+
assertNotNull(ctx);
75+
} finally {
76+
ssc.delete();
77+
}
78+
}
79+
80+
private static final class NullReturningTrustManagerFactorySpi extends TrustManagerFactorySpi {
81+
// The Provider(String, double, String) constructor is deprecated since JDK 9 in
82+
// favor of (String, String, String), but the replacement is unavailable on the
83+
// JDK 8 source level this module targets, so we suppress the deprecation warning.
84+
@SuppressWarnings("deprecation")
85+
static final Provider PROVIDER = new Provider("NullReturningProvider", 1.0, "test-only") {
86+
private static final long serialVersionUID = 1L;
87+
};
88+
89+
@Override
90+
protected void engineInit(KeyStore ks) {
91+
// no-op
92+
}
93+
94+
@Override
95+
protected void engineInit(ManagerFactoryParameters spec) {
96+
// no-op
97+
}
98+
99+
@Override
100+
protected TrustManager[] engineGetTrustManagers() {
101+
return null;
102+
}
103+
}
41104
}

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.