Certificates from Let's Encrypt are valid for weeks, not years. TsmAcmeRenewalManager renews them in good time, on a thread of its own, through the client set up in Get a certificate from Let's Encrypt.
procedure tTaskAcme.Start; begin { The manager obtains the first certificate if there is none, and renews it from then on - on its own thread, so this returns at once. } fRenewal := TsmAcmeRenewalManager.Create(nil); fRenewal.Acme := fAcme; fRenewal.ManageClientCertificate := True; fRenewal.OnRenewalFailed := RenewalFailed; fRenewal.OnLog := RenewalLog; fRenewal.Start; end; { Both events run on the manager's thread, so what they report is queued to the main thread. } procedure tTaskAcme.RenewalFailed(Sender: TObject; aCert: tAcmeManagedCert; const aError: string); begin { The certificate in service stays in service; the manager retries a passing failure itself, and stops on a lasting one until RetryNow. } Post('Certificate renewal failed: ' + aError); end; procedure tTaskAcme.RenewalLog(Sender: TObject; const aMessage: string); begin Post(aMessage); end; procedure tTaskAcme.Post(const aMessage: string); var lMessage: string; lHandler: TProc<string>; begin lMessage := aMessage; lHandler := fOnMessage; if Assigned(lHandler) then TThread.Queue(nil, procedure begin lHandler(lMessage); end); end; destructor tTaskAcme.Destroy; begin fRenewal.Free; // stops its thread fAcme.Free; fCertificateKey.Free; fAccountKey.Free; fDirectoryTrust.Free; inherited; end;
With ManageClientCertificate True, Start takes the client's own certificate - its domains, its key, its PFX file - and looks after it:
RenewalFraction) - day 60 of a 90-day certificate.The certificate in service stays in service. A passing failure - the network, a busy CA - is retried after MinBackoffSeconds (15 minutes), doubling each time up to MaxBackoffSeconds (a day). A lasting one - the CA refusing the names, say - stops the retries until you call RetryNow. Either way OnRenewalFailed fires; assign it, so someone hears about it well before the certificate expires.
OnRenewalFailed, OnLog and OnRenewed are called on the manager's thread. The sample hands what they report to the main thread; do the same before touching anything visible.
procedure StartServerFromPfx(aServer: TsmAsyncTLSServer; const aDataDir, aSecret: string); var lPfx: string; begin { After a restart, serve the certificate obtained last time. The server records the import while it is stopped and applies it as it starts. } lPfx := TPath.Combine(aDataDir, 'server.pfx'); if FileExists(lPfx) then aServer.ImportFromPFX(lPfx, tDualSecretKey.CreateForPFX(aSecret)); aServer.Active := True; end;
Start the server from the PFX file, set the client up again with the same account key file, and start the manager: it finds the certificate in the PFX file and carries on from there. The schedule itself is kept only in memory, so this is all a restart needs.
Free the manager before the client and the server: freeing it stops its thread. The client must not be used for anything else while the manager runs.
TsmAcmeRenewalManager · StreamSec.Mobile.AcmeRenewal