|
| 1 | +/** |
| 2 | + * Copyright (c) 2011, 2014 Eurotech and/or its affiliates |
| 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 | + * which accompanies this distribution, and is available at |
| 7 | + * http://www.eclipse.org/legal/epl-v10.html |
| 8 | + * |
| 9 | + * Contributors: |
| 10 | + * Cristiano De Alti - Eurotech (Initial contribution) |
| 11 | + * James Sutton - IBM (Fixing Copyright header and adding getSocketFactory) |
| 12 | + */ |
| 13 | + |
| 14 | +package org.eclipse.paho.client.mqttv3.test; |
| 15 | + |
| 16 | +import java.io.IOException; |
| 17 | +import java.io.InputStream; |
| 18 | +import java.security.KeyStore; |
| 19 | +import java.security.SecureRandom; |
| 20 | +import java.security.cert.Certificate; |
| 21 | +import java.security.cert.CertificateFactory; |
| 22 | +import java.util.Arrays; |
| 23 | +import java.util.Enumeration; |
| 24 | +import java.util.logging.Level; |
| 25 | +import java.util.logging.Logger; |
| 26 | + |
| 27 | +import javax.net.ssl.SSLContext; |
| 28 | +import javax.net.ssl.SSLSessionContext; |
| 29 | +import javax.net.ssl.SSLSocket; |
| 30 | +import javax.net.ssl.SSLSocketFactory; |
| 31 | +import javax.net.ssl.TrustManagerFactory; |
| 32 | + |
| 33 | +import org.eclipse.paho.client.mqttv3.MqttClient; |
| 34 | +import org.eclipse.paho.client.mqttv3.MqttConnectOptions; |
| 35 | +import org.eclipse.paho.client.mqttv3.test.logging.LoggingUtilities; |
| 36 | +import org.eclipse.paho.client.mqttv3.test.properties.TestProperties; |
| 37 | +import org.eclipse.paho.client.mqttv3.test.utilities.Utility; |
| 38 | +import org.junit.BeforeClass; |
| 39 | +import org.junit.Test; |
| 40 | + |
| 41 | + |
| 42 | +/** |
| 43 | + * This test aims to run some basic SSL functionality tests of the MQTT client |
| 44 | + */ |
| 45 | + |
| 46 | +public class SSLSessionResumptionTest { |
| 47 | + |
| 48 | + static final Class<?> cclass = SSLSessionResumptionTest.class; |
| 49 | + private static final String className = cclass.getName(); |
| 50 | + private static final Logger log = Logger.getLogger(className); |
| 51 | + |
| 52 | + |
| 53 | + private static String serverURI; |
| 54 | + private static String serverHost; |
| 55 | + private static int serverPort; |
| 56 | + private static final String certificateName = "iot.eclipse.org.crt"; |
| 57 | + |
| 58 | + |
| 59 | + /** |
| 60 | + * @throws Exception |
| 61 | + */ |
| 62 | + @BeforeClass |
| 63 | + public static void setUpBeforeClass() throws Exception { |
| 64 | + |
| 65 | + try { |
| 66 | + String methodName = Utility.getMethodName(); |
| 67 | + LoggingUtilities.banner(log, cclass, methodName); |
| 68 | + serverURI = "ssl://" + TestProperties.getServerURI().getHost(); |
| 69 | + serverHost = TestProperties.getServerURI().getHost(); |
| 70 | + serverPort = TestProperties.getServerSSLPort(); |
| 71 | + |
| 72 | + } |
| 73 | + catch (Exception exception) { |
| 74 | + log.log(Level.SEVERE, "caught exception:", exception); |
| 75 | + throw exception; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * This test involves inspecting the SSL debug log |
| 81 | + * and the Wireshark capture. |
| 82 | + * |
| 83 | + * Paho defeats the default SSL session caching which |
| 84 | + * allows to have abbreviated SSL handshakes on |
| 85 | + * following connections. |
| 86 | + * |
| 87 | + * @throws Exception |
| 88 | + */ |
| 89 | + @Test |
| 90 | + public void testSSLSessionInvalidated() throws Exception { |
| 91 | + //System.setProperty("javax.net.debug", "all"); |
| 92 | + |
| 93 | + SSLSocketFactory factory = getSocketFactory(certificateName); |
| 94 | + |
| 95 | + MqttConnectOptions options = new MqttConnectOptions(); |
| 96 | + options.setServerURIs(new String[] {serverURI}); |
| 97 | + options.setKeepAliveInterval(60); |
| 98 | + options.setSocketFactory(factory); |
| 99 | + |
| 100 | + MqttClient mqttClient = new MqttClient(serverURI, MqttClient.generateClientId()); |
| 101 | + |
| 102 | + |
| 103 | + log.info("Connecting to: " + serverURI); |
| 104 | + mqttClient.connect(options); |
| 105 | + |
| 106 | + Thread.sleep(2000); |
| 107 | + |
| 108 | + log.info("Disconnetting..."); |
| 109 | + mqttClient.disconnect(); |
| 110 | + |
| 111 | + Thread.sleep(2000); |
| 112 | + |
| 113 | + log.info("Connecting again... Paho will not be able to perform an abbreviated SSL handshake"); |
| 114 | + mqttClient.connect(options); |
| 115 | + |
| 116 | + Thread.sleep(2000); |
| 117 | + |
| 118 | + log.info("Disconnetting..."); |
| 119 | + mqttClient.disconnect(); |
| 120 | + |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * This test involves inspecting the SSL debug log |
| 125 | + * and the Wireshark capture. |
| 126 | + * |
| 127 | + * By default, Java caches SSL sessions which |
| 128 | + * allows to have abbreviated SSL handshakes on |
| 129 | + * following connections. |
| 130 | + * |
| 131 | + * @throws Exception |
| 132 | + */ |
| 133 | + @Test |
| 134 | + public void testSSLSessionCached() throws Exception { |
| 135 | + // System.setProperty("javax.net.debug", "all"); |
| 136 | + |
| 137 | + SSLSocketFactory factory = getSocketFactory(certificateName); |
| 138 | + |
| 139 | + |
| 140 | + log.info("Do handshake..."); |
| 141 | + doHandshake(factory, serverHost, serverPort); |
| 142 | + |
| 143 | + log.info("Done! Redo handshake... An abbreviated SSL handshake will be performed"); |
| 144 | + doHandshake(factory, serverHost, serverPort); |
| 145 | + |
| 146 | + log.info("Done!"); |
| 147 | + } |
| 148 | + |
| 149 | + private static void doHandshake(SSLSocketFactory factory, String host, int port) { |
| 150 | + SSLSocket socket = null; |
| 151 | + try { |
| 152 | + socket = (SSLSocket) factory.createSocket(host, port); |
| 153 | + |
| 154 | + socket.startHandshake(); |
| 155 | + |
| 156 | + SSLSessionContext ctx = socket.getSession().getSessionContext(); |
| 157 | + if (ctx != null) { |
| 158 | + Enumeration<byte[]> ids = ctx.getIds(); |
| 159 | + while (ids.hasMoreElements()) { |
| 160 | + byte[] id = ids.nextElement(); |
| 161 | + log.info("Session ID: " + Arrays.toString(id)); |
| 162 | + log.info("Cypher suite: " + ctx.getSession(id).getCipherSuite()); |
| 163 | + } |
| 164 | + } else { |
| 165 | + log.info("null SSLSessionContext"); |
| 166 | + } |
| 167 | + } catch (Exception e) { |
| 168 | + e.printStackTrace(); |
| 169 | + } finally { |
| 170 | + if (socket != null) { |
| 171 | + try { |
| 172 | + socket.close(); |
| 173 | + } catch (IOException e) { |
| 174 | + e.printStackTrace(); |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + private static SSLSocketFactory getSocketFactory(String certificateName) throws Exception { |
| 181 | + InputStream certStream = SSLSessionResumptionTest.class.getClassLoader().getResourceAsStream(certificateName); |
| 182 | + CertificateFactory certFactory = CertificateFactory.getInstance("X509"); |
| 183 | + Certificate certificate = certFactory.generateCertificate(certStream); |
| 184 | + SSLContext sslContext = SSLContext.getInstance("TLS"); |
| 185 | + TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); |
| 186 | + KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); |
| 187 | + keyStore.load(null); |
| 188 | + keyStore.setCertificateEntry("alias", certificate); |
| 189 | + trustManagerFactory.init(keyStore); |
| 190 | + sslContext.init(null, trustManagerFactory.getTrustManagers(), new SecureRandom()); |
| 191 | + return sslContext.getSocketFactory(); |
| 192 | + } |
| 193 | +} |
0 commit comments