如何使用 Windows SDK 中的工具为代码签名创建自签名证书?
6 回答
更新的答案
如果您使用以下 Windows 版本或更高版本:Windows Server 2012、Windows Server 2012 R2 或 Windows 8.1,则MakeCert 现在已弃用,Microsoft 建议使用PowerShell Cmdlet New-SelfSignedCertificate。
如果您使用的是旧版本,例如 Windows 7,则需要坚持使用 MakeCert 或其他解决方案。有人建议使用Public Key Infrastructure Powershell (PSPKI) Module。
原始答案
虽然您可以一次性创建自签名代码签名证书(SPC -软件发布者证书),但我更喜欢执行以下操作:
创建自签名证书颁发机构 (CA)
makecert -r -pe -n "CN=My CA" -ss CA -sr CurrentUser ^
-a sha256 -cy authority -sky signature -sv MyCA.pvk MyCA.cer
(^ = 允许批处理命令行换行)
这将创建一个带有可导出私钥 (-pe) 的自签名 (-r) 证书。它被命名为“我的 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
如果您使用密码,请使用以下密码
pvk2pfx -pvk MySPC.pvk -spc MySPC.cer -pfx MySPC.pfx -po fess
如果要保护 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
一些可能的时间戳 URLsigntool /t是:
http://timestamp.verisign.com/scripts/timstamp.dllhttp://timestamp.globalsign.com/scripts/timstamp.dllhttp://timestamp.comodoca.com/authenticodehttp://timestamp.digicert.com
完整的 Microsoft 文档
下载
对于非 .NET 开发人员,您需要一份 Windows SDK 和 .NET 框架的副本。此处提供当前链接:[SDK & .NET][5](将 makecert 安装在 `C:\Program Files\Microsoft SDKs\Windows\v7.1` 中)。你的旅费可能会改变。MakeCert 可从 Visual Studio 命令提示符获得。Visual Studio 2015 确实有它,它可以从 Windows 7 的“开始”菜单在“VS 2015 的开发人员命令提示符”或“VS2015 x64 本机工具命令提示符”下启动(可能所有这些都在同一个文件夹中)。
如答案中所述,为了使用不推荐的方式来签署您自己的脚本,应该使用New-SelfSignedCertificate。
- 生成密钥:
New-SelfSignedCertificate -DnsName email@yourdomain.com -Type CodeSigning -CertStoreLocation cert:\CurrentUser\My
- 导出不带私钥的证书:
Export-Certificate -Cert (Get-ChildItem Cert:\CurrentUser\My -CodeSigningCert)[0] -FilePath code_signing.crt
当您拥有多个证书时, [0] 将使此工作...显然使索引与您要使用的证书匹配...或使用一种过滤方式(通过指纹或颁发者)。
- 将其作为受信任的发布者导入
Import-Certificate -FilePath .\code_signing.crt -Cert Cert:\CurrentUser\TrustedPublisher
- 将其作为根证书颁发机构导入。
Import-Certificate -FilePath .\code_signing.crt -Cert Cert:\CurrentUser\Root
- 签署脚本(假设这里它被命名为 script.ps1,相应地修复路径)。
Set-AuthenticodeSignature .\script.ps1 -Certificate (Get-ChildItem Cert:\CurrentUser\My -CodeSigningCert)
显然,一旦您设置了密钥,您就可以简单地用它签署任何其他脚本。您可以在本文
中获得更详细的信息和一些故障排除帮助。
在 Powershell 中使用New-SelfSignedCertificate命令相当容易。打开 powershell 并运行这 3 个命令。
创建证书:
$cert = New-SelfSignedCertificate -DnsName www.yourwebsite.com -Type CodeSigning -CertStoreLocation Cert:\CurrentUser\My为其设置密码:
$CertPassword = ConvertTo-SecureString -String "my_passowrd" -Force -AsPlainText导出它:
Export-PfxCertificate -Cert "cert:\CurrentUser\My$($cert.Thumbprint)" -FilePath "d:\selfsigncert.pfx" -Password $CertPassword
您的证书selfsigncert.pfx将位于 @D:/
可选步骤:您还需要将证书密码添加到系统环境变量中。通过在 cmd 中输入以下内容来执行此操作:setx CSC_KEY_PASSWORD "my_password"
罗杰的回答很有帮助。
不过,我在使用它时遇到了一点麻烦,并且不断出现红色的“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
从 PowerShell 4.0 (Windows 8.1/ Server 2012 R2) 开始,可以在没有makecert.exe的情况下在 Windows 中制作证书。
您可以在 Visual Studio 2019 的项目属性中生成一个。在驱动程序签名部分,测试证书字段有一个下拉菜单。生成测试证书是其中一种选择。证书将位于扩展名为“cer”的文件中,通常与可执行文件或驱动程序位于同一输出目录中。