|
| 1 | +/** |
| 2 | + * Copyright (c) 2011, 2017 IBM |
| 3 | + * |
| 4 | + * All rights reserved. This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License v1.0 |
| 6 | + * and Eclipse Distribution License v1.0 which accompany this distribution. |
| 7 | + * |
| 8 | + * The Eclipse Public License is available at |
| 9 | + * http://www.eclipse.org/legal/epl-v10.html |
| 10 | + * and the Eclipse Distribution License is available at |
| 11 | + * http://www.eclipse.org/org/documents/edl-v10.php. |
| 12 | + * |
| 13 | + * Contributors: |
| 14 | + * James Sutton - IBM Adding HTTPS Hostname verification tests. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.eclipse.paho.client.mqttv3.test; |
| 18 | + |
| 19 | +import java.io.InputStream; |
| 20 | +import java.security.KeyStore; |
| 21 | +import java.security.SecureRandom; |
| 22 | +import java.security.cert.Certificate; |
| 23 | +import java.security.cert.CertificateFactory; |
| 24 | +import java.util.logging.Level; |
| 25 | +import java.util.logging.Logger; |
| 26 | + |
| 27 | +import javax.net.ssl.SSLContext; |
| 28 | +import javax.net.ssl.SSLHandshakeException; |
| 29 | +import javax.net.ssl.SSLSocketFactory; |
| 30 | +import javax.net.ssl.TrustManagerFactory; |
| 31 | + |
| 32 | +import org.eclipse.paho.client.mqttv3.MqttClient; |
| 33 | +import org.eclipse.paho.client.mqttv3.MqttConnectOptions; |
| 34 | +import org.eclipse.paho.client.mqttv3.test.logging.LoggingUtilities; |
| 35 | +import org.eclipse.paho.client.mqttv3.test.utilities.Utility; |
| 36 | +import org.junit.Assert; |
| 37 | +import org.junit.BeforeClass; |
| 38 | +import org.junit.Test; |
| 39 | + |
| 40 | +/** |
| 41 | + * This test aims to run some basic SSL functionality tests of the MQTT client |
| 42 | + */ |
| 43 | + |
| 44 | +public class TLSHostnameVerificationTest { |
| 45 | + |
| 46 | + static final Class<?> cclass = TLSHostnameVerificationTest.class; |
| 47 | + private static final String className = cclass.getName(); |
| 48 | + private static final Logger log = Logger.getLogger(className); |
| 49 | + |
| 50 | + private static String serverHost; |
| 51 | + private static int serverPort; |
| 52 | + private static String serverIP; |
| 53 | + private static String certificateName; |
| 54 | + private static int websocketPort; |
| 55 | + |
| 56 | + /** |
| 57 | + * @throws Exception |
| 58 | + */ |
| 59 | + @BeforeClass |
| 60 | + public static void setUpBeforeClass() throws Exception { |
| 61 | + |
| 62 | + try { |
| 63 | + String methodName = Utility.getMethodName(); |
| 64 | + LoggingUtilities.banner(log, cclass, methodName); |
| 65 | + // Overriding with iot.eclipse.org for the time being. |
| 66 | + |
| 67 | + serverHost = "iot.eclipse.org"; |
| 68 | + serverIP = "198.41.30.241"; |
| 69 | + serverPort = 8883; |
| 70 | + websocketPort = 443; |
| 71 | + |
| 72 | + certificateName = "iot.eclipse.org.crt"; |
| 73 | + |
| 74 | + } catch (Exception exception) { |
| 75 | + log.log(Level.SEVERE, "caught exception:", exception); |
| 76 | + throw exception; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * This test checks that the Java 7 HTTPS Style Hostname Verification works with |
| 82 | + * the Paho Client. This will verify that the Hostname we are connecting to |
| 83 | + * matches the one recorded in the Certificate |
| 84 | + * |
| 85 | + * @throws Exception |
| 86 | + */ |
| 87 | + @Test(timeout = 10000) |
| 88 | + public void testValidHTTPSStyleHostnameVerification() throws Exception { |
| 89 | + |
| 90 | + SSLSocketFactory socketFactory = getSocketFactory(certificateName); |
| 91 | + |
| 92 | + MqttConnectOptions connOpts = new MqttConnectOptions(); |
| 93 | + connOpts.setHttpsHostnameVerificationEnabled(true); |
| 94 | + connOpts.setSocketFactory(socketFactory); |
| 95 | + String serverURI = "ssl://" + serverHost + ":" + serverPort; |
| 96 | + |
| 97 | + MqttClient mqttClient = new MqttClient(serverURI, MqttClient.generateClientId()); |
| 98 | + |
| 99 | + log.info("Connecting to: " + serverURI); |
| 100 | + mqttClient.connect(connOpts); |
| 101 | + |
| 102 | + Thread.sleep(2000); |
| 103 | + |
| 104 | + log.info("Disconnetting..."); |
| 105 | + mqttClient.disconnect(); |
| 106 | + mqttClient.close(); |
| 107 | + |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * This test checks that the Java 7 HTTPS Style Hostname Verification works with |
| 112 | + * the Paho Client. This will verify that when connecting to a server via the IP |
| 113 | + * address, the Hostname verification will fail |
| 114 | + * |
| 115 | + * @throws Exception |
| 116 | + */ |
| 117 | + @Test(timeout = 10000) |
| 118 | + public void testInvalidHTTPSStyleHostnameVerification() throws Exception { |
| 119 | + |
| 120 | + SSLSocketFactory socketFactory = getSocketFactory(certificateName); |
| 121 | + |
| 122 | + MqttConnectOptions connOpts = new MqttConnectOptions(); |
| 123 | + connOpts.setHttpsHostnameVerificationEnabled(true); |
| 124 | + connOpts.setSocketFactory(socketFactory); |
| 125 | + String serverIPURI = "ssl://" + serverIP + ":" + serverPort; |
| 126 | + |
| 127 | + MqttClient mqttClient = new MqttClient(serverIPURI, MqttClient.generateClientId()); |
| 128 | + |
| 129 | + log.info("Connecting to: " + serverIPURI); |
| 130 | + try { |
| 131 | + mqttClient.connect(connOpts); |
| 132 | + } catch (Exception ex) { |
| 133 | + // We want to make sure that the returned exception is an SSLHandshakeException |
| 134 | + Assert.assertEquals(SSLHandshakeException.class, ex.getCause().getClass()); |
| 135 | + log.info("Expected Exception thrown: " + ex.getCause().getClass()); |
| 136 | + } finally { |
| 137 | + mqttClient.close(); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + |
| 142 | + /** |
| 143 | + * This test checks that the Java 7 HTTPS Style Hostname Verification works with |
| 144 | + * the Paho Client. This will verify that the Hostname we are connecting to |
| 145 | + * matches the one recorded in the Certificate |
| 146 | + * |
| 147 | + * @throws Exception |
| 148 | + */ |
| 149 | + @Test(timeout = 10000) |
| 150 | + public void testValidWebSocketHTTPSStyleHostnameVerification() throws Exception { |
| 151 | + |
| 152 | + SSLSocketFactory socketFactory = getSocketFactory(certificateName); |
| 153 | + |
| 154 | + MqttConnectOptions connOpts = new MqttConnectOptions(); |
| 155 | + connOpts.setHttpsHostnameVerificationEnabled(true); |
| 156 | + connOpts.setSocketFactory(socketFactory); |
| 157 | + String serverURI = "wss://" + serverHost + ":" + websocketPort + "/ws"; |
| 158 | + |
| 159 | + MqttClient mqttClient = new MqttClient(serverURI, MqttClient.generateClientId()); |
| 160 | + |
| 161 | + log.info("Connecting to: " + serverURI); |
| 162 | + mqttClient.connect(connOpts); |
| 163 | + |
| 164 | + Thread.sleep(2000); |
| 165 | + |
| 166 | + log.info("Disconnetting..."); |
| 167 | + mqttClient.disconnect(); |
| 168 | + mqttClient.close(); |
| 169 | + |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * This test checks that the Java 7 HTTPS Style Hostname Verification works with |
| 174 | + * the Paho Client. This will verify that when connecting to a server via the IP |
| 175 | + * address, the Hostname verification will fail |
| 176 | + * |
| 177 | + * @throws Exception |
| 178 | + */ |
| 179 | + @Test(timeout = 10000) |
| 180 | + public void testInvalidWebSocketHTTPSStyleHostnameVerification() throws Exception { |
| 181 | + |
| 182 | + SSLSocketFactory socketFactory = getSocketFactory(certificateName); |
| 183 | + |
| 184 | + MqttConnectOptions connOpts = new MqttConnectOptions(); |
| 185 | + connOpts.setHttpsHostnameVerificationEnabled(true); |
| 186 | + connOpts.setSocketFactory(socketFactory); |
| 187 | + String serverIPURI = "wss://" + serverIP + ":" + websocketPort + "/ws"; |
| 188 | + |
| 189 | + |
| 190 | + MqttClient mqttClient = new MqttClient(serverIPURI, MqttClient.generateClientId()); |
| 191 | + |
| 192 | + log.info("Connecting to: " + serverIPURI); |
| 193 | + try { |
| 194 | + mqttClient.connect(connOpts); |
| 195 | + } catch (Exception ex) { |
| 196 | + // We want to make sure that the returned exception is an SSLHandshakeException |
| 197 | + Assert.assertEquals(SSLHandshakeException.class, ex.getCause().getClass()); |
| 198 | + log.info("Expected Exception thrown: " + ex.getCause().getClass()); |
| 199 | + } finally { |
| 200 | + mqttClient.close(); |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + |
| 205 | + |
| 206 | + private static SSLSocketFactory getSocketFactory(String certificateName) throws Exception { |
| 207 | + // Load the certificate from src/test/resources and create a Certificate object |
| 208 | + InputStream certStream = cclass.getClassLoader().getResourceAsStream(certificateName); |
| 209 | + CertificateFactory certFactory = CertificateFactory.getInstance("X509"); |
| 210 | + Certificate certificate = certFactory.generateCertificate(certStream); |
| 211 | + SSLContext sslContext = SSLContext.getInstance("TLS"); |
| 212 | + TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); |
| 213 | + KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); |
| 214 | + keyStore.load(null); |
| 215 | + keyStore.setCertificateEntry("alias", certificate); |
| 216 | + trustManagerFactory.init(keyStore); |
| 217 | + sslContext.init(null, trustManagerFactory.getTrustManagers(), new SecureRandom()); |
| 218 | + return sslContext.getSocketFactory(); |
| 219 | + } |
| 220 | +} |
0 commit comments