Feature: Ship a built-in TLS certificate so encryption works out of the box

A fresh install used to require openssl before the server could speak TLS:
two empty paths in [tls] meant the identity failed to load and every
ClientHello was refused. TLS is now on by default and, when no custom
certificate is configured, the server generates its own self-signed one the
first time it needs it -- RSA-2048, SHA-256, CN=yama-server, CA:TRUE, valid
for ten years from one day before now. Plaintext clients are untouched, since
the device port still splits plaintext from TLS by sniffing the first six
bytes of a connection.

The certificate and its PKCS#8 private key are stored side by side in the
config under [tls] BuiltinCert / BuiltinKey as single-line base64 DER. Single
line, because the server has two config backends -- the registry for Release,
settings.ini for a Debug master -- and only a line without newlines means the
same read/write code serves both. That in turn required fixing the registry
reader: iniFile::GetStr copied into a 512 byte stack buffer, so a 1.5 KB value
came back as a truncated or empty string. The two-phase dynamic read now lives
in common/iniFile.h as ReadRegistryValueSz / ReadRegistrySz, and the copy that
client/TlsChannel.cpp had been carrying for its own trust anchor is gone. This
also makes the built-in certificate self-checking: Ensure() re-reads what it
just wrote and fails loudly if the round trip did not survive.

