|
| 1 | +param( |
| 2 | + [string]$CertificatePassword = "DevTools2024!", |
| 3 | + [string]$CertName = "DevTools Code Signing", |
| 4 | + [string]$OutputPath = ".\build\certs" |
| 5 | +) |
| 6 | + |
| 7 | +$ErrorActionPreference = "Stop" |
| 8 | + |
| 9 | +Write-Host "=== DevTools Self-Signed Certificate Creator ===" -ForegroundColor Cyan |
| 10 | + |
| 11 | +$pfxPath = Join-Path $OutputPath "DevToolsCodeSigning.pfx" |
| 12 | +$cerPath = Join-Path $OutputPath "DevToolsCodeSigning.cer" |
| 13 | + |
| 14 | +if (-not (Test-Path $OutputPath)) { |
| 15 | + New-Item -ItemType Directory -Path $OutputPath -Force | Out-Null |
| 16 | + Write-Host "Created output directory: $OutputPath" -ForegroundColor Green |
| 17 | +} |
| 18 | + |
| 19 | +Write-Host "`nStep 1: Creating self-signed certificate..." -ForegroundColor Yellow |
| 20 | + |
| 21 | +$cert = New-SelfSignedCertificate ` |
| 22 | + -Subject "CN=$CertName" ` |
| 23 | + -Type CodeSigningCert ` |
| 24 | + -KeyUsage DigitalSignature ` |
| 25 | + -FriendlyName "$CertName" ` |
| 26 | + -CertStoreLocation "Cert:\CurrentUser\My" ` |
| 27 | + -TextExtension @("2.5.29.37={text}1.3.6.1.5.5.7.3.3,1.3.6.1.4.1.311.10.3.13") ` |
| 28 | + -KeyExportPolicy Exportable ` |
| 29 | + -KeyLength 2048 ` |
| 30 | + -KeyAlgorithm RSA ` |
| 31 | + -HashAlgorithm SHA256 ` |
| 32 | + -NotAfter (Get-Date).AddYears(10) |
| 33 | + |
| 34 | +Write-Host "Certificate created with thumbprint: $($cert.Thumbprint)" -ForegroundColor Green |
| 35 | + |
| 36 | +Write-Host "`nStep 2: Exporting certificate to PFX file..." -ForegroundColor Yellow |
| 37 | + |
| 38 | +$securePassword = ConvertTo-SecureString -String $CertificatePassword -Force -AsPlainText |
| 39 | +Export-PfxCertificate -Cert $cert -FilePath $pfxPath -Password $securePassword | Out-Null |
| 40 | +Write-Host "PFX exported to: $pfxPath" -ForegroundColor Green |
| 41 | + |
| 42 | +Write-Host "`nStep 3: Exporting public certificate..." -ForegroundColor Yellow |
| 43 | +Export-Certificate -Cert $cert -FilePath $cerPath -Type CERT | Out-Null |
| 44 | +Write-Host "Public cert exported to: $cerPath" -ForegroundColor Green |
| 45 | + |
| 46 | +Write-Host "`nStep 4: Adding certificate to Trusted Publishers..." -ForegroundColor Yellow |
| 47 | + |
| 48 | +$trustedPublishers = Get-ChildItem -Path "Cert:\CurrentUser\TrustedPublisher" -ErrorAction SilentlyContinue |
| 49 | +$alreadyTrusted = $trustedPublishers | Where-Object { $_.Thumbprint -eq $cert.Thumbprint } |
| 50 | + |
| 51 | +if (-not $alreadyTrusted) { |
| 52 | + $cert | Export-Certificate -FilePath (Join-Path $OutputPath "temp.cer") -Type CERT -Force | Out-Null |
| 53 | + Import-Certificate -FilePath (Join-Path $OutputPath "temp.cer") -CertStoreLocation "Cert:\CurrentUser\TrustedPublisher" | Out-Null |
| 54 | + Remove-Item (Join-Path $OutputPath "temp.cer") -Force |
| 55 | + Write-Host "Certificate added to Trusted Publishers" -ForegroundColor Green |
| 56 | +} else { |
| 57 | + Write-Host "Certificate already in Trusted Publishers" -ForegroundColor Yellow |
| 58 | +} |
| 59 | + |
| 60 | +Write-Host "`nStep 5: Adding certificate to Root CA (for local trust)..." -ForegroundColor Yellow |
| 61 | + |
| 62 | +$rootCerts = Get-ChildItem -Path "Cert:\CurrentUser\Root" -ErrorAction SilentlyContinue |
| 63 | +$alreadyInRoot = $rootCerts | Where-Object { $_.Thumbprint -eq $cert.Thumbprint } |
| 64 | + |
| 65 | +if (-not $alreadyInRoot) { |
| 66 | + $cert | Export-Certificate -FilePath (Join-Path $OutputPath "temp.cer") -Type CERT -Force | Out-Null |
| 67 | + Import-Certificate -FilePath (Join-Path $OutputPath "temp.cer") -CertStoreLocation "Cert:\CurrentUser\Root" | Out-Null |
| 68 | + Remove-Item (Join-Path $OutputPath "temp.cer") -Force |
| 69 | + Write-Host "Certificate added to Trusted Root Certification Authorities" -ForegroundColor Green |
| 70 | +} else { |
| 71 | + Write-Host "Certificate already in Root CA store" -ForegroundColor Yellow |
| 72 | +} |
| 73 | + |
| 74 | +Write-Host @" |
| 75 | +
|
| 76 | +=== Certificate Creation Complete === |
| 77 | +
|
| 78 | +Certificate Details: |
| 79 | + Subject: $($cert.Subject) |
| 80 | + Thumbprint: $($cert.Thumbprint) |
| 81 | + Valid From: $($cert.NotBefore) |
| 82 | + Valid To: $($cert.NotAfter) |
| 83 | + |
| 84 | +Files Created: |
| 85 | + PFX (Private): $pfxPath |
| 86 | + CER (Public): $cerPath |
| 87 | + Password: $CertificatePassword |
| 88 | +
|
| 89 | +IMPORTANT: |
| 90 | + - Keep the PFX file and password secure! |
| 91 | + - The certificate is now trusted on THIS computer only |
| 92 | + - On other computers, users need to install the CER file to: |
| 93 | + 1. Trusted Publishers |
| 94 | + 2. Trusted Root Certification Authorities |
| 95 | +
|
| 96 | +"@ -ForegroundColor Cyan |
| 97 | + |
| 98 | +return @{ |
| 99 | + PfxPath = $pfxPath |
| 100 | + CerPath = $cerPath |
| 101 | + Thumbprint = $cert.Thumbprint |
| 102 | + Password = $CertificatePassword |
| 103 | +} |
0 commit comments