gpt4 book ai didi

c# - Azure IoT 中心对其他设备的未经授权访问

转载 作者:行者123 更新时间:2023-12-03 00:04:33 26 4
gpt4 key购买 nike

我在尝试使用 x509 证书通过 DPS 将第二台设备注册到 IoT 中心时遇到问题。我的根证书颁发机构在 DPS 和 IoT 中心(通过 openssl 生成)上都存在并经过验证。至于客户端证书,我会在应用程序启动后(如果不存在)在下面的代码中生成它。令我困扰的是,每个设备都正确注册到 Azure DPS,但只有第一个设备获得授权和注册。我在客户端证书创建过程中所做的事情是否可能会造成困惑?此外,在设备注册到 IoT 中心期间,在此行中发现了错误:

DeviceRegistrationResult result = await provisioningDeviceClient.RegisterAsync().ConfigureAwait(false);

添加错误:

2019/12/16 09:37:38.309|错误|尝试启动服务时发现错误设备无法在 IoT 中心注册:设备无法正确配置:AMQP 传输异常 |潮汐.DeviceAgent.DeviceAgent |

客户端证书生成

        X509Certificate2 caRootCertificate;
X509Store caStore = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
caStore.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);

X509Certificate2Collection signerCollection = (X509Certificate2Collection)caStore.Certificates.Find(X509FindType.FindByIssuerName, "CERTNAME", true);

caStore.Close();

if (signerCollection.Count != 0)
{
caRootCertificate = signerCollection[0];

using (var rsa = RSA.Create())
{
rsa.KeySize = 2048;

var clientCertificateRequest = new CertificateRequest($"CN={_writableOptions.Value.RegistrationId}", rsa, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);

clientCertificateRequest.CertificateExtensions.Add(new X509BasicConstraintsExtension(false, false, 0, false));

var issuerSubjectKey = caRootCertificate.Extensions["Subject Key Identifier"].RawData;
var segment = new ArraySegment<byte>(issuerSubjectKey, 2, issuerSubjectKey.Length - 2);
var authorityKeyIdentifier = new byte[segment.Count + 4];

authorityKeyIdentifier[0] = 0x30;
authorityKeyIdentifier[1] = 0x16;
authorityKeyIdentifier[2] = 0x80;
authorityKeyIdentifier[3] = 0x14;
segment.CopyTo(authorityKeyIdentifier, 4);
clientCertificateRequest.CertificateExtensions.Add(new X509Extension("2.5.29.35", authorityKeyIdentifier, false));


var sanBuilder = new SubjectAlternativeNameBuilder();
sanBuilder.AddDnsName(_writableOptions.Value.RegistrationId);
var sanExtension = sanBuilder.Build();
clientCertificateRequest.CertificateExtensions.Add(sanExtension);

clientCertificateRequest.CertificateExtensions.Add(new X509EnhancedKeyUsageExtension(new OidCollection { new Oid("1.3.6.1.5.5.7.3.2") }, false));
clientCertificateRequest.CertificateExtensions.Add(new X509SubjectKeyIdentifierExtension(clientCertificateRequest.PublicKey, false));

var notBefore = DateTimeOffset.UtcNow.AddDays(-1);

if (notBefore < caRootCertificate.NotBefore)
{
notBefore = new DateTimeOffset(caRootCertificate.NotBefore);
}

var notAfter = DateTimeOffset.UtcNow.AddDays(365);

if (notAfter > caRootCertificate.NotAfter)
{
notAfter = new DateTimeOffset(caRootCertificate.NotAfter);
}

var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
var unixTime = Convert.ToInt64((DateTime.UtcNow - epoch).TotalSeconds);
var serial = BitConverter.GetBytes(unixTime);

using (var cert = clientCertificateRequest.Create(caRootCertificate, notBefore, notAfter, serial))
{
X509Certificate2 client = cert.CopyWithPrivateKey(rsa);

return await Task.FromResult(client);
}
}
}
else
{
throw new FileNotFoundException($"Could not find a root certificate.");
}

DPS 设备注册

    Attestation attestation = X509Attestation.CreateFromClientCertificates(new X509Certificate2(certificate.Export(X509ContentType.Cert)));

IndividualEnrollment individualEnrollment = new IndividualEnrollment(_writableOptions.Value.RegistrationId, attestation)
{
DeviceId = _writableOptions.Value.DeviceId,
ProvisioningStatus = ProvisioningStatus.Enabled
};

individualEnrollmentResult = await _provisioningServiceClient.CreateOrUpdateIndividualEnrollmentAsync(individualEnrollment).ConfigureAwait(false);

设备注册到物联网中心

using (var certificatePassword = new X509Certificate2(certificate.GetRawCertData(), _writableOptions.Value.CertPass))
{
using (var security = new SecurityProviderX509Certificate(certificatePassword))
{
using (var transport = new ProvisioningTransportHandlerAmqp(TransportFallbackType.TcpOnly))
{
ProvisioningDeviceClient provisioningDeviceClient = ProvisioningDeviceClient.Create(_writableOptions.Value.AzureEndpoint, _writableOptions.Value.IdScope, security, transport);
DeviceRegistrationResult result = await provisioningDeviceClient.RegisterAsync().ConfigureAwait(false);
IAuthenticationMethod authenticationMethod = new DeviceAuthenticationWithX509Certificate(result.DeviceId, certificate);
DeviceClient deviceClient = DeviceClient.Create(result.AssignedHub, authenticationMethod, TransportType.Amqp_Tcp_Only);

return await Task.FromResult(deviceClient);
}
}
}

最佳答案

我发现了这个问题。当证书在商店中生成时,我使用 FindByIssuerName 来查找证书。

X509Certificate2Collection signerCollection = (X509Certificate2Collection)caStore.Certificates.Find(X509FindType.FindByIssuerName, "CERTNAME", true);

经进一步调查,店内有两张同名证书。问题:MMC 管理单元仅显示一个证书。环顾四周后,有人建议在商店上运行 storerepair 命令。运行存储修复命令后,我可以在 MMC 中看到两个证书,并能够删除有问题的证书,从而防止检测到有效证书。

Windows 版本:Windows Embedded 8.1 Industry Pro

关于c# - Azure IoT 中心对其他设备的未经授权访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59293609/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com