如何使用 Windows SDK 中的工具创建用于代码签名的自签名证书?

有帮助吗?

解决方案

更新答案

如果您使用以下 Windows 版本或更高版本:Windows Server 2012、Windows Server 2012 R2 或 Windows 8.1 然后 MakeCert 现已弃用, ,微软建议使用 PowerShell Cmdlet 新的自签名证书.

如果您使用的是旧版本(例如 Windows 7),则需要坚持使用 MakeCert 或其他解决方案。有些人 建议公钥基础设施 Powershell (PSPKI) 模块.

原答案

虽然您可以创建自签名代码签名证书(SPC - 软件发行者证书)一口气,我更喜欢执行以下操作:

创建自签名证书颁发机构 (CA)

makecert -r -pe -n "CN=My CA" -ss CA -sr CurrentUser ^
         -a sha256 -cy authority -sky signature -sv MyCA.pvk MyCA.cer

(^ = 允许批处理命令行换行)

这将创建一个自签名 (-r) 证书,带有可导出的私钥 (-pe)。它名为“My CA”,应放置在当前用户的 CA 存储中。我们正在使用 SHA-256 算法。密钥用于签名(-sky)。

私钥应存储在 MyCA.pvk 文件中,证书应存储在 MyCA.cer 文件中。

导入CA证书

因为如果您不信任 CA 证书,那么它就没有意义,因此您需要将其导入 Windows 证书存储中。你 使用证书 MMC 管理单元,但从命令行:

certutil -user -addstore Root MyCA.cer

创建代码签名证书 (SPC)

makecert -pe -n "CN=My SPC" -a sha256 -cy end ^
         -sky signature ^
         -ic MyCA.cer -iv MyCA.pvk ^
         -sv MySPC.pvk MySPC.cer

它与上面几乎相同,但我们提供了颁发者密钥和证书(-ic 和 -iv 开关)。

我们还需要将证书和密钥转换为 PFX 文件:

pvk2pfx -pvk MySPC.pvk -spc MySPC.cer -pfx MySPC.pfx

如果要保护 PFX 文件,请添加 -po 开关,否则 PVK2PFX 将创建一个没有密码的 PFX 文件。

使用证书对代码进行签名

signtool sign /v /f MySPC.pfx ^
              /t http://timestamp.url MyExecutable.exe

(了解为什么时间戳可能很重要)

如果将 PFX 文件导入证书存储(可以使用 PVKIMPRT 或 MMC 管理单元),则可以按如下方式对代码进行签名:

signtool sign /v /n "Me" /s SPC ^
              /t http://timestamp.url MyExecutable.exe

一些可能的时间戳 URL signtool /t 是:

  • http://timestamp.verisign.com/scripts/timstamp.dll
  • http://timestamp.globalsign.com/scripts/timstamp.dll
  • http://timestamp.comodoca.com/authenticode

完整的微软文档

下载

对于那些不是 .NET 开发人员的人,您将需要 Windows SDK 和 .NET 框架的副本。当前链接位于此处: 软件开发工具包和.NET (将 makecert 安装在 C:\Program Files\Microsoft SDKs\Windows\v7.1)。你的旅费可能会改变。

MakeCert 可从 Visual Studio 命令提示符获取。Visual Studio 2015 确实有它,并且可以从 Windows 7 中的“开始”菜单中的“VS 2015 开发人员命令提示符”或“VS2015 x64 本机工具命令提示符”(可能全部都在同一文件夹中)下启动。

其他提示

罗杰的回答非常有帮助。

不过,我在使用它时遇到了一些麻烦,并且不断出现红色的“Windows 无法验证此驱动程序软件的发布者”错误对话框。关键是安装测试根证书

certutil -addstore Root Demo_CA.cer

罗杰的回答并没有完全涵盖这一点。

这是一个对我有用的批处理文件(带有我的 .inf 文件,不包括在内)。它显示了如何从头到尾完成所有操作,根本没有GUI工具(除了一些密码提示外)。

