2014-06-11 23:40:19 +02:00
|
|
|
/*
|
2017-03-19 22:01:23 +01:00
|
|
|
* Copyright (c) 2012-2017 The original author or authors
|
2014-06-11 23:40:19 +02:00
|
|
|
* ------------------------------------------------------
|
|
|
|
|
* All rights reserved. This program and the accompanying materials
|
|
|
|
|
* are made available under the terms of the Eclipse Public License v1.0
|
|
|
|
|
* and Apache License v2.0 which accompanies this distribution.
|
|
|
|
|
*
|
|
|
|
|
* The Eclipse Public License is available at
|
|
|
|
|
* http://www.eclipse.org/legal/epl-v10.html
|
|
|
|
|
*
|
|
|
|
|
* The Apache License v2.0 is available at
|
|
|
|
|
* http://www.opensource.org/licenses/apache2.0.php
|
|
|
|
|
*
|
|
|
|
|
* You may elect to redistribute this code under either of these licenses.
|
|
|
|
|
*/
|
2017-02-01 16:17:40 +01:00
|
|
|
|
2015-11-20 20:54:27 +01:00
|
|
|
package io.moquette.server;
|
2014-05-22 23:53:58 +02:00
|
|
|
|
2018-04-25 19:39:12 +02:00
|
|
|
import static java.nio.charset.StandardCharsets.UTF_8;
|
2015-09-11 14:36:32 +09:00
|
|
|
import static org.junit.Assert.assertFalse;
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
import java.security.KeyManagementException;
|
|
|
|
|
import java.security.KeyStore;
|
|
|
|
|
import java.security.KeyStoreException;
|
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
|
import java.security.UnrecoverableKeyException;
|
|
|
|
|
import java.security.cert.CertificateException;
|
|
|
|
|
import java.util.Properties;
|
|
|
|
|
import javax.net.ssl.KeyManagerFactory;
|
|
|
|
|
import javax.net.ssl.SSLContext;
|
|
|
|
|
import javax.net.ssl.SSLSocketFactory;
|
|
|
|
|
import javax.net.ssl.TrustManager;
|
|
|
|
|
import javax.net.ssl.TrustManagerFactory;
|
2015-12-08 14:04:35 +01:00
|
|
|
import io.moquette.BrokerConstants;
|
2014-05-22 23:53:58 +02:00
|
|
|
import org.eclipse.paho.client.mqttv3.IMqttClient;
|
|
|
|
|
import org.eclipse.paho.client.mqttv3.MqttClient;
|
|
|
|
|
import org.eclipse.paho.client.mqttv3.MqttClientPersistence;
|
|
|
|
|
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
|
2015-09-11 14:36:32 +09:00
|
|
|
import org.eclipse.paho.client.mqttv3.MqttMessage;
|
|
|
|
|
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
|
2014-05-22 23:53:58 +02:00
|
|
|
import org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence;
|
|
|
|
|
import org.junit.After;
|
2017-08-09 14:47:36 +02:00
|
|
|
import org.junit.AfterClass;
|
2014-05-22 23:53:58 +02:00
|
|
|
import org.junit.Before;
|
|
|
|
|
import org.junit.BeforeClass;
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check that Moquette could also handle SSL.
|
|
|
|
|
*/
|
|
|
|
|
public class ServerIntegrationSSLTest {
|
2017-02-01 16:17:40 +01:00
|
|
|
|
2014-05-22 23:53:58 +02:00
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(ServerIntegrationSSLTest.class);
|
|
|
|
|
|
|
|
|
|
Server m_server;
|
|
|
|
|
static MqttClientPersistence s_dataStore;
|
|
|
|
|
|
|
|
|
|
IMqttClient m_client;
|
2016-03-06 19:03:01 +01:00
|
|
|
MessageCollector m_callback;
|
2014-05-22 23:53:58 +02:00
|
|
|
|
2017-08-09 14:47:36 +02:00
|
|
|
static String backup;
|
|
|
|
|
|
2014-05-22 23:53:58 +02:00
|
|
|
@BeforeClass
|
|
|
|
|
public static void beforeTests() {
|
|
|
|
|
String tmpDir = System.getProperty("java.io.tmpdir");
|
|
|
|
|
s_dataStore = new MqttDefaultFilePersistence(tmpDir);
|
2017-08-09 14:47:36 +02:00
|
|
|
backup = System.getProperty("moquette.path");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@AfterClass
|
|
|
|
|
public static void afterTests() {
|
|
|
|
|
if (backup == null)
|
|
|
|
|
System.clearProperty("moquette.path");
|
|
|
|
|
else
|
|
|
|
|
System.setProperty("moquette.path", backup);
|
2014-05-22 23:53:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void startServer() throws IOException {
|
|
|
|
|
String file = getClass().getResource("/").getPath();
|
|
|
|
|
System.setProperty("moquette.path", file);
|
|
|
|
|
m_server = new Server();
|
2015-03-14 17:31:11 +01:00
|
|
|
|
|
|
|
|
Properties sslProps = new Properties();
|
2015-12-08 14:04:35 +01:00
|
|
|
sslProps.put(BrokerConstants.SSL_PORT_PROPERTY_NAME, "8883");
|
|
|
|
|
sslProps.put(BrokerConstants.JKS_PATH_PROPERTY_NAME, "serverkeystore.jks");
|
|
|
|
|
sslProps.put(BrokerConstants.KEY_STORE_PASSWORD_PROPERTY_NAME, "passw0rdsrv");
|
|
|
|
|
sslProps.put(BrokerConstants.KEY_MANAGER_PASSWORD_PROPERTY_NAME, "passw0rdsrv");
|
|
|
|
|
sslProps.put(BrokerConstants.PERSISTENT_STORE_PROPERTY_NAME, IntegrationUtils.localMapDBPath());
|
2015-03-14 17:31:11 +01:00
|
|
|
m_server.startServer(sslProps);
|
2014-05-22 23:53:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Before
|
|
|
|
|
public void setUp() throws Exception {
|
2015-04-07 07:51:44 +02:00
|
|
|
String dbPath = IntegrationUtils.localMapDBPath();
|
|
|
|
|
File dbFile = new File(dbPath);
|
|
|
|
|
assertFalse(String.format("The DB storagefile %s already exists", dbPath), dbFile.exists());
|
2015-03-14 17:31:11 +01:00
|
|
|
|
2014-05-22 23:53:58 +02:00
|
|
|
startServer();
|
|
|
|
|
|
|
|
|
|
m_client = new MqttClient("ssl://localhost:8883", "TestClient", s_dataStore);
|
2017-02-01 16:17:40 +01:00
|
|
|
// m_client = new MqttClient("ssl://test.mosquitto.org:8883", "TestClient", s_dataStore);
|
|
|
|
|
|
2016-03-06 19:03:01 +01:00
|
|
|
m_callback = new MessageCollector();
|
2014-05-22 23:53:58 +02:00
|
|
|
m_client.setCallback(m_callback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@After
|
|
|
|
|
public void tearDown() throws Exception {
|
2015-04-08 23:19:23 +02:00
|
|
|
if (m_client != null && m_client.isConnected()) {
|
2014-05-22 23:53:58 +02:00
|
|
|
m_client.disconnect();
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-08 23:19:23 +02:00
|
|
|
if (m_server != null) {
|
|
|
|
|
m_server.stopServer();
|
|
|
|
|
}
|
|
|
|
|
String dbPath = IntegrationUtils.localMapDBPath();
|
|
|
|
|
File dbFile = new File(dbPath);
|
2014-05-22 23:53:58 +02:00
|
|
|
if (dbFile.exists()) {
|
|
|
|
|
dbFile.delete();
|
|
|
|
|
}
|
|
|
|
|
assertFalse(dbFile.exists());
|
|
|
|
|
}
|
2017-02-01 16:17:40 +01:00
|
|
|
|
2014-05-22 23:53:58 +02:00
|
|
|
@Test
|
|
|
|
|
public void checkSupportSSL() throws Exception {
|
|
|
|
|
LOG.info("*** checkSupportSSL ***");
|
|
|
|
|
SSLSocketFactory ssf = configureSSLSocketFactory();
|
2017-02-01 16:17:40 +01:00
|
|
|
|
2014-05-22 23:53:58 +02:00
|
|
|
MqttConnectOptions options = new MqttConnectOptions();
|
|
|
|
|
options.setSocketFactory(ssf);
|
|
|
|
|
m_client.connect(options);
|
|
|
|
|
m_client.subscribe("/topic", 0);
|
|
|
|
|
m_client.disconnect();
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-11 14:36:32 +09:00
|
|
|
@Test
|
|
|
|
|
public void checkSupportSSLForMultipleClient() throws Exception {
|
|
|
|
|
LOG.info("*** checkSupportSSLForMultipleClient ***");
|
|
|
|
|
SSLSocketFactory ssf = configureSSLSocketFactory();
|
2017-02-01 16:17:40 +01:00
|
|
|
|
2015-09-11 14:36:32 +09:00
|
|
|
MqttConnectOptions options = new MqttConnectOptions();
|
|
|
|
|
options.setSocketFactory(ssf);
|
|
|
|
|
m_client.connect(options);
|
|
|
|
|
m_client.subscribe("/topic", 0);
|
2017-02-01 16:17:40 +01:00
|
|
|
|
2015-09-11 14:36:32 +09:00
|
|
|
MqttClient secondClient = new MqttClient("ssl://localhost:8883", "secondTestClient", new MemoryPersistence());
|
|
|
|
|
MqttConnectOptions secondClientOptions = new MqttConnectOptions();
|
|
|
|
|
secondClientOptions.setSocketFactory(ssf);
|
|
|
|
|
secondClient.connect(secondClientOptions);
|
2018-04-25 19:39:12 +02:00
|
|
|
secondClient.publish("/topic", new MqttMessage("message".getBytes(UTF_8)));
|
2015-09-11 14:36:32 +09:00
|
|
|
secondClient.disconnect();
|
2017-02-01 16:17:40 +01:00
|
|
|
|
2015-09-11 14:36:32 +09:00
|
|
|
m_client.disconnect();
|
|
|
|
|
}
|
2017-02-01 16:17:40 +01:00
|
|
|
|
2014-05-22 23:53:58 +02:00
|
|
|
/**
|
|
|
|
|
* keystore generated into test/resources with command:
|
2017-02-01 16:17:40 +01:00
|
|
|
*
|
|
|
|
|
* keytool -keystore clientkeystore.jks -alias testclient -genkey -keyalg RSA -> mandatory to
|
|
|
|
|
* put the name surname -> password is passw0rd -> type yes at the end
|
|
|
|
|
*
|
|
|
|
|
* to generate the crt file from the keystore -- keytool -certreq -alias testclient -keystore
|
|
|
|
|
* clientkeystore.jks -file testclient.csr
|
|
|
|
|
*
|
2014-05-22 23:53:58 +02:00
|
|
|
* keytool -export -alias testclient -keystore clientkeystore.jks -file testclient.crt
|
2017-02-01 16:17:40 +01:00
|
|
|
*
|
|
|
|
|
* to import an existing certificate: keytool -keystore clientkeystore.jks -import -alias
|
|
|
|
|
* testclient -file testclient.crt -trustcacerts
|
2014-05-22 23:53:58 +02:00
|
|
|
*/
|
2017-02-01 16:17:40 +01:00
|
|
|
private SSLSocketFactory configureSSLSocketFactory() throws KeyManagementException, NoSuchAlgorithmException,
|
|
|
|
|
UnrecoverableKeyException, IOException, CertificateException, KeyStoreException {
|
2014-05-22 23:53:58 +02:00
|
|
|
KeyStore ks = KeyStore.getInstance("JKS");
|
|
|
|
|
InputStream jksInputStream = getClass().getClassLoader().getResourceAsStream("clientkeystore.jks");
|
|
|
|
|
ks.load(jksInputStream, "passw0rd".toCharArray());
|
|
|
|
|
|
|
|
|
|
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
|
|
|
|
|
kmf.init(ks, "passw0rd".toCharArray());
|
|
|
|
|
|
|
|
|
|
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
|
|
|
|
|
tmf.init(ks);
|
|
|
|
|
|
2017-02-01 16:17:40 +01:00
|
|
|
SSLContext sc = SSLContext.getInstance("TLS");
|
|
|
|
|
TrustManager[] trustManagers = tmf.getTrustManagers();
|
|
|
|
|
sc.init(kmf.getKeyManagers(), trustManagers, null);
|
2014-05-22 23:53:58 +02:00
|
|
|
|
|
|
|
|
SSLSocketFactory ssf = sc.getSocketFactory();
|
|
|
|
|
return ssf;
|
|
|
|
|
}
|
|
|
|
|
}
|