如何使用Windows SDK中的工具创建用于代码签名的自签名证书?
更新的答案
如果您使用以下Windows版本或更高版本:Windows Server 2012,Windows Server 2012 R2或Windows 8.1,则现在不建议使用MakeCert,并且Microsoft建议使用PowerShell Cmdlet New-SelfSignedCertificate。
如果您使用的是Windows 7等旧版本,则需要坚持使用MakeCert或其他解决方案。有人建议使用公钥基础结构Powershell(PSPKI)模块。
原始答案
虽然您可以一次性创建自签名的代码签名证书(SPC-软件发布者证书),但我更喜欢执行以下操作:
创建自签名证书颁发机构(CA)
1 2
   | 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管理单元,但可以从命令行使用:
1
   | certutil -user -addstore Root MyCA.cer  | 
 
创建代码签名证书(SPC)
1 2 3 4
   | 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文件:
1
   | pvk2pfx -pvk MySPC.pvk -spc MySPC.cer -pfx MySPC.pfx  | 
 
如果要保护PFX文件,请添加-po开关,否则PVK2PFX将创建没有密码短语的PFX文件。
使用证书签名代码
1 2
   | signtool sign /v /f MySPC.pfx ^ 
              /t http://timestamp.url MyExecutable.exe  | 
 
(看看为什么时间戳很重要)
如果将PFX文件导入证书存储(可以使用PVKIMPRT或MMC管理单元),则可以按以下方式签名:
1 2
   | signtool sign /v /n"Me" /s SPC ^ 
              /t http://timestamp.url MyExecutable.exe  | 
 
signtool /t的一些可能的时间戳URL是:
- 
http://timestamp.verisign.com/scripts/timstamp.dll
 
- 
http://timestamp.globalsign.com/scripts/timstamp.dll
 
- 
http://timestamp.comodoca.com/authenticode
 
完整的Microsoft文档
资料下载
对于不是.NET开发人员的用户,您将需要Windows SDK和.NET框架的副本。当前链接在此处可用:SDK和.NET(在C:\Program Files\Microsoft SDKs\Windows\v7.1中安装makecert)。你的旅费可能会改变。
可从Visual Studio命令提示符中获得MakeCert。 Visual Studio 2015确实具有此功能,可以从Windows 7的"开始菜单"下的" VS 2015开发人员命令提示符"或" VS2015 x64本机工具命令提示符"(可能它们都在同一个文件夹中)启动。
如答案中所述,为了使用一种不推荐使用的方式来签名自己的脚本,应该使用New-SelfSignedCertificate。
生成密钥:
1
   | New-SelfSignedCertificate -DnsName email@yourdomain.com -Type CodeSigning -CertStoreLocation cert:\CurrentUser\My  | 
 
导出不带私钥的证书:
1
   | Export-Certificate -Cert (Get-ChildItem Cert:\CurrentUser\My -CodeSigningCert)[0] -FilePath code_signing.crt  | 
 
如果您有多个证书,则[0]可使此功能有效。显然,使索引与要使用的证书相匹配...或使用过滤方式(通过指纹或发行方)。
1.将其导入为受信任的发布者
1
   | Import-Certificate -FilePath .\code_signing.crt -Cert Cert:\CurrentUser\TrustedPublisher  | 
 
将其导入为根证书颁发机构。
1 2
   | Import-Certificate -FilePath .\code_signing.crt -Cert Cert:\CurrentUser 
oot  | 
 
对脚本签名(假设此处名为script.ps1,并相应地修复路径)。
1
   | Set-AuthenticodeSignature .\script.ps1 -Certificate (Get-ChildItem Cert:\CurrentUser\My -CodeSigningCert)  | 
 
显然,一旦设置了密钥,就可以使用它对其他脚本进行签名。
您可以在本文中获得更多详细信息和一些故障排除帮助。
 
罗杰的回答很有帮助。
但是,我在使用它时遇到了一些麻烦,并一直显示红色的" Windows无法验证此驱动程序软件的发行者"错误对话框。关键是使用以下命令安装测试根证书
1
   | certutil -addstore Root Demo_CA.cer  | 
 
罗杰的答案还没有涵盖。
这是一个对我有用的批处理文件(带有我的.inf文件,不包括在内)。
它显示了如何从头到尾完成所有操作,完全没有GUI工具
(除了一些密码提示)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
   | 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中使用New-SelfSignedCertificate命令相当容易。
打开powershell并运行这3个命令。
1) Create certificate:
     $cert = New-SelfSignedCertificate -DnsName www.yourwebsite.com
  -Type CodeSigning -CertStoreLocation Cert:\CurrentUser\My
2) set the password for it:
  $CertPassword = ConvertTo-SecureString
  -String"my_passowrd" -Force –AsPlainText
3) Export it:
    Export-PfxCertificate -Cert"cert:\CurrentUser\My\$($cert.Thumbprint)"
  -FilePath"d:\selfsigncert.pfx" -Password $CertPassword
您的证书selfsigncert.pfx将位于@ D:/
可选步骤:您还需要将证书密码添加到系统环境变量中。为此,请在cmd中输入以下内容:setx CSC_KEY_PASSWORD"my_password"
从PowerShell 4.0(Windows 8.1 / Server 2012 R2)开始,可以在Windows中制作没有makecert.exe的证书。
您需要的命令是New-SelfSignedCertificate和Export-PfxCertificate。
使用PowerShell创建自签名证书中的说明。