Contents · Forum Home · StreamSec Tools 4.1.5.363 documentation

StreamSec.Mobile.AcmeRenewal

Feature notes: TLS implementation notes (RFC map) · ACME (RFC 8555) implementation notes

Classes, interfaces and records

classeAcmeRenewal

inherits from eAcmeError

Raised by the renewal manager for a wiring mistake on the host side - no client, no domains, no leaf where one is required. Descends from eAcmeError so that an application already catching ACME failures also catches these; the RFC 7807 problem fields (ErrorType, Detail, Status) stay empty because no server response is involved.

(no public members)

classtAcmeManagedCert

tAcmeManagedCert is one certificate under management: the identifiers it covers, its current DER (for the ARI identifier and the newOrder "replaces" field), its validity window, and the request key reused for the CSR and the served identity. The scheduling fields are owned by the worker thread.

constructor Create;
the manager's Manage builds these records; apps do not construct them directly
destructor Destroy; override;
frees Domains; the manager owns and frees every managed-cert record
property Domains: TStringList read fDomains;
the DNS identifiers this certificate covers; populated from Manage's aDomains

interfaceiAcmeRenewalGate

iAcmeRenewalGate is ONE serialisation point - the object two obtains that must not overlap meet on. Everything this unit serialises on is one of these, so acquiring and releasing are the same two calls whatever is being guarded.

NOT RECURSIVE, deliberately, and unlike the per-server gate behind TryBeginAcmeChallenge: that one is a TCriticalSection, whose TryEnter re-admits its OWNING thread - TryEnterCriticalSection on Windows, TMonitor.TryEnter on POSIX. Two obtains nested on one thread are still two obtains on one challenge slot, so a recursive gate cannot detect the case at all. TryEnter here refuses the owner, and HeldByCurrentThread lets a caller tell "someone else holds it, so poll" from "I hold it, so abandon" - polling for yourself never ends.

Every instance is owned by this unit for the life of the process, so a holder can never be left pointing at a freed gate.

function TryEnter: Boolean;
Non-blocking, so a caller polls and stays responsive to its own shutdown.
procedure Leave;
Releases a gate THIS thread holds. A release from any other thread does nothing: silently handing the gate away from its true owner is exactly the double obtain this unit exists to prevent, and it is what an unchecked Leave does.
function HeldByCurrentThread: Boolean;
True when this thread already holds it - see the re-entrancy note above.

interfaceiAcmeRenewalHold

iAcmeRenewalHold is the set of gates one obtain currently holds. RELEASING IS WHAT DROPPING THE REFERENCE DOES, so there is no way to take the gates and forget to give them back, no way to give them back twice, and no way to give back one that was never taken. A partly-acquired hold releases exactly the part it acquired.

function AlpnTarget: iAcmeAlpnTLS;
The server the gates were taken for, or nil in host-driven mode. Renew branches on THIS rather than re-reading the client, so the managed / host-driven decision and the gate decision are one decision, taken once, and cannot disagree with each other.

classTsmAcmeRenewalManager — component reference

inherits from TComponent

