【问题标题】:How to create the code signing certificate through the New-SelfSignedCertificate cmdlet如何通过 New-SelfSignedCertificate cmdlet 创建代码签名证书
【发布时间】:2020-03-31 02:40:05
【问题描述】:

PowerShell 4.0

makecert 工具具有-eku 选项,用于将增强的密钥使用对象标识符 (OID) 描述到证书中。它允许为 代码签名 和其他目的制作证书。但它不是 cmdlet。

新的 PowerShell 版本具有用于本地测试脚本的 New-SelfSignedCertificate cmdlet。但它创建的证书不能用于代码签名:

New-SelfSignedCertificate -DnsName www.SomeSite.com -CertStoreLocation Cert:\CurrentUser\My

我没有看到类似于 -eku 的选项。

如何设置我的新自签名证书(通过 New-SelfSignedCertificate cmdlet 创建)的目的地,以便将其用于代码签名?或者是否可以通过其他 cmdlet 执行相同的操作?

【问题讨论】:

    标签: powershell


    【解决方案1】:

    PS 4 上New-SelfSignedCertificate 的版本相当基础。

    但是Powershell v5 具有创建特定密钥所需的参数。

    具体来说是一个Keyusage 参数

    -- CertSign
    -- CRLSign
    -- DataEncipherment
    -- DecipherOnly
    -- DigitalSiganture
    -- EncipherOnly
    -- KeyAgreement
    -- KeyEncipherment
    -- None (default) 
    -- NonRepudiation
    

    KeyUsageProperty 服用

    -- All
    -- Decrypt
    -- KeyAgreement
    -- None (default) 
    -- Sign
    

    您是否专门与 v4 相关联?如果您可以升级到 v5,您应该能够实现您所需要的。

    【讨论】:

      【解决方案2】:

      重新提出这个问题,因为我也在寻找使用 PowerShell New-SelfSignedCertificate 命令为代码签名设置增强型密钥使用 (EKU) 字段的答案。

      可以使用-TextExtension参数来设置EKU值。例如,以下 PowerShell(在 PowerShell 5.1 上测试)脚本允许创建具有扩展密钥用途的 3 年自签名代码签名证书(并将其从当前用户的证书存储导出为 pfx 文件格式):

      # Enhanced Key Usage
      $EKU = "2.5.29.37"
      $EKU_CODE_SIGNING = "1.3.6.1.5.5.7.3.3"
      
      $certificate = New-SelfSignedCertificate -Subject "CN=Testing Code Signing,E=info@mycompany.com,O=My Company" `
                                -FriendlyName "My Code Signing Certificate" `
                                -NotAfter (Get-Date).AddYears(3) `
                                -CertStoreLocation Cert:\CurrentUser\My `
                                -TextExtension @("$EKU={text}$EKU_CODE_SIGNING")
      
      $password = ConvertTo-SecureString -String "mypassword" -Force -AsPlainText
      
      Export-PfxCertificate -Cert "Cert:\CurrentUser\My\$($certificate.Thumbprint)" -FilePath "codesigning.pfx" -Password $password
      

      注意:作为一种快捷方式,可以使用New-SelfSignedCertificate 命令指定-Type CodeSigningCert 参数,而不是将EKU_CODE_SIGNING 字符串显式添加到-TextExtension 参数。

      【讨论】:

        【解决方案3】:

        您可以使用 PS 的证书提供程序来访问不同的证书存储(用户与机器),但这无助于您的 OID 问题。我建议您查看 .NET 对 X509 证书的支持。谷歌“.net x509 证书”,你会在 MSDN 上找到 X509Certificate 类。从那里阅读类文档和任何概述文档,以查看是否支持创建 OID。如果 .NET 不支持它,那么您必须使用 P/Invoke 来调用本机 Windows CNG(下一代加密)API

        【讨论】:

          猜你喜欢
          • 2018-01-19
          • 2016-10-10
          • 2013-01-30
          • 1970-01-01
          • 1970-01-01
          • 2014-02-03
          • 2019-11-07
          • 2013-01-02
          • 1970-01-01
          相关资源
          最近更新 更多