Free ACME scripts / Windows
FortiMail
Installs a simple-acme certificate on FortiMail over SSH, in the background, and keeps trying until FortiMail serves it.
- Version
- 2.5
- Language
- PowerShell
- SHA-256
- 7b7aa6b7d32dfe27623bd06edad073ea2110ecaad72f2835cef5f4f2744ee22b
- Licence
- Apache-2.0, copyright FairSSL A/S
Download it on the server, check the SHA-256 and keep it as C:\simple-acme\Scripts\Deploy-FortiMail.ps1, so a newer version later replaces it without a change to your renewal:
# 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/fortimail-2.5.ps1' -OutFile 'C:\simple-acme\Scripts\Deploy-FortiMail.ps1'
(Get-FileHash 'C:\simple-acme\Scripts\Deploy-FortiMail.ps1' -Algorithm SHA256).Hash -eq '7B7AA6B7D32DFE27623BD06EDAD073EA2110ECAAD72F2835CEF5F4F2744EE22B'
Unblock-File 'C:\simple-acme\Scripts\Deploy-FortiMail.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 FortiMail. 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 FortiMail <id>", that runs once as SYSTEM with no time limit, one copy per FortiMail and certificate. On every attempt it:
- reads FortiMail's version and its default certificate;
- uploads the certificate with its intermediates as fssl-<id6>-<fp12> (a FortiMail that refuses the intermediates in the certificate gets the certificate alone), and offers each intermediate to the CA store, where a CA FortiMail already holds ("Found duplicated CA") or any other answer is never an error;
- makes it FortiMail's default certificate, which FortiMail uses for HTTPS, webmail and mail over TLS, reads that back, and reloads httpd and smtpd;
- looks with TLS handshakes on HTTPS and SMTP (STARTTLS) at what is served, for the log; the default certificate read back decides. If a service answered before and not after, the previous certificate is put back;
- keeps the new and the previous certificate and deletes older ones it made itself (also the dated ones from earlier versions of this script).
A failed attempt is tried again after 1, 2, 5, 10 and 30 minutes and then every hour, with no end, until FortiMail serves the certificate. 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\FortiMail\<id>\worker.log.
A certificate whose switch had to be put back is tried again after a day, not every hour, because every try reloads httpd and smtpd on a working mail gateway. A new certificate from simple-acme is tried at once.
A certificate clients 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).
The reload of httpd and smtpd disconnects web and mail sessions 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, FortiMail's model and 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 FortiMail host key is accepted on the first connection and kept for the whole machine in C:\ProgramData\FairSSL\FortiMail\hostkeys.json. A changed key is refused and reported as HOSTKEY_CHANGED; when FortiMail'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. |
| -FortiMailHost | FortiMail management IP address or hostname. |
| -Port | FortiMail SSH port. Defaults to 22. |
| -CertName | Name of this certificate in the FortiMail object names. Defaults to FilePrefix. |
| -UserName | FortiMail SSH user. Defaults to admin. The reload of httpd and smtpd needs a super admin. |
| -Password | FortiMail SSH password, in plain text or as '{vault://json/<name>}' in simple-acme's --scriptparameters, which simple-acme replaces before it starts this script. Without it the login is read from Windows Credential Manager (-CredentialTarget), as installations made with an earlier guide do. simple-acme keeps a vault password encrypted for this machine, so its renewal task, which runs as SYSTEM, can read it. A plain-text password is kept in simple-acme's renewal settings and in its log. |
| -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. |
| -ReloadServices | FortiMail services reloaded after the switch. Defaults to httpd and smtpd. |
| -SkipServiceReload | No reload after the switch; the TLS check then waits for FortiMail to serve the certificate by itself. |
| -HttpsPort | FortiMail's HTTPS port for the TLS check. Defaults to 443. |
| -SmtpPort | FortiMail's SMTP port for the STARTTLS check. Defaults to 25. |
| -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.
# 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.
# 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
.\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-FortiMail.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
.\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-FortiMail.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
.\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-FortiMail.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
.\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-FortiMail.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.
Feedback and improvements are welcome: info@fairssl.dk