File: X509Certificate2.psm1

package info (click to toggle)
python-azure 20250603%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 851,724 kB
  • sloc: python: 7,362,925; ansic: 804; javascript: 287; makefile: 195; sh: 145; xml: 109
file content (339 lines) | stat: -rw-r--r-- 9,839 bytes parent folder | download | duplicates (2)
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#Requires -Version 6.0

using namespace System.Security.Cryptography
using namespace System.Security.Cryptography.X509Certificates
using namespace System.Text

<#
.Synopsis
Generate an X509 v3 certificate.

.Description
Generates an [X509Certificate2] from either a subject name, or individual X500 distinguished names.

.Parameter SubjectName
The entire X500 subject name.

.Parameter Country
The country e.g., "US".

.Parameter State
The state or province e.g., "WA".

.Parameter City
The city or locality e.g., "Redmond".

.Parameter Organization
The organization e.g., "Microsoft".

.Parameter Department
The department e.g., "Azure SDK".

.Parameter CommonName
A common name e.g., "www.microsoft.com".

.Parameter SubjectAlternativeNames
Additional subject names from New-X509Certificate2SubjectAlternativeNames.

.Parameter KeySize
Size of the RSA key.

.Parameter KeyUsageFlags
Additional key usage flags.

.Parameter CA
Create self-signed certificate authority.

.Parameter TLS
Create self-signed certificate suitable for TLS.

.Parameter NotBefore
The start date when the certificate is valid. The default is the current time.

.Parameter ValidDays
How many days from NotBefore until the certificate expires.

.Example
New-X509Certificate2 -SubjectName 'E=opensource@microsoft.com, CN=Azure SDK, OU=Azure SDK, O=Microsoft, L=Redmond, S=WA, C=US' -ValidDays 3652

Create a self-signed certificate valid for 10 years from now.

.Example
New-X509Certificate2 -SubjectName 'CN=Azure SDK' -SubjectAlternativeNames (New-X509Certificate2SubjectAlternativeNames -EmailAddress azuresdk@microsoft.com) -KeyUsageFlags KeyEncipherment, NonRepudiation, DigitalSignature -CA -TLS -ValidDays 3652

Create a self-signed certificate valid for 10 years from now with an alternative name, additional key usages including TLS connections, and that can sign other certificate requests.
#>
function New-X509Certificate2 {
    [CmdletBinding(DefaultParameterSetName='SubjectName')]
    [OutputType([System.Security.Cryptography.X509Certificates.X509Certificate2])]
    param (
        [Parameter(ParameterSetName='SubjectName', Mandatory=$true, Position=0)]
        [string] $SubjectName,

        [Parameter(ParameterSetName='Builder', HelpMessage='Country Name (2 letter code)')]
        [Alias('C')]
        [string] $Country,

        [Parameter(ParameterSetName='Builder', HelpMessage='State or Province Name (full name)')]
        [Alias('S', 'Province')]
        [string] $State,

        [Parameter(ParameterSetName='Builder', HelpMessage='Locality Name (eg, city)')]
        [Alias('L', 'Locality')]
        [string] $City,

        [Parameter(ParameterSetName='Builder', HelpMessage='Organization Name (eg, company)')]
        [Alias('O')]
        [string] $Organization,

        [Parameter(ParameterSetName='Builder', HelpMessage='Organizational Unit Name (eg, section)')]
        [Alias('OU')]
        [string] $Department,

        [Parameter(ParameterSetName='Builder', HelpMessage='Common Name (e.g. server FQDN or YOUR name)')]
        [Alias('CN')]
        [string] $CommonName,

        [Parameter()]
        [ValidateNotNull()]
        [SubjectAlternativeNameBuilder] $SubjectAlternativeNames,

        [Parameter()]
        [ValidateSet(1024, 2048, 4096)]
        [int] $KeySize = 2048,

        [Parameter()]
        [X509KeyUsageFlags] $KeyUsageFlags,

        [Parameter()]
        [switch] $CA,

        [Parameter()]
        [switch] $TLS,

        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [DateTimeOffset] $NotBefore = [DateTimeOffset]::Now,

        [Parameter()]
        [ValidateRange(1, [int]::MaxValue)]
        [int] $ValidDays = 365
    )

    if ($PSCmdlet.ParameterSetName -eq 'Builder') {
        $sb = [StringBuilder]::new()
        if ($Country) { $null = $sb.Append("C=$Country,") }
        if ($State) { $null = $sb.Append("S=$State, ") }
        if ($City) { $null = $sb.Append("L=$City, ") }
        if ($Organization) { $null = $sb.Append("O=$Organization, ") }
        if ($Department) { $null = $sb.Append("OU=$Department, ") }
        if ($CommonName) { $null = $sb.Append("CN=$CommonName, ") }

        $SubjectName = [X500DistinguishedName]::new($sb.ToString()).Format($false)
    }

    $rsa = [RSA]::Create($KeySize)
    try {
        $req = [CertificateRequest]::new(
            [string] $SubjectName,
            $rsa,
            [HashAlgorithmName]::SHA256,
            [RSASignaturePadding]::Pkcs1
        )

        $req.CertificateExtensions.Add(
            [X509BasicConstraintsExtension]::new(
                $CA,
                $false,
                0,
                $true
            )
        )

        if ($SubjectAlternativeNames) {
            $req.CertificateExtensions.Add(
                $SubjectAlternativeNames.Build($false)
            )
        }

        if ($KeyUsageFlags) {
            $req.CertificateExtensions.Add(
                [X509KeyUsageExtension]::new(
                    $KeyUsageFlags,
                    $true
                )
            )
        }

        if ($TLS) {
            $oids = [OidCollection]::new()
            $null = $oids.Add([Oid]::new('1.3.6.1.5.5.7.3.1'))

            $req.CertificateExtensions.Add(
                [X509EnhancedKeyUsageExtension]::new(
                    $oids,
                    $false
                )
            )
        }

        $req.CreateSelfSigned($NotBefore, $NotBefore.AddDays($ValidDays))
    }
    finally {
        $rsa.Dispose()
    }
}

