Free ACME scripts / Windows

FortiGate

Installs a simple-acme certificate on FortiGate over SSH, in the background, and keeps trying until FortiGate serves it.

Version
2.6
Language
PowerShell
SHA-256
d5e84bc6ef71c9382b42bc3e73cc5d2293893925821b4af42c6119f260f5c5b9
Licence
Apache-2.0, copyright FairSSL A/S
Download fortigate-2.6.ps1

Download it on the server, check the SHA-256 and keep it as C:\simple-acme\Scripts\Deploy-FortiGate.ps1, so a newer version later replaces it without a change to your renewal:

PowerShell
# Windows PowerShell 5.1 needs TLS 1.2 switched on for this session
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
Invoke-WebRequest -UseBasicParsing -Uri 'https://sslbrain.com/scripts/files/fortigate-2.6.ps1' -OutFile 'C:\simple-acme\Scripts\Deploy-FortiGate.ps1'
(Get-FileHash 'C:\simple-acme\Scripts\Deploy-FortiGate.ps1' -Algorithm SHA256).Hash -eq 'D5E84BC6EF71C9382B42BC3E73CC5D2293893925821B4AF42C6119F260F5C5B9'
Unblock-File 'C:\simple-acme\Scripts\Deploy-FortiGate.ps1'

What it does

simple-acme runs this script as its installation step. The script checks the PEM files, hands the job to a worker and returns 0 straight away; simple-acme is never held up by FortiGate. When an administrator runs wacs.exe by hand, the script shows the worker's progress until its first attempt ends.

The worker is a scheduled task, "FairSSL FortiGate <id>", that runs once as SYSTEM with no time limit, one copy per FortiGate and certificate. On every attempt it:

  • reads FortiOS version, model, VDOM mode, HA mode, save mode, SSL VPN and the admin HTTPS port; on the secondary unit of an HA cluster it changes nothing (HA_SECONDARY: use the cluster's address);
  • on a FortiGate with multiple VDOMs, works as the account allows (see VDOMs below);
  • uploads the certificate with its intermediates as fssl-<id6>-<fp12>, and offers each intermediate to the CA store of the VDOM whose SSL VPN gets it (FortiGate refusing it, or already holding it, is never an error: FortiGate serves the chain from the certificate itself until it restarts, and from its CA store after that);
  • moves SSL VPN and admin HTTPS to it where they use this certificate's earlier object, a certificate for the same name (its common name is this certificate's common name or one of its names; also the timestamped objects of earlier versions of this script), or a factory certificate: SSL VPN's where it listens on an interface on a FortiGate without VDOMs, otherwise admin HTTPS's;
  • reads every change back in FortiGate's full settings, every 5 seconds for up to 30 seconds, and saves the configuration where cfg-save is manual; FortiGate's answer to the save must say it saved (CFG_SAVE_FAILED otherwise; only the super_admin profile may save). With cfg-save revert it changes nothing (CFG_SAVE_REVERT), because a save would also keep other administrators' changes. A change FortiGate does not show yet is not an error: the log says so in full sentences and when it is checked again;
  • looks with a TLS handshake at what the listener serves, for the log only: the configuration read over SSH decides. Only when the listener answered before the change and gives no handshake after it, the previous certificate is put back on that setting (ROLLED_BACK, tried again in a day or with the next certificate; ROLLBACK_FAILED when FortiGate did not take the previous certificate back);
  • keeps the new and the previous object and every object a setting uses, and deletes older objects of its own; it deletes no CA object.

A failed attempt is tried again after 1, 2, 5, 10 and 30 minutes and then every hour, with no end, until FortiGate has the certificate. FairSSL is told of a failure only when the certificate has still not been installed 15 minutes after the first failed attempt (attempt 5, after about 18 minutes); before that each attempt is reported as still trying. A change FortiGate accepted and does not show yet is reported the same way, 15 minutes after the first attempt; NO_ROLE, ROLLED_BACK, ROLLBACK_FAILED and CFG_SAVE_REVERT are reported at once, as only the customer can change them. A newer certificate from simple-acme in the meantime is taken along. After a restart of the server the task starts again by itself. The worker's log is C:\ProgramData\FairSSL\FortiGate\<id>\worker.log.

When neither SSL VPN nor admin HTTPS uses a certificate the worker may take over, it changes nothing, reports NO_ROLE and tries again once a day. Point admin HTTPS, or SSL VPN when it listens on an interface, at a factory certificate (Fortinet_Factory), or with several VDOMs SSL VPN at a certificate for the same name, and run the wacs.exe command of the guide again; the worker then takes it over at once.