Import has to go through NCRYPT_PKCS8_PRIVATE_KEY_BLOB, matching the import
path in BuildIdentityFromDer. NCRYPT_PKCS8_PRIV_KEY_INFO_BLOB, which most
samples reach for, is not implemented by the Microsoft Software KSP and
returns NTE_INVALID_PARAMETER; a probe confirmed it, and the export type that
works is the one the importer already expected. Building an identity is now
split into a file front-end (PEM, fullchain, PKCS#1 hints) and a shared DER
core, so the built-in and file paths cannot drift apart.

That key also exposed a second problem worth fixing here. The identity binds
its private key to the certificate by name -- CRYPT_KEY_PROV_INFO with
pwszContainerName -- and Schannel resolves that name on every handshake
through CryptAcquireCertificatePrivateKey. With the fixed name
"YamaServerTlsKey", two server processes under the same Windows user shared
one key slot: whichever loaded last overwrote it, and the other process's next
handshake picked up a private key that does not match its certificate. That is
exactly the debug-plus-release pairing used here, and defaulting to a built-in
certificate is what makes it reachable, since the debug instance no longer
points at the same PEM as the release instance. The name is now the base name
plus the first six bytes of the certificate's SHA-1 thumbprint, e.g.
YamaServerTlsKey-c0d469eae8a4: different certificates get different keys,
instances sharing one certificate still share one key. MCP picks this up too.
The effective name goes to the trace log so a collision is visible without a
debugger. A stale key from an older build may still sit in the key store under
the old name; nothing references it and it is not deleted automatically, since
a running instance of an older build could be using it.

The trust anchor still has to be pushed. The client pins exactly one root
through an exclusive-root chain engine and checks no host name, so a
self-signed CA:TRUE leaf chains cleanly to itself -- but only if the client is
told about it. That is the existing 启用 TLS / 切换到 WSS command, which now
takes the PEM from ServerTlsIdentity::AnchorPem instead of re-reading the
configured cert file, so it reports the same certificate the server actually
serves for both sources. Losing the stored certificate (cleared registry, new
Windows user) regenerates it, and every client must be re-pushed the anchor.

Two places that used to fall back to [tls] CertPath now skip that fallback
while the built-in certificate is active: the MCP HTTPS front end at startup
and the prefill in its settings dialog. A self-signed certificate cannot pass
validation at a public MCP client, and the retained CertPath is only a
leftover from switching back to a custom certificate, so pretending to use it
would fail later with a worse message. MCP HTTPS with empty paths already
fails closed with a log line.

The TLS dialog gains a 内置证书 checkbox, ticked by default and unticked when
certificate paths are present; ticking it greys out the path rows without
clearing them, and shows either "generated on first use" or the expiry date
read back from the stored certificate. Saving writes four keys -- Enabled,
UseBuiltIn, CertPath, KeyPath -- and an explicit UseBuiltIn keeps the paths
from being used as an implicit switch. Every reader of [tls] Enabled now
defaults to 1, so the menu check, the dialog and the loader agree on a fresh
install. The status bar panes on 无 / 有 grew a third state, 内置, and the
pane width is measured across all three.

Translations were added to en_US, zh_TW and de_DE; skipping the German file
would have left the new dialog text in Chinese there, as de_DE is loaded from
disk rather than embedded.

Verified with the debug server against openssl s_client. Both the generated
built-in certificate (CN=yama-server) and an existing custom one
(CN=localhost) complete a TLS 1.2 handshake with the pinned certificate, and
their log lines show distinct key names, c0d469eae8a4 and 272f2adfb19c. An
unmodified client still runs in plaintext without an anchor, an existing custom
certificate deployment behaves exactly as before, and client and server both
build clean at Debug x64.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-01 15:39:54 +01:00
1 parent 4f9bfb0fdc
commit c4021e1fc9
16 files changed
+697 -131

No files matched your search

+5
View File
@@ -2123,6 +2123,10 @@ TLS֤
启用加密=Verschluesselung aktivieren
证书路径=Zertifikatspfad
私钥路径=Pfad zum privaten Schluessel
内置证书=Integriertes Zertifikat
自签证书,有效期至=Selbstsigniertes Zertifikat, gueltig bis
首次启用时自动生成=Wird bei der ersten Verwendung automatisch erzeugt
内置证书生成/加载失败,加密功能未生效。请查看日志,或取消勾选「内置证书」后改用本地证书与私钥。=Integriertes Zertifikat konnte nicht erzeugt oder geladen werden; Verschluesselung ist inaktiv. Bitte Log pruefen oder "Integriertes Zertifikat" abwaehlen und ein lokales Zertifikat mit privatem Schluessel verwenden.
请填写证书路径=Bitte Zertifikatspfad angeben
请填写私钥路径=Bitte Pfad zum privaten Schluessel angeben
TLS 配置已保存。=TLS-Einstellungen gespeichert.
@@ -2171,6 +2175,7 @@ Y
; ===== Status Bar TLS/MCP =====
TLS: 有=TLS: Ja
TLS: 无=TLS: Nein
TLS: 内置=TLS: Integriert
MCP: 开=MCP: An
MCP: 关=MCP: Aus
; ===== MCP Settings (file write / HTTPS) =====
+5
View File
@@ -2138,6 +2138,10 @@ TLS֤
启用加密=Enable Encryption
证书路径=Certificate Path
私钥路径=Private Key Path
内置证书=Built-in Certificate
自签证书,有效期至=Self-signed certificate, valid until
首次启用时自动生成=Generated automatically on first use
内置证书生成/加载失败,加密功能未生效。请查看日志,或取消勾选「内置证书」后改用本地证书与私钥。=Failed to generate or load the built-in certificate; encryption is not active. Check the log, or clear Built-in Certificate and use a local certificate and private key instead.
请填写证书路径=Please specify the certificate path
请填写私钥路径=Please specify the private key path
TLS 配置已保存。=TLS settings saved.
@@ -2187,6 +2191,7 @@ WSS
; ===== Status Bar TLS/MCP =====
TLS: 有=TLS: Yes
TLS: 无=TLS: No
TLS: 内置=TLS: Built-in
MCP: 开=MCP: On
MCP: 关=MCP: Off
; ===== MCP Settings (file write / HTTPS) =====
+5
View File
@@ -2131,6 +2131,10 @@ TLS֤
启用加密=啟用加密
证书路径=憑證路徑
私钥路径=私鑰路徑
内置证书=內建憑證
自签证书,有效期至=自簽憑證,有效期限至
首次启用时自动生成=首次啟用時自動產生
内置证书生成/加载失败,加密功能未生效。请查看日志,或取消勾选「内置证书」后改用本地证书与私钥。=內建憑證產生或載入失敗,加密功能未生效。請查看記錄,或取消勾選內建憑證後改用本機憑證與私鑰。
请填写证书路径=請填寫憑證路徑
请填写私钥路径=請填寫私鑰路徑
TLS 配置已保存。=TLS 設定已儲存。
@@ -2180,6 +2184,7 @@ WSS
; ===== Status Bar TLS/MCP =====
TLS: 有=TLS: 有
TLS: 无=TLS: 無
TLS: 内置=TLS: 內建
MCP: 开=MCP: 開
MCP: 关=MCP: 關
; ===== MCP Settings (file write / HTTPS) =====