Public/New-PAOrder.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
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
function New-PAOrder {
    [CmdletBinding(SupportsShouldProcess,DefaultParameterSetName='FromScratch')]
    [OutputType('PoshACME.PAOrder')]
    param(
        [Parameter(ParameterSetName='FromScratch',Mandatory,Position=0)]
        [Parameter(ParameterSetName='ImportKey',Mandatory,Position=0)]
        [string[]]$Domain,
        [Parameter(ParameterSetName='FromCSR',Mandatory,Position=0)]
        [string]$CSRPath,
        [Parameter(ParameterSetName='FromScratch',Position=1)]
        [ValidateScript({Test-ValidKeyLength $_ -ThrowOnFail})]
        [string]$KeyLength='2048',
        [Parameter(ParameterSetName='ImportKey',Mandatory)]
        [string]$KeyFile,
        [ValidateScript({Test-ValidFriendlyName $_ -ThrowOnFail})]
        [string]$Name,
        [ValidateScript({Test-ValidPlugin $_ -ThrowOnFail})]
        [string[]]$Plugin,
        [hashtable]$PluginArgs,
        [string[]]$DnsAlias,
        [Parameter(ParameterSetName='FromScratch')]
        [Parameter(ParameterSetName='ImportKey')]
        [switch]$OCSPMustStaple,
        [Parameter(ParameterSetName='FromScratch')]
        [Parameter(ParameterSetName='ImportKey')]
        [switch]$AlwaysNewKey,
        [Parameter(ParameterSetName='FromScratch')]
        [Parameter(ParameterSetName='ImportKey')]
        [string]$FriendlyName,
        [Parameter(ParameterSetName='FromScratch')]
        [Parameter(ParameterSetName='ImportKey')]
        [string]$PfxPass='poshacme',
        [Parameter(ParameterSetName='FromScratch')]
        [Parameter(ParameterSetName='ImportKey')]
        [ValidateScript({Test-SecureStringNotNullOrEmpty $_ -ThrowOnFail})]
        [securestring]$PfxPassSecure,
        [Parameter(ParameterSetName='FromScratch')]
        [Parameter(ParameterSetName='ImportKey')]
        [switch]$Install,
        [switch]$UseSerialValidation,
        [int]$DnsSleep=120,
        [int]$ValidationTimeout=60,
        [string]$PreferredChain,
        [switch]$Force
    )

    # Make sure we have an account configured
    if (-not ($acct = Get-PAAccount)) {
        try { throw "No ACME account configured. Run Set-PAAccount or New-PAAccount first." }
        catch { $PSCmdlet.ThrowTerminatingError($_) }
    }

    # If using a pre-generated CSR, extract the details so we can generate expected parameters
    if ('FromCSR' -eq $PSCmdlet.ParameterSetName) {
        $CSRPath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($CSRPath)
        $csrDetails = Get-CsrDetails $CSRPath

        $Domain = $csrDetails.Domain
        $KeyLength = $csrDetails.KeyLength
        $OCSPMustStaple = New-Object Management.Automation.SwitchParameter($csrDetails.OCSPMustStaple)
    }

    # If importing a key, make sure it's valid so we can set the appropriate KeyLength
    if ('ImportKey' -eq $PSCmdlet.ParameterSetName) {
        $KeyFile = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($KeyFile)
        try {
            $kLength = [string]::Empty
            # we don't actually care about the key object, just the parsed length
            $null = New-PAKey -KeyFile $KeyFile -ParsedLength ([ref]$kLength)
            $KeyLength = $kLength
        }
        catch { $PSCmdlet.ThrowTerminatingError($_) }
    }

    # PfxPassSecure takes precedence over PfxPass if both are specified but we
    # need the value in plain text. So we'll just take over the PfxPass variable
    # to use for the rest of the function.
    if ($PfxPassSecure) {
        # throw a warning if they also specified PfxPass
        if ('PfxPass' -in $PSBoundParameters.Keys) {
            Write-Warning "PfxPass and PfxPassSecure were both specified. Using value from PfxPassSecure."
        }

        # override the existing PfxPass parameter
        $PfxPass = [pscredential]::new('u',$PfxPassSecure).GetNetworkCredential().Password
        $PSBoundParameters.PfxPass = $PfxPass
    }

    # check for an existing order
    if ($Name) {
        $order = Get-PAOrder -Name $Name
    } else {
        $order = Get-PAOrder -MainDomain $Domain[0]

        # set the default Name to a filesystem friendly version of the first domain
        $Name = $Domain[0].Replace('*','!')
    }

    # separate the SANs
    $SANs = @($Domain | Where-Object { $_ -ne $Domain[0] })

    # There's a chance we may be overwriting an existing order here. So check for
    # confirmation if certain conditions are true
    if (-not $Force) {

        $oldDomains = (@($order.MainDomain) + @($order.SANs) | Sort-Object) -join ','

        # skip confirmation if the Domains or KeyLength are different regardless
        # of the original order status or if the order is pending but expired
        if ( ($order -and ($KeyLength -ne $order.KeyLength -or
             ($oldDomains -ne ($Domain | Sort-Object) -join ',') -or
             ($order.status -eq 'pending' -and (Get-DateTimeOffsetNow) -gt ([DateTimeOffset]::Parse($order.expires))) ))) {
            # do nothing

        # confirm if previous order is still in progress
        } elseif ($order -and $order.status -in 'pending','ready','processing') {

            if (-not $PSCmdlet.ShouldContinue("Do you wish to overwrite?",
                "Existing order with status $($order.status).")) { return }

        # confirm if previous order not up for renewal
        } elseif ($order -and $order.status -eq 'valid' -and
                    (Get-DateTimeOffsetNow) -lt ([DateTimeOffset]::Parse($order.RenewAfter))) {

            if (-not $PSCmdlet.ShouldContinue("Do you wish to overwrite?",
                "Existing order has not reached suggested renewal window.")) { return }
        }
    }

    Write-Debug "Creating new $KeyLength order with domains: $($Domain -join ', ')"

    # Force a key change if the KeyLength is different than the old order
    if ($order -and $order.KeyLength -ne $KeyLength) {
        $ForceNewKey = $true
    }

    # build the protected header for the request
    $header = @{
        alg   = $acct.alg;
        kid   = $acct.location;
        nonce = $script:Dir.nonce;
        url   = $script:Dir.newOrder;
    }

    # super lazy IPv4 address regex, but we just need to be able to
    # distinguish from an FQDN
    $reIPv4 = [regex]'^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$'

    # build the payload object
    $payload = @{identifiers=@()}
    foreach ($d in $Domain) {

        # IP identifiers (RFC8738) are an extension to the original ACME protocol
        # https://tools.ietf.org/html/rfc8738
        #
        # So we have to distinguish between domain FQDNs and IPv4/v6 addresses
        # and send the appropriate identifier type for each one. We don't care
        # if the IP address entered is actually valid or not, only that it is
        # parsable as an IP address and should be sent as one rather than a
        # DNS name.

        if ($d -match $reIPv4 -or $d -like '*:*') {
            Write-Debug "$d identified as IP address. Attempting to parse."
            $ip = [ipaddress]$d

            $payload.identifiers += @{type='ip';value=$ip.ToString()}
        }
        else {
            $payload.identifiers += @{type='dns';value=$d}
        }

    }
    $payloadJson = $payload | ConvertTo-Json -Depth 5 -Compress

    # send the request
    try {
        $response = Invoke-ACME $header $payloadJson $acct -EA Stop
    } catch { throw }

    # process the response
    $order = $response.Content | ConvertFrom-Json
    $order.PSObject.TypeNames.Insert(0,'PoshACME.PAOrder')

    # fix any dates that may have been parsed by PSCore's JSON serializer
    $order.expires = Repair-ISODate $order.expires

    # Per https://tools.ietf.org/html/rfc8555#section-7.1.3
    # In the returned order object, there is no guarantee that the list of identifiers
    # match the sequence they were submitted in. The list of authorizations may not match
    # either. And the identifiers and authorizations may not even match each other's
    # sequence.
    #
    # Unfortunately, things like DNS plugins and challenge aliases currently depend on
    # the assumption that the sequence of identifiers and the sequence of authorizations
    # all match the original sequence of the submitted domains. So we need to make sure
    # that it's true until we refactor things so those assumptions aren't necessary anymore.

    # set the order's identifiers to the original payload's identifiers since that was
    # correct already
    $order.identifiers = $payload.identifiers

    # unfortunately, there's no way to know which authorization URL is for which identifier
    # just by parsing it. So we need to query the details for each one in order to put them
    # in the right order
    $auths = Get-PAAuthorization $order.authorizations
    for ($i=0; $i -lt $order.identifiers.Count; $i++) {
        $auth = $auths | Where-Object { $_.fqdn -eq $order.identifiers[$i].value }
        $order.authorizations[$i] = $auth.location
    }

    # make sure FriendlyName is non-empty
    if ([String]::IsNullOrWhiteSpace($FriendlyName)) {
        $FriendlyName = $Domain[0]
    }

    # add additional members we'll need for later
    $order | Add-Member -NotePropertyMembers @{
        MainDomain          = $Domain[0]
        SANs                = $SANs
        KeyLength           = $KeyLength
        CertExpires         = $null
        RenewAfter          = $null
        OCSPMustStaple      = $OCSPMustStaple.IsPresent
        Plugin              = @('Manual')
        DnsAlias            = $null
        DnsSleep            = $DnsSleep
        ValidationTimeout   = $ValidationTimeout
        FriendlyName        = $FriendlyName
        PfxPass             = $PfxPass
        Install             = $Install.IsPresent
        UseSerialValidation = $UseSerialValidation.IsPresent
        PreferredChain      = $PreferredChain
        AlwaysNewKey        = $AlwaysNewKey.IsPresent
    }

    # override AlwaysNewKey if they're importing the private key
    if ($order.AlwaysNewKey -and 'ImportKey' -eq $PSCmdlet.ParameterSetName) {
        Write-Warning "AlwaysNewKey was disabled because private key was imported using the KeyFile parameter."
        $order.AlwaysNewKey = $false
    }

    # make sure there's a certificate field for later
    if ('certificate' -notin $order.PSObject.Properties.Name) {
        $order | Add-Member 'certificate' $null
    }

    # add the CSR data if we have it
    if ('FromCSR' -eq $PSCmdlet.ParameterSetName) {
        $order | Add-Member 'CSRBase64Url' $csrDetails.Base64Url
    }

    # update other optional fields
    if ('Plugin' -in $PSBoundParameters.Keys) {
        $order.Plugin = @($Plugin)
    }
    if ('DnsAlias' -in $PSBoundParameters.Keys) {
        $order.DnsAlias = @($DnsAlias)
    }

    # add the location from the header
    if ($response.Headers.ContainsKey('Location')) {
        $location = $response.Headers['Location'] | Select-Object -First 1
        Write-Debug "Adding location $location"
        $order | Add-Member -MemberType NoteProperty -Name 'location' -Value $location
    } else {
        try { throw 'No Location header found in newOrder output' }
        catch { $PSCmdlet.ThrowTerminatingError($_) }
    }

    # add the Name and Folder properties
    $order | Add-Member 'Name' $Name -Force
    $order | Add-Member 'Folder' (Join-Path $acct.Folder $Name) -Force

    # save it to memory and disk
    $order.Name | Out-File (Join-Path $acct.Folder 'current-order.txt') -Force -EA Stop
    $script:Order = $order
    Update-PAOrder $order -SaveOnly

    # export plugin args now that the order exists on disk
    if ('PluginArgs' -in $PSBoundParameters.Keys) {
        Export-PluginArgs -Order $order -PluginArgs $PluginArgs
    }

    # Make a local copy of the specified CSR file
    if ('FromCSR' -eq $PSCmdlet.ParameterSetName) {
        $csrDest = Join-Path $order.Folder 'request.csr'
        if ($CSRPath -ne $csrDest) {
            Copy-Item -Path $CSRPath -Destination $csrDest
        }
    }

    # Determine whether to remove the old private key. This is necessary if it exists
    # and we're using a CSR or it's explicitly requested or the new KeyLength doesn't match the old one.
    $keyPath = Join-Path $order.Folder 'cert.key'
    $removeOldKey = ( (Test-Path $keyPath -PathType Leaf) -and
                      ($order.AlwaysNewKey -or $ForceNewKey -or 'FromCSR' -eq $PSCmdlet.ParameterSetName) )

    # backup the old private key if necessary
    if ($removeOldKey) {
        Write-Verbose "Removing old private key"
        $oldKey = Get-ChildItem $keyPath
        $oldKey | Move-Item -Destination { "$($_.FullName).bak" } -Force
    }

    # backup any old certs/requests that might exist
    $oldFiles = Get-ChildItem (Join-Path $order.Folder *) -Include cert.cer,cert.pfx,chain.cer,fullchain.cer,fullchain.pfx
    $oldFiles | Move-Item -Destination { "$($_.FullName).bak" } -Force

    # remove old chain files
    Get-ChildItem (Join-Path $order.Folder 'chain*.cer') -Exclude chain.cer |
        Remove-Item -Force

    # Make a local copy of the private key if it was specified.
    if ('ImportKey' -eq $PSCmdlet.ParameterSetName) {
        if ($keyPath -ne $KeyFile) {
            Copy-Item -Path $KeyFile -Destination $keyPath
        }
    }

    return $order
}