Free ACME scripts / Windows

SQL Server certificate renewal script for simple-acme

Installs a simple-acme certificate on one Microsoft SQL Server instance and restarts that instance only.

The scripts are made for simple-acme and lego, which FairSSL, the company behind sslbrain, sponsors.

Version
1.1
Language
PowerShell
SHA-256
be368b0cb167dc56cab62b9701b91b5ce8b3b42d15373f9ac2629cb5cc031de3
Licence
Apache-2.0, copyright FairSSL A/S
Download sql-server-1.1.ps1

Download it on the server, check the SHA-256 and keep it as C:\simple-acme\Scripts\ImportSQLFairSSL.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/free-acme-scripts/files/sql-server-1.1.ps1' -OutFile 'C:\simple-acme\Scripts\ImportSQLFairSSL.ps1'
(Get-FileHash 'C:\simple-acme\Scripts\ImportSQLFairSSL.ps1' -Algorithm SHA256).Hash -eq 'BE368B0CB167DC56CAB62B9701B91B5CE8B3B42D15373F9AC2629CB5CC031DE3'
Unblock-File 'C:\simple-acme\Scripts\ImportSQLFairSSL.ps1'

What it does

FairSSL edition of ImportSQL.ps1 from win-acme (Apache License 2.0, https://github.com/win-acme/win-acme, dist/Scripts/ImportSQL.ps1). It writes the same setting: the thumbprint, in lower case, in the instance's SuperSocketNetLib\Certificate registry value. What changed, and why:

  • One instance, by name. The original wrote the setting of whichever instance sorted last in the registry and restarted MSSQLSERVER, the default instance, which on a server with only SQLEXPRESS does not exist. This script finds the instance in "Instance Names\SQL", writes its setting and restarts its service (MSSQLSERVER or MSSQL$<name>) and the services that depend on it, nothing else. With one instance on the server no name is needed; with several the second parameter names it.
  • Read access to the private key for the account the instance runs as (NT Service\MSSQL$<name> for a named instance), so a named instance can load the certificate without an extra --acl-read in the command.
  • Success only when it is true. The original printed the error and ended with exit code 0, so simple-acme and FairSSL reported a failed installation as a success. This script ends with exit code 1 unless the instance's new log says the certificate was loaded for encryption.
  • SQL Server that will not start with the new certificate is started again with the certificate it had before (or, when that one is gone, with none, so it uses its self-generated one), the services that depend on it are started again, and the run fails with the reason.

Runs on Windows Server 2012 and newer (PowerShell 3 and later, .NET 4.5).

Exit codes: 0 the instance runs and its log shows the new certificate loaded, or the instance is stopped and uses the certificate when it is started 1 anything else; the output says what happened and what state the instance is in

Requirements

  • Windows Server with Windows PowerShell 5.1, run as Administrator
  • simple-acme, which runs the script after each issuance and renewal

Parameters

-NewCertThumbprint Thumbprint of the new certificate. simple-acme passes it as {CertThumbprint}. simple-acme must have put it in LocalMachine\My (--certificatestore My).
-InstanceName The SQL Server instance: MSSQLSERVER for the default instance, otherwise its name (for example SQLEXPRESS). Needed only when the server has more than one instance.

Get a certificate and run the script

Made for simple-acme; works with win-acme too.

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.

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\ImportSQLFairSSL.ps1" `
  --scriptparameters "<parameters>"

Choose the ACME server

The command uses Let's Encrypt. For another ACME server, put its options in place of --baseuri "…"; for acme.sslbrain.com, --validation none replaces --validation selfhosting.

Let's Encrypt --baseuri "https://acme-v02.api.letsencrypt.org/directory" No account to create: the client registers one with your e-mail address. Let's Encrypt documentation
Google Trust Services --baseuri "https://dv.acme-v02.api.pki.goog/directory" --eab-key-identifier "EAB_KID" --eab-key "EAB_HMAC" 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
ZeroSSL --baseuri "https://acme.zerossl.com/v2/DV90" --eab-key-identifier "EAB_KID" --eab-key "EAB_HMAC" 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
acme.sslbrain.com --baseuri "https://acme.sslbrain.com/acme/directory" --eab-key-identifier "EAB_KID" --eab-key "EAB_HMAC" --validation none 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

Licence and credits

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

Based on a script from simple-acme (formerly win-acme), Apache-2.0. Thanks to the simple-acme project.

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