Public/Set-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
function Set-PAOrder {
    [CmdletBinding(SupportsShouldProcess)]
    param(
        [Parameter(Position=0,ValueFromPipeline,ValueFromPipelineByPropertyName)]
        [string]$MainDomain,
        [switch]$RevokeCert,
        [switch]$NoSwitch,
        [switch]$Force
    )

    Begin {
        # Make sure we have an account configured
        if (!(Get-PAAccount)) {
            throw "No ACME account configured. Run Set-PAAccount or New-PAAccount first."
        }
    }

    Process {

        # throw an error if there's no current order and no MainDomain
        # passed in
        if (!$script:Order -and !$MainDomain) {
            throw "No ACME order configured. Run New-PAOrder or specify a MainDomain."
        }

        # There are 3 types of calls the user might be making here.
        # - order switch
        # - order switch and revocation
        # - revocation only (possibly bulk via pipeline)
        # The default is to switch orders. So we have a -NoSwitch parameter to
        # indicate a non-switching revocation. But there's a chance they could forget
        # to use it for a bulk update. For now, we'll just let it happen and switch
        # to whatever order came through the pipeline last.

        if ($NoSwitch -and $MainDomain) {
            # This is a non-switching revocation, so grab a cached reference to the
            # order specified
            $order = Get-PAOrder $MainDomain

            if ($null -eq $order) {
                Write-Warning "Specified order for $MainDomain was not found. No changes made."
                return
            }

        } elseif (!$script:Order -or ($MainDomain -and ($MainDomain -ne $script:Order.MainDomain))) {
            # This is a definite order switch

            # refresh the cached copy
            Update-PAOrder $MainDomain

            Write-Debug "Switching to order $MainDomain"

            # save it as current
            $MainDomain | Out-File (Join-Path $script:AcctFolder 'current-order.txt') -Force

            # reload the cache from disk
            Import-PAConfig 'Order'

            # grab a local reference to the newly current order
            $order = $script:Order

        } else {
            # This is effectively a non-switching revocation because they didn't
            # specify a MainDomain. So just use the current order.
            $order = $script:Order
        }

        # revoke if necessary
        if ($RevokeCert) {

            # make sure the order is valid
            if ($order.status -ne 'valid') {
                Write-Warning "Unable to revoke certificate. Order for $($order.MainDomain) is not valid."
                return
            }

            # make sure the cert file actually exists
            $certFile = Join-Path (Join-Path $script:AcctFolder $order.MainDomain.Replace('*','!')) 'cert.cer'
            if (!(Test-Path $certFile -PathType Leaf)) {
                Write-Warning "Unable to revoke certificate. $certFile not found."
                return
            }

            # confirm revocation unless -Force was used
            if (!$Force) {
                if (!$PSCmdlet.ShouldContinue("Are you sure you wish to revoke $($order.MainDomain)?",
                "Revoking a certificate is irreversible and will immediately break any services using it.")) {
                    Write-Verbose "Revocation aborted for $($order.MainDomain)."
                    return
                }
            }

            Write-Verbose "Revoking certificate $($order.MainDomain)."

            # grab the cert file contents, strip the headers, and join the lines
            $certStart = -1; $certEnd = -1;
            $certLines = Get-Content $certFile
            for ($i=0; $i -lt $certLines.Count; $i++) {
                if ($certLines[$i] -eq '-----BEGIN CERTIFICATE-----') {
                    $certStart = $i + 1
                } elseif ($certLines[$i] -eq '-----END CERTIFICATE-----') {
                    $certEnd = $i - 1
                    break
                }
            }
            if ($certStart -lt 0 -or $certEnd -lt 0) {
                throw "Malformed certificate file. $certFile"
            }
            $certStr = $certLines[$certStart..$certEnd] -join '' | ConvertTo-Base64Url -FromBase64

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

            $payloadJson = "{`"certificate`":`"$certStr`"}"

            # send the request
            try {
                $response = Invoke-ACME $header $payloadJson $acct -EA Stop
            } catch { throw }
            Write-Debug "Response: $($response.Content)"

            # refresh the order
            Update-PAOrder $order.MainDomain

        }

    }





    <#
    .SYNOPSIS
        Set the current ACME order.
 
    .DESCRIPTION
        Switch between ACME orders on an account and/or revoke an order's certificate. Revoked certificate orders are not deleted and can be re-requested Submit-Renewal or New-PACertificate.
 
    .PARAMETER MainDomain
        The primary domain for the order. For a SAN order, this was the first domain in the list when creating the order.
 
    .PARAMETER RevokeCert
        If specified, a request will be sent to the associated ACME server to revoke the certificate on this order. Clients may wish to do this if the certificate is decommissioned or the private key has been compromised. A warning will be displayed if the order is not currently valid or the existing certificate file can't be found.
 
    .PARAMETER NoSwitch
        If specified, the currently selected order will not change. Useful primarily for bulk certificate revocation. This switch is ignored if no MainDomain is specified.
 
    .PARAMETER Force
        If specified, confirmation prompts for certificate revocation will be skipped.
 
    .EXAMPLE
        Set-PAOrder site1.example.com
 
        Switch to the specified domain's order.
 
    .EXAMPLE
        Set-PAORder -RevokeCert
 
        Revoke the current order's certificate.
 
    .LINK
        Project: https://github.com/rmbolger/Posh-ACME
 
    .LINK
        Get-PAOrder
 
    .LINK
        New-PAOrder
 
    #>

}