Azure-iot-sdk-c: How to implement x509 authentication in iOS?

Created on 10 Oct 2019  ยท  10Comments  ยท  Source: Azure/azure-iot-sdk-c

I am trying connect Azure IOT Hub from iOS devices by using MQTT protocol with x509 certificate and private key.
I am using Azure iOS sample project with iOS SDK from cocoaPods. I modified the iothub_sample.m with x509 sample c code for x509 testing. And it failed with error "Unsupported x509".

From issue 1115 and issue 942, it tells that x509 authentication is currently not support in iOS SDK but it is in backlog and it can be workaround by building the C SDK using OpenSSL.

Now i succeed to build the C SDK using OpenSSL and it looks correct with c executable file on my mac:
Creating IoTHub handle Sending message 1 to IoTHub Sending message 2 to IoTHub Sending message 3 to IoTHub Sending message 4 to IoTHub Sending message 5 to IoTHub Confirmation callback received for message 1 with result IOTHUB_CLIENT_CONFIRMATION_OK Confirmation callback received for message 2 with result IOTHUB_CLIENT_CONFIRMATION_OK Confirmation callback received for message 3 with result IOTHUB_CLIENT_CONFIRMATION_OK Confirmation callback received for message 4 with result IOTHUB_CLIENT_CONFIRMATION_OK Confirmation callback received for message 5 with result IOTHUB_CLIENT_CONFIRMATION_OK

I want to know how to use those C SDK in iOS and when will x509 be available in iOS SDK(cocoaPods)?
(I found several .a libraries and header file in the C SDK project but i am not sure how to correctly import them into my iOS project).

question

Most helpful comment

Succeed to integrate the c sdk in my iOS project and works with x509 authentication through openssl. Below are the steps what i did, hope it helps other poor iOS guys like me. I don't know cmake so it must looks stupid that i build the sdk, please don't hesitate to help to improve, thx :)

1. Import OpenSSL library and cURL library to iOS project

You can easily find their source code on github but it's not friendly to generate libraries for iOS. After a million times try, finally i found workable pod library for OpenSSL and framework for cURL, just add the pod for OpenSSL and import the framework for cURL in the iOS project.

It's still recommended to build the libraries from source code if it is comfortable for you.

Then add-lz to Other Linker Flags in your project build settings, this will against the built-in zlib.

2. Build C SDK for iOS

  1. Clone C SDK
  2. git clone [email protected]:Azure/azure-iot-sdk-c.git.

  3. Generate an XCode Project, set to use openssl

  4. cd C SDK root directory
  5. mkdir cmake
  6. cd cmake
  7. cmake -G Xcode -DOPENSSL_ROOT_DIR:PATH=/usr/local/opt/openssl -Duse_openssl:bool=ON ..

DOPENSSL_ROOT_DIR is the path of your openssl library in step 1

  1. Config build settings
  2. Open azure_iot_sdks.xcodeproj in cmake folder
  3. Edit Build Settings for all library targets (aziotsharedutil, iothub_client ...)

    • Change Base SDK from macos to ios

    • Add architectures whatever you need

  4. Edit Search Paths for target aziotsharedutil to link OpenSSL library and cURL library that in step 1

    • If you use static library, add path for header file to Header Search Paths and add path for .a library to Library Search Paths

    • If you use framework, just add the path to Framework Search Paths

For example, this is you openssl static library, you need to add ../openssl/include to Header Search Paths and add ../openssl/lib to Library Search Paths
openssl
โ”œโ”€โ”€ include
โ”‚ย ย  โ””โ”€โ”€ openssl
โ”‚ย ย  โ”œโ”€โ”€ aes.h
โ”‚ย ย  โ”œโ”€โ”€ ... (header files)
โ””โ”€โ”€ lib
โ”œโ”€โ”€ libcrypto.a
โ””โ”€โ”€ libssl.a

  1. Build C SDK
  2. If all the configuration are correct, you should be able to build all the libraries with iOS device or iOS simulator in azure_iot_sdks.xcodeproj

3. Import C SDK to iOS project

  • After step 2, you will get all C SDK libraries you need in /cmake folder in your C SDK project. However, the headers and static libraries are scattered in different paths, you have to link them to your iOS project.
  • You can refer to C SDK sample project to check all the header search path, for example, open azure_iot_sdks.xcodeproj, go to Build Settings > Search Paths of any target for sample (like iothub_II_Client_x509_sample), you can see the search path for each library.

You can refer to https://github.com/Azure/azure-iot-sdk-c/issues/1260 if you there is a problem.

All 10 comments

Hi @rafilho @YoDaMa , could you help?

Hi @alain-noa,

Please refer to our documentation for getting started with development on iOS: https://docs.microsoft.com/en-us/azure/iot-hub/quickstart-send-telemetry-ios
If this is missing the information you are looking for, please let us know so that we can update the documentation.

Also, thanks for the feedback on x509 support. This is currently in our backlog but do not have an ETA for this yet. Please vote on this User voice item, which can also be used for tracking our plans. I just added it, so it may not appear immediately, so please check back later in the day if the link doesn't work yet.