VDOMs are found without a parameter. The administrator of one VDOM works in its own VDOM, the one its login lands in: the certificate, the CA store and SSL VPN are changed there, and SSL VPN gets the certificate whatever it used before. Admin HTTPS and cfg-save are global, and FortiGate does not let a VDOM administrator change or read them, so admin HTTPS is left as it is and a global administrator saves the configuration where cfg-save is manual; the log and the report say so. The administrator of several VDOMs is shown only the VDOM it starts in, and works there with the global administrator's rule. A global administrator's worker reads SSL VPN in every VDOM and moves it in each VDOM where it uses this certificate or a certificate for the same name. It takes over no factory certificate on SSL VPN, because each VDOM may be another customer's. Its certificate is uploaded once in global, because admin HTTPS can only use a global certificate, and every VDOM uses that object; admin HTTPS is changed in global. An account FortiGate shows neither its status nor its global settings changes only this certificate's object or a certificate for the same name.

A certificate browsers would refuse is not installed: an expired one, one that is not valid yet, and one with another key than RSA or ECDSA (Ed25519, for one).

When SSL VPN moves to the new certificate, connected SSL VPN users are disconnected for a moment. simple-acme's scheduled task starts renewals between 01:00 and 05:00 server time by default; C:\simple-acme\fairssl-readme.txt says how to choose another time.

Each attempt's result goes to the report address (-ReportUrl; FairSSL's control panel in FairSSL's download): the ACME account, renewal id, domain, thumbprint, expiry, whether the installation worked, the error and the worker's own log of that attempt; also this script's file name, version and SHA-256, FortiGate's model and FortiOS version, the server's Windows and PowerShell version and the certificate's key type and SHA-256 fingerprint. Never the password, the private key or the certificate. A report the receiver has not confirmed waits in the job folder and is sent again.

The FortiGate host key is accepted on the first connection and kept for the whole machine in C:\ProgramData\FairSSL\FortiGate\hostkeys.json. A changed key is refused and reported as HOSTKEY_CHANGED; when FortiGate's key really changed, delete its line there.

Requires Windows PowerShell 5.1 and .NET Framework 4.7.2 or newer. Posh-SSH 4.0.0 ships in Scripts\Modules\Posh-SSH and is loaded from there.

Requirements

  • Windows Server with Windows PowerShell 5.1, run as Administrator
  • simple-acme, which runs the script after each issuance and renewal
  • Posh-SSH 4.0.0 in Scripts\Modules\Posh-SSH (see below)

Parameters

-StorePath Folder where simple-acme stores PEM files.
-FilePrefix PEM filename prefix configured in simple-acme.
-FirewallHost FortiGate management IP address or hostname.
-Port FortiGate SSH port. Defaults to 22.
-CertLabel Name of this certificate in the FortiGate object names. Defaults to FilePrefix.
-UserName FortiGate SSH user. Defaults to admin.
-Password FortiGate SSH password, in plain text or as '{vault://json/<name>}' in simple-acme's --scriptparameters, which simple-acme replaces before it starts this script. Leave it out: the script then reads the password from simple-acme's vault itself (-SecretName), so it is on no command line. A password given here is kept in simple-acme's renewal settings and in its log, and is on powershell.exe's command line at every renewal.
-SecretName Name of the FortiGate password in simple-acme's vault (wacs.exe: O, S, A); default fortigate. simple-acme keeps it encrypted for this machine, so its renewal task, which runs as SYSTEM, can read it. Without -Password and without a vault secret of this name, the login is read from Windows Credential Manager (-CredentialTarget), as installations made with an earlier guide do.
-VaultFile simple-acme's secrets.json, when the same secret name holds different passwords for different ACME servers. Normally left out.
-RenewalId simple-acme's {RenewalId}, used in the reports. Installations made with an earlier guide do not pass it; the script then finds the renewal itself.
-CredentialTarget Windows Credential Manager target, used only without -Password and without a vault secret named -SecretName.
-UpdateAdmin Accepted from installations made with an earlier guide. Admin HTTPS is moved by the rules above, so it changes nothing.
-SkipSslVpnUpdate Leaves SSL VPN as it is. Accepted from installations made with an earlier guide.
-ReportUrl Where each result is reported: an https address. Leave it out: the address in $DefaultReportUrl below is used. FairSSL's download has FairSSL's address there; in the script library it is empty, and then nothing is sent. -CallbackUrl is the earlier name of this parameter and still works.
-Worker Used by the script itself: runs the background worker for -JobFolder.
-JobFolder Used by the script itself: the worker's job folder.

