/******************************************************************************************
* This example code is provided for illustrative purposes only and provided 'AS IS'
* without warranty of any kind.
*
* Copyright ©2014 Chrysler Group LLC. All Rights Reserved. All information contained
* herein is,and remains the property of Chrysler Group LLC and its suppliers, if any.
* The intellectual and technical concepts contained herein are proprietary to Chrysler
* Group LLC and its suppliers and may be covered by U.S. and Foreign Patents, patents in
* process, and are protected by trade secret or copyright law. Dissemination of this
* information or reproduction of this material is strictly forbidden unless prior written
* permission is obtained from Chrysler Group LLC.
*
* Chrysler, Jeep, Dodge, Ram, SRT, Mopar and the Pentastar logo are registered trademarks of
* Chrysler Group LLC. FIAT is a registered trademark of Fiat group Marketing & Corporate
* Communication S.p.A. used under license by Chrysler Group LLC.
*******************************************************************************************/
package com.fca.uconnect.global.client;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.SecureRandom;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import com.amazonaws.services.iot.client.AWSIotException;
import com.amazonaws.services.iot.client.AWSIotMqttClient;
public class ExampleForMQTTUsingKeyStore {
private AWSIotMqttClient mqttClient;
public void process() {
KeyStorePasswordPair pair = getKeyStorePasswordPair("certificateFile", "privateKeyFile","KeyAlgorithm");
mqttClient = new AWSIotMqttClient("clientEndpoint", "TBMClientId", pair.keyStore, pair.keyPassword);
mqttClient.setConnectionTimeout(5000); // In this example Time out for establishing Connection is set to 5 Seconds.
mqttClient.setKeepAliveInterval(1440000); // In this example Keep Alive is set to 24 minutes.
try {
mqttClient.connect();
} catch (AWSIotException e) {
e.printStackTrace();
}
}
public static KeyStorePasswordPair getKeyStorePasswordPair(String certificateFile, String privateKeyFile,
String keyAlgorithm) {
if (certificateFile == null || privateKeyFile == null) {
System.out.println("Certificate or private key file missing");
return null;
}
Certificate certificate = null;
File file = new File(certificateFile);
if (!file.exists()) {
System.out.println("Certificate file not found: " + certificateFile);
return null;
}
try (BufferedInputStream stream = new BufferedInputStream(new FileInputStream(file))) {
CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
certificate = certFactory.generateCertificate(stream);
} catch (IOException | CertificateException e) {
System.out.println("Failed to load certificate file " + certificateFile);
}
//get the Private key for private File , It can be implemented with Java.security Package
PrivateKey privateKey = null;
//get KeyStorePasswordPair , It can be implemented with Java.security Package.
return keystorePasswordPair;
}
public static class KeyStorePasswordPair {
public KeyStore keyStore;
public String keyPassword;
public KeyStorePasswordPair(KeyStore keyStore, String keyPassword) {
this.keyStore = keyStore;
this.keyPassword = keyPassword;
}
}
}
Download Example Code here