constructor Create(aOwner: TComponent); override;
Create constructs the component with the default cadence and scheduling parameters (PollSeconds, RenewalFraction 2/3, jitter, backoff bounds); Destroy stops any running worker and frees the managed-cert records it owns. Drop the component on a form or create it in code like any TComponent. » member page
destructor Destroy; override; procedure Start;
Start the background worker that keeps the managed certificates renewed with Let's Encrypt; Stop terminates it. A worker is optional - a host may instead call EvaluateOnce from its own service timer / cron. » member page
procedure Stop; function Manage(aDomains: TStrings; const aLeafDer: tBytes; aCertKey: TComponent; const aTag: string = ''): tAcmeManagedCert; overload;
Register a certificate already issued by Let's Encrypt for automatic renewal, and return the record the manager owns. Full description »
function Manage(aDomains: TStrings; const aLeafCert: iCertificate; aCertKey: TComponent; const aTag: string = ''): tAcmeManagedCert; overload; function Manage(const aTag: string = ''): tAcmeManagedCert; overload;
Manage THE CLIENT'S OWN configured certificate, issuing it if it does not exist yet. Takes nothing, because the client already carries everything the answer needs: Domains, CertificateKey, and where to persist the result - exactly the set the parameterless ObtainCertificate consumes. Full description »
procedure Poke;
Poke wakes the worker to re-evaluate immediately (e.g. after Manage or a config change). » member page
procedure EvaluateOnce;
EvaluateOnce runs one scheduling pass synchronously on the calling thread. Intended for tests and for a host that prefers to drive the cadence itself (its own service timer / cron) instead of Start/Stop. » member page
procedure RetryNow(aCert: tAcmeManagedCert);
RetryNow clears a cert's given-up / backoff state and re-evaluates it on the next pass. Call it after fixing whatever caused a permanent failure (e.g. a bad CSR key, a CAA record, an account/EAB problem); a still-running worker is poked to act at once. Does nothing harmful on a cert that was not given up. » member page
function TryBeginRenewal(out aHold: iAcmeRenewalHold): Boolean;
TryBeginRenewal brackets a MANUAL obtain - one the application issues on this manager's client itself - with exactly the gates this manager's own renewals take, in the same order, so a hand-driven ObtainCertificate cannot overlap a scheduled renewal: if FMgr.TryBeginRenewal(lHold) then try FAcme.... Full description »
property Running: Boolean read GetRunning;
True while the background worker started by Start is running and not yet stopping; a host that drives EvaluateOnce from its own timer, never calling Start, leaves this False. Poll it to decide whether Start/Stop is needed. » member page
property Acme: TsmAcmeClient read fClient write SetAcme;
Set this to the configured TsmAcmeClient that talks to Let's Encrypt (its AccountKey, DirectoryUrl, CertificateKey, ... Full description »
property ManageClientCertificate: Boolean read fManageClientCert write fManageClientCert default False;
Set this and the host writes NO CODE AT ALL: Start registers the client's own configured certificate - its Domains, its CertificateKey, persisted at its PfxFileName - and keeps it renewed, issuing the first one if the box has none. Full description »
property PollSeconds: Integer read fPollSeconds write fPollSeconds default 3600;
Upper bound (seconds) on how long the worker sleeps between evaluations; the actual sleep is the sooner of this and the next scheduled event. The default (1 hour) is ample for Let's Encrypt's 90-day certificates. » member page
property RenewalFraction: Double read fRenewalFraction write fRenewalFraction;
Fallback renewal trigger, used only when the CA advertises no ARI window: renew once the certificate is this fraction through its lifetime. The default 2/3 renews a Let's Encrypt 90-day certificate at about day 60 (~30 days of margin), matching Let's Encrypt's own advice to renew with a third of the lifetime left. » member page
property JitterSeconds: Integer read fJitterSeconds write fJitterSeconds;
JitterSeconds spreads the fallback trigger so a fleet does not renew at the same instant. » member page
property MinBackoffSeconds: Integer read fMinBackoffSeconds write fMinBackoffSeconds;
Initial retry delay after a TRANSIENT renewal failure (DNS/TLS/rate-limit/ socket error, a just-invalid authorization, ...); the backoff doubles from here on each further failure. Default 900 s (15 min). Permanent failures ignore this and park the cert until RetryNow. » member page
property MaxBackoffSeconds: Integer read fMaxBackoffSeconds write fMaxBackoffSeconds;
Ceiling for the doubling transient-failure backoff, so a cert that keeps failing still retries at least this often instead of drifting to days. Default 86400 s (24 h). » member page
property OnRenewed: tAcmeRenewedEvent read fOnRenewed write fOnRenewed;
Fires after Let's Encrypt issues a renewed certificate, with the managed record and the new PEM chain. In host-driven mode this is where you install it (e.g. ReplaceTLSServerCertificate); in managed mode the install has already happened and this is only a notification. » member page
property OnRenewalFailed: tAcmeRenewFailedEvent read fOnRenewFailed write fOnRenewFailed;
Fires when a renewal attempt fails. For a transient failure the manager has scheduled an exponential-backoff retry; for a permanent one (aCert.GivenUp is True) it has stopped retrying until RetryNow. Use it to alert or log - the existing, still-valid certificate stays in service either way. » member page
property OnLog: tAcmeRenewLogEvent read fOnLog write fOnLog;
Fires with a human-readable line at each scheduling/renewal step - wire it to your log to watch renewals happen. » member page
property OnClassifyError: tAcmeClassifyErrorEvent read fOnClassifyError write fOnClassifyError;
Fires on every failed renewal, after the built-in verdict is computed and before backoff/give-up is applied, so the host can override it (see tAcmeClassifyErrorEvent). » member page

Types (enumerations, sets, aliases)

proctAcmeRenewedEvent

Callback signatures for the manager's notifications: OnRenewed (a renewed PEM chain is ready), OnRenewalFailed (an attempt failed), OnLog (a progress line).

  tAcmeRenewedEvent = procedure(Sender: TObject; aCert: tAcmeManagedCert;
                                const aChainPem: string) of object;

proctAcmeRenewFailedEvent

  tAcmeRenewFailedEvent = procedure(Sender: TObject; aCert: tAcmeManagedCert;
                                    const aError: string) of object;

proctAcmeRenewLogEvent

  tAcmeRenewLogEvent = procedure(Sender: TObject; const aMessage: string) of object;

proctAcmeClassifyErrorEvent

Lets the host override the transient/permanent verdict for a failed renewal. aPermanent arrives preset from eAcmeError.IsPermanent (False for a non-ACME exception); set it True to stop retrying, or False to keep retrying.

  tAcmeClassifyErrorEvent = procedure(Sender: TObject; aCert: tAcmeManagedCert;
                                      E: Exception; var aPermanent: Boolean) of object;