Client support for SNI

Hi,

I am trying to implement an MQTT client using HiveMQ Client java. This client connects to a MQTT broker (fqdn: broker.server.com) through a proxy (IP 40.40.40.40).

I will need to specify the SNI for the SSL handshake to work.

Something like:

SSLParameters sslParams = sslSocket.getSSLParameters();
SNIHostName sniHostName = new SNIHostName(“broker.server.com”); // The specific hostname for SNI
sslParams.setServerNames(Collections.singletonList(sniHostName));
sslSocket.setSSLParameters(sslParams)

I have looked a lot in HiveMQ java client but I couldn’t find a way to specify the SNI hostName in the HiveMQ client ssl configuration.

Anyone has an idea on how to implement this.

Thanks,

Hi @maratusa,

Welcome aboard. Exploring MQTT and HiveMQ can be a fun journey, and we’re here to help you along the way.

What kind of proxy is the 40.40.40.40? If it is directly forwarding TCP to the remote server, connect to the proxy and disable the server certificate validation.

package org.hivemq.examples.client.proxy;
import com.hivemq.client.mqtt.MqttClient;
import com.hivemq.client.mqtt.mqtt5.Mqtt5BlockingClient;

import static com.hivemq.client.mqtt.MqttGlobalPublishFilter.ALL;
import static java.nio.charset.StandardCharsets.UTF_8;

public class Main {

    public static void main(String[] args) throws Exception {

        final String proxyHost = "40.40.40.40";
        final int proxyPort = 8883;
        final String username = "MQTT_USER";
        final String password = "MQTT_PASSWORD";


        final Mqtt5BlockingClient client = MqttClient.builder()
                .useMqttVersion5()
                .identifier("hivemq-mqtt-client-super")
                .serverHost(proxyHost)
                .serverPort(proxyPort)
                .sslConfig()
                .hostnameVerifier((s, sslSession) -> true)
                .applySslConfig()
                .buildBlocking();

        try {
            client.connectWith()
                    .simpleAuth()
                    .username(username)
                    .password(UTF_8.encode(password))
                    .applySimpleAuth()
                    .send();

            System.out.println("Connected successfully");

            client.subscribeWith()
                    .topicFilter("my/test/topic")
                    .send();

            client.toAsync().publishes(ALL, publish -> {
                System.out.println("Received message: " +
                        publish.getTopic() + " -> " +
                        UTF_8.decode(publish.getPayload().get()));
                // disconnect the client after a message was received
                client.disconnect();
            });

            // publish a message to the topic "my/test/topic"
            client.publishWith()
                    .topic("my/test/topic")
                    .payload(UTF_8.encode("Hello"))
                    .send();
        } catch (Exception e) {
            System.err.println("Connection failed: " + e.getMessage());
            e.printStackTrace();
        }
    }
}

I hope this helps.

Best,
Dasha from The HiveMQ Team