forked from microsoft/msquic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install-test-certificates.ps1
66 lines (48 loc) · 2.32 KB
/
install-test-certificates.ps1
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<#
.SYNOPSIS
This script provides helpers to generate test certificate for MsQuic tests.
.PARAMETER OutputFile
Specifies the build configuration to test.
.EXAMPLE
install-test-certificates.ps1 -OutputFile ./artifacts/bin/macos/x64_Debug_openssl/test.pfx
#>
param (
[Parameter(Mandatory = $true)]
[string]$OutputFile = ""
)
$Subject = [X500DistinguishedName]::new("CN=localhost")
[System.DateTimeOffset]$NotBefore = [System.DateTimeOffset]::Now.AddDays(-1)
[System.DateTimeOffset]$NotAfter = [System.DateTimeOffset]::Now.AddDays(365)
# EKU
$EkuOidCollection = [System.Security.Cryptography.OidCollection]::new()
$EkuOidCollection.Add([System.Security.Cryptography.Oid]::new("1.3.6.1.5.5.7.3.1", "Server Authentication"))
$EnhancedKeyUsages = [System.Security.Cryptography.X509Certificates.X509EnhancedKeyUsageExtension]::new($EkuOidCollection, <# critical #> $false)
# Create Basic Constraints
$BasicConstraints = [System.Security.Cryptography.X509Certificates.X509BasicConstraintsExtension]::new(
<# certificateAuthority #> $false,
<# hasPathLengthConstraint #> $false,
<# pathLengthConstraint #> 0,
<# critical #> $false)
$Extensions = [System.Collections.Generic.List[System.Security.Cryptography.X509Certificates.X509Extension]]::new()
$Extensions.Add($EnhancedKeyUsages)
$Extensions.Add($BasicConstraints)
$PrivateKey = [System.Security.Cryptography.RSA]::Create(2048)
# Create Certificate Request
$CertRequest = [System.Security.Cryptography.X509Certificates.CertificateRequest]::new(
$Subject,
$PrivateKey,
[System.Security.Cryptography.HashAlgorithmName]::SHA256,
[System.Security.Cryptography.RSASignaturePadding]::Pkcs1)
# Create the Subject Key Identifier extension
$SubjectKeyIdentifier = [System.Security.Cryptography.X509Certificates.X509SubjectKeyIdentifierExtension]::new(
$CertRequest.PublicKey,
<# critical #> $false)
$Extensions.Add($SubjectKeyIdentifier)
foreach ($Extension in $Extensions)
{
$CertRequest.CertificateExtensions.Add($Extension)
}
$CertificateWithKey = $CertRequest.CreateSelfSigned($NotBefore, $NotAfter)
$Pfx = $CertificateWithKey.Export([System.Security.Cryptography.X509Certificates.X509ContentType]::Pfx, "PLACEHOLDER");
Set-Content $OutputFile -Value $Pfx -AsByteStream
Write-Output "Generated $OutputFile"