Install Posh-SSH 4.0.0

The script talks to the appliance over SSH with Posh-SSH 4.0.0 (BSD-3-Clause) and loads it from Modules\Posh-SSH beside the script. Without it the script stops with POSH_SSH_MISSING. On Windows PowerShell 5.1 the PowerShell Gallery needs TLS 1.2 and the NuGet provider first; Save-Module downloads the module and Move-Item puts it in place. Posh-SSH needs .NET Framework 4.7.2 or newer.

PowerShell
# Run as Administrator in Windows PowerShell 5.1. $scripts is the folder that holds the script.
& {
  $ErrorActionPreference = 'Stop'
  $scripts = 'C:\simple-acme\Scripts'
  # The PowerShell Gallery requires TLS 1.2, and PowerShellGet needs the NuGet provider
  [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
  Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force | Out-Null
  $download = (New-Item -ItemType Directory -Force -Path "$scripts\Modules\posh-ssh-download-$(Get-Random)").FullName
  Save-Module -Name Posh-SSH -RequiredVersion 4.0.0 -Repository PSGallery -Path $download
  # Only now that the new copy is here: rename an earlier one aside, put the new one in place, delete the earlier one
  if (Test-Path "$scripts\Modules\Posh-SSH") { Rename-Item "$scripts\Modules\Posh-SSH" -NewName "Posh-SSH.old-$(Get-Random)" }
  Move-Item -Path "$download\Posh-SSH\4.0.0" -Destination "$scripts\Modules\Posh-SSH"
  Remove-Item -Recurse -Force $download, "$scripts\Modules\Posh-SSH.old-*" -ErrorAction SilentlyContinue
  Test-Path "$scripts\Modules\Posh-SSH\Posh-SSH.psd1"
}

When Save-Module stops at the NuGet provider

On some servers, for example Windows Server 2012 R2 with PowerShell 5.1, the NuGet provider cannot be downloaded, so Install-PackageProvider and Save-Module stop. Then take the same Posh-SSH 4.0.0 package straight from the PowerShell Gallery. The block checks its SHA-256 (D8BAD0148629043E257B24A3D01C5F353775E8D391E9203B5AE0C319E3839C91) and unpacks and installs nothing if it differs.

PowerShell
# Run as Administrator in Windows PowerShell 5.1. $scripts is the folder that holds the script.
& {
  $ErrorActionPreference = 'Stop'
  $scripts = 'C:\simple-acme\Scripts'
  [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
  $zip = Join-Path $env:TEMP "Posh-SSH-4.0.0-$(Get-Random).zip"
  Invoke-WebRequest -UseBasicParsing -Uri 'https://www.powershellgallery.com/api/v2/package/Posh-SSH/4.0.0' -OutFile $zip
  $sha256 = (Get-FileHash $zip -Algorithm SHA256).Hash
  if ($sha256 -ne 'D8BAD0148629043E257B24A3D01C5F353775E8D391E9203B5AE0C319E3839C91') {
    throw "The package has SHA-256 $sha256, not the one of Posh-SSH 4.0.0. Nothing was installed."
  }
  $download = (New-Item -ItemType Directory -Force -Path "$scripts\Modules\posh-ssh-download-$(Get-Random)").FullName
  Add-Type -AssemblyName System.IO.Compression.FileSystem
  [IO.Compression.ZipFile]::ExtractToDirectory($zip, $download)
  # The package's own NuGet files are not part of the module
  Remove-Item -Recurse -Force -LiteralPath "$download\_rels", "$download\package", "$download\[Content_Types].xml", "$download\Posh-SSH.nuspec"
  # Only now that the new copy is here: rename an earlier one aside, put the new one in place, delete the earlier one
  if (Test-Path "$scripts\Modules\Posh-SSH") { Rename-Item "$scripts\Modules\Posh-SSH" -NewName "Posh-SSH.old-$(Get-Random)" }
  Move-Item -Path $download -Destination "$scripts\Modules\Posh-SSH"
  Remove-Item -Recurse -Force $zip, "$scripts\Modules\Posh-SSH.old-*" -ErrorAction SilentlyContinue
  Test-Path "$scripts\Modules\Posh-SSH\Posh-SSH.psd1"
}

Paste a block as a whole and press Enter, twice if the prompt shows >>. It stops at the first error and then leaves an installed Posh-SSH as it was. The last line answers True when the module is where the script looks for it.

Get a certificate and run the script

Run simple-acme's wacs.exe from its folder (here C:\simple-acme) as Administrator. It orders the certificate, saves it in the Windows certificate store and as PEM files, runs the script, and creates the scheduled task that renews the certificate and runs the script again. Replace www.example.com, the e-mail address and the script's parameters (see Parameters; simple-acme fills in tokens such as {CertThumbprint}, {StorePath} and {RenewalId}). --validation selfhosting answers the CA's HTTP challenge on port 80; on a web server use --validation filesystem --webroot <site folder> instead.

Let's Encrypt

Directory: https://acme-v02.api.letsencrypt.org/directory. No account to create: the client registers one with your e-mail address. Let's Encrypt documentation

simple-acme
.\wacs.exe `
  --baseuri "https://acme-v02.api.letsencrypt.org/directory" `
  --accepttos --emailaddress "you@example.com" `
  --source manual --host "www.example.com" `
  --validation selfhosting `
  --store certificatestore,pemfiles --pemfilespath "C:\simple-acme\Certificates" `
  --installation script `
  --script ".\Scripts\Deploy-FortiGate.ps1" `
  --scriptparameters "<parameters>"
Google Trust Services

Directory: https://dv.acme-v02.api.pki.goog/directory. Create an EAB key in a Google Cloud project (gcloud publicca external-account-keys create). Put the key's id and HMAC in place of EAB_KID and EAB_HMAC. Google Public CA tutorial

simple-acme
.\wacs.exe `
  --baseuri "https://dv.acme-v02.api.pki.goog/directory" `
  --eab-key-identifier "EAB_KID" --eab-key "EAB_HMAC" `
  --accepttos --emailaddress "you@example.com" `
  --source manual --host "www.example.com" `
  --validation selfhosting `
  --store certificatestore,pemfiles --pemfilespath "C:\simple-acme\Certificates" `
  --installation script `
  --script ".\Scripts\Deploy-FortiGate.ps1" `
  --scriptparameters "<parameters>"
ZeroSSL

Directory: https://acme.zerossl.com/v2/DV90. Copy the EAB credentials from the Developer section of your ZeroSSL account. Put the key's id and HMAC in place of EAB_KID and EAB_HMAC. ZeroSSL ACME documentation

simple-acme
.\wacs.exe `
  --baseuri "https://acme.zerossl.com/v2/DV90" `
  --eab-key-identifier "EAB_KID" --eab-key "EAB_HMAC" `
  --accepttos --emailaddress "you@example.com" `
  --source manual --host "www.example.com" `
  --validation selfhosting `
  --store certificatestore,pemfiles --pemfilespath "C:\simple-acme\Certificates" `
  --installation script `
  --script ".\Scripts\Deploy-FortiGate.ps1" `
  --scriptparameters "<parameters>"
acme.sslbrain.com

Directory: https://acme.sslbrain.com/acme/directory. Create an EAB key in sslbrain Cloud (Basic or higher) under ACME clients, Connect a client. sslbrain Cloud validates the domain through a CNAME redirect, a DNS API or pre-validation, not your ACME client. Put the key's id and HMAC in place of EAB_KID and EAB_HMAC. sslbrain plans

simple-acme
.\wacs.exe `
  --baseuri "https://acme.sslbrain.com/acme/directory" `
  --eab-key-identifier "EAB_KID" --eab-key "EAB_HMAC" `
  --accepttos --emailaddress "you@example.com" `
  --source manual --host "www.example.com" `
  --validation none `
  --store certificatestore,pemfiles --pemfilespath "C:\simple-acme\Certificates" `
  --installation script `
  --script ".\Scripts\Deploy-FortiGate.ps1" `
  --scriptparameters "<parameters>"

Report each result to your own receiver

The script can send the result of every installation to an HTTPS address of yours, so one place shows the status, errors and expiry of all your servers. It sends nothing until you give it the address: pass -ReportUrl 'https://reports.example.com/acme' or set the report address in the variable at the top of the script.

Each report is a JSON POST; your receiver answers {"ok": true}, and a report without that answer is kept and sent again. The fields are in the report format.

Licence and credits

Copyright FairSSL A/S, licensed under the Apache License 2.0: use it, change it and share it, also commercially.

Runs in simple-acme (formerly win-acme), Apache-2.0.

Uses Posh-SSH by Carlos Perez, BSD-3-Clause.

One script per server, or sslbrain does it for all your servers without scripts.

See how sslbrain works

Feedback and improvements are welcome: info@fairssl.dk