Edit

Share via


Create demo certificates to test IoT Edge device features

Applies to: IoT Edge 1.5 checkmark IoT Edge 1.5

Important

IoT Edge 1.5 LTS is the supported release. IoT Edge 1.4 LTS is end of life as of November 12, 2024. If you are on an earlier release, see Update IoT Edge.

IoT Edge devices require certificates for secure communication between the runtime, the modules, and any downstream devices. If you don't have a certificate authority to create the required certificates, you can use demo certificates to try out IoT Edge features in your test environment. This article describes the functionality of the certificate generation scripts that IoT Edge provides for testing.

Warning

These certificates expire in 30 days, and should not be used in any production scenario.

You can create certificates on any machine and then copy them over to your IoT Edge device, or generate the certificates directly on the IoT Edge device.

Prerequisites

A development machine with Git installed.

Download test certificate scripts and set up working directory

The IoT Edge repository on GitHub includes certificate generation scripts that you can use to create demo certificates. This section provides instructions for preparing the scripts to run on your computer, either on Windows or Linux.

To create demo certificates on a Linux device, you need to clone the generation scripts and set them up to run locally in bash.

  1. Clone the IoT Edge git repo, which contains scripts to generate demo certificates.

    git clone https://github.com/Azure/iotedge.git
    
  2. Create a directory in which you want to work and copy the certificate scripts there. All certificate and key files will be created in this directory.

    mkdir wrkdir
    cd wrkdir
    cp ../iotedge/tools/CACertificates/*.cnf .
    cp ../iotedge/tools/CACertificates/certGen.sh .
    

Create root CA certificate

Run this script to generate a root CA that is required for each step in this article.

The root CA certificate is used to make all the other demo certificates for testing an IoT Edge scenario. You can keep using the same root CA certificate to make demo certificates for multiple IoT Edge or downstream devices.

If you already have one root CA certificate in your working folder, don't create a new one. The new root CA certificate will overwrite the old, and any downstream certificates made from the old one will stop working. If you want multiple root CA certificates, be sure to manage them in separate folders.

  1. Navigate to the working directory wrkdir where you placed the certificate generation scripts.

  2. Create the root CA certificate and one intermediate certificate.

    ./certGen.sh create_root_and_intermediate
    

    This script command creates several certificate and key files, but when articles ask for the root CA certificate, use the following file:

    certs/azure-iot-test-only.root.ca.cert.pem

This certificate is required before you can create more certificates for your IoT Edge devices and downstream devices as described in the next sections.

Create identity certificate for the IoT Edge device

IoT Edge device identity certificates are used to provision IoT Edge devices if you choose to use X.509 certificate authentication. If you use symmetric key for authenticating to IoT Hub or DPS, these certificates aren't needed, and you can skip this section.

These certificates work whether you use manual provisioning or automatic provisioning through the Azure IoT Hub Device Provisioning Service (DPS).

Device identity certificates go in the Provisioning section of the config file on the IoT Edge device.

  1. Navigate to the working directory wrkdir that has the certificate generation scripts and root CA certificate.

  2. Create the IoT Edge device identity certificate and private key with the following command:

    ./certGen.sh create_edge_device_identity_certificate "<device-id>"
    

    The name that you pass in to this command is the device ID for the IoT Edge device in IoT Hub.

  3. The script creates several certificate and key files, including three that you use when creating an individual enrollment in DPS and installing the IoT Edge runtime:

    Type File Description
    Device identity certificate certs/iot-edge-device-identity-<device-id>.cert.pem Signed by the intermediate certificate generated earlier. Contains just the identity certificate. Specify in configuration file for DPS individual enrollment or IoT Hub provisioning.
    Full chain certificate certs/iot-edge-device-identity-<device-id>-full-chain.cert.pem Contains the full certificate chain including the intermediate certificate. Specify in configuration file for IoT Edge to present to DPS for group enrollment provisioning.
    Private key private/iot-edge-device-identity-<device-id>.key.pem Private key associated with the device identity certificate. Should be specified in configuration file as long as you're using some sort of certificate authentication (thumbprint or CA) for either DPS or IoT Hub.

Create Edge CA certificates

These certificates are required for gateway scenarios because the Edge CA certificate is how the IoT Edge device verifies its identity to downstream devices. You can skip this section if you're not connecting any downstream devices to IoT Edge.

The Edge CA certificate is also responsible for creating certificates for modules running on the device, but IoT Edge runtime can create temporary certificates if Edge CA isn't configured. Edge CA certificates go in the Edge CA section of the config.toml file on the IoT Edge device. To learn more, see Understand how Azure IoT Edge uses certificates.

  1. Navigate to the working directory that has the certificate generation scripts and root CA certificate.

  2. Create the IoT Edge CA certificate and private key with the following command. Provide a name for the CA certificate. The name passed to the create_edge_device_ca_certificate command should not be the same as the hostname parameter in the config file or the device's ID in IoT Hub.

    ./certGen.sh create_edge_device_ca_certificate "<CA cert name>"
    
  3. This script command creates several certificate and key files. The following certificate and key pair need to be copied over to an IoT Edge device and referenced in the config file:

    • certs/iot-edge-device-ca-<CA cert name>-full-chain.cert.pem
    • private/iot-edge-device-ca-<CA cert name>.key.pem

Create downstream device certificates

These certificates are required for setting up a downstream IoT device for a gateway scenario and want to use X.509 authentication with IoT Hub or DPS. If you want to use symmetric key authentication, you don't need to create certificates for the downstream device and can skip this section.

There are two ways to authenticate an IoT device using X.509 certificates: using self-signed certs or using certificate authority (CA) signed certs.

  • For X.509 self-signed authentication, sometimes referred to as thumbprint authentication, you need to create new certificates to place on your IoT device. These certificates have a thumbprint in them that you share with IoT Hub for authentication.
  • For X.509 certificate authority (CA) signed authentication, you need a root CA certificate registered in IoT Hub or DPS that you use to sign certificates for your IoT device. Any device using a certificate that was issued by the root CA certificate or any of its intermediate certificates can authenticate as long as the full chain is presented by the device.

The certificate generation scripts can help you make demo certificates to test out either of these authentication scenarios.

Self-signed certificates

When you authenticate an IoT device with self-signed certificates, you need to create device certificates based on the root CA certificate for your solution. Then, you retrieve a hexadecimal "thumbprint" from the certificates to provide to IoT Hub. Your IoT device also needs a copy of its device certificates so that it can authenticate with IoT Hub.

  1. Navigate to the working directory that has the certificate generation scripts and root CA certificate.

  2. Create two certificates (primary and secondary) for the downstream device. An easy naming convention to use is to create the certificates with the name of the IoT device and then the primary or secondary label. For example:

    ./certGen.sh create_device_certificate "<device name>-primary"
    ./certGen.sh create_device_certificate "<device name>-secondary"
    

    This script command creates several certificate and key files. The following certificate and key pairs needs to be copied over to the downstream IoT device and referenced in the applications that connect to IoT Hub:

    • certs/iot-device-<device name>-primary-full-chain.cert.pem
    • certs/iot-device-<device name>-secondary-full-chain.cert.pem
    • certs/iot-device-<device name>-primary.cert.pem
    • certs/iot-device-<device name>-secondary.cert.pem
    • certs/iot-device-<device name>-primary.cert.pfx
    • certs/iot-device-<device name>-secondary.cert.pfx
    • private/iot-device-<device name>-primary.key.pem
    • private/iot-device-<device name>-secondary.key.pem
  3. Retrieve the SHA1 thumbprint (called a thumbprint in IoT Hub contexts) from each certificate. The thumbprint is a 40 hexadecimal character string. Use the following openssl command to view the certificate and find the thumbprint:

    openssl x509 -in certs/iot-device-<device name>-primary.cert.pem -text -thumbprint | sed 's/[:]//g'
    

    You provide both the primary and secondary thumbprint when you register a new IoT device using self-signed X.509 certificates.

CA-signed certificates

When you authenticate an IoT device with CA-signed certificates, you need to upload the root CA certificate for your solution to IoT Hub. Use the same root CA certificate to create device certificates to put on your IoT device so that it can authenticate with IoT Hub.

The certificates in this section are for the steps in the IoT Hub X.509 certificate tutorial series. See Understanding Public Key Cryptography and X.509 Public Key Infrastructure for the introduction of this series.

  1. Upload the root CA certificate file from your working directory, certs\azure-iot-test-only.root.ca.cert.pem, to your IoT hub.

  2. If automatic verification isn't selected, use the code provided in the Azure portal to verify that you own that root CA certificate.

    ./certGen.sh create_verification_certificate "<verification code>"
    
  3. Create a certificate chain for your downstream device. Use the same device ID that the device is registered with in IoT Hub.

    ./certGen.sh create_device_certificate "<device id>"
    

    This script command creates several certificate and key files. The following certificate and key pairs needs to be copied over to the downstream IoT device and referenced in the applications that connect to IoT Hub:

    • certs/iot-device-<device id>.cert.pem
    • certs/iot-device-<device id>.cert.pfx
    • certs/iot-device-<device id>-full-chain.cert.pem
    • private/iot-device-<device id>.key.pem