Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 自动化在.NET Core中创建私钥、CSR和最终签名证书的过程_C#_.net_Ssl_.net Core_Azure Iot Hub - Fatal编程技术网

C# 自动化在.NET Core中创建私钥、CSR和最终签名证书的过程

C# 自动化在.NET Core中创建私钥、CSR和最终签名证书的过程,c#,.net,ssl,.net-core,azure-iot-hub,C#,.net,Ssl,.net Core,Azure Iot Hub,有人向我提供了一个中间证书(由根证书签名)及其密码。为了安全地连接到SSL enpoint,我需要提供一个签名证书 以下是我的工作: 创建私钥 使用该私钥创建CSR 使用提供的中间证书进行签名 我使用openssl实现了所有这些,它工作得完美无缺:TLS端点接受了我的连接请求。在C#中,尽管它不起作用。你知道少了什么吗 var password = new SecureString(); password.AppendChar('x'); pass

有人向我提供了一个中间证书(由根证书签名)及其密码。为了安全地连接到SSL enpoint,我需要提供一个签名证书

以下是我的工作:

  • 创建私钥
  • 使用该私钥创建CSR
  • 使用提供的中间证书进行签名
  • 我使用openssl实现了所有这些,它工作得完美无缺:TLS端点接受了我的连接请求。在C#中,尽管它不起作用。你知道少了什么吗

            var password = new SecureString();
            password.AppendChar('x');
            password.AppendChar('y');
            password.AppendChar('z');
    
            // Intermediate certificate with pw protection
            X509Certificate2 intermediateCert = new X509Certificate2("intermediate.pfx", password);
            X509Certificate2 certificate;
    
           using (RSA rsa = RSA.Create(2048))
            {
    
                //A client creates a certificate signing request.
                CertificateRequest req = new CertificateRequest(
                        new X500DistinguishedName("CN=myname"),
                        rsa,
                        HashAlgorithmName.SHA256,
                        RSASignaturePadding.Pkcs1);
    
                req.CertificateExtensions.Add(
                    new X509BasicConstraintsExtension(false, false, 0, false));
    
                req.CertificateExtensions.Add(
                    new X509KeyUsageExtension(
                        X509KeyUsageFlags.DigitalSignature | X509KeyUsageFlags.KeyEncipherment,
                        false));
    
                req.CertificateExtensions.Add(
                    new X509EnhancedKeyUsageExtension(
                        new OidCollection
                        {
                            new Oid("1.3.6.1.5.5.7.3.**1**")
                        },
                        true));
    
                req.CertificateExtensions.Add(
                    new X509SubjectKeyIdentifierExtension(req.PublicKey, false));
    
    
                X500DistinguishedName dname = new X500DistinguishedName(intermediateCert.Subject);
                X509SignatureGenerator gen = X509SignatureGenerator.CreateForRSA((RSA)intermediateCert.PrivateKey, RSASignaturePadding.Pkcs1);
    
                certificate = req.Create(
                    dname, gen,
                    DateTimeOffset.UtcNow.AddDays(-1),
                    DateTimeOffset.UtcNow.AddDays(10),
                    new byte[] {1, 2, 3, 4});
    
              // Use certificate to connect to Azure IOT HUB
            }
    
    编辑:本例中的命名端点是Azure IoT Hub。错误消息是:

    AMQP例外: {“错误代码”:401002,“跟踪ID”:“e930cb7c-d1a5-4ad0-bb5d-e9b4ee97e8b2”,“消息”:“IoTubUnauthorizedAccess”,“时间UTC”:“2018-10-12T18:08:23.2328669Z”}

    编辑:工作证书

    X509v3 extensions:
        X509v3 Basic Constraints:
            CA:FALSE
        Netscape Cert Type:
            SSL Server
        Netscape Comment:
            OpenSSL Generated Server Certificate
        X509v3 Subject Key Identifier:
            9E:86:FF:19:70:DB:5D:56:2D:16:48:E3:81:76:66:FD:17:C8:82:9A
        X509v3 Authority Key Identifier:
            keyid:E0:31:C5:81:6F:75:6A:AC:9C:F1:B1:B8:72:B7:E3:C6:AB:4C:E9:89
            DirName:/CN=abuscert
            serial:01
    
        X509v3 Key Usage: critical
            Digital Signature, Key Encipherment
        X509v3 Extended Key Usage:
            TLS Web Server Authentication
    
    用C#创建的一个:


    你能更详细地描述一下“不起作用”吗?有例外吗?证书已生成,但不知何故无效(如何无效?)更新了我的答案。仅供参考,我使用相同的中间证书使用openssl创建签名证书,因此它不是Azure IoT Hub端点问题。您用于EKU的OID为“1.3.6.1.5.5.7.3.8”。您是否有意创建时间戳证书(看起来很奇怪)。如果将工作openssl证书的证书与此证书的输出进行比较,有什么不同?此外,通常EKU扩展未标记为关键(构造函数中
    X509EnhancedKeyUsageExtension
    的最后一个参数应为false)。相反,
    BasicConstraints
    约束应该是关键的,因此将其设置为true;见编辑EKU和基本约束对我来说似乎是可疑的(我怀疑netscape扩展的问题)。
    X509v3 extensions:
                X509v3 Basic Constraints:
                    CA:FALSE
                X509v3 Key Usage:
                    Digital Signature, Key Encipherment
                X509v3 Extended Key Usage:
                    TLS Web Server Authentication, Time Stamping
                X509v3 Subject Key Identifier:
                    00:5E:F2:48:5C:E4:CA:B5:F3:5F:A6:0E:62:7C:FF:61:87:B1:D5:89