快速入门:使用 Azure IoT C SDK 预配 X.509 模拟设备Quickstart: Provision an X.509 simulated device using the Azure IoT C SDK

本快速入门介绍如何在 Windows 开发计算机上创建和运行 X.509 设备模拟器。In this quickstart, you will learn how to create and run an X.509 device simulator on a Windows development machine. 你将使用设备预配服务实例的注册,将此模拟设备配置为分配到 IoT 中心。You will configure this simulated device to be assigned to an IoT hub using an enrollment with a Device Provisioning Service instance. 将使用 Azure IoT C SDK 中的示例代码来模拟设备的启动序列。Sample code from the Azure IoT C SDK will be used to simulate a boot sequence for the device. 将根据预配服务的注册来识别该设备,然后将其分配到 IoT 中心。The device will be recognized based on the enrollment with the provisioning service and assigned to the IoT hub.

如果不熟悉自动预配过程,请查看预配概述。If you're unfamiliar with the process of autoprovisioning, review the provisioning overview. 另外,在继续学习本快速入门之前,请确保已完成通过 Azure 门户设置 IoT 中心设备预配服务中的步骤。Also, make sure you've completed the steps in Set up IoT Hub Device Provisioning Service with the Azure portal before continuing with this quickstart.

Azure IoT 设备预配服务支持两类注册:The Azure IoT Device Provisioning Service supports two types of enrollments:

本文将演示单个注册。This article will demonstrate individual enrollments.

如果没有 Azure 订阅,请在开始前创建一个试用订阅If you don't have an Azure subscription, create a Trial Subscription before you begin.

先决条件Prerequisites

以下先决条件适用于 Windows 开发环境。The following prerequisites are for a Windows development environment. 对于 Linux 或 macOS,请参阅 SDK 文档的准备开发环境中的相应部分。For Linux or macOS, see the appropriate section in Prepare your development environment in the SDK documentation.

为 Azure IoT C SDK 准备开发环境Prepare a development environment for the Azure IoT C SDK

在本部分,你将准备一个开发环境,用于生成包含 X.509 启动序列示例代码的 Azure IoT C SDKIn this section, you will prepare a development environment used to build the Azure IoT C SDK, which includeS the sample code for the X.509 boot sequence.

  1. 下载 CMake 生成系统Download the CMake build system.

    在进行 CMake 安装之前,必须在计算机上安装 Visual Studio 必备组件(Visual Studio 和“使用 C++ 的桌面开发”工作负荷)。It is important that the Visual Studio prerequisites (Visual Studio and the 'Desktop development with C++' workload) are installed on your machine, before starting the CMake installation. 满足先决条件并验证下载内容后,安装 CMake 生成系统。Once the prerequisites are in place, and the download is verified, install the CMake build system.

  2. 查找最新版本 SDK 的标记名称。Find the tag name for the latest release of the SDK.

  3. 打开命令提示符或 Git Bash shell。Open a command prompt or Git Bash shell. 运行以下命令以克隆最新版本的 Azure IoT C SDK GitHub 存储库。Run the following commands to clone the latest release of the Azure IoT C SDK GitHub repository. 使用在上一步中找到的标记作为 -b 参数的值:Use the tag you found in the previous step as the value for the -b parameter:

    git clone -b <release-tag> https://github.com/Azure/azure-iot-sdk-c.git
    cd azure-iot-sdk-c
    git submodule update --init
    

    应该预料到此操作需要几分钟才能完成。You should expect this operation to take several minutes to complete.

  4. 在 git 存储库的根目录中创建 cmake 子目录,并导航到该文件夹。Create a cmake subdirectory in the root directory of the git repository, and navigate to that folder. azure-iot-sdk-c 目录运行以下命令:Run the following commands from the azure-iot-sdk-c directory:

    mkdir cmake
    cd cmake
    
  5. 代码示例使用 X.509 证书通过 X.509 身份验证提供证明。The code sample uses an X.509 certificate to provide attestation via X.509 authentication. 运行以下命令,以生成特定于你的开发平台(包括设备预配客户端)的 SDK 版本。Run the following command to build a version of the SDK specific to your development platform that includes the device provisioning client. cmake 目录中生成模拟设备的 Visual Studio 解决方案。A Visual Studio solution for the simulated device is generated in the cmake directory.

    cmake -Duse_prov_client:BOOL=ON ..
    

    如果 cmake 找不到 C++ 编译器,则可能会在运行以上命令时出现生成错误。If cmake does not find your C++ compiler, you might get build errors while running the above command. 如果出现这种情况,请尝试在 Visual Studio 命令提示符窗口中运行该命令。If that happens, try running this command in the Visual Studio command prompt.

    生成成功后,最后的几个输出行如下所示:Once the build succeeds, the last few output lines look similar to the following output:

    $ cmake -Duse_prov_client:BOOL=ON ..
    -- Building for: Visual Studio 16 2019
    -- The C compiler identification is MSVC 19.23.28107.0
    -- The CXX compiler identification is MSVC 19.23.28107.0
    
    ...
    
    -- Configuring done
    -- Generating done
    -- Build files have been written to: C:/code/azure-iot-sdk-c/cmake
    

