|
| 1 | +/* Copyright (c) 2018 Pivotal Software, Inc. All rights reserved. */ |
| 2 | +package com.rabbitmq.integration.tests; |
| 3 | + |
| 4 | +import com.rabbitmq.jms.admin.RMQConnectionFactory; |
| 5 | +import org.junit.Before; |
| 6 | +import org.junit.BeforeClass; |
| 7 | +import org.junit.Test; |
| 8 | + |
| 9 | +import javax.jms.Connection; |
| 10 | +import javax.jms.JMSException; |
| 11 | +import javax.jms.JMSSecurityException; |
| 12 | +import javax.net.ssl.KeyManagerFactory; |
| 13 | +import javax.net.ssl.SSLContext; |
| 14 | +import javax.net.ssl.TrustManagerFactory; |
| 15 | +import java.io.FileInputStream; |
| 16 | +import java.security.KeyStore; |
| 17 | + |
| 18 | +import static org.junit.Assert.assertNotNull; |
| 19 | + |
| 20 | +/** |
| 21 | + * Integration test for hostname verification with TLS. |
| 22 | + */ |
| 23 | +public class SSLHostnameVerificationIT { |
| 24 | + |
| 25 | + static SSLContext sslContext; |
| 26 | + RMQConnectionFactory cf; |
| 27 | + |
| 28 | + @BeforeClass |
| 29 | + public static void initCrypto() throws Exception { |
| 30 | + String keystorePath = System.getProperty("test-keystore.ca"); |
| 31 | + assertNotNull(keystorePath); |
| 32 | + String keystorePasswd = System.getProperty("test-keystore.password"); |
| 33 | + assertNotNull(keystorePasswd); |
| 34 | + char[] keystorePassword = keystorePasswd.toCharArray(); |
| 35 | + |
| 36 | + KeyStore tks = KeyStore.getInstance("JKS"); |
| 37 | + tks.load(new FileInputStream(keystorePath), keystorePassword); |
| 38 | + |
| 39 | + TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509"); |
| 40 | + tmf.init(tks); |
| 41 | + |
| 42 | + String p12Path = System.getProperty("test-client-cert.path"); |
| 43 | + assertNotNull(p12Path); |
| 44 | + String p12Passwd = System.getProperty("test-client-cert.password"); |
| 45 | + assertNotNull(p12Passwd); |
| 46 | + |
| 47 | + KeyStore ks = KeyStore.getInstance("PKCS12"); |
| 48 | + char[] p12Password = p12Passwd.toCharArray(); |
| 49 | + ks.load(new FileInputStream(p12Path), p12Password); |
| 50 | + |
| 51 | + KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); |
| 52 | + kmf.init(ks, p12Password); |
| 53 | + |
| 54 | + sslContext = SSLContext.getInstance("TLSv1.2"); |
| 55 | + sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); |
| 56 | + } |
| 57 | + |
| 58 | + @Before |
| 59 | + public void init() { |
| 60 | + cf = new RMQConnectionFactory(); |
| 61 | + cf.useSslProtocol(sslContext); |
| 62 | + cf.setHostnameVerification(true); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void hostnameVerificationEnabledShouldPassForLocalhost() throws JMSException { |
| 67 | + cf.setHost("localhost"); |
| 68 | + Connection connection = null; |
| 69 | + try { |
| 70 | + connection = cf.createConnection(); |
| 71 | + } finally { |
| 72 | + if (connection != null) { |
| 73 | + connection.close(); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + @Test(expected = JMSSecurityException.class) |
| 79 | + public void hostnameVerificationEnabledShouldFailForLoopbackInterface() throws JMSException { |
| 80 | + cf.setHost("127.0.0.1"); |
| 81 | + cf.createConnection(); |
| 82 | + } |
| 83 | +} |
0 commit comments