如何通过新的自签名证书 cmdlet 创建代码签名证书



PowerShell 4.0

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

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

New-SelfSignedCertificate -DnsName www.SomeSite.com -CertStoreLocation Cert:CurrentUserMy

我没有看到与-eku类似的选项.

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

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,则应该能够实现所需的目标。

恢复这个问题,因为我也在寻找使用 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:CurrentUserMy `
                          -TextExtension @("$EKU={text}$EKU_CODE_SIGNING")
$password = ConvertTo-SecureString -String "mypassword" -Force -AsPlainText
Export-PfxCertificate -Cert "Cert:CurrentUserMy$($certificate.Thumbprint)" -FilePath "codesigning.pfx" -Password $password

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

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

最新更新