Free ACME scripts / Windows

AD FS WAP

Install / enable an ACME certificate on Microsoft AD FS or Microsoft Web Application Proxy (WAP) from simple-acme. Detects which of the two roles this server runs and applies the matching installation.

Version
1.2
Language
PowerShell
SHA-256
34e2a1912d810b2b93891e669e2f7b220a0b94ef8c3213de640aa3ce956efc96
Licence
Apache-2.0, copyright FairSSL A/S
Download adfs-wap-1.2.ps1

Download it on the server, check the SHA-256 and keep it as C:\simple-acme\Scripts\ImportADFS-WAP.v1.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/adfs-wap-1.2.ps1' -OutFile 'C:\simple-acme\Scripts\ImportADFS-WAP.v1.ps1'
(Get-FileHash 'C:\simple-acme\Scripts\ImportADFS-WAP.v1.ps1' -Algorithm SHA256).Hash -eq '34E2A1912D810B2B93891E669E2F7B220A0B94EF8C3213DE640AA3CE956EFC96'
Unblock-File 'C:\simple-acme\Scripts\ImportADFS-WAP.v1.ps1'

What it does

FairSSL installation script for the Microsoft federation stack. simple-acme runs this after it has obtained and stored the certificate; the script's job is to make this server present that certificate.

AD FS and Web Application Proxy are mutually exclusive Windows roles, and both sit behind the same federation hostname, so the two are easy to mix up. This script removes the guesswork: it checks which role service exists (adfssrv = AD FS, appproxysvc = WAP), states the detected role in the log, and runs the corresponding installation. If neither role is installed it stops with a clear error. This script replaces ImportADFS.v2.ps1 and ImportWAP.v1.ps1, which remain as compatibility wrappers for existing renewals and simply forward here.

Both role paths load their PowerShell module locally, in-process (no WinRM remoting), so the script works on domain-joined servers as well as workgroup boxes in a DMZ.

On an AD FS server, in order: 1. Loads the AD FS module (Import-Module ADFS). 2. Finds the certificate by thumbprint in LocalMachine\My or LocalMachine\WebHosting (some simple-acme configurations store it in WebHosting). If it is missing and a CacheFile was supplied, it imports the PFX as a fallback. 3. Grants read access to the certificate's private key to the AD FS service account, to the account AD FS actually runs as (a domain account or gMSA), and to NT SERVICE\drs when Device Registration Service is present. Handles RSA and ECDSA, CNG and CAPI keys. 4. Assigns the certificate to AD FS:

  • Service-Communications certificate (Set-AdfsCertificate). This is a configuration-database write, made only on the PRIMARY node; on a secondary it is skipped and arrives by replication.
  • SSL certificate (Set-AdfsSslCertificate). On Server 2016+ this is applied across the whole farm from the PRIMARY node and needs the certificate present on every node first, so on a secondary 2016+ node this script does not set it (the primary does); on Server 2012 R2 each node applies its own binding. It never touches the token-signing or token-decrypting certificates, which AD FS manages itself. 5. Restarts the AD FS service (adfssrv) so the change takes effect, unless -SkipRestart is given. The restart disconnects users who are signing in through AD FS for a moment. simple-acme installs a renewal between 01:00 and 05:00 server time by default; "When the renewal runs" in fairssl-readme.txt says how to choose another time.

The exit code is 1, so simple-acme and FairSSL report a failure, when the Service-Communications or SSL certificate cannot be set where this node sets it, or when adfssrv (or, with -RestartService, appproxysvc) does not restart; 0 only when all of it worked. A step that fails does not stop the steps after it, because simple-acme removes the previous certificate after the run also when the run fails.

AD FS farm rollout (designed to work unattended, without anyone watching): Every node uses the SAME private key and CSR (generated once, locally or in the FairSSL certificate tool) and runs simple-acme with --source csr --csrfile/--pkfile, so FairSSL returns the identical certificate to every node. Each node registers its own ACME account: give each node its own EAB key, or use one EAB key that can connect several ACME clients. All the keys belong to the same FairSSL profile, so the certificate and its thumbprint are the same either way. The first node that renews issues the certificate and the others receive that same certificate instead of issuing their own; the FairSSL ACME server keeps orders for the same names about 30 seconds apart, so the first order finishes first. Schedule the renewals (settings.json StartBoundary, RandomDelay 00:00:00) so they do not all run at once:

  • one node first - it issues the certificate;
  • the remaining secondaries a minute or two later - they receive the already-issued certificate;
  • the PRIMARY last - it receives the certificate and applies the farm-wide SSL binding once every node holds it (it also retries for up to about 15 minutes by default, configurable with -SslBindingTimeoutMinutes, so a node that is slightly behind is tolerated without depending on exact scheduling). Because the primary retries, it is enough to set RandomDelay 00:00:00 on every node and StartBoundary one minute earlier on one node than on all the others, for example 02:17:00 on that node and 02:18:00 on the rest. With the default random delay of up to 4 hours the nodes renew hours apart. "When the renewal runs" in fairssl-readme.txt says how to change the two values; every node restarts AD FS when it installs the certificate.

On a Web Application Proxy server, in order: 1. Loads the WebApplicationProxy module (Import-Module WebApplicationProxy) and confirms this is a Web Application Proxy server. 2. Finds the certificate as above (My / WebHosting / CacheFile fallback). 3. Reads the names actually in the certificate (subject CN + SANs) and binds it to whatever those names cover:

  • the AD FS federation proxy SSL certificate (Set-WebApplicationProxySslCertificate) when the certificate covers the federation service host this proxy points at;
  • any published web application (Set-WebApplicationProxyApplication -ExternalCertificateThumbprint) whose external address the certificate covers. A single certificate can therefore update the federation proxy, one or more published applications, or both (for a SAN / wildcard certificate). If the certificate matches nothing on this server, the script stops with an error rather than silently doing nothing, so an unattended renewal is reported as failed instead of appearing to succeed. 4. Reports exactly what was bound. It does NOT restart the Web Application Proxy service by default - the binding is normally served without a restart. Pass -RestartService if a server does not pick up the new certificate.

WAP serves the certificate through http.sys (the SYSTEM account), so on a WAP server the service account does not need read access to the private key and the script does not change any ACLs. If a particular setup ever needs it, add --acl-read "NT SERVICE\appproxysvc" to the simple-acme command.

It never touches the WAP proxy-trust certificate (the client-authentication certificate WAP uses to authenticate to AD FS); that is managed by the AD FS trust and renewed by WAP itself.

Several WAP servers (same certificate on each): every WAP server behind a load balancer, and the AD FS servers behind them, must present the SAME certificate for a given site. A normal renewal makes a new key on each server, so each server would get the same names with a different thumbprint, and device registration, modern authentication and connection reuse break. The WAP cmdlet is per-server (there is no primary and no replication), so each server runs this script and binds its own copy. To make every server end up with the identical certificate (same thumbprint), give them the SAME private key and CSR (generated once, locally or in the FairSSL certificate tool) and run simple-acme with --source csr --csrfile/--pkfile, so FairSSL returns the identical certificate to each. Each server registers its own ACME account: give each server its own EAB key, or use one EAB key that can connect several ACME clients; all the keys belong to the same FairSSL profile. Schedule the renewals so one server runs a minute before the rest: RandomDelay 00:00:00 on every server and StartBoundary one minute earlier on one server than on all the others, for example 02:17:00 on that server and 02:18:00 on the rest. The first one issues the certificate and the others reuse it; the FairSSL ACME server keeps orders for the same names about 30 seconds apart, so the first order finishes first. "When the renewal runs" in fairssl-readme.txt says how to change the two values.

Several published sites with different names: each site can have its own certificate and its own renewal. The script reads the names in each certificate and binds it to the published applications those names cover.

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 freshly issued certificate ({CertThumbprint}). simple-acme has already placed it in the LocalMachine certificate store (My or WebHosting).
-CacheFile Optional PFX path ({CacheFile}). Only used as a fallback to import the certificate if it is not already present in the store.
-PfxPassword Password for CacheFile ({CachePassword}).
-SslBindingTimeoutMinutes AD FS only. How long the primary node waits (retrying every 30 s) for every farm node to hold the certificate before applying the farm-wide SSL binding. Default 15. Can be raised (for example 60) for large farms; simple-acme's Script.Timeout must allow it - the FairSSL bundle sets it to 3600 s (1 hour).
-SkipRestart AD FS only. Do not restart the adfssrv service after assigning the certificate.
-ApplicationName WAP only. Bind the certificate to this published application explicitly, instead of (or in addition to) auto-matching by hostname.
-SetProxyCertificate WAP only. Force setting the AD FS federation proxy SSL certificate even if the federation host cannot be read or matched. Use for alternate-hostname setups.
-RestartService WAP only. Restart the Web Application Proxy service (appproxysvc) after binding. Off by default; the binding is normally served without a restart. Use this if a server does not pick up the new certificate.
-Role Auto (default), AdFs or Wap. Auto detects the role: a server with the appproxysvc service, or whose adfssrv service runs Microsoft.IdentityServer.ProxyService.exe (the AD FS proxy that WAP installs), is WAP; a server whose adfssrv runs Microsoft.IdentityServer.ServiceHost.exe is AD FS. Use AdFs or Wap to override detection on an unusual setup.
-DebugOn Verbose tracing.

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\ImportADFS-WAP.v1.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\ImportADFS-WAP.v1.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\ImportADFS-WAP.v1.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\ImportADFS-WAP.v1.ps1" `
  --scriptparameters "<parameters>"

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.

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