【问题标题】:Unable to code sign with Windows signtool无法使用 Windows 签名工具进行代码签名
【发布时间】:2016-12-12 10:08:08
【问题描述】:

我试图通过创建我自己的自签名证书并成功签署文件来了解 Windows 上可执行文件的代码签名。 signtool 抛出错误并说“未找到符合所有给定条件的证书”。

我做错了什么?

这是我所做的:

按照 MS 和其他博客的一些说明,我创建了一个自签名证书,如下所示:

New-SelfSignedCertificate -certstorelocation Cert:\LocalMachine\my -dnsname cameronnokes.com

$pwd = ConvertTo-SecureString -String "password" -Force -AsPlainText

Export-PfxCertificate -cert Cert:\LocalMachine\my\C7A94086D80A42151551A9FCCEACBC0B4A9ABA1A -FilePath 'C:\Users\Cameron Nokes\selfcert.pfx' -Password $pwd

Get-ChildItem -Recurse Cert:\LocalMachine\my

最后一个 Get-ChildItem 命令显示指纹和主题,看起来就像我期望的那样。

现在,我在 cmd 中运行:

signtool.exe sign /f selfcert.pfx /p password /debug test.ps1


The following certificates were considered:
    Issued to: cameronnokes.com
    Issued by: cameronnokes.com
    Expires:   Sat Aug 05 12:37:28 2017
    SHA1 hash: C7A94086D80A42151551A9FCCEACBC0B4A9ABA1A

After EKU filter, 0 certs were left.
After expiry filter, 0 certs were left.
After Private Key filter, 0 certs were left.
SignTool Error: No certificates were found that met all the given criteria.

【问题讨论】:

  • 您必须生成具有代码签名增强密钥用法的证书。你不是,这就是为什么它说在 EKU 过滤器之后没有留下任何东西。 New-SelfSignedCertificate 不允许您指定 EKU,因此您不能为此使用它。

标签: windows powershell cmd code-signing


【解决方案1】:

花点时间记住 certreq 的怪癖,默认情况下它就在那里,您可以使用它为您生成密钥。

提出这个请求.inf:

[NewRequest]
Subject = "CN=cameronnokes.com"
Exportable = TRUE
KeyLength = 2048
KeySpec = 1
KeyUsage = 0xA0
RequestType = Cert

[EnhancedKeyUsageExtension]
OID = 1.3.6.1.5.5.7.3.3 ; Code signing

然后运行这个命令:

certreq -new request.inf nothing.csr

然后您可以同时删除 request.inf 和 nothing.csr。此时,您的个人商店中有一个自签名代码签名证书。

$Certificate = Get-ChildItem cert:\CurrentUser\My |
    Where-Object { $_.EnhancedKeyUsageList.FriendlyName -eq 'Code Signing' -and $_.NotAfter -gt (Get-Date) } |
    Sort-Object NotAfter |
    Select-Object -Last 1

你在选择证书的时候不会真的想这么模糊,这样只是为了示例。

最后,签个ps1:

Set-AuthenticodeSignature test.ps1 -Certificate $Certificate

简单吧?

【讨论】:

  • 太棒了,这成功了!我正在寻找一种专门使用signtool签名的方法。创建证书后,我可以通过将 SHA1 传递给 signtool 来成功签名。完全简单。哈。
  • 这看起来完全符合我的需要 - 除了我运行 certreq 时,它提示我选择智能卡设备,我不知道为什么。
  • 啊 - 添加 ProviderName="Microsoft Enhanced Cryptographic Provider v1.0"[NewRequest] 部分为我解决了这个问题,找到了 social.technet.microsoft.com/Forums/office/en-US/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-13
  • 2023-02-26
  • 1970-01-01
  • 2013-04-21
  • 1970-01-01
  • 2012-09-10
  • 1970-01-01
相关资源
最近更新 更多