Fix: build the TLS chain engine with the Windows 7 config size

A client generated for Windows 7 loaded its embedded trust anchor fine but then
failed with "CertCreateCertificateChainEngine failed" and refused to connect
(fail-closed), so it never came online.

The client project does not define _WIN32_WINNT, so the Windows SDK compiles it
for Windows 10, which adds dwExclusiveFlags to CERT_CHAIN_ENGINE_CONFIG.
sizeof(ec) is therefore larger than the struct Windows 7 knows, and Win7's
CertCreateCertificateChainEngine rejects the oversized cbSize
(ERROR_INVALID_PARAMETER). Pass the Windows 7 size instead, up to
hExclusiveTrustedPeople; newer systems accept the older size and treat the
unused dwExclusiveFlags as zero.

Also log GetLastError() on failure so a future failure is diagnosable from the
client log.

Verified by generating a client against the rebuilt server and connecting from
a Windows 7 host, plus a Debug x64 client build.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-26 18:41:06 +01:00
1 parent 5387603c73
commit 85f7f1aa67
1 file changed
+10 -2
+10 -2
View File
@@ -15,6 +15,7 @@
#include <fstream> // std::ifstream
#include <iterator> // std::istreambuf_iterator
#include <string>
#include <cstddef> // offsetof(CERT_CHAIN_ENGINE_CONFIG 的 Win7 兼容尺寸)
#include <map> // std::map(域名解析缓存)
#include <mutex> // std::mutex
@@ -421,12 +422,19 @@ TlsAnchor BuildTlsAnchor(bool allowEmbedded, bool quiet, HCERTSTORE* outStore,
// 严格钉扎:链引擎只信任该 store 里的根证书,排除系统根库。否则攻击者拿一张任意
// 公共 CA 签发的证书就能通过 CertGetCertificateChain(默认引擎信任系统根库)。
CERT_CHAIN_ENGINE_CONFIG ec = {};
ec.cbSize = sizeof(ec);
// 兼容 Windows 7:本工程未定义 _WIN32_WINNT,SDK 默认按 Win10 编译,结构体因此
// 含只有 Win8+ 才有的 dwExclusiveFlags,sizeof 比 Win7 已知尺寸大 8 字节。Win7
// 的 CertCreateCertificateChainEngine 只认识结构体到 hExclusiveTrustedPeople 为止,
// 传入更大的 cbSize 会返回 FALSE(ERROR_INVALID_PARAMETER),客户端随即 fail-closed。
// 这里显式按 Win7 尺寸传递;Win8+ 接受较旧尺寸,未使用的 dwExclusiveFlags 视为 0。
ec.cbSize = (DWORD)offsetof(CERT_CHAIN_ENGINE_CONFIG, hExclusiveTrustedPeople)
+ (DWORD)sizeof(HCERTSTORE);
ec.hExclusiveRoot = store;
HCERTCHAINENGINE engine = NULL;
if (!CertCreateCertificateChainEngine(&ec, &engine)) {
DWORD le = GetLastError();
CertCloseStore(store, 0);
err = "CertCreateCertificateChainEngine failed";
err = "CertCreateCertificateChainEngine failed (error " + std::to_string(le) + ")";
return TLS_ANCHOR_INVALID;
}