【问题标题】:Code signing with signtool fails due to private key filter由于私钥过滤器,使用 signtool 进行代码签名失败
【发布时间】:2015-04-25 19:00:36
【问题描述】:

在尝试签署由我工作的公司创建的某些安装程序时,我遇到了一个错误,我无法解决。我正在使用已在另一台机器(Win7)上成功使用的相同证书,以相同的方式签署准相同的安装程序。无论如何,在运行 CruiseControl.net 的 Windows Server 2008 上,我尝试使用 signtool.exe 对安装程序进行签名,但失败并出现以下错误:

The following certificates were considered:
    Issued to: <our company>
    Issued by: <some ca>
    Expires:   <is valid>
    SHA1 hash: <...>

    Issued to: <...>
    Issued by: <...>
    Expires:   <...>
    SHA1 hash: <...>

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

我尝试将证书安装到不同的证书存储,尝试了不同版本的signtool.exe并尝试直接使用.cer文件,但没有任何区别。在所有情况下,我都会收到上述错误。我尝试了以下命令行命令

signtool.exe sign /debug /n "MyCompany" C:\my\installer.exe
signtool.exe sign /debug /f C:\path\to\my\certificate.cer C:\my\installer.exe

但在某些情况下我离开了 /debug。我做错了什么或遗漏了什么?

【问题讨论】:

  • 我有一个相关的问题,即私钥过滤器在正常运行 cmd 时会丢弃我的证书,但如果我以管理员身份运行它,signtool 会正确选择正确的证书并签名。 stackoverflow.com/questions/56563732/…

标签: windows certificate code-signing private-key signtool


【解决方案1】:

只是为了确保...
您需要一个特殊的“代码签名”证书才能签署您的代码...
我浪费了几个小时试图用我们的域证书签署我们的应用程序,这是不可能的。

