# TLS 1.3 over MQTT broker and client

**URL:** <https://community.hivemq.com/t/tls-1-3-over-mqtt-broker-and-client/2030>\
**Category:** Uncategorized\
**Created:** [May 1, 2023, 12:34am UTC](https://community.hivemq.com/t/tls-1-3-over-mqtt-broker-and-client/2030 "2023-05-01T00:34:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sinem](https://avatars.discourse-cdn.com/v4/letter/s/c77e96/32.png) [@sinem](https://community.hivemq.com/u/sinem)\
**Post date:** [May 1, 2023, 12:34am UTC](https://community.hivemq.com/t/tls-1-3-over-mqtt-broker-and-client/2030/1 "2023-05-01T00:34:36Z")

</div>

I am using rumqttd as a broker, I keep receiving errors when I try implementing TLS 1.3 over my client code which is  
use tokio::{task, time};

use rumqttc::{self, AsyncClient, MqttOptions, QoS};  
use std::error::Error;  
use std::time::Duration;

#[tokio::main(worker\_threads = 1)]  
async fn main() → Result\<(), Box\> {  
pretty\_env\_logger::init();  
// color\_backtrace::install();

```
let mut broker = "127.0.0.1";
if env!("HOME") == "/root" {
  broker = "mqtt-broker";
}

let mut mqttoptions = MqttOptions::new("test-1", broker, 1883);
mqttoptions.set_keep_alive(Duration::from_secs(5));

let (client, mut eventloop) = AsyncClient::new(mqttoptions, 19);
task::spawn(async move {
    requests(client).await;
    time::sleep(Duration::from_secs(3)).await;
});

loop {
    let event = eventloop.poll().await;
    match &event {
        Ok(v) => {
            println!("Event = {v:?}");
        }
        Err(e) => {
            println!("Error = {e:?}");
            return Ok(());
        }
    }
}

```

}

async fn requests(client: AsyncClient) {  
client  
.subscribe(“hello/world”, QoS::AtMostOnce)  
.await  
.unwrap();

```
for i in 1..=19 {
    client
        .publish("hello/world", QoS::AtMostOnce, false, vec![1; i])
        .await
        .unwrap();

    time::sleep(Duration::from_secs(1)).await;
}

time::sleep(Duration::from_secs(120)).await;

```

}

Can someone please help me to modify this code and provide me the cargo toml?

This is where I came so far but I am receiving 10 errors for this version

use std::fs;  
use std::io::BufReader;  
use std::net::TcpStream;  
use std::path::Path;

use futures\_util::future::{FutureExt, TryFutureExt};  
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader as AsyncBufReader};  
use tokio::net::TcpStream as AsyncTcpStream;  
use tokio\_rustls::client::TlsStream;  
use tokio\_rustls::rustls::internal::pemfile::{certs, pkcs8\_private\_keys};  
use tokio\_rustls::rustls::{ClientConfig, NoClientAuth, ServerCertVerified, TLSError};  
use tokio\_rustls::TlsConnector;  
use mqtt3::RequestError;  
use mqtt3::{MqttRead, MqttWrite, QoS};

#[tokio::main]  
async fn main() → Result\<(), Box\> {  
// Load the certificates and private key  
let certfile = &mut BufReader::new(fs::File::open(“certs/cert.pem”)?);  
let keyfile = &mut BufReader::new(fs::File::open(“certs/key.pem”)?);  
let cert\_chain = certs(certfile).unwrap();  
let mut keys = pkcs8\_private\_keys(keyfile).unwrap();  
let key = keys.remove(0);

```
// Create the TLS configuration
let mut client_config = ClientConfig::new();
client_config
    .set_single_client_cert(cert_chain, key)
    .map_err(|e| RequestError::SslError(e.to_string()))?;
client_config
    .set_protocols(&[b"mqtt".to_vec()]);
client_config
    .dangerous()
    .set_certificate_verifier(Arc::new(NoClientAuth::new()));

// Create the TLS connector
let tls_connector = TlsConnector::from(Arc::new(client_config));

// Connect to the broker
let mut mqttoptions = mqtt3::MqttOptions::new("test-1", "localhost", 8883);
mqttoptions.set_keep_alive(5);
mqttoptions.set_clean_session(true);

// Create a new AsyncClient with the TLS connector
let stream = AsyncTcpStream::connect("localhost:8883").await?;
let stream = tls_connector.connect("localhost", stream).await?;

let mut client = mqtt3::AsyncClient::new(stream, 10);
let mut eventloop = client.eventloop();

// Connect to the broker
let (ack, _) = eventloop.connect(mqttoptions).await?;
println!("Connected: {:?}", ack);

// Subscribe to a topic
eventloop
    .subscribe("test", QoS::AtMostOnce)
    .await
    .unwrap();

// Publish a message to the topic
eventloop
    .publish("test", QoS::AtMostOnce, false, "Hello, world!".to_string())
    .await
    .unwrap();

// Read messages from the topic
let mut stream = eventloop.stream();
while let Some(msg) = stream.try_next().await? {
    println!("Received: {:?}", msg);
}

Ok(())

```

}

---

<div class="post-metadata">

**Author:** ![AaronTLFranz](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.hivemq.com/aarontlfranz/32/638_2.png) [@AaronTLFranz](https://community.hivemq.com/u/AaronTLFranz)\
**Post date:** [May 1, 2023, 12:49pm UTC](https://community.hivemq.com/t/tls-1-3-over-mqtt-broker-and-client/2030/2 "2023-05-01T12:49:27Z")

</div>

Hello @sinem ,

Thank you for the outreach and interested in HiveMQ!

One detail I wanted to confirm first is if TLS version 1.3 has been specified as the protocol version on your broker. This will ensure that TLS 1.3 is supported by the broker, as well. Some more information, including a code example using the HiveMQ Client, is available in this [thread](https://community.hivemq.com/t/tls1-3-hivemq-broker/1013/3).

Best,  
Aaron from HiveMQ Team

---

<div class="post-metadata">

**Author:** ![sinem](https://avatars.discourse-cdn.com/v4/letter/s/c77e96/32.png) [@sinem](https://community.hivemq.com/u/sinem)\
**Post date:** [May 1, 2023, 2:34pm UTC](https://community.hivemq.com/t/tls-1-3-over-mqtt-broker-and-client/2030/3 "2023-05-01T14:34:36Z")

</div>

Dear AaronTLFranz,  
By any chance is there a client code written in RUST language?

---

<div class="post-metadata">

**Author:** ![AaronTLFranz](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.hivemq.com/aarontlfranz/32/638_2.png) [@AaronTLFranz](https://community.hivemq.com/u/AaronTLFranz)\
**Post date:** [May 1, 2023, 3:17pm UTC](https://community.hivemq.com/t/tls-1-3-over-mqtt-broker-and-client/2030/4 "2023-05-01T15:17:19Z")

</div>

Hello @sinem ,

There are client libraries that exist for RUST, though they are not maintained by HiveMQ. For example, the Eclipse Paho Rust Client Library can be found [here](https://github.com/eclipse/paho.mqtt.rust).

Best,  
Aaron from HiveMQ Team
