|
18 | 18 | import com.rabbitmq.client.Address; |
19 | 19 | import com.rabbitmq.client.ConnectionFactory; |
20 | 20 | import com.rabbitmq.client.test.TestUtils; |
| 21 | +import org.junit.BeforeClass; |
21 | 22 | import org.junit.Test; |
| 23 | +import org.junit.runner.RunWith; |
| 24 | +import org.junit.runners.Parameterized; |
22 | 25 |
|
23 | 26 | import javax.net.ssl.KeyManagerFactory; |
24 | 27 | import javax.net.ssl.SSLContext; |
25 | 28 | import javax.net.ssl.SSLHandshakeException; |
26 | 29 | import javax.net.ssl.TrustManagerFactory; |
27 | 30 | import java.io.FileInputStream; |
28 | | -import java.io.IOException; |
29 | | -import java.security.KeyManagementException; |
30 | 31 | import java.security.KeyStore; |
31 | | -import java.security.KeyStoreException; |
32 | | -import java.security.NoSuchAlgorithmException; |
33 | | -import java.security.UnrecoverableKeyException; |
34 | | -import java.security.cert.CertificateException; |
35 | | -import java.util.concurrent.TimeoutException; |
| 32 | +import java.util.function.Consumer; |
36 | 33 |
|
| 34 | +import static com.rabbitmq.client.test.TestUtils.getSSLContext; |
37 | 35 | import static java.util.Collections.singletonList; |
38 | 36 | import static org.junit.Assert.assertNotNull; |
39 | 37 | import static org.junit.Assert.fail; |
40 | 38 |
|
41 | | -public class HostnameVerification extends UnverifiedConnection { |
42 | | - |
43 | | - public void openConnection() |
44 | | - throws IOException, TimeoutException { |
45 | | - try { |
46 | | - String keystorePath = System.getProperty("test-keystore.ca"); |
47 | | - assertNotNull(keystorePath); |
48 | | - String keystorePasswd = System.getProperty("test-keystore.password"); |
49 | | - assertNotNull(keystorePasswd); |
50 | | - char[] keystorePassword = keystorePasswd.toCharArray(); |
51 | | - |
52 | | - KeyStore tks = KeyStore.getInstance("JKS"); |
53 | | - tks.load(new FileInputStream(keystorePath), keystorePassword); |
54 | | - |
55 | | - TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509"); |
56 | | - tmf.init(tks); |
57 | | - |
58 | | - String p12Path = System.getProperty("test-client-cert.path"); |
59 | | - assertNotNull(p12Path); |
60 | | - String p12Passwd = System.getProperty("test-client-cert.password"); |
61 | | - assertNotNull(p12Passwd); |
62 | | - KeyStore ks = KeyStore.getInstance("PKCS12"); |
63 | | - char[] p12Password = p12Passwd.toCharArray(); |
64 | | - ks.load(new FileInputStream(p12Path), p12Password); |
65 | | - |
66 | | - KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); |
67 | | - kmf.init(ks, p12Password); |
68 | | - |
69 | | - SSLContext c = getSSLContext(); |
70 | | - c.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); |
71 | | - c.init(null, tmf.getTrustManagers(), null); |
72 | | - |
73 | | - connectionFactory = TestUtils.connectionFactory(); |
74 | | - connectionFactory.useSslProtocol(c); |
75 | | - connectionFactory.enableHostnameVerification(); |
76 | | - } catch (NoSuchAlgorithmException ex) { |
77 | | - throw new IOException(ex.toString()); |
78 | | - } catch (KeyManagementException ex) { |
79 | | - throw new IOException(ex.toString()); |
80 | | - } catch (KeyStoreException ex) { |
81 | | - throw new IOException(ex.toString()); |
82 | | - } catch (CertificateException ex) { |
83 | | - throw new IOException(ex.toString()); |
84 | | - } catch (UnrecoverableKeyException ex) { |
85 | | - throw new IOException(ex.toString()); |
86 | | - } |
87 | | - |
88 | | - try { |
89 | | - connection = connectionFactory.newConnection( |
90 | | - () -> singletonList(new Address("127.0.0.1", ConnectionFactory.DEFAULT_AMQP_OVER_SSL_PORT))); |
91 | | - fail("The server certificate isn't issued for 127.0.0.1, the TLS handshake should have failed"); |
92 | | - } catch (SSLHandshakeException ignored) { |
93 | | - } catch (IOException e) { |
94 | | - fail(); |
95 | | - } |
| 39 | +@RunWith(Parameterized.class) |
| 40 | +public class HostnameVerification { |
| 41 | + |
| 42 | + static SSLContext sslContext; |
| 43 | + @Parameterized.Parameter |
| 44 | + public Consumer<ConnectionFactory> customizer; |
| 45 | + |
| 46 | + @Parameterized.Parameters |
| 47 | + public static Object[] data() { |
| 48 | + return new Object[] { |
| 49 | + blockingIo(enableHostnameVerification()), |
| 50 | + nio(enableHostnameVerification()), |
| 51 | + }; |
| 52 | + } |
| 53 | + |
| 54 | + private static Consumer<ConnectionFactory> blockingIo(final Consumer<ConnectionFactory> customizer) { |
| 55 | + return connectionFactory -> { |
| 56 | + connectionFactory.useBlockingIo(); |
| 57 | + customizer.accept(connectionFactory); |
| 58 | + }; |
96 | 59 | } |
97 | 60 |
|
98 | | - public void openChannel() { |
| 61 | + private static Consumer<ConnectionFactory> nio(final Consumer<ConnectionFactory> customizer) { |
| 62 | + return connectionFactory -> { |
| 63 | + connectionFactory.useNio(); |
| 64 | + customizer.accept(connectionFactory); |
| 65 | + }; |
| 66 | + } |
| 67 | + |
| 68 | + private static Consumer<ConnectionFactory> enableHostnameVerification() { |
| 69 | + return connectionFactory -> connectionFactory.enableHostnameVerification(); |
| 70 | + } |
| 71 | + |
| 72 | + @BeforeClass |
| 73 | + public static void initCrypto() throws Exception { |
| 74 | + String keystorePath = System.getProperty("test-keystore.ca"); |
| 75 | + assertNotNull(keystorePath); |
| 76 | + String keystorePasswd = System.getProperty("test-keystore.password"); |
| 77 | + assertNotNull(keystorePasswd); |
| 78 | + char[] keystorePassword = keystorePasswd.toCharArray(); |
| 79 | + |
| 80 | + KeyStore tks = KeyStore.getInstance("JKS"); |
| 81 | + tks.load(new FileInputStream(keystorePath), keystorePassword); |
| 82 | + |
| 83 | + TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509"); |
| 84 | + tmf.init(tks); |
| 85 | + |
| 86 | + String p12Path = System.getProperty("test-client-cert.path"); |
| 87 | + assertNotNull(p12Path); |
| 88 | + String p12Passwd = System.getProperty("test-client-cert.password"); |
| 89 | + assertNotNull(p12Passwd); |
| 90 | + KeyStore ks = KeyStore.getInstance("PKCS12"); |
| 91 | + char[] p12Password = p12Passwd.toCharArray(); |
| 92 | + ks.load(new FileInputStream(p12Path), p12Password); |
| 93 | + |
| 94 | + KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); |
| 95 | + kmf.init(ks, p12Password); |
| 96 | + |
| 97 | + sslContext = getSSLContext(); |
| 98 | + sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); |
99 | 99 | } |
100 | 100 |
|
101 | | - @Test |
102 | | - public void sSL() { |
| 101 | + @Test(expected = SSLHandshakeException.class) |
| 102 | + public void hostnameVerificationFailsBecauseCertificateNotIssuedForLoopbackInterface() throws Exception { |
| 103 | + ConnectionFactory connectionFactory = TestUtils.connectionFactory(); |
| 104 | + connectionFactory.useSslProtocol(sslContext); |
| 105 | + customizer.accept(connectionFactory); |
| 106 | + connectionFactory.newConnection( |
| 107 | + () -> singletonList(new Address("127.0.0.1", ConnectionFactory.DEFAULT_AMQP_OVER_SSL_PORT))); |
| 108 | + fail("The server certificate isn't issued for 127.0.0.1, the TLS handshake should have failed"); |
103 | 109 | } |
104 | 110 | } |
0 commit comments