【讨论】:

    【解决方案2】:

    我遇到了同样的问题。这就是我所做的 - 假设您已经设置了代码签名的其余先决条件。

    1. 关闭解决方案并关闭 VS IDE
    2. 以管理员身份运行启动 Visual Studio IDE
    3. 打开解决方案并重建它

    这次签名过程成功了。

    我认为您拥有用于签名的正确证书文件和私钥,并将证书导入到受信任的存储区。

    如果没有,您可以继续阅读。

    1. 导入pfx文件-“xyz.pfx”::打开命令提示符(启动-运行 作为管理员权限)并运行以下命令

      certutil.exe -p mypassword -importpfx xyz.pfx

      命令的输出如下所示:

      证书“CN=证书名称,O=BLAH,OU=BLAH,E=john.doe@yourorg.com”添加到 店铺。 CertUtil: -importPFX 命令成功完成。

    2. 使用以下命令将证书添加到受信任的根证书颁发机构。有很多方法可以做,但我用这个。打开命令提示符(启动 - 以管理员权限运行)并运行以下命令

      certmgr.exe -add -c mycertificate.cer -s -r localMachine root

      你会看到这样的输出消息

      CertMgr 成功

    3. 签署程序集。 Visual Studio IDE 中的简单过程。

      • 右键单击解决方案中的项目并打开其属性。
      • 打开Signing部分并点击Sign the Assembly复选框(启用)
      • 使用浏览选项选择 pfx 文件并输入密码
        您也可以创建一个新的,但您需要一个与此 pfx 文件匹配的证书文件,否则您的签名将失败。
      • 保存更改
      • 现在,点击 Build Events 部分并点击 Edit Post-Build 以打开构建后事件的编辑器。输入以下命令。

        "C:\Program Files (x86)\Windows Kits\10\bin\10.0.18362.0\x64\signtool.exe" sign /v /sm "$(TargetPath)"

        signtool.exe 的路径取决于您安装 Windows SDK 的位置/是否安装。

    现在构建解决方案,您的解决方案应该会成功构建并且您的二进制文件已签名。确保以管理员权限运行 VS IDE。

    【讨论】:

    • 我想我发现了同样的问题。我问了这个问题,希望能更多地理解它,并发现是否有不需要我以管理员身份运行所有内容的修复程序。 stackoverflow.com/questions/56563732/…
    【解决方案3】:

    我还必须使用从其他来源(类似于您)收到的证书签署文件。对我来说,问题是我只在我的电脑上使用“当前用户”选项安装了证书。一旦我使用“本地机器”选项安装它,它就可以工作了。

    【讨论】:

      【解决方案4】:

      我在 Win7 机器上遇到了同样的问题,并尝试了好人在这篇文章中提出的所有建议,但没有成功。然后,即使我的机器只有一个帐户并且它具有管理员权限,我通过“以管理员身份运行”打开命令提示符窗口,然后我安装的所有版本的 signtool.exe 开始工作。

      【讨论】:

        【解决方案5】:

        为了签署文件,您需要拥有证书的私钥,该私钥不包含在您从 Windows 7 机器复制的 *.cer 文件中。要导出证书及其私钥,您可以关注instructions supplied here

        请注意,如果证书在创建时设置为允许导出私钥,您将只能导出私钥(通过将-pe 传递给makecert

        【讨论】:

        • 亲爱的阿迪,非常感谢您,但这不是问题所在。我想我进口了原版的cer,但即使我没有(实际上我不知道,因为我是从同事那里收到的,但我认为它是原版)我也尝试使用原版,我收到了在磁盘上。
        • 正如我所说,*.cer 文件(无论是否为原始文件)不包含私钥,因此不能用于签署文件。您应该要求您的同事导出证书,这将产生一个您需要导入的 *.pfx 文件。只有这样您才能签署文件。
        • 非常感谢您的帮助,我的同事假期回来了,他确实给了我导出的文件。
        【解决方案6】:

        我有相同的症状,但完全是不同的原因。正如许多开发人员所做的那样,我的系统上安装了一堆不同的工具链。我只是对他们进行了调查,以展示这看起来如何;滚动到此答案的底部以查看完整列表。

        我已经像往常一样在提升的命令提示符下使用 certutil -importPFX cert.pfx 将我的代码签名证书从 VeriSign 安装到系统证书存储(需要 /smsigntool.exe)。

        第一次测试看起来很有希望,但突然签名开始失败。

        为了调试问题,我首先使用signtool.exe sign /debug /v /a /sm ... 来查看问题所在。输出看起来像这样(另见问题):

        The following certificates were considered:
            Issued to: localhost
            Issued by: localhost
            Expires:   Tue Dec 26 00:00:00 2017
            SHA1 hash: <...>
        
            Issued to: <...>
            Issued by: Symantec Class 3 SHA256 Code Signing CA
            Expires:   <...>
            SHA1 hash: <...>
        
        After EKU filter, 1 certs were left.
        After expiry filter, 1 certs were left.
        After Root Name filter, 1 certs were left.
        After Private Key filter, 0 certs were left.
        SignTool Error: No certificates were found that met all the given criteria.
        

        我可以排除丢失的私钥,因为证书存储清楚地表明我有一个匹配的私钥

        现在我记得有 some recent patches 允许 Windows 7 接受使用具有 SHA256 哈希的证书生成的签名。当然,大多数较早的文章都会声明 Windows 7 根本无法处理 SHA-2 哈希。

        所以这已经让我朝着“它必须是涉及签名的旧版本”的方向轻推。

        仍然决定删除证书加密钥并使用前面显示的调用重新导入它。

        然后,在调查了我的系统后(见答案底部),我发现了高达 五个 不同版本的signtool.exe。所以我开始尝试最新的版本(6.3.9600.17298,来自 Windows 8.1 SDK),它立即生效

        signtool.exe sign /debug /v /a /sm /r VeriSign /ac MSCV-VSClass3.cer /ph  /t "http://timestamp.verisign.com/scripts/timstamp.dll" *.exe
        
        The following certificates were considered:
            Issued to: localhost
            Issued by: localhost
            Expires:   Tue Dec 26 00:00:00 2017
            SHA1 hash: <...>
        
            Issued to: <...>
            Issued by: Symantec Class 3 SHA256 Code Signing CA
            Expires:   <...>
            SHA1 hash: <...>
        
        After EKU filter, 1 certs were left.
        After expiry filter, 1 certs were left.
        After Root Name filter, 1 certs were left.
        After Private Key filter, 1 certs were left.
        The following certificate was selected:
            Issued to: <...>
            Issued by: Symantec Class 3 SHA256 Code Signing CA
            Expires:   <...>
            SHA1 hash: <...>
        
        Cross certificate chain (using machine store):
            Issued to: Microsoft Code Verification Root
            Issued by: Microsoft Code Verification Root
            Expires:   Sat Nov 01 13:54:03 2025
            SHA1 hash: 8FBE4D070EF8AB1BCCAF2A9D5CCAE7282A2C66B3
        
                Issued to: VeriSign Class 3 Public Primary Certification Authority - G5
                Issued by: Microsoft Code Verification Root
                Expires:   Mon Feb 22 19:35:17 2021
                SHA1 hash: 57534CCC33914C41F70E2CBB2103A1DB18817D8B
        
                    Issued to: Symantec Class 3 SHA256 Code Signing CA
                    Issued by: VeriSign Class 3 Public Primary Certification Authority - G5
                    Expires:   Sat Dec 09 23:59:59 2023
                    SHA1 hash: 007790F6561DAD89B0BCD85585762495E358F8A5
        
                        Issued to: <...>
                        Issued by: Symantec Class 3 SHA256 Code Signing CA
                        Expires:   <...>
                        SHA1 hash: <...>
        
        
        The following additional certificates will be attached:
            Issued to: VeriSign Class 3 Public Primary Certification Authority - G5
            Issued by: Microsoft Code Verification Root
            Expires:   Mon Feb 22 19:35:17 2021
            SHA1 hash: 57534CCC33914C41F70E2CBB2103A1DB18817D8B
        
            Issued to: Symantec Class 3 SHA256 Code Signing CA
            Issued by: VeriSign Class 3 Public Primary Certification Authority - G5
            Expires:   Sat Dec 09 23:59:59 2023
            SHA1 hash: 007790F6561DAD89B0BCD85585762495E358F8A5
        
        Done Adding Additional Store
        Successfully signed: <...>.exe
        
        Number of files successfully Signed: 1
        Number of warnings: 0
        Number of errors: 0
        

        进一步追踪这个问题,我以为我找到了问题所在。然而,事实证明,我得到的错误并不是我在旧的signtool.exe 版本中看到的错误。相反,旧版本会分别抱怨 /ac/fd/ph 是无法识别的命令行选项。

        所以我需要更深入地挖掘,结果发现我的(替代)文件管理器是罪魁祸首。我通常使用该文件管理器和一个方便的键盘快捷键在相应的文件夹中启动我的命令提示符。事实证明,它有时不会传递环境变量——本质上是文件管理器“忘记”了环境变量。事实证明这是根本原因。使用 Win+R 打开命令提示符,然后使用 cmd Enter 不会公开此行为,尽管从同一文件夹执行 signtool.exe

        我对此的最佳猜测是,由于 PATH 变量或类似变量搞砸了,signtool.exe 最终选择了错误的 DLL。值得注意的是 mssign32.dllwintrust.dll 在 Windows SDK 8.0 和 8.1 的同一文件夹中伴随 signtool.exe,但不适用于任何早期版本的 signtool.exe,它们将选择“全局”系统范围的 DLL,无论结果如何。


        在我的系统上,我有五个不同版本的signtool.exe

        signtool.exe 5.2.3790.1830

        甚至不理解我使用的/ac/ph 参数(也不是/fd)。但奇怪的是,没有这两个参数。

        • C:\Program Files (x86)\Microsoft Visual Studio 8\SDK\v2.0\Bin\signtool.exe

        signtool.exe 6.0.4002.0

        甚至不理解我使用的/ac/ph 参数(也不是/fd)。但奇怪的是,没有这两个参数。

        • C:\Program Files (x86)\Microsoft Visual Studio 8\Common7\Tools\Bin\signtool.exe

        signtool.exe 6.1.7600.16385

        了解/fd sha256的第一个版本。

        • C:\WINDDK\7600.16385.1\bin\amd64\SignTool.exe
        • C:\WINDDK\7600.16385.1\bin\x86\SignTool.exe
        • C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\signtool.exe
        • C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\signtool.exe
        • C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\Bin\signtool.exe

        signtool.exe 6.2.9200.20789

        • C:\Program Files (x86)\Windows Kits\8.0\bin\x64\signtool.exe
        • C:\Program Files (x86)\Windows Kits\8.0\bin\x86\signtool.exe

        signtool.exe 6.3.9600.17298

        • C:\Program Files (x86)\Windows Kits\8.1\bin\arm\signtool.exe
        • C:\Program Files (x86)\Windows Kits\8.1\bin\x64\signtool.exe
        • C:\Program Files (x86)\Windows Kits\8.1\bin\x86\signtool.exe

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2022-08-06
          • 2016-05-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-01-13
          • 1970-01-01
          相关资源
          最近更新 更多