|
17 | 17 | package io.netty.handler.ssl; |
18 | 18 |
|
19 | 19 | import io.netty.util.internal.EmptyArrays; |
| 20 | +import org.junit.jupiter.api.TestInfo; |
20 | 21 | import org.junit.jupiter.api.function.Executable; |
21 | 22 | import org.junit.jupiter.params.ParameterizedTest; |
| 23 | +import org.junit.jupiter.params.provider.Arguments; |
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; |
|
35 | 35 | import java.util.Date; |
36 | 36 | import java.util.List; |
37 | 37 | import java.util.Set; |
| 38 | +import javax.net.ssl.ExtendedSSLSession; |
| 39 | +import javax.net.ssl.SNIHostName; |
| 40 | +import javax.net.ssl.SSLEngine; |
| 41 | +import javax.net.ssl.SSLSession; |
| 42 | +import javax.net.ssl.SSLSocket; |
| 43 | +import javax.net.ssl.X509ExtendedTrustManager; |
| 44 | +import javax.security.auth.x500.X500Principal; |
38 | 45 |
|
| 46 | +import static io.netty.handler.ssl.EnhancingX509ExtendedTrustManager.ALTNAME_DNS; |
| 47 | +import static io.netty.handler.ssl.EnhancingX509ExtendedTrustManager.ALTNAME_IP; |
| 48 | +import static io.netty.handler.ssl.EnhancingX509ExtendedTrustManager.ALTNAME_URI; |
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.Named.named; |
| 55 | +import static org.junit.jupiter.params.provider.Arguments.arguments; |
44 | 56 |
|
45 | 57 | public class EnhancedX509ExtendedTrustManagerTest { |
46 | 58 |
|
| 59 | + private static final String HOSTNAME = "netty.io"; |
| 60 | + private static final String SAN_ENTRY_DNS = "some.netty.io"; |
| 61 | + private static final String SAN_ENTRY_IP = "127.0.0.1"; |
| 62 | + private static final String SAN_ENTRY_URI = "URI:https://uri.netty.io/profile"; |
| 63 | + |
| 64 | + private static final String COMMON_NAME = "leaf.netty.io"; |
| 65 | + |
47 | 66 | private static final X509Certificate TEST_CERT = new X509Certificate() { |
48 | 67 |
|
49 | 68 | @Override |
50 | 69 | public Collection<List<?>> getSubjectAlternativeNames() { |
51 | | - return Arrays.asList(Arrays.asList(1, new Object()), Arrays.asList(2, "some.netty.io")); |
| 70 | + return Arrays.asList(Arrays.asList(1, new Object()), |
| 71 | + Arrays.asList(ALTNAME_DNS, SAN_ENTRY_DNS), Arrays.asList(ALTNAME_IP, SAN_ENTRY_IP), |
| 72 | + Arrays.asList(ALTNAME_URI, SAN_ENTRY_URI), Arrays.asList(1 /* rfc822Name */, SAN_ENTRY_RFC822)); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public X500Principal getSubjectX500Principal() { |
| 77 | + return new X500Principal("CN=" + COMMON_NAME + ", O=Netty"); |
52 | 78 | } |
53 | 79 |
|
54 | 80 | @Override |
@@ -192,7 +218,7 @@ public void checkClientTrusted(X509Certificate[] chain, String authType, Socket |
192 | 218 | @Override |
193 | 219 | public void checkServerTrusted(X509Certificate[] chain, String authType, Socket socket) |
194 | 220 | throws CertificateException { |
195 | | - throw new CertificateException("No subject alternative DNS name matching netty.io."); |
| 221 | + throw newCertificateExceptionWithMatchingMessage(); |
196 | 222 | } |
197 | 223 |
|
198 | 224 | @Override |
@@ -203,7 +229,7 @@ public void checkClientTrusted(X509Certificate[] chain, String authType, SSLEngi |
203 | 229 | @Override |
204 | 230 | public void checkServerTrusted(X509Certificate[] chain, String authType, SSLEngine engine) |
205 | 231 | throws CertificateException { |
206 | | - throw new CertificateException("No subject alternative DNS name matching netty.io."); |
| 232 | + throw newCertificateExceptionWithMatchingMessage(); |
207 | 233 | } |
208 | 234 |
|
209 | 235 | @Override |
@@ -214,32 +240,49 @@ public void checkClientTrusted(X509Certificate[] chain, String authType) { |
214 | 240 | @Override |
215 | 241 | public void checkServerTrusted(X509Certificate[] chain, String authType) |
216 | 242 | throws CertificateException { |
217 | | - throw new CertificateException("No subject alternative DNS name matching netty.io."); |
| 243 | + throw newCertificateExceptionWithMatchingMessage(); |
218 | 244 | } |
219 | 245 |
|
220 | 246 | @Override |
221 | 247 | public X509Certificate[] getAcceptedIssuers() { |
222 | 248 | return new X509Certificate[0]; |
223 | 249 | } |
| 250 | + |
| 251 | + private CertificateException newCertificateExceptionWithMatchingMessage() { |
| 252 | + return new CertificateException("No subject alternative DNS name matching " + HOSTNAME + " found."); |
| 253 | + } |
224 | 254 | }); |
225 | 255 |
|
226 | | - static List<Executable> throwingMatchingExecutables() { |
227 | | - return Arrays.asList(new Executable() { |
| 256 | + static List<Arguments> throwingMatchingExecutables() { |
| 257 | + return Arrays.asList(arguments(named("checkServerTrusted", new Executable() { |
228 | 258 | @Override |
229 | 259 | public void execute() throws Throwable { |
230 | 260 | MATCHING_MANAGER.checkServerTrusted(new X509Certificate[] { TEST_CERT }, null); |
231 | 261 | } |
232 | | - }, new Executable() { |
| 262 | + })), arguments(named("checkServerTrusted with SSLEngine", new Executable() { |
233 | 263 | @Override |
234 | 264 | public void execute() throws Throwable { |
235 | | - MATCHING_MANAGER.checkServerTrusted(new X509Certificate[] { TEST_CERT }, null, (SSLEngine) null); |
| 265 | + SSLSession session = mockSSLSession(); |
| 266 | + SSLEngine engine = Mockito.mock(SSLEngine.class); |
| 267 | + Mockito.when(engine.getHandshakeSession()).thenReturn(session); |
| 268 | + MATCHING_MANAGER.checkServerTrusted(new X509Certificate[] { TEST_CERT }, null, engine); |
236 | 269 | } |
237 | | - }, new Executable() { |
| 270 | + })), arguments(named("checkServerTrusted with SSLSocket", new Executable() { |
238 | 271 | @Override |
239 | 272 | public void execute() throws Throwable { |
240 | | - MATCHING_MANAGER.checkServerTrusted(new X509Certificate[] { TEST_CERT }, null, (SSLSocket) null); |
| 273 | + SSLSession session = mockSSLSession(); |
| 274 | + SSLSocket socket = Mockito.mock(SSLSocket.class); |
| 275 | + Mockito.when(socket.getHandshakeSession()).thenReturn(session); |
| 276 | + MATCHING_MANAGER.checkServerTrusted(new X509Certificate[] { TEST_CERT }, null, socket); |
241 | 277 | } |
242 | | - }); |
| 278 | + }))); |
| 279 | + } |
| 280 | + |
| 281 | + private static SSLSession mockSSLSession() { |
| 282 | + ExtendedSSLSession session = Mockito.mock(ExtendedSSLSession.class); |
| 283 | + Mockito.when(session.getRequestedServerNames()).thenReturn(Arrays.asList(new SNIHostName(HOSTNAME))); |
| 284 | + Mockito.when(session.getPeerHost()).thenReturn(HOSTNAME); |
| 285 | + return session; |
243 | 286 | } |
244 | 287 |
|
245 | 288 | private static final EnhancingX509ExtendedTrustManager NON_MATCHING_MANAGER = |
@@ -286,32 +329,42 @@ public X509Certificate[] getAcceptedIssuers() { |
286 | 329 | } |
287 | 330 | }); |
288 | 331 |
|
289 | | - static List<Executable> throwingNonMatchingExecutables() { |
290 | | - return Arrays.asList(new Executable() { |
| 332 | + static List<Arguments> throwingNonMatchingExecutables() { |
| 333 | + return Arrays.asList(arguments(named("checkServerTrusted", new Executable() { |
291 | 334 | @Override |
292 | 335 | public void execute() throws Throwable { |
293 | 336 | NON_MATCHING_MANAGER.checkServerTrusted(new X509Certificate[] { TEST_CERT }, null); |
294 | 337 | } |
295 | | - }, new Executable() { |
| 338 | + })), arguments(named("checkServerTrusted with SSLEngine", new Executable() { |
296 | 339 | @Override |
297 | 340 | public void execute() throws Throwable { |
298 | 341 | NON_MATCHING_MANAGER.checkServerTrusted(new X509Certificate[] { TEST_CERT }, null, (SSLEngine) null); |
299 | 342 | } |
300 | | - }, new Executable() { |
| 343 | + })), arguments(named("checkServerTrusted with SSLSocket", new Executable() { |
301 | 344 | @Override |
302 | 345 | public void execute() throws Throwable { |
303 | 346 | NON_MATCHING_MANAGER.checkServerTrusted(new X509Certificate[] { TEST_CERT }, null, (SSLSocket) null); |
304 | 347 | } |
305 | | - }); |
| 348 | + }))); |
306 | 349 | } |
307 | 350 |
|
308 | 351 | @ParameterizedTest |
309 | 352 | @MethodSource("throwingMatchingExecutables") |
310 | | - void testEnhanceException(Executable executable) { |
| 353 | + void testEnhanceException(Executable executable, TestInfo testInfo) { |
311 | 354 | CertificateException exception = assertThrows(CertificateException.class, executable); |
312 | 355 | // We should wrap the original cause with our own. |
313 | 356 | assertInstanceOf(CertificateException.class, exception.getCause()); |
314 | | - assertThat(exception.getMessage()).contains("some.netty.io"); |
| 357 | + String message = exception.getMessage(); |
| 358 | + if (testInfo.getDisplayName().contains("with")) { |
| 359 | + // The following data can be extracted only when we run the test with SSLEngine or SSLSocket: |
| 360 | + assertThat(message).contains("SNIHostName=" + HOSTNAME); |
| 361 | + assertThat(message).contains("peerHost=" + HOSTNAME); |
| 362 | + } |
| 363 | + assertThat(message).contains("DNS:" + SAN_ENTRY_DNS); |
| 364 | + assertThat(message).contains("IP:" + SAN_ENTRY_IP); |
| 365 | + assertThat(message).contains("URI:" + SAN_ENTRY_URI); |
| 366 | + assertThat(message).contains("CN=" + COMMON_NAME); |
| 367 | + assertThat(message).doesNotContain(SAN_ENTRY_RFC822); |
315 | 368 | } |
316 | 369 |
|
317 | 370 | @ParameterizedTest |
|