Creating an IoT Device, the Device Key and the Device Certificate

This page describes how to set up an IoT Device in Azure IoT Hub using the X.509 certificate authentication method. It also addressed the creation of the X.509 certificates needed for secure communication with Azure IoT Hub via the Cloud Sink object.

There can be only one connection between a specific device and the Azure IoT Hub. If a new device with the same Device ID is connected from somewhere else, all previous connections will be abandoned.

Prerequisites

  • A self-signed Root CA Certificate or a certificate issued by a trusted Certificate Authority (CA)

    For production environments it is recommended to purchase X.509 certificates from a certificate authority (CA).

Creating the IoT Device in Azure IoT Hub

1 Go to the Azure Portal and select the IoT Hub resource. You can find it in   All Resources  

2 Click on IoT Devices to create a new device ( IoT Hub  IoT devices  + New ).

1 Enter a name for the Device ID and keep a note of it as we will need it later. In this example indevice is used as a name.

2 Set Authentication type to X.509 CA Signed and leave everything else as it is. Then click the   Create   button.

Proof of Possession

The Root CA certificate from the Prerequisites needs to be uploaded to the Azure IoT Hub and verified. To do this:

Upload Root CA Certificate and Obtain the Verfication Code

1 In your IoT Hub resource, go to the Settings section and click on Certificates, then click on + Add to add a new certificate.

2 In the Add Certificate dialog, enter a Certificate Name.

3 Click on the open icon for the Certificate .pem or .cer file field. Locate and upload the Root CA certificate (called 'rootCA.pem' in the example).

4 The certificate should now appear in the certificate list. Click on the newly added certificate to open its properties, next click Generate Verification Code button.

5 Copy the newly generated contents of the Verification Code field to the clipboard. In the following steps this will be referred to as the Verification Code.

Creating the Verification Certificate

1 In the command prompt window, create a new verification key file by using the following command:

openssl genrsa -out verification.key 2048

2 With this key file create a Certificate Signing Request by executing this command:

openssl req -new -key verification.key -out verification.csr

You are asked for some certificate data. While most of this information is related to your company, the Common Name field needs to be set to the Verification Code copied from the IoT Hub resource.

Example certificate data dialog:
Country Name (2 letter code) [AU]:DE
State or Province Name (full name) [Some-State]:NRW
Locality Name (eg, city) []:Cologne
Organization Name (eg, company) [Internet Widgits Pty Ltd]:inmation
Organizational Unit Name (eg, section) []:
Common Name (e.g. server FQDN or YOUR name) []:*7A69A4702DA903A41C3A5BC5575A8E3F49BEC5E5BA2D4CE1*
Email Address []:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

3 Grant this request using the CA Root certificate and the CA Root private key to generate the Verification Certificate:

openssl x509 -req -in verification.csr -CA rootCA.pem -CAkey rootKey.pem -CAcreateserial -out verificationCert.pem -days 1024 -sha256

This creates a new file called`verificationCert.pem`.

Uploading and Verifying the Certificate

1 Import the verificationCert.pem file on the IoT Hub.

2 click   Verify   button

Creating the Device Key and the Device Certificate

1 In the command prompt window, create the Device Key which will be used to create the Device Certificate (both are required for the Cloud Sink configuration):

openssl genrsa -out device.key 2048

2 Create a CSR (Certificate Signing Request) for the Device Certificate:

openssl req -key device.key -out device.csr

Again, you’re prompted for the certificate data. In this case, the Common Name needs to specify the Device ID ('indevice' in the example).

Example certificate data dialog:
Country Name (2 letter code) [AU]:DE
State or Province Name (full name) [Some-State]:NRW
Locality Name (eg, city) []:Cologne
Organization Name (eg, company) [Internet Widgits Pty Ltd]:inmation
Organizational Unit Name (eg, section) []:
Common Name (e.g. server FQDN or YOUR name) []:indevice
Email Address []:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

3 To grant the CSR and sign it with the CA Root Certificate from the Prerequisites (and thus create the Device Certificate) run this command:

openssl x509 -req -days 365 -in device.csr -CA rootCA.pem -CAkey rootKey.pem -CAcreateserial -out device.crt

This creates the Device Certificate (called 'device.crt') which - together with the Device Key - is required for authentication in the Cloud Sink configuration.