<#
.Synopsis
Create subject alternative names for New-X509Certificate2.

.Description
Subject alternative names include DNS names, email addresses, and IP addresses for which a certificate may also authenticate connections.

.Parameter DnsName
One or more DNS names e.g., "www.microsoft.com".

.Parameter EmailAddress
One or more email addresses e.g., "opensource@microsoft.com".

.Parameter IpAddress
One or more IP addresses.

.Parameter Uri
Additional URIs not covered by other options.

.Parameter UserPrincipalName
Additional user names not covered by other options.
#>
function New-X509Certificate2SubjectAlternativeNames {
    [CmdletBinding()]
    [OutputType([System.Security.Cryptography.X509Certificates.SubjectAlternativeNameBuilder])]
    param (
        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [string[]] $DnsName,

        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [string[]] $EmailAddress,

        [Parameter()]
        [ValidateScript({[System.Net.IPAddress]::TryParse($_, [ref] $null)})]
        [string[]] $IpAddress,

        [Parameter()]
        [ValidateScript({[System.Uri]::TryParse($_, [ref] $null)})]
        [string[]] $Uri,

        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [string[]] $UserPrincipalName
    )

    $subjectAlternativeNames = [SubjectAlternativeNameBuilder]::new()

    foreach ($value in $DnsName) {
        $subjectAlternativeNames.AddDnsName($value)
    }

    foreach ($value in $EmailAddress) {
        $subjectAlternativeNames.AddEmailAddress($value)
    }

    foreach ($value in $IpAddress) {
        $subjectAlternativeNames.AddIpAddress($value)
    }

    foreach ($value in $Uri) {
        $subjectAlternativeNames.AddUri($value)
    }

    foreach ($value in $UserPrincipalName) {
        $subjectAlternativeNames.AddUserPrincipalName($value)
    }

    $subjectAlternativeNames
}

<#
.Synopsis
Exports a certificate to a file.

.Description
Exports an X509Certificate2 to a file in one of the given formats.

.Parameter Path
The path to the file to save.

.Parameter Type
The type of encoding for the file to save.

.Parameter Certificate
The certificate to save.
#>
function Export-X509Certificate2 {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory=$true, Position=0)]
        [string] $Path,

        [Parameter(Position=1)]
        [ValidateSet('Certificate', 'CertificateBase64', 'Pfx', 'Pkcs1', 'Pkcs12', 'Pkcs12Base64', 'Pkcs8', 'PrivateKey')]
        [string] $Type = 'Pfx',

        [Parameter(Mandatory=$true, ValueFromPipeline=$true)]
        [X509Certificate2] $Certificate
    )

    if ($Type -in 'Pfx', 'Pkcs12') {
        $Certificate.Export([X509ContentType]::Pfx) | Set-Content $Path -AsByteStream
    } else {
        Format-X509Certificate2 -Type $Type -Certificate $Certificate | Set-Content $Path -Encoding ASCII
    }
}

<#
.Synopsis
Formats a certificate.

.Description
Formats a certificate and prints it to the output buffer e.g., console. Useful for piping to clip.exe in Windows and pasting into code (additional formatting may be required).

.Parameter Type
The type of encoding for the output.

.Parameter Certificate
The certificate to format.
#>
function Format-X509Certificate2 {
    [CmdletBinding()]
    param (
        [Parameter(Position=0)]
        [ValidateSet('Certificate', 'CertificateBase64', 'Pkcs1', 'Pkcs12Base64', 'Pkcs8', 'PrivateKey')]
        [string] $Type = 'Certificate',

        [Parameter(Mandatory=$true, ValueFromPipeline=$true)]
        [X509Certificate2] $Certificate
    )

    function ConvertTo-Pem($tag, $data) {
        @"
-----BEGIN $tag-----
$([Convert]::ToBase64String($data, 'InsertLineBreaks'))
-----END $tag-----
"@
    }

    if ($Type -eq 'Certificate') {
        ConvertTo-Pem 'CERTIFICATE' $Certificate.RawData
    } elseif ($Type -eq 'CertificateBase64') {
        [Convert]::ToBase64String($Certificate.RawData, 'InsertLineBreaks')
    } elseif ($Type -eq 'Pkcs1') {
        ConvertTo-Pem 'RSA PRIVATE KEY' $Certificate.PrivateKey.ExportRSAPrivateKey()
    } elseif ($Type -eq 'Pkcs12Base64') {
        [Convert]::ToBase64String($Certificate.Export([X509ContentType]::Pfx), 'InsertLineBreaks')
    } elseif ($Type -in 'Pkcs8', 'PrivateKey') {
        ConvertTo-Pem 'PRIVATE KEY' $Certificate.PrivateKey.ExportPkcs8PrivateKey()
    }
}