From 85f7f1aa67a0be84ebd052d93a93fb5ac311ffc6 Mon Sep 17 00:00:00 2001 From: yuanyuanxiang <962914132@qq.com> Date: Wed, 25 Nov 2026 14:37:53 +0100 Subject: [PATCH] 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 --- client/TlsChannel.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/client/TlsChannel.cpp b/client/TlsChannel.cpp index 675a845..58b70df 100644 --- a/client/TlsChannel.cpp +++ b/client/TlsChannel.cpp @@ -15,6 +15,7 @@ #include // std::ifstream #include // std::istreambuf_iterator #include +#include // offsetof(CERT_CHAIN_ENGINE_CONFIG 的 Win7 兼容尺寸) #include // std::map(域名解析缓存) #include // 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; }