|
15 | 15 | */ |
16 | 16 | package io.netty.handler.ssl; |
17 | 17 |
|
| 18 | +import io.netty.handler.ssl.util.SelfSignedCertificate; |
18 | 19 | import org.junit.jupiter.api.Assertions; |
19 | 20 | import org.junit.jupiter.api.Test; |
20 | 21 | import org.junit.jupiter.api.function.Executable; |
21 | 22 |
|
| 23 | +import javax.net.ssl.ManagerFactoryParameters; |
22 | 24 | import javax.net.ssl.SSLException; |
| 25 | +import javax.net.ssl.TrustManager; |
| 26 | +import javax.net.ssl.TrustManagerFactory; |
| 27 | +import javax.net.ssl.TrustManagerFactorySpi; |
23 | 28 | import java.io.File; |
| 29 | +import java.security.KeyStore; |
| 30 | +import java.security.Provider; |
| 31 | + |
| 32 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
24 | 33 |
|
25 | 34 | public class JdkSslServerContextTest extends SslContextTest { |
26 | 35 |
|
@@ -38,4 +47,58 @@ public void execute() throws Throwable { |
38 | 47 | } |
39 | 48 | }); |
40 | 49 | } |
| 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 | + } |
41 | 104 | } |