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; }