Integrate with Microsoft Azure IoT Hub
Microsoft Azure IoT Hub is a popular messaging service for connecting devices to cloud-based IoT solutions. Devices can send or receive messages from IoT Hub using the MQTT 3.1.1 protocol.
The Azure IoT Hub supports device authentication using mutual TLS (mTLS), using either X.509 thumbprint authentication or X.509 CA authentication. Learn more.
This tutorial describes how to configure DigiCert® Device Trust Manager to issue x.509 device certificates and use them for mutual TLS (mTLS) authentication with the Azure IoT Hub using x.509 CA authentication over MQTT 3.1.1 protocol.
Note
This tutorial uses DigiCert® TrustEdge MQTT client to perform mTLS from a device to Azure IoT Hub. However, you can use any MQTT 3.1.1 client of your choice.
What you will accomplish
By completing this tutorial, you will:
Configure Device Trust Manager to issue device certificates
Set up Azure IoT Hub with X.509 CA authentication
Issue certificates to test IoT devices
Establish secure mTLS connections between devices and Azure IoT Hub
Verify successful authentication using the MQTT protocol
Before you begin
Before starting this tutorial, ensure you have the following resources and access:
Access and accounts:
Azure subscription: An active Azure account with permissions to create IoT Hub resources. The free tier is sufficient for testing
Contact your company’s Azure subscription administrator to create an Azure login with sufficient permissions to create an Azure IoT Hub resource.
DigiCert® ONE account: Your DigiCert® ONE account must have the
Solution Administratorrole in Device Trust ManagerContact your DigiCert® account representative to set up your DigiCert® ONE account.
Device Trust Manager account: An active Device Trust Manager account
Contact your DigiCert account representative to provide you with a user account with the
Solution Administratorrole in Device Trust Manager.Certificate Authority: A Root CA and an Intermediate CA configured in Device Trust Manager for issuing certificates
Hardware and software:
IoT test device: A physical or virtual machine running Ubuntu 22.04 or later
TrustEdge MQTT client: Installed on a test device for performing mTLS authentication
OpenSSL: Command-line tool for certificate operations (included with Ubuntu).
Documentation:
Review the following documents before proceeding:
Device Trust Manager Certificate management policies
DigiCert TrustEdge MQTT client documentation
Set up a test device
Configure a Linux-based test device that acts as your IoT device throughout this tutorial. For this tutorial, we use a virtual or physical machine running Ubuntu 22.04 LTS. We refer to this device as TestIoTDevice01 throughout this documentation.
Log in to your test device (TestIoTDevice01) using SSH or direct console access.
Download and install TrustEdge on the device.
Set up Device Trust Manager
To perform this action, you must have a user role that contains the Solution administrator permission.
Sign in:
Sign in to DigiCert ONE.
In the Managers () menu, select Device Trust.
Create a division:
In the Device Trust Manager menu, go to Divisions.
Learn more about Divisions in Device Trust Manager.
Select Create division.
Enter a Name for the division and, optionally, a description.
Select a Primary zone from the dropdown under the Rendezvous zones section.
(Optional) Select a Secondary zone from the dropdown under the Rendezvous zones section as a backup.
Click Create new division.
Create a certificate profile:
In the Device Trust Manager menu, go to Certificate management > Certificate profiles.
Learn more about Certificate profiles in Device Trust Manager.
Click Create certificate profile.
Enter a Name for the certificate profile.
Use DigiCert ONE as the CA source, or choose one from the list.
Under Template, select either End entity or Intermediate CA, depending on your requirement.
Choose a certificate template that this certificate profile will use. Configurable custom field options are loaded based on your chosen template.
Note
Select which divisions can use the certificate profile.
All divisions: The certificate profile is available for use by all divisions in the account, making it an account-wide profile.
Specific divisions: Select one or more divisions that should have access to the certificate profile. The profile will only be available to the chosen divisions.
Under Custom fields to include in certificate, configure fields as needed. For example, you could enable the common name, set it as required, and specify a default value.
Select Create to save the certificate profile.
Create a certificate management policy:
In the Device Trust Manager menu, go to Certificate management > Certificate management policies.
Learn more about Certificate management policies in Device Trust Manager.
Select Create certificate management policy.
Enter a Name for the certificate management policy.
Choose a Division to assign the policy to.
Under the Select the certificate management model, choose Policy will be used for certificate issuance only. Requires an Essentials license.
The scope of this tutorial is only to implement certificate issuance, not device management; hence, we recommend choosing the above certificate management model.
Under the Certificate management methods select Single certificate request through portal and REST API and register a single device.
The scope of this tutorial is to request a certificate through the Device Trust Manager portal; hence, we recommend choosing the above certificate management method.
Select Next to proceed to the certificate settings.
Select the End entity certificate profile you have already created (as part of certificate profile creation).
The End entity certificate profile defines the certificate structure, including subject fields, extensions, and validity period.
Optionally, you can also select the default Basic client certificate profile.
Select an Issuing CA from the available options. This is the Certificate Authority that will sign the certificates issued under this policy.
Learn more about Issuing CA in Device Trust Manager.
Under the Keypair generation settings, select Server-side keypair generation.
Under the Default key type and key size for when DigiCert generates the keypair, select RSA 2048 for the keypair.
You can choose whether the private key is generated on the device or server-side and delivered to the device in the certificate response. However, considering the scope of this tutorial, we recommend choosing the above methods.
Click Next.
Select Finish to create the certificate management policy.
Download the Issuing CA:
You will need to upload this CA certificate to Azure IoT Hub to establish trust.
In DigiCert® ONE, in the Manager menu (grid at top right), select Private CA.
Under Manage CAs, select Intermediates.
On the Intermediate Certificate Authorities page, find the certificate you want to download.
Click More actions > Download the certificate as a .pem file.
Save the downloaded CA.
Request a device certificate
Issue a device certificate from Device Trust Manager for your test IoT device.
In the Device Trust Manager menu, go to Certificate management > Certificates.
Select Request certificate > Request a certificate for a device.
From the Device Group dropdown menu, select an appropriate device group.
From the Certificate management policy dropdown menu, select the certificate management policy associated with the device group.
On the Key generation type step:
Note
The Key generation type option is displayed based on your selection of the Device group and the Certificate management policy.
I have the keypairs and will provide the CSRs or public keys in the request:
Upload a CSV file or a zipped CSV containing the device data. You can download the provided template for formatting guidance.
Key pairs will be generated server side by this application, and the private key and certificate will be included in response:
Select the Key generation type dropdown menu.
Provide a Common name for the certificate.
Optionally, provide an Organization name.
Click Add Value to provide an organization unit value (optional).
Provide a Description (optional).
Click Submit certificate request.
Save the password.
Download the certificate after successful submission of the certificate request.
Extract the certificate and the private key
Use OpenSSL to extract the certificate and the private key from the downloaded PKCS#12 file.
Extract the certificate:
Run the following command to extract the certificate. Enter the saved password when prompted for Import Password:
openssl pkcs12 -in <filename>.pfx -clcerts -nokeys -out device_cert.pem
Extract the encrypted private key:
Run the following command to extract the private key. Enter the saved password when prompted for Import Password. You will also be prompted to enter a PEM passphrase twice to protect the extracted key; enter the same password for the PEM passphrase also:
openssl pkcs12 -in <filename>.pfx -nocerts -out device_key_secure.pem
Remove the passphrase from the private key:
Run the following command to remove the passphrase from the private key. Enter the saved password when prompted to enter a PEM passphrase for the private key:
openssl rsa -in device_key_secure.pem -out device_key_insecure.pem
Copy the device_cert.pem and device_key_insecure.pem files to your test IoT device.
Set up the Azure IoT Hub
Create and configure an Azure IoT Hub instance to accept X.509 CA-signed device connections:
Create the IoT Hub Resource:
Sign in to the Azure Portal with your Azure subscription credentials.
From the Azure home page, under Azure services, select Create a resource.
In the search box, type IoT Hub.
Select the IoT Hub tile from the search results.
Select Create.
Configure the IoT Hub settings:
Complete the following fields:
Subscription: Your Azure subscription details
Resource Group: New or existing resource group name
IoT Hub Name: Globally unique name for your IoT Hub
Tier: Pricing tier
Select Review + create.
Once deployment is complete, select Go to resource.
On the IoT Hub Overview page, locate and copy the Hostname field (for example, myiothub.azure-devices.net). You will need this hostname for device connections.
Upload and verify the CA certificate:
Register the issuing CA certificate on the IoT Hub to enable X.509 CA authentication.
On the IoT Hub navigation bar, under Security settings, select Certificates.
Select Add to upload a new certificate.
Enter a friendly certificate name (for example, Contoso_Issuing_CA).
Browse and select the Issuing CA PEM file you have downloaded.
Enable the option Set certificate status to verified on upload.
Select Save.
Register the device on IoT Hub
Create a device identity on Azure IoT Hub that will authenticate using X.509 CA-signed certificates.
On the Azure IoT Hub management interface, go to Device management > Devices.
Select Add Device.
Enter a name for the Device ID (for example, TestIoTDevice01).
Select X.509 CA Signed for the authentication.
Select Save to create the device.
Your device is now registered and ready to authenticate using its certificate.
Perform mTLS Authentication
Use the MQTT client installed on the device to authenticate with IoT Hub using mTLS over TCP port 8883.
Note
IoT Hub does not support custom MQTT topics. You must use the IoT Hub predefined topic structures. For more information, see Communicate with an IoT hub using the MQTT protocol.
Execute the following command on your test IoT device to establish an mTLS connection and publish a test message to IoT Hub:
trustedge mqtt \ --mqtt_version 3.1.1 \ --mqtt_servername myiothub.azure-devices.net \ --mqtt_pub_topic devices/TestIoTDevice01/messages/events/ \ --mqtt_pub_message "test message" \ --mqtt_port 8883 \ --mqtt_client_id TestIoTDevice01 \ --mqtt_username myiothub.azure-devices.net/TestIoTDevice01/?api-version=2021-04-12 \ --mqtt_transport SSL \ --ssl_key_file device_key_insecure.pem \ --ssl_cert_file device_cert.pem \ --ssl_ca_file DigiCertGlobalRootG2.crt.pem
Command breakdown:
--mqtt_version: MQTT protocol version. It must be set to version 3.1.1
mqtt_servername: IoT Hub hostname in the format of <hostname>.azure-devices.net
--mqtt_pub_topic: MQTT topic for publishing telemetry messages. Must follow IoT Hub's predefined format of devices/<devicename>/messages/events/
--mqtt_pub_message: The message payload to publish to IoT Hub
--mqtt_port: TCP port for secure MQTT connections. It must be set to port 8883
--mqtt_client_id: MQTT client identifier. Must match the <devicename> registered in IoT Hub
--mqtt_username: MQTT username in IoT Hub's required format of <hostname>.azure-devices.net/<devicename>?api-version=2021-04-12
--mqtt_transport: Transport protocol. Must be set to SSL for TLS-encrypted connections.
--ssl_key_file: Path to the device's private key file in PEM format. For example, device_key_insecure.pem file is generated in the above command
--ssl_cert_file: Path to the device's X.509 certificate file in PEM format. For example, device_cert.pem file is generated in the above command
--ssl_ca_file: DigiCert Global Root G2 public root CA used by the IoT Hub azure-devices.net public endpoint.
You can download the DigiCert Global Root G2 public root CA from https://cacerts.digicert.com/DigiCertGlobalRootG2.crt.pem
Expected output:
If mTLS authentication is successful, your output resembles the example below:
SSL Certificate is trusted Connection Success! Calling MQTT_publish Exit flag set...
Use HiveMQ MQTT CLI (alternative client)
The HiveMQ MQTT CLI is an alternative MQTT client that supports X.509 authentication. This example shows the equivalent connection using HiveMQ:
mqtt pub -V 3 \
-h "myiothub.azure-devices.net" \
-p 8883 \
-i "TestIoTDevice01" \
-u "myiothub.azure-devices.net/TestIoTDevice01/?api-version=2021-04-12" \
-s \
--cafile "DigiCertGlobalRootG2.crt.pem" \
--cert "device_cert.pem" \
--key "device_key_insecure.pem" \
-t "devices/TestIoTDevice01/messages/events/" \
-m '{"temp":23.4}' \
-q 1 -d