REM Demo of signing a printer driver with a self-signed test certificate.
REM Run as administrator (else devcon won't be able to try installing the driver)
REM Use a single 'x' as the password for all certificates for simplicity.

PATH %PATH%;"c:\Program Files\Microsoft SDKs\Windows\v7.1\Bin";"c:\Program Files\Microsoft SDKs\Windows\v7.0\Bin";c:\WinDDK\7600.16385.1\bin\selfsign;c:\WinDDK\7600.16385.1\Tools\devcon\amd64

makecert -r -pe -n "CN=Demo_CA" -ss CA -sr CurrentUser ^
   -a sha256 -cy authority -sky signature ^
   -sv Demo_CA.pvk Demo_CA.cer

makecert -pe -n "CN=Demo_SPC" -a sha256 -cy end ^
   -sky signature ^
   -ic Demo_CA.cer -iv Demo_CA.pvk ^
   -sv Demo_SPC.pvk Demo_SPC.cer

pvk2pfx -pvk Demo_SPC.pvk -spc Demo_SPC.cer ^
   -pfx Demo_SPC.pfx ^
   -po x

inf2cat /drv:driver /os:XP_X86,Vista_X64,Vista_X86,7_X64,7_X86 /v

signtool sign /d "description" /du "www.yoyodyne.com" ^
   /f Demo_SPC.pfx ^
   /p x ^
   /v driver\demoprinter.cat

certutil -addstore Root Demo_CA.cer

rem Needs administrator. If this command works, the driver is properly signed.
devcon install driver\demoprinter.inf LPTENUM\Yoyodyne_IndustriesDemoPrinter_F84F

rem Now uninstall the test driver and certificate.
devcon remove driver\demoprinter.inf LPTENUM\Yoyodyne_IndustriesDemoPrinter_F84F

certutil -delstore Root Demo_CA

正如答案中所述,为了使用一种未弃用的方式来签署您自己的脚本,应该使用 新的自签名证书.

  1. 生成密钥:
    New-SelfSignedCertificate -DnsName email@yourdomain.com -Type CodeSigning -CertStoreLocation cert:\CurrentUser\My

  2. 导出不带私钥的证书:
    Export-Certificate -Cert (Get-ChildItem Cert:\CurrentUser\My -CodeSigningCert)[0] -FilePath code_signing.crt 当您拥有多个证书时,[0] 将使此工作有效...显然,使索引与您要使用的证书相匹配......或者使用某种方式进行过滤(通过指纹或发行者)。

  3. 将其导入为受信任的发布者
    Import-Certificate -FilePath .\code_signing.crt -Cert Cert:\CurrentUser\TrustedPublisher

  4. 将其导入为根证书颁发机构。
    Import-Certificate -FilePath .\code_signing.crt -Cert Cert:\CurrentUser\Root

  5. 签署剧本。
    Set-AuthenticodeSignature .\script.ps1 -Certificate (Get-ChildItem Cert:\CurrentUser\My -CodeSigningCert)

显然,一旦设置了密钥,您就可以简单地用它签署任何其他脚本。
您可以在以下位置获取更详细的信息和一些故障排除帮助: 本文.

从 PowerShell 4.0 开始(Windows 8.1/服务器2012 R2) 可以在 Windows 中制作证书,无需 生成证书文件.

您需要的命令是 新的自签名证书出口Pfx证书.

说明位于 使用 PowerShell 创建自签名证书.

使用起来相当简单 新的自签名证书 Powershell 中的命令。打开 powershell 并运行这 3 个命令。

1) 创建证书:
$ cert = new -selfSignedCertificate -dnsname www.yourwebsite.com -type codeSigning -certStoreLocation -cert cent: currentuser my my my

2) 为其设置密码:
$ certpassword = convertto -securestring -String“ my_passowrd” - force - asplaintext

3) 导出它:
export -pfxcertificate -cert“ cert: currentuser my $($ cert.thumbprint)” -filepath“ d: selfsigncert.pfx” -password $ certpassword

您的证书 自签名证书.pfx 将位于@ D:/


可选步骤: 您还需要将证书密码添加到系统环境变量中。通过在 cmd 中输入以下内容来执行此操作: setx CSC_KEY_PASSWORD "my_password"

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top