Free ACME scripts / Windows

Dynamics CRM

Install an ACME certificate on Microsoft Dynamics CRM 2016 / Dynamics 365 Customer Engagement (on-premises) 9.x with claims-based authentication and Internet-Facing Deployment (IFD), from simple-acme.

Version
1.1
Language
PowerShell
SHA-256
53bde9890234e20d6e5a36cf72205507b3bf2fec5949473ca5a50a4df63305bd
Licence
Apache-2.0, copyright FairSSL A/S
Download dynamics-crm-1.1.ps1

Download it on the server, check the SHA-256 and keep it as C:\simple-acme\Scripts\ImportDynamicsCRM.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/dynamics-crm-1.1.ps1' -OutFile 'C:\simple-acme\Scripts\ImportDynamicsCRM.v1.ps1'
(Get-FileHash 'C:\simple-acme\Scripts\ImportDynamicsCRM.v1.ps1' -Algorithm SHA256).Hash -eq '53BDE9890234E20D6E5A36CF72205507B3BF2FEC5949473CA5A50A4DF63305BD'
Unblock-File 'C:\simple-acme\Scripts\ImportDynamicsCRM.v1.ps1'

What it does

FairSSL installation script for Dynamics CRM. simple-acme runs it on the CRM server after it has stored the certificate in LocalMachine\My. The same advice is in the ACME Easy guide "Microsoft Dynamics CRM (IFD)" and on fairssl.dk (Guides > Dynamics CRM).

CRM uses the certificate twice: IIS serves it on the CRM website's https bindings, and CRM encrypts its claims sign-ins to it. CRM finds the claims encryption certificate by its subject (ClaimsSettings), in LocalMachine\My, and finds NONE when two certificates there have that subject. So the moment simple-acme stores a renewed certificate beside the previous one, CRM sign-ins stop until this script has run. In order, it:

1. Checks the certificate: private key, RSA, Server Authentication, and a private key in a legacy CSP. CRM does not use a certificate whose key is in a CNG key storage provider (measured on CRM 9.1); simple-acme stores keys in a CSP unless its settings say otherwise. With "UseNextGenerationCryptoApi": true in simple-acme's settings.json the script stops here, before it has changed anything: remove that setting and renew again. 2. Gives the account CRMAppPool runs as (normally NETWORK SERVICE) Read on the private key. 3. When the subject differs from the one CRM uses, writes the new subject to ClaimsSettings with the CRM PowerShell snap-in. Only a CRM Deployment Administrator can do that, and Local System (the account the scheduled renewal runs as) cannot. Run the first simple-acme command in PowerShell as a Deployment Administrator; renewals keep the subject and need no CRM account. 4. Moves every https binding of the CRM website to the certificate. 5. Archives the other certificates in LocalMachine\My with the same subject. Archived certificates are hidden from lookups, not deleted, and stay in the store: simple-acme does not see them to remove them. 6. Restarts IIS (iisreset), and waits until IIS serves the certificate and CRM publishes it in its federation metadata, for up to -CrmStartMinutes (CRM starts slowly on a busy server). CRM is down for a few minutes at every run, the first one and each renewal. 7. When AD FS runs on this server, refreshes every relying party trust whose federation metadata now publishes the new certificate and that still encrypts to another one (Update-AdfsRelyingPartyTrust), so sign-ins keep working at once. With AD FS on another server, AD FS picks up the new certificate by its own metadata monitoring (every 24 hours unless lowered); the script prints the command that does it at once. AD FS encrypts every sign-in to the certificate in the relying party trust, so until then internal and external sign-ins fail. Set-AdfsProperties -MonitoringInterval 15 on the AD FS server makes it check every 15 minutes; the CRM relying party trusts must have monitoring and automatic update on, the default when they were created from CRM's metadata address.

IfdSettings are not changed: CRM reads them unchanged after a certificate change (measured on CRM 9.1).

If anything fails after the first change, the script puts back what it changed (bindings, archive flags, ClaimsSettings), archives the new certificate if it shares the subject of the previous one, resets IIS again, and exits 1 with the reason in the simple-acme log. CRM then runs on the previous certificate. simple-acme 2.4 records a renewal as done even when its installation script fails, so it does not retry by itself: fix the cause and run .\wacs.exe --renew --force.

The script does not touch AD FS's own certificates. Renew AD FS (and a Web Application Proxy in front of IFD) with the FairSSL guides for AD FS and WAP and ImportADFS-WAP.v1.ps1. With AD FS on the CRM server, give that renewal its own name (for example sts.contoso.com), not the CRM wildcard: another certificate with CRM's subject in LocalMachine\My stops CRM's sign-ins again until this script runs.

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 ({CertThumbprint}). simple-acme has already stored it in LocalMachine\My (--certificatestore My).
-CrmStartMinutes How long to wait after iisreset for IIS to serve and CRM to publish the certificate. Default 15. A failed run waits up to twice this (the restore waits for CRM again), and simple-acme stops the script at Script.Timeout, 3600 seconds in the FairSSL download: keep twice this value below it.
-SkipAdfsRefresh Do not refresh the AD FS relying party trusts on this server.
-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\ImportDynamicsCRM.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\ImportDynamicsCRM.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\ImportDynamicsCRM.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\ImportDynamicsCRM.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