0

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

Dalton Cézane
  • 3,672
  • 2
  • 35
  • 60

1 Answers1

0

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.

Michael Xu
  • 4,382
  • 1
  • 8
  • 16
  • Hello, @Michael . Is there some good reason to use these `goto` in this code? I would code something like this: `if ((cert=BIO_new(BIO_s_file())) == NULL || BIO_read_filename(cert,file) <= 0) return; else { x = PEM_read_bio_X509_AUX(cert,NULL, NULL, NULL); BIO_free(cert); return x;` – Dalton Cézane May 09 '18 at 15:19
  • Hi,@Sumana Bagchi,a `goto` statement in C programming provides an unconditional jump from the 'goto' to a labeled statement in the same function. Your code will also work . – Michael Xu May 10 '18 at 01:30
  • Yes, @Michael . I just asked because, for many programmers, the `goto` statement is a kind of "bad practice". I am not telling that your code is bad. Just a curiosity if there was some special reason to use it in your code. [Here we have some opinions about it](https://stackoverflow.com/questions/3517726/what-is-wrong-with-using-goto). But I think you already know. =) Regards. – Dalton Cézane May 10 '18 at 02:56