Skip to content
Reference

Windows elevation patterns

Each enrollment on Windows runs TPM2_ActivateCredential, which Windows allows only in an elevated process. The SDK never elevates for you: RootHeraldEnrollComplete returns RH_ERR_ELEVATION_REQUIRED instead, with nothing consumed, and you choose how to get there. RootHeraldEnrollBegin, attesting, minting keys and signing run without elevation.

One elevation per enrollment

Each installation enrolls once, so each costs one elevation. If you ship an installer, enroll from it (Pattern 1) and you never prompt. Otherwise, enroll only when an installation needs it; see When to enroll.

Only the second leg elevates

RootHeraldEnrollBegin creates the attestation key and hands it back as a blob, so the two legs need not share a process. The smallest elevated surface is a helper that runs RootHeraldEnrollComplete alone, with the blob and the server's reply as its inputs, and hands the activation body back. The steps themselves are in Enroll a Windows device. The usual way in is to relaunch your own executable with an argument your startup checks first:

main.cppcpp
int main(int argc, char** argv) {
    if (argc >= 2 && strcmp(argv[1], "--activate-worker") == 0) {
        return RunActivateWorker(argv[2]);
    }

    // ...normal startup
}

Have the elevated worker and your unelevated app relay through the same server and the same API key, so both halves of the enrollment reach the same Root Herald account.

Pattern 1: Enroll during installation

Your installer already runs elevated, and the user has already approved its prompt, so enrolling there costs nothing: no second prompt, no relaunch, nothing for your app to do at runtime. Make the two enrollment calls from a custom action (MSI), an install step (NSIS or Inno) or a post-install task. The installer needs to reach your server and stay running across the round trip.

The installer runs as an administrator, so write the attestation key blob where the per-user app that attests can read it: a DPAPI blob under the installing user's profile, or a file in your app's data directory with an ACL for that user.

Don't fail the install if enrollment fails

A machine with no TPM, a disabled TPM, or no network at install time must still install your product. Treat enrollment there as best effort, and let Pattern 2 enroll the installations that missed it on first launch.

Pattern 2: Relaunch yourself elevated

For a desktop app with no installer, run RootHeraldEnrollBegin and the relay unelevated, then relaunch your own executable elevated with --activate-worker for the second leg. That's one prompt, and the worker talks back to your unelevated app over a named pipe for the blob, the reply and the activation body.

self-shim.cppcpp
static int RunSelfElevationShim(const wchar_t* pipeName) {
    wchar_t exe[MAX_PATH] = {0};

    GetModuleFileNameW(NULL, exe, MAX_PATH);

    wchar_t args[512] = {0};

    swprintf_s(args, 512, L"--activate-worker \"%s\"", pipeName);

    SHELLEXECUTEINFOW sei = { sizeof(sei) };

    sei.fMask = SEE_MASK_NOCLOSEPROCESS;
    sei.lpVerb = L"runas";
    sei.lpFile = exe;
    sei.lpParameters = args;
    sei.nShow = SW_HIDE;

    if (!ShellExecuteExW(&sei) || !sei.hProcess) {
        return 1;
    }

    WaitForSingleObject(sei.hProcess, INFINITE);

    DWORD code = 1;

    GetExitCodeProcess(sei.hProcess, &code);

    CloseHandle(sei.hProcess);

    return (int)code;
}

ShellExecuteExW fails when the user declines the prompt, so the shim returns 1; otherwise it returns the worker's exit code.

Label the worker's pipe for your unelevated app

If the elevated worker creates the named pipe, give it a security descriptor with a medium integrity label (S:(ML;;NW;;;ME)). A pipe made with default security takes the worker's high integrity, Windows stops your medium-integrity app from opening it, and enrollment hangs.

Pattern 3: From a browser

If your users attest from a browser, the client application behind your bridge enrolls, using Pattern 1 or Pattern 4, since a link or an extension starts it without elevation. See Browser.

Pattern 4: A privileged helper or service

If your app already runs elevated, or you can install a small signed helper or Windows service, make the enrollment calls from there: no relaunch and no prompt. An elevated service also suits unattended fleets, where nobody is there to approve a prompt.

A service that enrolls holds the blob. Either the service attests and mints on the app's behalf, or it hands the blob to the per-user app through storage that app can read. The blob is a credential on that machine.

Pattern 5: When you can't elevate at all

Microsoft Store (MSIX) apps, tightly sandboxed processes and some locked-down machines can't elevate. Detect that and carry on without hardware attestation, or enroll later from a context that can, rather than prompting for something the user can't approve.

degrade.cppcpp
RH_STATUS st = RootHeraldEnrollComplete(h, reply, strlen(reply),
                                        buf, sizeof buf, &len,
                                        ak, ak_len);

if (st == RH_ERR_ELEVATION_REQUIRED) {
    log_warn("device not hardware-attested: enrollment needs elevation");

    return CONTINUE_WITHOUT_ATTESTATION;
}