创建自签名的 X.509 设备证书Create a self-signed X.509 device certificate

本部分将使用自签名的 X.509 证书,请务必记住以下几点:In this section you, will use a self-signed X.509 certificate, it is important to keep in mind the following points:

  • 自签名证书仅用于测试,不应在生产环境中使用。Self-signed certificates are for testing only, and should not be used in production.
  • 自签名证书的默认过期日期为一年。The default expiration date for a self-signed certificate is one year.

你将使用来自 Azure IoT C SDK 的示例代码创建要与模拟设备的个人注册条目一起使用的证书。You will use sample code from the Azure IoT C SDK to create the certificate to be used with the individual enrollment entry for the simulated device.

  1. 启动 Visual Studio 并打开名为 azure_iot_sdks.sln 的新解决方案文件。Launch Visual Studio and open the new solution file named azure_iot_sdks.sln. 此解决方案文件位于先前在 azure-iot-sdk-c git 存储库的根目录中创建的 cmake 文件夹中。This solution file is located in the cmake folder you previously created in the root of the azure-iot-sdk-c git repository.

  2. 在 Visual Studio 菜单中,选择“生成” > “生成解决方案”以生成解决方案中的所有项目。 On the Visual Studio menu, select Build > Build Solution to build all projects in the solution.

  3. 在 Visual Studio 的“解决方案资源管理器”窗口中,导航到 Provision_Tools 文件夹。 In Visual Studio's Solution Explorer window, navigate to the Provision_Tools folder. 右键单击“dice_device_enrollment”项目,然后选择“设置为启动项目”。 Right-click the dice_device_enrollment project and select Set as Startup Project.

  4. 在 Visual Studio 菜单中,选择“调试” > “开始执行(不调试)”以运行该解决方案。 On the Visual Studio menu, select Debug > Start without debugging to run the solution. 在输出窗口中,当系统提示时输入 i 完成单个注册。In the output window, enter i for individual enrollment when prompted.

    输出窗口会显示在本地为模拟设备生成的自签名 X.509 证书。The output window displays a locally generated self-signed X.509 certificate for your simulated device. 将输出复制到剪贴板,从 -----BEGIN CERTIFICATE----- 开始,到第一个 -----END CERTIFICATE----- 结束,确保将这两行也包括进去。Copy the output to clipboard, starting from -----BEGIN CERTIFICATE----- and ending with the first -----END CERTIFICATE-----, making sure to include both of these lines as well. 只需要输出窗口中的第一个证书。You need only the first certificate from the output window.

  5. 使用文本编辑器将证书保存到名为 X509testcert.pem 的新文件中。Using a text editor, save the certificate to a new file named X509testcert.pem.

在门户中创建设备注册项Create a device enrollment entry in the portal

  1. 登录到 Azure 门户,选择左侧菜单上的“所有资源”按钮,打开设备预配服务 。Sign in to the Azure portal, select the All resources button on the left-hand menu and open your Device Provisioning service.

  2. 选择“管理注册”选项卡,然后选择顶部的“添加个人注册”按钮 。Select the Manage enrollments tab, then select the Add individual enrollment button at the top.

  3. 在“添加注册”面板中输入以下信息,然后按“保存”按钮 。In the Add Enrollment panel, enter the following information, then press the Save button.

    • 机制: 选择“X.509” 作为标识证明机制 。Mechanism: Select X.509 as the identity attestation Mechanism.

    • 主要证书 .pem 或 .cer 文件: 选择“选择文件”,以选择前面创建的证书文件 X509testcert.pem 。Primary certificate .pem or .cer file: Choose Select a file to select the certificate file, X509testcert.pem, you created earlier.

    • IoT 中心设备 ID: 输入 test-docs-cert-device 作为设备的 ID 。IoT Hub Device ID: Enter test-docs-cert-device to give the device an ID.

      在门户中为 X.509 证明添加单个注册Add individual enrollment for X.509 attestation in the portal

      成功注册以后,X.509 设备会在“单独注册”选项卡的“注册 ID”列下显示为 riot-device-certOn successful enrollment, your X.509 device appears as riot-device-cert under the Registration ID column in the Individual Enrollments tab.

模拟设备的首次启动顺序Simulate first boot sequence for the device

在本部分更新向设备预配服务实例发送设备启动序列的示例代码。In this section, update the sample code to send the device's boot sequence to your Device Provisioning Service instance. 此启动序列使得设备可被识别并分配到与设备预配服务实例链接的 IoT 中心。This boot sequence will cause the device to be recognized and assigned to an IoT hub linked to the Device Provisioning Service instance.

  1. 在 Azure 门户中,选择设备预配服务的“概述”选项卡,记下“ID 范围”的值 。In the Azure portal, select the Overview tab for your Device Provisioning service and note the ID Scope value.

    从门户边栏选项卡中提取设备预配服务终结点信息

  2. 在 Visual Studio 的“解决方案资源管理器”窗口中,导航到 Provision_Samples 文件夹。 In Visual Studio's Solution Explorer window, navigate to the Provision_Samples folder. 展开名为 prov_dev_client_sample 的示例项目。Expand the sample project named prov_dev_client_sample. 展开“源文件”,打开 prov_dev_client_sample.cExpand Source Files, and open prov_dev_client_sample.c.

  3. 找到 id_scope 常量,将值替换为前面复制的“ID 范围”值。 Find the id_scope constant, and replace the value with your ID Scope value that you copied earlier.

    static const char* id_scope = "0ne00002193";
    
  4. 在同一文件中找到 main() 函数的定义。Find the definition for the main() function in the same file. 确保 hsm_type 变量设置为 SECURE_DEVICE_TYPE_X509 而不是 SECURE_DEVICE_TYPE_TPM,如下所示。Make sure the hsm_type variable is set to SECURE_DEVICE_TYPE_X509 instead of SECURE_DEVICE_TYPE_TPM as shown below.

    SECURE_DEVICE_TYPE hsm_type;
    //hsm_type = SECURE_DEVICE_TYPE_TPM;
    hsm_type = SECURE_DEVICE_TYPE_X509;
    
  5. 右键单击“prov_dev_client_sample”项目, 然后选择“设为启动项目”。 Right-click the prov_dev_client_sample project and select Set as Startup Project.

  6. 在 Visual Studio 菜单中,选择“调试” > “开始执行(不调试)”以运行该解决方案。 On the Visual Studio menu, select Debug > Start without debugging to run the solution. 对于重新生成项目的提示,请选择“是”,以便在运行项目之前重新生成项目 。In the prompt to rebuild the project, select Yes to rebuild the project before running.

    以下输出示例显示预配设备客户端示例成功启动,然后连接到预配服务实例来获取 IoT 中心信息并注册:The following output is an example of the provisioning device client sample successfully booting up, and connecting to the provisioning Service instance to get IoT hub information and registering:

    Provisioning API Version: 1.2.7
    
    Registering... Press enter key to interrupt.
    
    Provisioning Status: PROV_DEVICE_REG_STATUS_CONNECTED
    Provisioning Status: PROV_DEVICE_REG_STATUS_ASSIGNING
    Provisioning Status: PROV_DEVICE_REG_STATUS_ASSIGNING
    
    Registration Information received from service:
    test-docs-hub.azure-devices.cn, deviceId: test-docs-cert-device
    
  7. 在门户中导航到已链接到预配服务的 IoT 中心,然后选择“IoT 设备”选项卡 。将模拟的 X.509 设备成功预配到中心以后,设备 ID 会显示在“IoT 设备”边栏选项卡上,“状态”为“已启用” 。In the portal, navigate to the IoT hub linked to your provisioning service and select the IoT devices tab. On successful provisioning of the simulated X.509 device to the hub, its device ID appears on the IoT devices blade, with STATUS as enabled. 你可能需要按顶部的“刷新”按钮 。You might need to press the Refresh button at the top.

    设备注册到 IoT 中心

清理资源Clean up resources

如果打算继续使用和探索设备客户端示例,请勿清理在本快速入门中创建的资源。If you plan to continue working on and exploring the device client sample, do not clean up the resources created in this quickstart. 如果不打算继续学习,请按以下步骤删除本快速入门中创建的所有资源。If you do not plan to continue, use the following steps to delete all resources created by this quickstart.

  1. 关闭计算机上的设备客户端示例输出窗口。Close the device client sample output window on your machine.
  2. 在 Azure 门户的左侧菜单中选择“所有资源”,然后选择设备预配服务 。From the left-hand menu in the Azure portal, select All resources and then select your Device Provisioning service. 打开服务的“管理注册”,然后选择“个人注册”选项卡 。选中在本快速入门中注册的设备的“注册 ID”旁边的复选框,然后按窗格顶部的“删除”按钮 。Open Manage Enrollments for your service, and then select the Individual Enrollments tab. Select the check box next to the REGISTRATION ID of the device you enrolled in this quickstart, and press the Delete button at the top of the pane.
  3. 在 Azure 门户的左侧菜单中选择“所有资源”,然后选择 IoT 中心 。From the left-hand menu in the Azure portal, select All resources and then select your IoT hub. 打开中心的“IoT 设备”,选中在本快速入门中注册的设备的“设备 ID”旁边的复选框,然后按窗格顶部的“删除”按钮 。Open IoT devices for your hub, select the check box next to the DEVICE ID of the device you registered in this quickstart, and then press the Delete button at the top of the pane.

后续步骤Next steps

本快速入门介绍了如何在 Windows 计算机上创建模拟 X.509 设备,以及如何使用门户中的 Azure IoT 中心设备预配服务将其预配到 IoT 中心。In this quickstart, you've created a simulated X.509 device on your Windows machine and provisioned it to your IoT hub using the Azure IoT Hub Device Provisioning Service on the portal. 若要了解如何以编程方式注册 X.509 设备,请继续阅读快速入门中关于 X.509 设备的编程注册内容。To learn how to enroll your X.509 device programmatically, continue to the quickstart for programmatic enrollment of X.509 devices.