-
Notifications
You must be signed in to change notification settings - Fork 151
Expand file tree
/
Copy pathNew-DevSigningCert.ps1
More file actions
51 lines (39 loc) · 2.07 KB
/
Copy pathNew-DevSigningCert.ps1
File metadata and controls
51 lines (39 loc) · 2.07 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
param(
[string]$PfxPath = 'cert\IntelligentTerminalDev.pfx',
[string]$CerPath = 'artifacts\local-installer\IntelligentTerminalDev.cer',
[string]$Subject = 'CN=Intelligent Terminal Dev'
)
$ErrorActionPreference = 'Stop'
New-Item -ItemType Directory -Path (Split-Path $PfxPath -Parent) -Force | Out-Null
New-Item -ItemType Directory -Path (Split-Path $CerPath -Parent) -Force | Out-Null
# Use pure .NET so this works in both Windows PowerShell and pwsh
# without needing the Cert: PSDrive or PKI module.
Add-Type -AssemblyName System.Security
$rsa = [System.Security.Cryptography.RSA]::Create(2048)
$req = [System.Security.Cryptography.X509Certificates.CertificateRequest]::new(
$Subject,
$rsa,
[System.Security.Cryptography.HashAlgorithmName]::SHA256,
[System.Security.Cryptography.RSASignaturePadding]::Pkcs1
)
# Code-signing EKU
$ekuOids = [System.Security.Cryptography.OidCollection]::new()
$ekuOids.Add([System.Security.Cryptography.Oid]::new('1.3.6.1.5.5.7.3.3')) | Out-Null
$req.CertificateExtensions.Add(
[System.Security.Cryptography.X509Certificates.X509EnhancedKeyUsageExtension]::new($ekuOids, $false))
# Basic constraints (not a CA)
$req.CertificateExtensions.Add(
[System.Security.Cryptography.X509Certificates.X509BasicConstraintsExtension]::new($false, $false, 0, $false))
$notBefore = [DateTimeOffset]::UtcNow.AddDays(-1)
$notAfter = [DateTimeOffset]::UtcNow.AddYears(3)
$cert = $req.CreateSelfSigned($notBefore, $notAfter)
Write-Host "Subject: $($cert.Subject)"
Write-Host "NotAfter: $($cert.NotAfter)"
# Export PFX (empty password)
$pfxBytes = $cert.Export([System.Security.Cryptography.X509Certificates.X509ContentType]::Pfx)
[System.IO.File]::WriteAllBytes((Resolve-Path '.').Path + '\' + $PfxPath, $pfxBytes)
# Export CER (public key only)
$cerBytes = $cert.Export([System.Security.Cryptography.X509Certificates.X509ContentType]::Cert)
[System.IO.File]::WriteAllBytes((Resolve-Path '.').Path + '\' + $CerPath, $cerBytes)
Write-Host "PFX: $PfxPath ($((Get-Item $PfxPath).Length) bytes)"
Write-Host "CER: $CerPath ($((Get-Item $CerPath).Length) bytes)"