Skip to content

Use APPDATA for the default Windows token path - #225

Open
LinuxJedi wants to merge 2 commits into
wolfSSL:masterfrom
LinuxJedi:fix-windows-appdata-token-path
Open

LinuxJedi wants to merge 2 commits into
wolfSSL:masterfrom
LinuxJedi:fix-windows-appdata-token-path

Conversation

@LinuxJedi

@LinuxJedi LinuxJedi commented Oct 6, 2026 •

Copy link
Copy Markdown
Member

On Windows, wolfPKCS11_Store_Name() looked up the default token directory with getenv("%APPDIR%"). getenv() does not expand %VAR% syntax and APPDIR is not a standard Windows variable, so the lookup always returned NULL. Since 2.0, Windows has had no per-user default: token data went to WOLFPKCS11_DEFAULT_TOKEN_PATH, or token storage failed if that was not set.

  • Read APPDATA instead, so the default is %APPDATA%\wolfPKCS11. There is no USERPROFILE fallback, mirroring the POSIX branch which only reads HOME. A single lookup keeps the token location deterministic rather than depending on which variables a given process inherited.
  • token_path_test now expects only %APPDATA%\wolfPKCS11 (it previously also accepted a USERPROFILE fallback the library never had) and no longer unsets APPDIR.
  • README now documents %APPDATA%\wolfPKCS11 and notes the behavior change.
  • Windows CI now runs token_path_test. The existing job builds the Visual Studio solution, which enables wolfTPM, so its tests cannot run on runners without a TPM; nothing exercised this path, which is how the bug went unnoticed. A new cmake-token-path job in win-test.yml builds wolfSSL and wolfPKCS11 with CMake and MSVC as static libraries without wolfTPM and runs token_path_test via ctest. To build the test on Windows, testdata.h gains an unsetenv() shim next to its existing setenv() shim, and token_path_test.c includes io.h for _access().

Behavior change on Windows: %APPDATA%\wolfPKCS11 is checked before WOLFPKCS11_DEFAULT_TOKEN_PATH, so Windows builds that relied on the build-time default will now use the per-user directory. Deployments with existing tokens there need to set WOLFPKCS11_TOKEN_PATH to that directory or move the token files. This is called out in the README and should go in the next release notes.

Testing:

  • Linux (debian:trixie container, wolfSSL master with the CI configure flags): ./configure && make && make -j1 check passes (55 pass, 4 skip, 0 fail), including token_path_test.
  • macOS: pre-commit hook make test passes (55 pass, 4 skip, 0 fail).
  • Windows, locally: built with the same CMake options as the new CI job using mingw-w64 (ucrt) and ran token_path_test.exe under Wine. All 9 checks pass and the token file is created in C:\users\<user>\AppData\Roaming\wolfPKCS11. As a negative control, restoring the old %APPDIR% lookup makes C_Initialize fail and the test exit 1, so the new job would have caught the original bug.
  • Windows, MSVC: covered by the new cmake-token-path CI job on this PR.

The Windows branch of wolfPKCS11_Store_Name() looked up the default
token directory with getenv("%APPDIR%"). getenv() does not expand %VAR%
syntax and APPDIR is not a standard Windows variable, so the lookup
always failed and Windows had no per-user default: token data went to
WOLFPKCS11_DEFAULT_TOKEN_PATH, or storage failed if it was not set.

Read APPDATA instead, giving %APPDATA%\wolfPKCS11. There is no
USERPROFILE fallback, mirroring the POSIX branch which only reads HOME.
Update token_path_test and the README to match, and note in the README
that Windows deployments with existing tokens in the build-time default
directory need to set WOLFPKCS11_TOKEN_PATH or move the files.
Copilot AI balanced review requested due to automatic review settings October 6, 2026 13:50

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

The Windows-specific regression test is not runnable or executed in Windows CI.

Review effort: Balanced
Findings: 1 Medium severity

Open (1)
What changed in this PR

Updates Windows token storage to use the standard per-user APPDATA directory.

Changes:

  • Uses %APPDATA%\wolfPKCS11 as the Windows default.
  • Updates path expectations and migration documentation.
  • Removes obsolete APPDIR handling.
File Description
src/​internal.c Reads APPDATA for Windows storage.
tests/​token_path_test.c Updates Windows path expectations.
README.md Documents behavior and migration impact.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread tests/token_path_test.c
The Windows CI job only builds the Visual Studio solution, which enables
wolfTPM and so cannot run tests on runners without a TPM. Nothing
exercised the Windows default token path, which is how the %APPDIR%
lookup went unnoticed.

Add a job that builds wolfSSL and wolfPKCS11 with CMake and MSVC as
static libraries without wolfTPM, and runs token_path_test. Add an
unsetenv() shim next to the existing Windows setenv() shim in
testdata.h and include io.h for _access() so the test builds on
Windows.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants