title: Connecting to an MQTT broker using TLS with a Pre-shared Key
body: <p>I am trying to connect to an MQTT broker using a Java console application, with the broker only supporting TLS with a pre-shared key (not SSL or other client certificate file options). I have the following dependencies:</p>
  
  <pre><code>&lt;dependency&gt;
      &lt;groupId&gt;org.eclipse.paho&lt;/groupId&gt;
      &lt;artifactId&gt;org.eclipse.paho.client.mqttv3&lt;/artifactId&gt;
      &lt;version&gt;1.1.0&lt;/version&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;org.bouncycastle&lt;/groupId&gt;
      &lt;artifactId&gt;bcprov-jdk15on&lt;/artifactId&gt;
      &lt;version&gt;1.56&lt;/version&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>I am using the following page as a starting point:
  <a href="http://www.hivemq.com/blog/mqtt-client-library-encyclopedia-eclipse-paho-java" rel="nofollow noreferrer">http://www.hivemq.com/blog/mqtt-client-library-encyclopedia-eclipse-paho-java</a></p>
  
  <p>And they have the following section for an SSL connection:</p>
  
  <pre><code>MqttClient client = new MqttClient("ssl://yourbroker:8883", MqttClient.generateClientId(), new MemoryPersistence());
  
  SSLContext sslContext = SSLContext.getInstance("SSL");
  TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
  KeyStore keyStore = readKeyStore();
  trustManagerFactory.init(keyStore);
  sslContext.init(null, trustManagerFactory.getTrustManagers(), new SecureRandom());
  
  MqttConnectOptions options = new MqttConnectOptions();
  options.setSocketFactory(sslContext.getSocketFactory());
  
  client.connect(options);
  </code></pre>
  
  <p>What I really need is a sample that uses TLS with a pre-shared key, I have done quite a bit of searching and can find a TLS PSK sample (<a href="http://tiebing.blogspot.com.au/2013/09/java-bouncy-castle-tls-psk-example.html" rel="nofollow noreferrer">http://tiebing.blogspot.com.au/2013/09/java-bouncy-castle-tls-psk-example.html</a>) but do not know how to connect the MQTT client to that TLS PSK example.</p>
  
  <p>Any assistance would be greatly appreciated, thanks!</p>