|
17 | 17 | package io.netty.handler.ssl; |
18 | 18 |
|
19 | 19 | import io.netty.util.internal.EmptyArrays; |
| 20 | +import io.netty.util.internal.PlatformDependent; |
| 21 | +import org.junit.jupiter.api.TestInfo; |
20 | 22 | import org.junit.jupiter.api.function.Executable; |
21 | 23 | import org.junit.jupiter.params.ParameterizedTest; |
22 | 24 | import org.junit.jupiter.params.provider.MethodSource; |
| 25 | +import org.mockito.Mockito; |
23 | 26 |
|
24 | | -import javax.net.ssl.SSLEngine; |
25 | | -import javax.net.ssl.SSLSocket; |
26 | | -import javax.net.ssl.X509ExtendedTrustManager; |
27 | 27 | import java.math.BigInteger; |
28 | 28 | import java.net.Socket; |
29 | 29 | import java.security.Principal; |
|
32 | 32 | import java.security.cert.X509Certificate; |
33 | 33 | import java.util.Arrays; |
34 | 34 | import java.util.Collection; |
| 35 | +import java.util.Collections; |
35 | 36 | import java.util.Date; |
36 | 37 | import java.util.List; |
37 | 38 | import java.util.Set; |
| 39 | +import javax.net.ssl.SSLEngine; |
| 40 | +import javax.net.ssl.SSLSession; |
| 41 | +import javax.net.ssl.SSLSocket; |
| 42 | +import javax.net.ssl.X509ExtendedTrustManager; |
| 43 | +import javax.security.auth.x500.X500Principal; |
38 | 44 |
|
| 45 | +import static io.netty.handler.ssl.EnhancingX509ExtendedTrustManager.ALTNAME_DNS; |
| 46 | +import static io.netty.handler.ssl.EnhancingX509ExtendedTrustManager.ALTNAME_IP; |
| 47 | +import static io.netty.handler.ssl.EnhancingX509ExtendedTrustManager.ALTNAME_URI; |
| 48 | +import static io.netty.handler.ssl.SniClientJava8TestUtil.mockSSLSessionWithSNIHostNameAndPeerHost; |
39 | 49 | import static org.assertj.core.api.Assertions.assertThat; |
40 | 50 | import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
41 | 51 | import static org.junit.jupiter.api.Assertions.assertNull; |
42 | 52 | import static org.junit.jupiter.api.Assertions.assertThrows; |
43 | 53 | import static org.junit.jupiter.api.Assertions.fail; |
| 54 | +import static org.junit.jupiter.api.Assumptions.assumeTrue; |
44 | 55 |
|
45 | 56 | public class EnhancedX509ExtendedTrustManagerTest { |
46 | 57 |
|
| 58 | + private static final String HOSTNAME = "netty.io"; |
| 59 | + private static final String SAN_ENTRY_DNS = "some.netty.io"; |
| 60 | + private static final String SAN_ENTRY_IP = "127.0.0.1"; |
| 61 | + private static final String SAN_ENTRY_URI = "URI:https://uri.netty.io/profile"; |
| 62 | + |
| 63 | + private static final String COMMON_NAME = "leaf.netty.io"; |
| 64 | + |
47 | 65 | private static final X509Certificate TEST_CERT = new X509Certificate() { |
48 | 66 |
|
49 | 67 | @Override |
50 | 68 | public Collection<List<?>> getSubjectAlternativeNames() { |
51 | | - return Arrays.asList(Arrays.asList(1, new Object()), Arrays.asList(2, "some.netty.io")); |
| 69 | + return Arrays.asList(Arrays.asList(1, new Object()), |
| 70 | + Arrays.asList(ALTNAME_DNS, SAN_ENTRY_DNS), Arrays.asList(ALTNAME_IP, SAN_ENTRY_IP), |
| 71 | + Arrays.asList(ALTNAME_URI, SAN_ENTRY_URI), Arrays.asList(1 /* rfc822Name */, SAN_ENTRY_RFC822)); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public X500Principal getSubjectX500Principal() { |
| 76 | + return new X500Principal("CN=" + COMMON_NAME + ", O=Netty"); |
52 | 77 | } |
53 | 78 |
|
54 | 79 | @Override |
@@ -192,7 +217,7 @@ public void checkClientTrusted(X509Certificate[] chain, String authType, Socket |
192 | 217 | @Override |
193 | 218 | public void checkServerTrusted(X509Certificate[] chain, String authType, Socket socket) |
194 | 219 | throws CertificateException { |
195 | | - throw new CertificateException("No subject alternative DNS name matching netty.io."); |
| 220 | + throw newCertificateExceptionWithMatchingMessage(); |
196 | 221 | } |
197 | 222 |
|
198 | 223 | @Override |
@@ -203,7 +228,7 @@ public void checkClientTrusted(X509Certificate[] chain, String authType, SSLEngi |
203 | 228 | @Override |
204 | 229 | public void checkServerTrusted(X509Certificate[] chain, String authType, SSLEngine engine) |
205 | 230 | throws CertificateException { |
206 | | - throw new CertificateException("No subject alternative DNS name matching netty.io."); |
| 231 | + throw newCertificateExceptionWithMatchingMessage(); |
207 | 232 | } |
208 | 233 |
|
209 | 234 | @Override |
@@ -214,16 +239,23 @@ public void checkClientTrusted(X509Certificate[] chain, String authType) { |
214 | 239 | @Override |
215 | 240 | public void checkServerTrusted(X509Certificate[] chain, String authType) |
216 | 241 | throws CertificateException { |
217 | | - throw new CertificateException("No subject alternative DNS name matching netty.io."); |
| 242 | + throw newCertificateExceptionWithMatchingMessage(); |
218 | 243 | } |
219 | 244 |
|
220 | 245 | @Override |
221 | 246 | public X509Certificate[] getAcceptedIssuers() { |
222 | 247 | return new X509Certificate[0]; |
223 | 248 | } |
| 249 | + |
| 250 | + private CertificateException newCertificateExceptionWithMatchingMessage() { |
| 251 | + return new CertificateException("No subject alternative DNS name matching " + HOSTNAME + " found."); |
| 252 | + } |
224 | 253 | }); |
225 | 254 |
|
226 | 255 | static List<Executable> throwingMatchingExecutables() { |
| 256 | + if (PlatformDependent.javaVersion() < 8) { |
| 257 | + return Collections.emptyList(); |
| 258 | + } |
227 | 259 | return Arrays.asList(new Executable() { |
228 | 260 | @Override |
229 | 261 | public void execute() throws Throwable { |
@@ -232,12 +264,18 @@ public void execute() throws Throwable { |
232 | 264 | }, new Executable() { |
233 | 265 | @Override |
234 | 266 | public void execute() throws Throwable { |
235 | | - MATCHING_MANAGER.checkServerTrusted(new X509Certificate[] { TEST_CERT }, null, (SSLEngine) null); |
| 267 | + SSLSession session = mockSSLSessionWithSNIHostNameAndPeerHost(HOSTNAME); |
| 268 | + SSLEngine engine = Mockito.mock(SSLEngine.class); |
| 269 | + Mockito.when(engine.getHandshakeSession()).thenReturn(session); |
| 270 | + MATCHING_MANAGER.checkServerTrusted(new X509Certificate[] { TEST_CERT }, null, engine); |
236 | 271 | } |
237 | 272 | }, new Executable() { |
238 | 273 | @Override |
239 | 274 | public void execute() throws Throwable { |
240 | | - MATCHING_MANAGER.checkServerTrusted(new X509Certificate[] { TEST_CERT }, null, (SSLSocket) null); |
| 275 | + SSLSession session = mockSSLSessionWithSNIHostNameAndPeerHost(HOSTNAME); |
| 276 | + SSLSocket socket = Mockito.mock(SSLSocket.class); |
| 277 | + Mockito.when(socket.getHandshakeSession()).thenReturn(session); |
| 278 | + MATCHING_MANAGER.checkServerTrusted(new X509Certificate[] { TEST_CERT }, null, socket); |
241 | 279 | } |
242 | 280 | }); |
243 | 281 | } |
@@ -307,16 +345,28 @@ public void execute() throws Throwable { |
307 | 345 |
|
308 | 346 | @ParameterizedTest |
309 | 347 | @MethodSource("throwingMatchingExecutables") |
310 | | - void testEnhanceException(Executable executable) { |
| 348 | + void testEnhanceException(Executable executable, TestInfo testInfo) { |
| 349 | + assumeTrue(PlatformDependent.javaVersion() >= 8); |
311 | 350 | CertificateException exception = assertThrows(CertificateException.class, executable); |
312 | 351 | // We should wrap the original cause with our own. |
313 | 352 | assertInstanceOf(CertificateException.class, exception.getCause()); |
314 | | - assertThat(exception.getMessage()).contains("some.netty.io"); |
| 353 | + String message = exception.getMessage(); |
| 354 | + if (testInfo.getDisplayName().contains("with")) { |
| 355 | + // The following data can be extracted only when we run the test with SSLEngine or SSLSocket: |
| 356 | + assertThat(message).contains("SNIHostName=" + HOSTNAME); |
| 357 | + assertThat(message).contains("peerHost=" + HOSTNAME); |
| 358 | + } |
| 359 | + assertThat(message).contains("DNS:" + SAN_ENTRY_DNS); |
| 360 | + assertThat(message).contains("IP:" + SAN_ENTRY_IP); |
| 361 | + assertThat(message).contains("URI:" + SAN_ENTRY_URI); |
| 362 | + assertThat(message).contains("CN=" + COMMON_NAME); |
| 363 | + assertThat(message).doesNotContain(SAN_ENTRY_RFC822); |
315 | 364 | } |
316 | 365 |
|
317 | 366 | @ParameterizedTest |
318 | 367 | @MethodSource("throwingNonMatchingExecutables") |
319 | 368 | void testNotEnhanceException(Executable executable) { |
| 369 | + assumeTrue(PlatformDependent.javaVersion() >= 8); |
320 | 370 | CertificateException exception = assertThrows(CertificateException.class, executable); |
321 | 371 | // We should not wrap the original cause with our own. |
322 | 372 | assertNull(exception.getCause()); |
|