Skip to content

Trying to port to VB.Net #7

@stefxx

Description

@stefxx

Not an issue, but a question. Hopefully you allow me to post it here as I have been searching the net for days without result. I am trying to use the AWS IoT MQTT from the new v2 API, in my .Net project. Something is wrong with my certificate, but the code runs fine until I try to connect to the AWS MQTT. I can't find the issue, what do I miss?

Most important parts of my code below. I use the M2Mqtt for the actual MQTT connection. After I have been successfully authenticated to the API, this is what I do:

' Do a GET and (empty) POST to /service/users/client. Not sure why this is neccessary.
result = LG_API(API_ThinQ2_Url & "/service/users/client", "GET")
result = LG_API(API_ThinQ2_Url & "/service/users/client", "POST", "")

' Create a new public/private key pair, and create a CSR
Dim RSA2048 As RSA = RSA.Create(2048)
Dim distinguishedName As X500DistinguishedName = New X500DistinguishedName("CN=AWS IoT Certificate, O=Amazon")
Dim req As CertificateRequest = New CertificateRequest(distinguishedName, RSA2048, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1)

' Save the private and public key
Dim CertPrivKey = Certificate.ExportPrivateKey(RSA2048)
File.WriteAllText(Path.GetTempPath() & "Certificate.private.key", CertPrivKey)
Dim CertPubKey = Certificate.ExportPublicKey(RSA2048)
File.WriteAllText(Path.GetTempPath() & "Certificate.public.key", CertPubKey)

' Create the actual request in PEM format. Make sure the change the Accept header to */*
CertCSR = PemEncodeSigningRequest(req)
result = LG_API(API_ThinQ2_Url & "/service/users/client/certificate", "POST", "{""csr"": """ & CertCSR & """}", "*/*")
Dim Cert As CertificateClass = JsonConvert.DeserializeObject(Of CertificateClass)(result, New JsonDeserializeSettings)

' Save the certificate as PEM file
CertPem = Cert.result.certificatePem
File.WriteAllText(Path.GetTempPath() & "Certificate.pem", CertPem)

' Add the private key to the certificate and save as Pfx
Dim Cert2 = New X509Certificate2()
Cert2.Import(Path.GetTempPath() & "Certificate.pem")
Dim CertWithKey As X509Certificate2 = Cert2.CopyWithPrivateKey(RSA2048)
File.WriteAllBytes(Path.GetTempPath() & "Certificate.pfx", certWithKey.Export(X509ContentType.Pfx, "P@SSW0rd"))

' Get the CA certificate and save as PEM
CertCA = LG_API("https://www.websecurity.digicert.com/content/dam/websitesecurity/digitalassets/desktop/pdfs/roots/VeriSign-Class%203-Public-Primary-Certification-Authority-G5.pem", "GET", "", "*/*")
File.WriteAllText(Path.GetTempPath() & "root.pem", CertCA)

' Get the required MQTT endpoints
result = LG_API("https://common.lgthinq.com/route", "GET")
Dim Route As RouteClass = JsonConvert.DeserializeObject(Of RouteClass)(result, New JsonDeserializeSettings)
Dim Uri As New Uri(Route.result.mqttServer)
Dim iotEndpoint As String = Uri.Host
Dim brokerPort As Integer = Uri.Port

' Get the certificates
Dim clientCert = New X509Certificate2(Path.GetTempPath & "Certificate.pfx", "P@SSW0rd")
Dim caCert = New X509Certificate(Path.GetTempPath & "root.pem")

' Setup the MqttClient and attach the certificates
Dim client = New MqttClient(iotEndpoint, brokerPort, True, caCert, clientCert, MqttSslProtocols.TLSv1_2)
AddHandler client.MqttMsgPublishReceived, AddressOf Client_MqttMsgPublishReceived
AddHandler client.MqttMsgSubscribed, AddressOf Client_MqttMsgSubscribed
Dim clientId As String = Guid.NewGuid().ToString()

' Connect to the AWS IoT MQTT
client.Connect(clientId)

Note that LG_API is simply returning the data from the API. The client.Connect results in the following error: "AuthenticationException: The remote certificate is invalid according to the validation procedure."

Anything obvious I am missing here? Thanks!!

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions