azure - 在 Azure 设备预配服务中对多个设备使用相同的 X509 证书

标签 azure iot azure-iot-hub

我必须在 Azure 设备预配服务中注册多个设备,并且我正在使用组注册来实现相同的目的。我也创建了自签名 X509 证书和注册组。我使用示例代码向该组注册了一个模拟设备。我想创建另一个具有相同证书的模拟设备并注册到组中。那可能吗?示例应用程序的输入是设备配置服务的 ID 范围和证书。如何添加另一个设备。

    if (string.IsNullOrWhiteSpace(s_idScope))
    {
        Console.WriteLine("ProvisioningDeviceClientX509 <IDScope>");
        return 1;
    }

    X509Certificate2 certificate = LoadProvisioningCertificate();

    using (var security = new SecurityProviderX509Certificate(certificate))


    {
        ProvisioningDeviceClient provClient =
            ProvisioningDeviceClient.Create(GlobalDeviceEndpoint, s_idScope, security, transport);

        var sample = new ProvisioningDeviceClientSample(provClient, security);
        sample.RunSampleAsync().GetAwaiter().GetResult();
    }

    return 0;
}

    private static X509Certificate2 LoadProvisioningCertificate()
{
    string certificatePassword = ReadCertificatePassword();

    var certificateCollection = new X509Certificate2Collection();
    certificateCollection.Import(s_certificateFileName, certificatePassword, X509KeyStorageFlags.UserKeySet);

            X509Certificate2 certificate = null;

            foreach (X509Certificate2 element in certificateCollection)
            {
                Console.WriteLine($"Found certificate: {element?.Thumbprint} {element?.Subject}; PrivateKey: {element?.HasPrivateKey}");
                if (certificate == null && element.HasPrivateKey)
                {
                    certificate = element;
                }
                else
                {
                    element.Dispose();
                }
            }

            if (certificate == null)
            {
                throw new FileNotFoundException($"{s_certificateFileName} did not contain any certificate with a private key.");
            }
            else
            {
                Console.WriteLine($"Using certificate {certificate.Thumbprint} {certificate.Subject}");
            }

            return certificate;
        }

        private static string ReadCertificatePassword()
        {
            var password = new StringBuilder();
            Console.WriteLine($"Enter the PFX password for {s_certificateFileName}:");

            while (true)
            {
                ConsoleKeyInfo key = Console.ReadKey(true);
                if (key.Key == ConsoleKey.Backspace)
                {
                    if (password.Length > 0)
                    {
                        password.Remove(password.Length - 1, 1);
                        Console.Write("\b \b");
                    }
                }
                else if (key.Key == ConsoleKey.Enter)
                {
                    Console.WriteLine();
                    break;
                }
                else
                {
                    Console.Write('*');
                    password.Append(key.KeyChar);
                }
            }

            return password.ToString();
        }
    }
}

最佳答案

在 Azure 中实现的客户端身份验证(用于验证参与方的 X.509)需要每个终端节点具有唯一的叶证书和私钥,有点像公钥/私钥对。

此 key 对用于验证当事人的身份。

每个端节点必须拥有唯一的 key 对才能执行此操作。该 key 对是从受信任的证书链生成的,生成的 key 对称为叶子。

证书链可以是 CA 签名的,也可以是自签名的(自签名仅用于开发/测试目的,不适合生产)。

在此链中,您有一个根证书,您可以从中生成叶子。您可以在链中生成任意数量的叶子。每个独特的叶子都可以用作每个设备的 key 对。

对于您的情况,您可以使用 OpenSSL 生成自签名根证书,然后为您的所有设备生成尽可能多的自签名叶证书。

关于azure - 在 Azure 设备预配服务中对多个设备使用相同的 X509 证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53946286/

相关文章:

Azure 策略审核经典 SQL 漏洞评估

android - 蓝牙脉搏血氧仪

azure - 未从 Azure IoT MQTT 代理接收订阅主题的消息

azure-functions - 具有 IoT 中心触发获取发送设备的 Azure 功能

azure - 当前物联网边缘解决方案的应用程序洞察集成最佳实践

azure - 如何从前端获取来自azure服务总线主题的通知?

azure - 使用 Azure CLI 列出事件的 Azure Monitor 警报

azure - 删除 Azure SDK 生成的日志

node.js - 使用 Node.js 的单独索引表从 dynamodb 表获取项目

java - 如何使用java SDK生成AWS bootstrap证书?