|
16 | 16 | package io.netty.handler.ssl; |
17 | 17 |
|
18 | 18 | import io.netty.handler.ssl.util.InsecureTrustManagerFactory; |
| 19 | +import org.junit.jupiter.api.Test; |
19 | 20 |
|
| 21 | +import javax.net.ssl.ManagerFactoryParameters; |
20 | 22 | import javax.net.ssl.SSLException; |
| 23 | +import javax.net.ssl.TrustManager; |
| 24 | +import javax.net.ssl.TrustManagerFactory; |
| 25 | +import javax.net.ssl.TrustManagerFactorySpi; |
21 | 26 | import java.io.File; |
| 27 | +import java.security.KeyStore; |
| 28 | +import java.security.Provider; |
| 29 | + |
| 30 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
22 | 31 |
|
23 | 32 | public class JdkSslClientContextTest extends SslContextTest { |
24 | 33 | @Override |
25 | 34 | protected SslContext newSslContext(File crtFile, File keyFile, String pass) throws SSLException { |
26 | 35 | return new JdkSslClientContext(crtFile, InsecureTrustManagerFactory.INSTANCE, crtFile, keyFile, pass, |
27 | 36 | null, null, IdentityCipherSuiteFilter.INSTANCE, ApplicationProtocolConfig.DISABLED, 0, 0); |
28 | 37 | } |
| 38 | + |
| 39 | + // Reproduces https://github.com/netty/netty/issues/14488 |
| 40 | + // Before the fix, wrapIfNeeded did tms.length without a null check, so a |
| 41 | + // TrustManagerFactory whose getTrustManagers() returns null surfaced as an |
| 42 | + // opaque NullPointerException wrapped in SSLException. After the fix the |
| 43 | + // context is built successfully, matching pre-4.1.114 behavior that relied |
| 44 | + // on SSLContext.init accepting a null TrustManager[] to use platform defaults. |
| 45 | + @Test |
| 46 | + public void testTrustManagerFactoryReturningNullDoesNotThrowNpe() throws Exception { |
| 47 | + TrustManagerFactory tmf = new TrustManagerFactory( |
| 48 | + new NullReturningTrustManagerFactorySpi(), NullReturningTrustManagerFactorySpi.PROVIDER, "null") { |
| 49 | + }; |
| 50 | + // TrustManagerFactory must be initialized before SslContextBuilder will accept it; |
| 51 | + // without this call the builder throws before reaching the code path under test. |
| 52 | + tmf.init((KeyStore) null); |
| 53 | + |
| 54 | + SslContext ctx = SslContextBuilder.forClient() |
| 55 | + .sslProvider(SslProvider.JDK) |
| 56 | + .trustManager(tmf) |
| 57 | + .build(); |
| 58 | + // Success is "did not throw". Before the fix this call produced |
| 59 | + // SSLException("failed to initialize the client-side SSL context") |
| 60 | + // caused by NullPointerException from wrapIfNeeded. |
| 61 | + assertNotNull(ctx); |
| 62 | + } |
| 63 | + |
| 64 | + private static final class NullReturningTrustManagerFactorySpi extends TrustManagerFactorySpi { |
| 65 | + // The Provider(String, double, String) constructor is deprecated since JDK 9 in |
| 66 | + // favor of (String, String, String), but the replacement is unavailable on the |
| 67 | + // JDK 8 source level this module targets, so we suppress the deprecation warning. |
| 68 | + @SuppressWarnings("deprecation") |
| 69 | + static final Provider PROVIDER = new Provider("NullReturningProvider", 1.0, "test-only") { |
| 70 | + private static final long serialVersionUID = 1L; |
| 71 | + }; |
| 72 | + |
| 73 | + @Override |
| 74 | + protected void engineInit(KeyStore ks) { |
| 75 | + // no-op |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + protected void engineInit(ManagerFactoryParameters spec) { |
| 80 | + // no-op |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + protected TrustManager[] engineGetTrustManagers() { |
| 85 | + return null; |
| 86 | + } |
| 87 | + } |
29 | 88 | } |