TCP TLS communication in Xamarin Forms Application

iSukhi 6 Reputation points
2021-03-30T09:17:42.743+00:00

I want to establish connection between the client and server over TCP using TLS in my Xamarin forms Application. I am facing issues making a successful connection using certificate. Please guide me.

Developer technologies .NET Xamarin
{count} votes

1 answer

Sort by: Most helpful
  1. Jeremy Kaiser 1 Reputation point
    2021-04-22T16:56:36.193+00:00

    I would say you could do it using MQTTnet but I am having similar issues connecting to aws.

    private async Task OnConnectToIoTHubCommandAsync()
    {
    const string server = "sample.amazonaws.com";
    const string clientId = "XXXXXXXXXXKELYG82g";
    const string machineId = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXX";
    const string topic = "helloServer" + machineId;

                byte[] caCertRawBytes;
                byte[] clientCertRawBytes;
    
                using (var stream = await FileSystem.OpenAppPackageFileAsync("AmazonRootCA1.crt"))
                {
                    using (var memoryStream = new MemoryStream())
                    {
                        stream.CopyTo(memoryStream);
                        caCertRawBytes = memoryStream.ToArray();
                    }
                }
    
                using (var stream = await FileSystem.OpenAppPackageFileAsync("Private.pfx"))
                {
                    using (var memoryStream = new MemoryStream())
                    {
                        stream.CopyTo(memoryStream);
                        clientCertRawBytes = memoryStream.ToArray();
                    }
                }
    
                var caCert = new X509Certificate(caCertRawBytes);
                var clientCert = new X509Certificate2(clientCertRawBytes, "PfxPassword"); 
    
                var source = new CancellationTokenSource();
                var token = source.Token;
    
                var mqttOptions = new MqttClientOptionsBuilder()
                                      .WithTcpServer(server, 8883)
                                      .WithTls(new MqttClientOptionsBuilderTlsParameters
                                      {
                                          UseTls = true,
                                          Certificates = new List<X509Certificate> { caCert, clientCert },
                                          AllowUntrustedCertificates = false,
                                          IgnoreCertificateChainErrors = false,
                                          IgnoreCertificateRevocationErrors = false,
                                      })
                                      .Build();
    
                // Create a new MQTT client.
                var mqttClient = new MqttFactory().CreateMqttClient();
                await mqttClient.ConnectAsync(mqttOptions, token);
                await Task.CompletedTask.ConfigureAwait(false);
            }
    

    The output is an authentication failed see inner exception but the only thing in there is a System Exception exception and a timeout. I'm posting because my problem seems to be closely related to yours. The pfx file is a combination of the thing cert and the private key.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.