XNetworking: read the certificate chain through the SDK's layouts #37

Closed
github-bot wants to merge 1 commit from gh-pr/37/head into gh-pr/37/base
Owner

@⁠grooving6 on GitHub · 2026-11-22 17:30 UTC · original
fix/xnetworking-cert-chain-offsets → main · +107 −21 · 2 files · 1 commits · closed without merging

Problem

extract_chain_certs (hle/xgameruntime/networking.rs) walks the chain with hand-written x64 offsets that don't match wincrypt.h:

field code read SDK layout
CERT_CHAIN_CONTEXT.cChain +16 +12
CERT_CHAIN_CONTEXT.rgpChain +24 +16
CERT_SIMPLE_CHAIN.cElement +20 +12
CERT_SIMPLE_CHAIN.rgpElement +24 +16

At +24 sits cLowerQualityChainContext plus padding, so the walk usually finds no certificates and XNetworkingVerifyServerCertificate fails every thumbprint pin with CERT_E_UNTRUSTEDROOT. If those bytes are ever non-zero, it follows a junk pointer instead. docs/xnetworking-rust-review.md already flagged that these offsets "do not look consistent with the documented field order".

Fix

The chain is now read through windows-sys's CERT_CHAIN_CONTEXT, CERT_SIMPLE_CHAIN and CERT_CHAIN_ELEMENT, which needs one new windows-sys feature, Win32_Security_Cryptography. The function also gets a # Safety section and // SAFETY: comments. Nothing else changes: TLS policy, pin matching and result handling stay as they were.

Tests

  • cert_context_matches_sdk_layout: the module's own CertContext checked against the SDK's CERT_CONTEXT (size and every field offset).
  • chain_certs_follow_sdk_layout: a two-certificate chain built from the SDK structs must come back leaf first. Null and zero-count chains come back empty.
  • chain_certs_from_crypt32: a real chain from CertGetCertificateChain for a certificate in the machine's root store must have that certificate as its leaf, compared by SHA-1. If the root store is empty, the test skips instead of failing.

I checked that the tests catch the bug: with the old offsets put back temporarily, chain_certs_follow_sdk_layout and chain_certs_from_crypt32 both fail, because both get an empty list. That includes the real crypt32 chain.

What ran

Windows 11, Rust 1.100 x86_64-pc-windows-msvc, MSVC 14.44, Windows SDK 10.0.26100 (DXC from it), AMD Radeon RX 7900 XTX (RDNA3):

  • cargo build --workspace + cargo test --workspace -- --test-threads=4: 344 passed, 0 failed, 34 ignored (the opt-in #[ignore] probes and surveys).
  • The full suite ran twice: once without Gaming Services, where XCL uses its own XAsync fallback, and once with Gaming Services 39.118 (System32\xgameruntime.dll). Both runs pass.
  • The GPU tests ran on the RX 7900 XTX rather than skipping (gpu:: tests: 157 passed). The README lists GPUs other than RDNA4 as untested, so this is also a first data point for RDNA3.
  • No XCL_SAMPLE_* game data was set, so those tests ran without doing anything.
  • cargo clippy -p xcl --all-targets: no warnings on the changed lines. The rest of the file and the crate already had warnings, and I left those alone.

🤖 Generated with Claude Code

**@⁠grooving6** on GitHub · 2026-11-22 17:30 UTC · [original](https://github.com/xcl-dev/xcl/pull/37) `fix/xnetworking-cert-chain-offsets` → `main` · +107 −21 · 2 files · 1 commits · closed without merging ## Problem `extract_chain_certs` (`hle/xgameruntime/networking.rs`) walks the chain with hand-written x64 offsets that don't match `wincrypt.h`: | field | code read | SDK layout | |---|---|---| | `CERT_CHAIN_CONTEXT.cChain` | +16 | **+12** | | `CERT_CHAIN_CONTEXT.rgpChain` | +24 | **+16** | | `CERT_SIMPLE_CHAIN.cElement` | +20 | **+12** | | `CERT_SIMPLE_CHAIN.rgpElement` | +24 | **+16** | At +24 sits `cLowerQualityChainContext` plus padding, so the walk usually finds no certificates and `XNetworkingVerifyServerCertificate` fails every thumbprint pin with `CERT_E_UNTRUSTEDROOT`. If those bytes are ever non-zero, it follows a junk pointer instead. `docs/xnetworking-rust-review.md` already flagged that these offsets "do not look consistent with the documented field order". ## Fix The chain is now read through `windows-sys`'s `CERT_CHAIN_CONTEXT`, `CERT_SIMPLE_CHAIN` and `CERT_CHAIN_ELEMENT`, which needs one new `windows-sys` feature, `Win32_Security_Cryptography`. The function also gets a `# Safety` section and `// SAFETY:` comments. Nothing else changes: TLS policy, pin matching and result handling stay as they were. ## Tests - `cert_context_matches_sdk_layout`: the module's own `CertContext` checked against the SDK's `CERT_CONTEXT` (size and every field offset). - `chain_certs_follow_sdk_layout`: a two-certificate chain built from the SDK structs must come back leaf first. Null and zero-count chains come back empty. - `chain_certs_from_crypt32`: a real chain from `CertGetCertificateChain` for a certificate in the machine's root store must have that certificate as its leaf, compared by SHA-1. If the root store is empty, the test skips instead of failing. I checked that the tests catch the bug: with the old offsets put back temporarily, `chain_certs_follow_sdk_layout` and `chain_certs_from_crypt32` both fail, because both get an empty list. That includes the real crypt32 chain. ## What ran Windows 11, Rust 1.100 `x86_64-pc-windows-msvc`, MSVC 14.44, Windows SDK 10.0.26100 (DXC from it), **AMD Radeon RX 7900 XTX (RDNA3)**: - `cargo build --workspace` + `cargo test --workspace -- --test-threads=4`: **344 passed, 0 failed, 34 ignored** (the opt-in `#[ignore]` probes and surveys). - The full suite ran twice: once without Gaming Services, where XCL uses its own XAsync fallback, and once with Gaming Services 39.118 (`System32\xgameruntime.dll`). Both runs pass. - The GPU tests ran on the RX 7900 XTX rather than skipping (`gpu::` tests: 157 passed). The README lists GPUs other than RDNA4 as untested, so this is also a first data point for RDNA3. - No `XCL_SAMPLE_*` game data was set, so those tests ran without doing anything. - `cargo clippy -p xcl --all-targets`: no warnings on the changed lines. The rest of the file and the crate already had warnings, and I left those alone. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
extract_chain_certs walked CERT_CHAIN_CONTEXT / CERT_SIMPLE_CHAIN with
hand-written offsets that do not match wincrypt.h on x64: cChain and
cElement are at +12 and rgpChain / rgpElement at +16, not +16/+24 and
+20/+24. The walk read cLowerQualityChainContext plus padding as the
chain array, so it usually found no certificates and any thumbprint pin
failed with CERT_E_UNTRUSTEDROOT (a non-zero read would have followed a
junk pointer).

Read the chain through windows-sys's CERT_CHAIN_CONTEXT,
CERT_SIMPLE_CHAIN and CERT_CHAIN_ELEMENT (feature
Win32_Security_Cryptography) instead.

Tests: CertContext checked against the SDK's CERT_CONTEXT; a chain built
from the SDK structs (fails with the old offsets); a real crypt32 chain
for a root-store certificate yields that certificate as its leaf.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:05:34 +00:00
github-bot closed this pull request 2026-11-26 04:05:34 +00:00

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.