Thanks!

Hi @gregman-msft thanks for explanation! We will vote it on Azure feedback forums.

x509 authentication is very important for us and we are currently blocked by iOS platform. We will keep trying to workaround it before it is officially supported in iOS, any advice or guidance would be greatly appreciated!

Still cannot vote the item for "This idea is awaiting moderator approval".

Succeed to integrate the c sdk in my iOS project and works with x509 authentication through openssl. Below are the steps what i did, hope it helps other poor iOS guys like me. I don't know cmake so it must looks stupid that i build the sdk, please don't hesitate to help to improve, thx :)

1. Import OpenSSL library and cURL library to iOS project

You can easily find their source code on github but it's not friendly to generate libraries for iOS. After a million times try, finally i found workable pod library for OpenSSL and framework for cURL, just add the pod for OpenSSL and import the framework for cURL in the iOS project.

It's still recommended to build the libraries from source code if it is comfortable for you.

Then add-lz to Other Linker Flags in your project build settings, this will against the built-in zlib.

2. Build C SDK for iOS

  1. Clone C SDK
  2. git clone [email protected]:Azure/azure-iot-sdk-c.git.

  3. Generate an XCode Project, set to use openssl

  4. cd C SDK root directory
  5. mkdir cmake
  6. cd cmake
  7. cmake -G Xcode -DOPENSSL_ROOT_DIR:PATH=/usr/local/opt/openssl -Duse_openssl:bool=ON ..

DOPENSSL_ROOT_DIR is the path of your openssl library in step 1

  1. Config build settings
  2. Open azure_iot_sdks.xcodeproj in cmake folder
  3. Edit Build Settings for all library targets (aziotsharedutil, iothub_client ...)

    • Change Base SDK from macos to ios

    • Add architectures whatever you need

  4. Edit Search Paths for target aziotsharedutil to link OpenSSL library and cURL library that in step 1

    • If you use static library, add path for header file to Header Search Paths and add path for .a library to Library Search Paths

    • If you use framework, just add the path to Framework Search Paths

For example, this is you openssl static library, you need to add ../openssl/include to Header Search Paths and add ../openssl/lib to Library Search Paths
openssl
โ”œโ”€โ”€ include
โ”‚ย ย  โ””โ”€โ”€ openssl
โ”‚ย ย  โ”œโ”€โ”€ aes.h
โ”‚ย ย  โ”œโ”€โ”€ ... (header files)
โ””โ”€โ”€ lib
โ”œโ”€โ”€ libcrypto.a
โ””โ”€โ”€ libssl.a

  1. Build C SDK
  2. If all the configuration are correct, you should be able to build all the libraries with iOS device or iOS simulator in azure_iot_sdks.xcodeproj

3. Import C SDK to iOS project

  • After step 2, you will get all C SDK libraries you need in /cmake folder in your C SDK project. However, the headers and static libraries are scattered in different paths, you have to link them to your iOS project.
  • You can refer to C SDK sample project to check all the header search path, for example, open azure_iot_sdks.xcodeproj, go to Build Settings > Search Paths of any target for sample (like iothub_II_Client_x509_sample), you can see the search path for each library.

You can refer to https://github.com/Azure/azure-iot-sdk-c/issues/1260 if you there is a problem.

@alain-noa thanks so much for your detailed explanation, I hope this will help some of the other users looking for the same support. I've been trying to fix the user voice site issue, but very glad you have been able to unblock yourself. We'll reference this for any other similar requests in the meantime.

Add a missing detail:

Screen Shot 2019-10-31 at 1 39 01 PM

Add -lz to Other Linker Flags in your project build settings, this will against the built-in zlib.

Which resolve below compile error:

Undefined symbols for architecture x86_64:
"_inflate", referenced from:
_inflate_stream in curl(libcurl_la-content_encoding.o)
"_zlibVersion", referenced from:
_gzip_init_writer in curl(libcurl_la-content_encoding.o)
_curl_version in curl(libcurl_la-version.o)
_curl_version_info in curl(libcurl_la-version.o)
"_inflateEnd", referenced from:
_inflate_stream in curl(libcurl_la-content_encoding.o)
_exit_zlib in curl(libcurl_la-content_encoding.o)
"_inflateInit2_", referenced from:
_inflate_stream in curl(libcurl_la-content_encoding.o)
_gzip_init_writer in curl(libcurl_la-content_encoding.o)
"_inflateInit_", referenced from:
_deflate_init_writer in curl(libcurl_la-content_encoding.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I create a template project for reference.

Thank you very much, @alain-noa !

@alain-noa, @gregman-msft, @ewertons, thank you for your contribution to our open-sourced project! Please help us improve by filling out this 2-minute customer satisfaction survey

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vpetrigo picture vpetrigo  ยท  5Comments

moncef-bibani picture moncef-bibani  ยท  4Comments

hizzawaseem picture hizzawaseem  ยท  3Comments

vpetrigo picture vpetrigo  ยท  6Comments

caobaokun picture caobaokun  ยท  7Comments