Azure / azure-iot-sdk-c

A C99 SDK for connecting devices to Microsoft Azure IoT services
https://azure.github.io/azure-iot-sdk-c
Other
580 stars 738 forks source link

Provisioning with X509 doesn't work on windows #2603

Closed aleksandarkrstic closed 3 months ago

aleksandarkrstic commented 3 months ago

Hello everyone,

I have a problem trying to register my device using DPS. I am using lts_08_2023 Azure C SDK. I have Root, Intermediate and Device certificate, I have uploaded Root to DPS, Intermediate to enrollment group and tried to register my device using the following code:

prov_dev_security_init(SECURE_DEVICE_TYPE_X509);
PROV_DEVICE_HANDLE prov_dev_handle = Prov_Device_Create(global_prov_uri, id_scope, Prov_Device_MQTT_WS_Protocol);

if(prov_dev_handle != NULL)
{

//x509certificate here is const char * device certificate only (tried also full chain, didn't work)
Prov_Device_SetOption(prov_dev_handle , OPTION_X509_CERT, x509certificate);

//x509pk is device private key
Prov_Device_SetOption(prov_dev_handle , OPTION_X509_PRIVATE_KEY, x509pk);

Prov_Device_Register_Device(prov_dev_handle , register_callback, NULL, registration_status_callback, NULL);

while(!registration_complete)
    ThreadAPI_Sleep(100);

Prov_Device_Destroy(prov_dev_handle );
prov_dev_security_deinit();
}

The following code works well with lts_08_2023 SDK on Linux but it doesn't work on Windows. This is the ouput I get: Error: Time:Sun Mar 31 22:46:00 2024 File:C:\Users\Aleksandar\Desktop\azure-iot-sdk-c\c-utility\adapters\socketio_win32.c Func:socketio_open Line:457 Failure: socket create failure 10093. Error: Time:Sun Mar 31 22:46:00 2024 File:C:\Users\Aleksandar\Desktop\azure-iot-sdk-c\c-utility\adapters\tlsio_openssl.c Func:on_underlying_io_open_complete Line:851 Invalid open_result. Expected result is IO_OPEN_OK. Error: Time:Sun Mar 31 22:46:00 2024 File:C:\Users\Aleksandar\Desktop\azure-iot-sdk-c\umqtt\src\mqtt_client.c Func:onOpenComplete Line:452 Error: failure opening connection to endpoint Error: Time:Sun Mar 31 22:46:00 2024 File:C:\Users\Aleksandar\Desktop\azure-iot-sdk-c\provisioning_client\src\prov_transport_mqtt_common.c Func:mqtt_error_callback Line:139 MQTT communication error Error: Time:Sun Mar 31 22:46:00 2024 File:C:\Users\Aleksandar\Desktop\azure-iot-sdk-c\c-utility\adapters\tlsio_openssl.c Func:tlsio_openssl_open Line:1451 Failed opening the underlying I/O. Error: Time:Sun Mar 31 22:46:00 2024 File:C:\Users\Aleksandar\Desktop\azure-iot-sdk-c\c-utility\src\uws_client.c Func:uws_client_open_async Line:1721 Opening the underlying IO failed Error: Time:Sun Mar 31 22:46:00 2024 File:C:\Users\Aleksandar\Desktop\azure-iot-sdk-c\c-utility\src\wsio.c Func:wsio_open Line:519 Opening the uws instance failed. Error: Time:Sun Mar 31 22:46:00 2024 File:C:\Users\Aleksandar\Desktop\azure-iot-sdk-c\umqtt\src\mqtt_client.c Func:mqtt_client_connect Line:1117 Error: io_open failed Error: Time:Sun Mar 31 22:46:00 2024 File:C:\Users\Aleksandar\Desktop\azure-iot-sdk-c\provisioning_client\src\prov_transport_mqtt_common.c Func:create_connection Line:645 Failure connecting to mqtt server Error: Time:Sun Mar 31 22:46:00 2024 File:C:\Users\Aleksandar\Desktop\azure-iot-sdk-c\provisioning_client\src\prov_transport_mqtt_common.c Func:prov_transport_common_mqtt_dowork Line:919 unable to create mqtt connection Error: Time:Sun Mar 31 22:46:00 2024 File:C:\Users\Aleksandar\Desktop\azure-iot-sdk-c\provisioning_client\src\prov_device_ll_client.c Func:on_transport_registration_data Line:777 Failure retrieving data from the provisioning service

Also, tried to follow this tutorial https://learn.microsoft.com/en-us/azure/iot-dps/tutorial-custom-hsm-enrollment-group-x509?pivots=programming-language-ansi-c, did everything the same but the error is the same on Windows. Can you please advise how to fix this?

Also tried custom_hsm_example, still the same, Linux works and Windows doesn't. But the last thing I tried was setting Prov_Device_SetOption options with no custom_hsm_lib and the certificates I got from tutorial on how to provision devices. I built the sdk with openssl on windows, also tried schannel with putting certificates in the store - same error.

Can someone help please? Any help will be much appreciated and thanks in advance.

aleksandarkrstic commented 3 months ago

Found a solution, I didn't call IoTHub_Init before prov_dev_security_init. On Linux it works if you call IoTHub_Init later when you want to send telemetry for example (after registration) but on Windows I had to call IoTHub_Init first and then prov_dev_security_init. You can close this.