如何在Windows上为代码签名创build自签名证书?

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

更新的答案

如果使用以下Windows版本或更高版本:Windows Server 2012,Windows Server 2012 R2或Windows 8.1,则现在不推荐使用MakeCert ,Microsoftbuild议使用PowerShell Cmdlet New-SelfSignedCertificate

如果您使用的是Windows 7等旧版本,则需要使用MakeCert或其他解决scheme。 有人提出 公钥基础设施功率模块(PSPKI)模块 。

原始答复

虽然您可以一次性创build自签名代码签名证书(SPC – 软件发行者证书 ),但我更愿意执行以下操作:

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

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

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

这会创build一个自签名(-r)证书,并带有一个可导出的私钥(-pe)。 它被命名为“我的CA”,应该放在当前用户的CA商店中。 我们正在使用SHA-256algorithm。 关键是用于签署(-sky)。

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

导入CA证书

因为如果您不信任CA证书就毫无意义,您需要将其导入Windows证书存储区。 您可以使用证书MMCpipe理单元,但从命令行:

 certutil -user -addstore Root MyCA.cer 

创build代码签名证书(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将创build一个没有密码的PFX文件。

使用证书进行签名代码

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

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

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

 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文档

  • signtool
  • makecert
  • pvk2pfx

下载

对于那些不是.NET开发人员,您将需要一个Windows SDK和.NET框架的副本。 当前的链接可以在这里find: SDK&.NET (在C:\Program Files\Microsoft SDKs\Windows\v7.1安装makecert)。 你的旅费可能会改变。

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

罗杰的回答非常有帮助。

但是,使用它有点麻烦,并且不断得到红色的“Windows无法validation此驱动程序软件的发布者”错误对话框。 关键是安装testing根证书

 certutil -addstore Root Demo_CA.cer 

罗杰的回答并不完全掩盖。

这是一个batch file,为我工作(与我的.inf文件,不包括在内)。 它展示了如何从头到尾完成所有工作(除了一些密码提示)。

 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 ^ /px ^ /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中创build证书。

您需要的命令是New-SelfSignedCertificate和Export-PfxCertificate 。

说明在使用PowerShell创build自签名证书

在Powershell中使用New-SelfSignedCertificate命令相当简单。 打开PowerShell并运行这3个命令。

1) 创build证书
$ cert = New-SelfSignedCertificate -DnsName http://www.yourwebsite.com – typesCodeSigning -CertStoreLocation证书:\ CurrentUser \ My

2) 设置密码
$ CertPassword = ConvertTo-SecureString -String“my_passowrd”-Force -AsPlainText

3) 出口
Export-PfxCertificate -Cert“cert:\ CurrentUser \ My \ $($ cert.Thumbprint)”-FilePath“d:\ testcert.pfx”-Password $ CertPassword

您的证书testcert.pfx将位于@ D:/