Skip to content

servers: reload renewed web certificates without a restart - #2081

Merged
epoberezkin merged 94 commits into
simplex-chat:masterfrom
sh-agents:sh/web-cert-reload
Nov 19, 2026
Merged

epoberezkin merged 94 commits into
simplex-chat:masterfrom
sh-agents:sh/web-cert-reload

Conversation

@sh-agents

@sh-agents sh-agents commented Nov 17, 2026 •

Copy link
Copy Markdown

smp-server and xftp-server read the web certificate ([WEB] cert and key) at start, so a renewed Let's Encrypt certificate is served only after a restart; a server that runs longer than 60 days after the last restart serves an expiring, then an expired certificate to browsers and web clients.

Plan: plans/2026-11-17-reload-web-certificates.md

Changes

  • Transport: the web credential is stored in ReloadableCredential:
    • its files;
    • the check of the server type;
    • a TVar with the credential in use, read in onServerNameIndication at these handshakes:
      • protocol port: with SNI;
      • separate port: all.
  • Reload: every credentialCheckInterval (60 s), the files are parsed and compared with the credential in use. Changed files are accepted only with the private key of the certificate (checkKeyPair) and, on smp-server, with an RSA key of at least 4096 bits, as at start. The previous credential is kept in use after a rejection; a new web.crt copied before its web.key is handled this way.
  • Logging: the result of the last rejected load is kept as the state of the reload thread; while the loaded result is equal to the last rejected one, it is neither checked nor logged again. The decision is made in the pure function checkLoadedCredential. xftp-server with [WEB] https and [WEB] static_path builds two ReloadableCredential values from the same files, used by the SNI listener and the separate port; each event is logged twice, by their two reload threads.
  • Errors are values of CredentialError; IOException and PEMError from reading the files are caught.
  • The reload thread is run by withCredentialReload, together with the listener in which the TVar is read. withCredentialReload is called in:
    • runTransportServerSocketState, for SNI on smp-server and xftp-server;
    • runHTTPSServer, for the separate HTTPS port.
  • Start: the reason is added to invalid credential, and unreadable files are reported the same way; the exit code is 1, as before. On the separate HTTPS port, the CredentialError is logged with its reason. smp-server with an empty certificate file exits with Error: no certificate, where it gave unsupported HTTPS credentials, required 4096-bit RSA before.
  • The minimum RSA key size is minHTTPSRSAKeyBits (4096), also in the error message and the certbot hint.
  • The protocol certificate (server.crt, server.key) is read at start only, as before.

Cryptography

checkKeyPair is listed in protocol/crypto-registry.md as a restricted use. In tls 1.9, only the key types of a credential and the curve of an EC private key are checked (isDigitalSignaturePair), and every handshake with a mismatched pair would be aborted.

The public key of the leaf certificate is compared with a public key obtained from the private key file. Sources of that key:

  • RSA: the public fields stored with the private key;
  • Ed25519, Ed448: toPublic of the private key;
  • ECDSA P-256: pointBaseMul of the private scalar, after the certificate point is decoded with unserializePoint and validated, and the curve names are found equal. Other curves are rejected with CEUnsupportedCurve: only P-256 keys are used for signatures in tls 1.9 (kxSupportedPrivKeyEC), and every handshake with a P-384 or P-521 credential would be aborted. The check of the server type is applied first, so that smp-server rejects a reloaded EC certificate with the error given at start.

Other key types are rejected. pointBaseMul is computed in variable time, once per change of the files in each reload thread.

Tests

  • Files copied in order, a new web.crt, then web.key, with the certificate served to an SNI client checked at each step. Listeners:
    • the transport;
    • the shared port of smp-server;
    • the SNI port of xftp-server;
    • the separate HTTPS port.
  • A deleted key file and an empty certificate file are rejected, and the current certificate is still served.
  • Key types:
    • smp-server rejects RSA 2048 and ECDSA P-256 certificates;
    • xftp-server serves ECDSA P-256, then rejects ECDSA P-384 and keeps P-256;
    • on the separate HTTPS port with checkHTTPSCredentials, RSA 2048 is rejected.
  • Start, in the sequential "Server CLIs" tests, with the output captured, exit code 1 and prException equal to Nothing:
    • smp-server started with an RSA 2048 web certificate: Error: unsupported HTTPS credentials, required 4096-bit RSA;
    • an empty key file: invalid credential: no private key.
  • Separate HTTPS port at start: CENoPrivateKey is thrown for an empty key file.
  • Credential check, with the expected error:
    • RSA, ECDSA, Ed448 and Ed25519 pairs: accepted when matching, CEKeyMismatch with another key of the same type;
    • a key of another type: CEKeyMismatch;
    • DSA certificate: CEUnsupportedKeyType;
    • empty certificate file: CENoCertificate;
    • missing and malformed files: CEReadError and CEPEMError;
    • EC point off its curve: CEInvalidECPoint;
    • ECDSA P-384 pair: CEUnsupportedCurve;
    • P-256 certificate with its scalar under a P-384 curve name: CEKeyMismatch.
  • Order of checks (checkLoadedCredential): an ECDSA P-384 pair is rejected with CEUnsupportedHTTPSKey under checkHTTPSCredentials (smp-server) and with CEUnsupportedCurve under acceptCredential (xftp-server).
  • checkHTTPSCredentials: an empty certificate chain is rejected with CENoCertificate.
  • Reload decision (checkLoadedCredential): unchanged files, a rejection and its repetition, another mismatched pair and a load error after it, a repeated load error, an accepted renewal, and the check of the server type.
XFTP
Server CLIs
SMP server via TLS

@sh-agents
sh-agents marked this pull request as ready for review November 18, 2026 15:30

checkHTTPSCredentials :: CredentialCheck
checkHTTPSCredentials (X.CertificateChain cc, _k) =
-- LetsEncrypt provides ECDSA with insecure curve p256 (https://safecurves.cr.yp.to)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the comment is confusing. When it happens? What happens when it happens?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment comes from #1327 and moved here with checkHTTPSCredentials.

checkHTTPSCredentials is applied to smp-server's web certificate at start and on every reload. A certificate that isn't 4096-bit RSA is rejected: at start, smp-server exits with an error; on reload, the rejection is logged and the old certificate stays in use.

The comment means Let's Encrypt's ECDSA curves (P-256, P-384) are not safe curves, which is why RSA is required.

@epoberezkin
epoberezkin merged commit 880d089 into simplex-chat:master Nov 19, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants