eclipse-paho / paho.mqtt.m2mqtt

Eclipse Public License 1.0
521 stars 303 forks source link

bidirectional SSL authentication #102

Open tangqiwen opened 5 years ago

tangqiwen commented 5 years ago

I have implemented bidirectional SSL authentication in java. When implementing bidirectional SSL authentication in java, it needs one more file (ca. crt, client. crt, client. key) I want to use m2mqtt to implement two-way SSL authentication. But the following code doesn't work. The following code uses only two files. How can I implement bidirectional SSL authentication? Thank you!!

static void testTwo() {

        string content = "hello mqtt";
        string topic = "test/test";
        X509Certificate caCert = new X509Certificate(@"D:\develop\mqtt\cafiles\test\my-ca.crt");
        X509Certificate clientCert = new X509Certificate(@"D:\develop\mqtt\cafiles\test\wantech.crt");
        string brokerHostName = "iot.xxxx.com";
        int brokerPort = 3883;
        string clientId = Guid.NewGuid().ToString();
        byte[] qosLevels = { MqttMsgBase.QOS_LEVEL_AT_MOST_ONCE };

        try
        {
            //单向SSL通信
            MqttClient client = new MqttClient(brokerHostName, brokerPort, true, caCert, clientCert, MqttSslProtocols.TLSv1_2, new RemoteCertificateValidationCallback(cafileValidCallback));
            //消息接受
            client.MqttMsgPublishReceived += new MqttClient.MqttMsgPublishEventHandler(messageReceive);
            //连接Broker
            client.Connect(clientId, "test", "123456");
            string strValue = Convert.ToString(content);
            client.Publish(topic, Encoding.UTF8.GetBytes(strValue), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);

        }
        catch (System.Exception)
        {                
            Console.WriteLine("连接失败!");
            Console.ReadKey();
        }

    }