Search Unity

Does anyone know how to use m2mqtt on Unity3D (with Tls1.2)?

Discussion in 'Scripting' started by aditya_unity92, Nov 9, 2020.

  1. aditya_unity92

    aditya_unity92

    Joined:
    Aug 27, 2020
    Posts:
    5
    I am developing an AR app in Unity and I need to connect to an MQTT broker with TLS protocol enabled.

    I am able to connect to non-TLS enabled MQTT brokers successfully(In unity), and I can use the certificate the admin gave me with the MQTT explorer application to connect to the TLS enabled broker successfully. (the broker is using TLS 1.2)

    But when I attempt to connect to the TLS enabled server with the paho-m2mqtt library for c# in Unity (https://github.com/eclipse/paho.mqtt.m2mqtt), I get the following exception:

    Connection error: uPLibrary.Networking.M2Mqtt.Exceptions.MqttConnectionException: Exception connecting to the broker
    ---> System.ArgumentException: SSL/TLS protocol version not supported


    This is the code I am using to test the connection (I have the server certificate, client certificate, and client key stored at Assets/StreamingAssests, and I could be using the certs wrong when making the MqttClient, but this is how most people were connecting):

    Code (CSharp):
    1. public void connect(broker, port)
    2. {
    3.     string caCertLoc = Application.streamingAssetsPath + "/ca.crt";
    4.     string clientCertLoc = Application.streamingAssetsPath + "/clientCert.crt";
    5.  
    6.     X509Certificate2 caCert = new X509Certificate2(caCertLoc);
    7.  
    8.     X509Certificate2 clientCert = new X509Certificate2(clientCertLoc);
    9.  
    10.     MqttClient client = new MqttClient(broker, port, true,
    11.                                        X509Certificate.CreateFromCertFile(caCertLoc),
    12.                                        X509Certificate2.CreateFromCertFile(clientCertLoc),
    13.                                        MqttSslProtocols.TLSv1_2,
    14.                                        MyRemoteCertificateValidationCallback);
    15.  
    16.     string clientId = Guid.NewGuid().ToString();
    17.  
    18.     try
    19.     {
    20.         client.Connect(clientId);
    21.     }
    22.     catch (Exception e)
    23.     {
    24.         UnityEngine.Debug.LogError("Connection error: " + e);
    25.     }
    26. }
    I would appreciate any help as I am completely stuck on this.
     
  2. SimPio

    SimPio

    Joined:
    Jan 6, 2022
    Posts:
    1
    Did you find any solution to this problem? Facing the same issue