Feature: Embed the master's TLS certificate in generated clients
When the server has TLS enabled, the builder can now bake its certificate into the client it produces, so a distributed client speaks TLS on its very first connection instead of waiting for the anchor to be pushed after a plaintext login. The dialog gets a checkbox (IDC_CHECK_TLS_ANCHOR) that is only enabled for payloads and protocols where an embedded anchor makes sense; the choice is remembered even when the control flips to disabled and back, so switching protocols cannot silently undo a deliberate opt-out. Every label variant stays short enough to fit the 196 DLU field, including the German ones. A template that predates the anchor block no longer aborts generation: it warns, clears the checkbox and produces a plain client, so a master upgraded without rebuilding the ghost/ServerDll resources does not block every build. The certificate lands in a fixed 4 KiB block (magic, version, length, FNV-1a hash, master address, PEM) carved out of the payload at build time. The client reads its trust anchor from four sources in order: the tls_disabled marker, the certificate the server pushed into the registry, a PEM file next to the executable, and finally the embedded block. The embedded block only applies to a connection whose target matches the master recorded at generation time, compared as an address set: an exact string match when only one address is configured, or an intersection of resolved IPv4 addresses otherwise. A transferred, shared or parallel instance falls back to the old plaintext first-connect path instead of pinning itself to a certificate it can never verify. That decision is made per connection, not per process: shared and parallel instances are threads inside a single client process, so a process-wide flag let whichever instance connected first decide for all of them, and the first load of the TLS state cached that answer for good. The TLS layer now keeps two cached states (embedded anchor allowed, and not allowed), picks one per handshake, and reports the anchor source of that handshake for the login capability bit. Matching by resolved address also keeps parallel mode working, where the connection goes to the single IP the domain pool selected while the configured master is a domain or a list. Server side, generated and updated payloads get the block written in place; the client reports the capability bit on login so the console can tell which hosts are already TLS-only on first connect. Loading the anchor decodes every PEM block in the chain rather than just the leaf, and an anchor that exists but fails to load keeps the old fail-closed behaviour. A pushed anchor only reconnects when the transport of the live connection actually changes, judged against that connection's own target rather than the process-wide switch. This was verified end to end against a freshly generated client with an empty registry: the first connection was a TLS 1.2 ClientHello 0.2 s after start, and a four-certificate fullchain loaded all four blocks. Re-verified after the per-connection rework with the anchor block patched into a debug client: with a matching master the first bytes are a ClientHello, with a foreign master the client logs the skip and sends plaintext, and a domain master resolves to the IP it connects to and still matches. A master domain that no longer resolves falls back to plaintext within a second and is not looked up again for a minute, instead of stalling every reconnect on a DNS timeout while holding the lock the certificate verification needs. Co-Authored-By: deepseek-flash
This commit is contained in:
1 parent
c4021e1fc9
commit
951b4072e5
31 files changed
+1911
-122
No files matched your search
@@ -24,6 +24,7 @@
|
||||
#include "common/DateVerify.h"
|
||||
#include "common/LANChecker.h"
|
||||
#include "common/scheduler.h"
|
||||
#include "common/tls_anchor.h" // 内嵌锚块:COMMAND_UPDATE 判断新镜像有没有带锚
|
||||
#include "session.h"
|
||||
extern "C" {
|
||||
#include "ServiceWrapper.h"
|
||||
@@ -804,18 +805,60 @@ void ResponseDisable(IOCPClient *client, const char* type, LPBYTE data, int size
|
||||
|
||||
extern "C" bool IsSystemInSession0();
|
||||
|
||||
// 「取消 TLS」抑制标记:让客户端压过出厂内嵌锚回到明文(设计文档 §3.5)。
|
||||
// 直写注册表而不是 m_cfg->SetStr(""):空 REG_SZ 在 ReadTrustAnchor 里仍算「已配置」,
|
||||
// 会被当作 fail-closed 的空锚,与「没有标记」完全不是一回事。
|
||||
static void SetTlsDisabledMarker(bool disabled)
|
||||
{
|
||||
std::string subKey = GetRegistryName() + "\\settings";
|
||||
HKEY hKey = NULL;
|
||||
if (RegOpenKeyExA(GetCurrentUserRegistryKey(), subKey.c_str(), 0,
|
||||
KEY_SET_VALUE, &hKey) != ERROR_SUCCESS)
|
||||
return;
|
||||
if (disabled) {
|
||||
const char* v = "1";
|
||||
RegSetValueExA(hKey, "tls_disabled", 0, REG_SZ, (const BYTE*)v, 2);
|
||||
} else {
|
||||
RegDeleteValueA(hKey, "tls_disabled");
|
||||
}
|
||||
RegCloseKey(hKey);
|
||||
}
|
||||
|
||||
// 扫一段镜像里有没有「带正文的锚块」。生成端写锚时会连同 .rdata 里的 magic 字面量
|
||||
// 一起留在二进制里,所以不能只认第一处命中,必须逐个做结构校验(同 common/tls_anchor.h
|
||||
// 的说明)。被 UPX/AES 包过的镜像扫不到——那就按「没带锚」处理,退回原有行为。
|
||||
static bool PayloadCarriesAnchor(const BYTE* data, size_t size)
|
||||
{
|
||||
if (!data || size < sizeof(EmbeddedTlsAnchor))
|
||||
return false;
|
||||
const size_t last = size - sizeof(EmbeddedTlsAnchor);
|
||||
for (size_t i = 0; i <= last; ++i) {
|
||||
const char* p = (const char*)data + i;
|
||||
if (p[0] != 'Y' || !IsAnchorMagic(p))
|
||||
continue;
|
||||
EmbeddedTlsAnchor block;
|
||||
memcpy(&block, p, sizeof(block)); // 对齐安全:不直接解引用未对齐指针
|
||||
if (IsAnchorBlockValid(block) && block.length > 0)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// 信任锚已写入/删除:热重载 TLS 状态,只在传输方式真正变化(明文 ↔ TLS)时断开重连;
|
||||
// 仅换证书(仍是 TLS)不打断现有会话,新锚从下一条新连接开始生效。与 Linux 版的
|
||||
// linux/main.cpp::ApplyTlsAnchorChange 同一语义。
|
||||
static void ApplyTlsAnchorChange(IOCPClient* client)
|
||||
{
|
||||
const bool before = TlsEnabled();
|
||||
// 前后都用**本连接**的判定:分享/并行实例连的不是生成时那台主控,它实际在走明文,
|
||||
// 而进程级 TlsEnabled() 看的是「允许内嵌锚」那套状态、恒为 true —— 拿它比较会漏掉
|
||||
// 明文 → TLS 的切换,客户端明明收到了锚却继续明文连(设计文档 §3.6)。
|
||||
const bool before = client ? client->IsTlsActive() : TlsEnabled();
|
||||
std::string err;
|
||||
if (!TlsReload(err)) {
|
||||
Mprintf("[TLS] reload failed: %s (keeping current transport)\n", err.c_str());
|
||||
return;
|
||||
}
|
||||
const bool after = TlsEnabled();
|
||||
const bool after = client ? client->TlsEnabledForCurrentTarget() : TlsEnabled();
|
||||
if (before == after) {
|
||||
Mprintf("[TLS] trust anchor updated, transport unchanged (%s)\n",
|
||||
after ? "TLS" : "plaintext");
|
||||
@@ -881,9 +924,20 @@ VOID CKernelManager::OnReceive(PBYTE szBuffer, ULONG ulLength)
|
||||
// 服务端下发信任锚证书:payload = PEM 正文(szBuffer+1 .. ulLength-1)。
|
||||
// 写入注册表 settings\tls_cert 后热重载:明文 → TLS 的切换会断开本连接,重连
|
||||
// 循环随即用 TLS 重新登录(此前按明文回退的 WSS 客户端就此升级为 WSS)。
|
||||
//
|
||||
// 分享实例一律忽略:它和主实例是同一进程里的两个 ClientApp 线程(同一 exe
|
||||
// 路径、同一份配置),注册表键同为 Software\<exe 哈希>\settings。分享方下发锚
|
||||
// 会改掉主实例连原主控用的 tls_cert,主实例下次重连必然握手失败且无法自愈
|
||||
//(设计文档 §3.8)。与 COMMAND_SET_PROTO 同一策略——被分享的主机无权切换
|
||||
// 自己的传输方式。
|
||||
if (m_ClientApp && m_ClientApp->IsShared()) {
|
||||
Mprintf("[TLS] ignored: shared instance follows the sharing host's anchor\n");
|
||||
break;
|
||||
}
|
||||
if (ulLength > 1) {
|
||||
std::string certPem((char*)szBuffer + 1, ulLength - 1);
|
||||
m_cfg->SetStr("settings", "tls_cert", certPem);
|
||||
SetTlsDisabledMarker(false); // 覆盖掉此前的「取消 TLS」,否则新锚被标记作废
|
||||
ApplyTlsAnchorChange(m_ClientObject);
|
||||
} else {
|
||||
// 没有证书正文就等于「启用但没有锚」——不该悄悄忽略,留一行便于排查。
|
||||
@@ -893,6 +947,13 @@ VOID CKernelManager::OnReceive(PBYTE szBuffer, ULONG ulLength)
|
||||
}
|
||||
case COMMAND_TLS_DISABLE: {
|
||||
// 删除注册表信任锚后热重载:TLS → 明文时断开重连(WSS 客户端会回到明文回退)。
|
||||
// 同时写抑制标记:只删 tls_cert 的话,内嵌锚客户端会回落到出厂锚继续发
|
||||
// ClientHello,而服务端已按明文处理 → fail-closed 永久失联(设计文档 §3.5)。
|
||||
if (m_ClientApp && m_ClientApp->IsShared()) {
|
||||
Mprintf("[TLS] ignored: shared instance follows the sharing host's anchor\n");
|
||||
break;
|
||||
}
|
||||
SetTlsDisabledMarker(true);
|
||||
std::string subKey = GetRegistryName() + "\\settings";
|
||||
HKEY hKey = NULL;
|
||||
if (RegOpenKeyExA(GetCurrentUserRegistryKey(), subKey.c_str(), 0,
|
||||
@@ -946,6 +1007,10 @@ VOID CKernelManager::OnReceive(PBYTE szBuffer, ULONG ulLength)
|
||||
std::string certPem((const char*)szBuffer + 1 + sizeof(ProtoSwitchPacket), certLen);
|
||||
const std::string prevCert = m_cfg->GetStr("settings", "tls_cert");
|
||||
m_cfg->SetStr("settings", "tls_cert", certPem);
|
||||
// 必须同时清掉抑制标记:留着它 ReadTrustAnchor 会直接判「未配置」,
|
||||
// BuildTlsAnchor 返回 TLS_ANCHOR_NONE,ApplyTlsStateLocked 走成功分支把
|
||||
// g_tlsEnabled 置 false 并返回 true——切换照常进行、WSS 静默降级成明文 WS。
|
||||
SetTlsDisabledMarker(false);
|
||||
std::string err;
|
||||
if (!TlsReload(err)) {
|
||||
// 放弃本次切换(不写 proto_override/entry_*,不断连):PEM 加载不了时客户端
|
||||
@@ -1589,6 +1654,22 @@ VOID CKernelManager::OnReceive(PBYTE szBuffer, ULONG ulLength)
|
||||
|
||||
case COMMAND_UPDATE: {
|
||||
auto typ = m_conn->ClientType();
|
||||
// 本次下发的镜像若带着内嵌信任锚,就先清掉本机的「取消 TLS」抑制标记:否则重装/
|
||||
// 更新之后,运维刚内嵌的锚会被上一轮「取消 TLS」留下的标记悄悄作废(§6.5)。
|
||||
// 只在镜像里真有可用锚时才清——反过来一律清除,会让一台已被要求走明文的主机
|
||||
// 重新发 ClientHello,撞上明文服务端就是不可恢复的失联。
|
||||
{
|
||||
ULONGLONG size = 0;
|
||||
if (ulLength > 1 + sizeof(ULONGLONG)) {
|
||||
memcpy(&size, (const char*)szBuffer + 1, sizeof(ULONGLONG));
|
||||
const size_t avail = (size_t)ulLength - 1 - sizeof(ULONGLONG);
|
||||
if (size > 0 && (ULONGLONG)avail >= size &&
|
||||
PayloadCarriesAnchor(szBuffer + 1 + sizeof(ULONGLONG), (size_t)size)) {
|
||||
SetTlsDisabledMarker(false);
|
||||
Mprintf("[TLS] update payload carries an embedded anchor; cleared tls_disabled\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
if (typ == CLIENT_TYPE_DLL || typ == CLIENT_TYPE_MODULE) {
|
||||
ULONGLONG size = 0;
|
||||
memcpy(&size, (const char*)szBuffer + 1, sizeof(ULONGLONG));
|
||||
|
||||
Reference in new issue
Block a user