Sumana Bagchi
Sumana Bagchi

Reputation: 9

Azure IOTHub - Certificate parameter (file path) in HTTPS Connection with C SDK

I am trying to make a HTTPS communication with Azure IOTHub through IotHub C SDK. I have gone through the samples provided by the Azure. Where they are actually storing the certificate or the private key in static const char and passing those in IoTHubClient_LL_SetOption() function along with the IotHub Client Handle. But for me , I have the certificate in my local machine along with the private key. I do not want to read those files and storing those in parameter. So is there any way I can pass the file path as a parameter to the function without reading the files?

When I am searching for some solutions over net I found they have that interface in their c# SDK:

var cert = new X509Certificate2("/file/path_to_certificate", "123");

Kindly let me know if it is possible to pass the file path as a parameter using IOTHUB C SDK just like the C# or there is any other interface I can use without reading the files.

Reference: https://github.com/Azure/azure-iot-sdk-c/blob/master/iothub_client/samples/iothub_ll_client_x509_sample/iothub_ll_client_x509_sample.c

Upvotes: 0

Views: 150

Answers (1)

Michael Xu
Michael Xu

Reputation: 4432

IoT Hub SDK for C does not provide the method for that.It provides utility functions in x509_schannel.c.You can use OpenSSL library like following code.

Use OpenSSL:

static X509 *load_cert(const char *file)
{
    X509 *x=NULL;
    BIO *cert;

    if ((cert=BIO_new(BIO_s_file())) == NULL)
        goto end;

    if (BIO_read_filename(cert,file) <= 0)
        goto end;

    x=PEM_read_bio_X509_AUX(cert,NULL, NULL, NULL);
end:
    if (cert != NULL) BIO_free(cert);
    return(x);
}

BTW, you can download the openssl source code and compile the library to use for azure iothub sdk project.

Upvotes: 0

Related Questions