forked from microsoft/msquic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall-test-certificates.ps1
More file actions
194 lines (149 loc) · 7.62 KB
/
Copy pathinstall-test-certificates.ps1
File metadata and controls
194 lines (149 loc) · 7.62 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<#
.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 = ""
)
Set-StrictMode -Version 'Latest'
$PSDefaultParameterValues['*:ErrorAction'] = 'Stop'
[System.DateTimeOffset]$NotBefore = [System.DateTimeOffset]::Now.AddDays(-1)
[System.DateTimeOffset]$NotAfter = [System.DateTimeOffset]::Now.AddDays(365)
function CreateRootCertificate() {
$Subject = [X500DistinguishedName]::new("CN=MsQuicPkcs12Root")
# KU
$KeyUsage = [System.Security.Cryptography.X509Certificates.X509KeyUsageExtension]::new(
[System.Security.Cryptography.X509Certificates.X509KeyUsageFlags]::DigitalSignature +
[System.Security.Cryptography.X509Certificates.X509KeyUsageFlags]::KeyCertSign,
$true)
# Create Basic Constraints
$BasicConstraints = [System.Security.Cryptography.X509Certificates.X509BasicConstraintsExtension]::new(
<# certificateAuthority #> $true,
<# hasPathLengthConstraint #> $true,
<# pathLengthConstraint #> 1,
<# critical #> $true)
$Extensions = [System.Collections.Generic.List[System.Security.Cryptography.X509Certificates.X509Extension]]::new()
$Extensions.Add($KeyUsage)
$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)
return $CertificateWithKey
}
function CreateIntermediateCertificate($RootCert) {
$Subject = [X500DistinguishedName]::new("CN=MsQuicPkcs12Intermediate")
# KU
$KeyUsage = [System.Security.Cryptography.X509Certificates.X509KeyUsageExtension]::new(
[System.Security.Cryptography.X509Certificates.X509KeyUsageFlags]::DigitalSignature +
[System.Security.Cryptography.X509Certificates.X509KeyUsageFlags]::KeyCertSign,
$true)
# Create Basic Constraints
$BasicConstraints = [System.Security.Cryptography.X509Certificates.X509BasicConstraintsExtension]::new(
<# certificateAuthority #> $true,
<# hasPathLengthConstraint #> $true,
<# pathLengthConstraint #> 0,
<# critical #> $true)
$Extensions = [System.Collections.Generic.List[System.Security.Cryptography.X509Certificates.X509Extension]]::new()
$Extensions.Add($KeyUsage)
$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)
}
$Serial = [byte[]]::new(16)
$Random = [System.Random]::new()
$Random.NextBytes($Serial)
$Cert = $CertRequest.Create($RootCert, $NotBefore, $NotAfter, $Serial)
return [System.Security.Cryptography.X509Certificates.RSACertificateExtensions]::CopyWithPrivateKey($Cert, $PrivateKey)
}
function CreateLeafCert($Signer) {
$Subject = [X500DistinguishedName]::new("CN=localhost")
# 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 #> $true)
$KeyUsage = [System.Security.Cryptography.X509Certificates.X509KeyUsageExtension]::new(
[System.Security.Cryptography.X509Certificates.X509KeyUsageFlags]::DigitalSignature,
$true)
# Create Basic Constraints
$BasicConstraints = [System.Security.Cryptography.X509Certificates.X509BasicConstraintsExtension]::new(
<# certificateAuthority #> $false,
<# hasPathLengthConstraint #> $false,
<# pathLengthConstraint #> 0,
<# critical #> $true)
$SanBuilder = [System.Security.Cryptography.X509Certificates.SubjectAlternativeNameBuilder]::new()
$SanBuilder.AddDnsName("localhost")
$SanBuilder.AddIpAddress([System.Net.IPAddress]::Parse("127.0.0.1"))
$SanBuilder.AddIpAddress([System.Net.IPAddress]::Parse("::1"))
$Extensions = [System.Collections.Generic.List[System.Security.Cryptography.X509Certificates.X509Extension]]::new()
$Extensions.Add($SanBuilder.Build())
$Extensions.Add($KeyUsage)
$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)
}
$Serial = [byte[]]::new(16)
$Random = [System.Random]::new()
$Random.NextBytes($Serial)
$Cert = $CertRequest.Create($Signer, $NotBefore, $NotAfter, $Serial)
return [System.Security.Cryptography.X509Certificates.RSACertificateExtensions]::CopyWithPrivateKey($Cert, $PrivateKey)
}
$Root = CreateRootCertificate
$IntermediateSigner = CreateIntermediateCertificate $Root
$Leaf = CreateLeafCert $IntermediateSigner
$Collection = [System.Security.Cryptography.X509Certificates.X509Certificate2Collection]::new()
$Collection.Add($Leaf[1]) | Out-Null # TODO(AnRossi):Why is $Leaf an array of an Int and the Cert?
#Export the intermediate and root certs and then import them to drop their private keys.
$Collection.Import($IntermediateSigner.rawData)
$Collection.Import($Root.rawData)
$Pfx = $Collection.Export([System.Security.Cryptography.X509Certificates.X509ContentType]::Pfx, "PLACEHOLDER");
Set-Content $OutputFile -Value $Pfx -AsByteStream
Write-Output "Generated $OutputFile"
# Clean up the signer's private keys
$Root.PrivateKey.Clear()
$IntermediateSigner.PrivateKey.Clear()