Skip to content

Instantly share code, notes, and snippets.

@atrenton
Created August 18, 2020 22:32
Show Gist options
  • Save atrenton/5993f849e106a0607be2dfde03b190d4 to your computer and use it in GitHub Desktop.
Save atrenton/5993f849e106a0607be2dfde03b190d4 to your computer and use it in GitHub Desktop.
Create self-signed code signing certificate with PowerShell
# Create-SelfSigned-Certificate.ps1
#requires -Version 5
#-------------------------------------------------------------------------------
# Create self signed code signing certificate
# REF: https://docs.microsoft.com/en-us/windows/uwp/packaging/create-certificate-package-signing
#-------------------------------------------------------------------------------
Import-Module PKI
$storeLocation = 'cert:\CurrentUser\My'
#-------------------------------------------------------------------------------
# Certificate Types
# REF: https://docs.microsoft.com/en-us/windows/desktop/seccertenroll/supported-extensions#enhancedkeyusage
# EKU TYPES (EnhancedKeyUsage) (2.5.29.37)
# Signing Software (1.3.6.1.5.5.7.3.3)
# -KeyUsage DigitalSignature
# -KeySpec Signature
#-------------------------------------------------------------------------------
# https://docs.microsoft.com/en-us/powershell/module/pkiclient/new-selfsignedcertificate?view=win10-ps
$cert = New-SelfSignedCertificate `
-CertStoreLocation $storeLocation `
-FriendlyName 'Test Code Signing' `
-HashAlgorithm sha256 `
-KeyAlgorithm RSA `
-KeyLength 2048 `
-KeyExportPolicy Exportable `
-KeyUsage DigitalSignature `
-KeySpec Signature `
-NotAfter (Get-Date).AddYears(1) `
-Subject 'CN=YOUR NAME, O=Open Source Developer, C=US' `
-TextExtension @('2.5.29.37={text}1.3.6.1.5.5.7.3.3')
$certPath = "$($storeLocation)\$($cert.thumbprint)"
Write-Host "New self-signed certificate created: $certPath"
$cerFilePath = 'D:\Scripts\Test-Code-Signing.cer'
$pfxFilePath = 'D:\Scripts\Test-Code-Signing.pfx'
$pwd = ConvertTo-SecureString -String 'changeit' -Force -AsPlainText
# Export cert as PFX (with private key) and as a DER-encoded .cer file
Export-PfxCertificate -Cert $certPath -FilePath $pfxFilePath -Password $pwd
Export-Certificate -Cert $certPath -FilePath $cerFilePath
# Install .cer file in Trusted Root Certification Authorities
Import-Certificate -FilePath $cerFilePath -CertStoreLocation 'cert:\CurrentUser\Root'
#-------------------------------------------------------------------------------
# OTHER EXAMPLES
# GitHub Gist: https://gist.github.com/RomelSan/bea2443684aa0883b117c37bac1de520
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment