Programing

Windows에서 코드 서명을 위해 자체 서명 된 인증서를 작성하는 방법

lottogame 2020. 5. 5. 19:32
반응형

Windows에서 코드 서명을 위해 자체 서명 된 인증서를 작성하는 방법


Windows SDK의 도구를 사용하여 코드 서명을위한 자체 서명 인증서를 작성하는 방법


업데이트 된 답변

Windows Server 2012, Windows Server 2012 R2 또는 Windows 8.1과 같은 Windows 버전 이상을 사용하는 경우 이제 MakeCert는 더 이상 사용되지 않으며 PowerShell Cmdlet New-SelfSignedCertificate를 사용 하는 것이 좋습니다 .

Windows 7과 같은 이전 버전을 사용하는 경우 MakeCert 또는 다른 솔루션을 사용해야합니다. 어떤 사람들 PSPKI (Public Key Infrastructure Powershell) 모듈을 제안 합니다 .

원래 답변

한 번에 자체 서명 된 코드 서명 인증서 (SPC- Software Publisher Certificate )를 작성할 수 있지만 다음을 수행하는 것이 좋습니다 .

자체 서명 인증 기관 (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) 인증서를 만듭니다. 이름은 "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

전체 Microsoft 설명서

다운로드

.NET 개발자가 아닌 경우 Windows SDK 및 .NET 프레임 워크의 사본이 필요합니다. 현재 링크는 SDK 및 .NET에서 사용할 수 있습니다 (makecert를에 설치 C:\Program Files\Microsoft SDKs\Windows\v7.1). 귀하의 마일리지가 다를 수 있습니다.

MakeCert는 Visual Studio 명령 프롬프트에서 사용할 수 있습니다. Visual Studio 2015에는 Windows 7의 시작 메뉴에서 "VS 2015 용 개발자 명령 프롬프트"또는 "VS2015 x64 기본 도구 명령 프롬프트"(아마도 모두 동일한 폴더에 있음)에서 시작할 수 있습니다.


로저의 대답은 매우 도움이되었습니다.

I had a little trouble using it, though, and kept getting the red "Windows can't verify the publisher of this driver software" error dialog. The key was to install the test root certificate with

certutil -addstore Root Demo_CA.cer

which Roger's answer didn't quite cover.

Here is a batch file that worked for me (with my .inf file, not included). It shows how to do it all from start to finish, with no GUI tools at all (except for a few password prompts).

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

As stated in the answer, in order to use a non deprecated way to sign your own script, one should use New-SelfSignedCertificate.

  1. Generate the key:
    New-SelfSignedCertificate -DnsName email@yourdomain.com -Type CodeSigning -CertStoreLocation cert:\CurrentUser\My

  2. Export the certificate without the private key:
    Export-Certificate -Cert (Get-ChildItem Cert:\CurrentUser\My -CodeSigningCert)[0] -FilePath code_signing.crt The [0] will make this work for cases when you have more than one certificate... Obviously make the index match the certificate you want to use... or use a way to filtrate (by thumprint or issuer).

  3. Import it as Trusted Publisher
    Import-Certificate -FilePath .\code_signing.crt -Cert Cert:\CurrentUser\TrustedPublisher

  4. Import it as a Root certificate authority.
    Import-Certificate -FilePath .\code_signing.crt -Cert Cert:\CurrentUser\Root

  5. Sign the script.
    Set-AuthenticodeSignature .\script.ps1 -Certificate (Get-ChildItem Cert:\CurrentUser\My -CodeSigningCert)

Obviously once you have setup the key, you can simply sign any other scripts with it.
You can get more detailed information and some troubleshooting help in this article.


It's fairly easy using the New-SelfSignedCertificate command in Powershell. Open powershell and run these 3 commands.

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

Your certificate selfsigncert.pfx will be located @ D:/


Optional step: You would also require to add certificate password to system environment variables. do so by entering below in cmd: setx CSC_KEY_PASSWORD "my_password"


As of PowerShell 4.0 (Windows 8.1/Server 2012 R2) it is possible to make a certificate in Windows without makecert.exe.

The commands you need are New-SelfSignedCertificate and Export-PfxCertificate.

Instructions are in Creating Self Signed Certificates with PowerShell.

참고URL : https://stackoverflow.com/questions/84847/how-do-i-create-a-self-signed-certificate-for-code-signing-on-windows

반응형