openai/codex · error · anyhow::Error

CryptUnprotectData failed

Error message

CryptUnprotectData failed: {}

What it means

CryptUnprotectData returned 0 while decrypting a DPAPI blob that was protected with CRYPTPROTECT_LOCAL_MACHINE. Decryption requires the same machine scope and a non-UI-forbidden context; failure typically means the blob was protected under a different machine key, is corrupt, or the process lacks access to the machine protection key.

Solutions

  1. Check the GetLastError code: NTE_BAD_KEY_STATE or key-access errors indicate the machine key does not match or is inaccessible
  2. Ensure the blob was produced on the same machine and with CRYPTPROTECT_LOCAL_MACHINE; blobs from another install cannot be decrypted
  3. Delete stale secret blobs and re-run setup to re-protect credentials under the current machine key
  4. Run under an identity with access to the machine DPAPI key store
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at codex-rs/windows-sandbox-rs/src/dpapi.rs:73 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of openai/codex@339751715c (2026-08-25). Data as JSON: /api/errors/bdc3b248985f3995. Report an issue: GitHub.

Appendix: source

Thrown at codex-rs/windows-sandbox-rs/src/dpapi.rs:73

    let mut in_blob = make_blob(blob);
    let mut out_blob = CRYPT_INTEGER_BLOB {
        cbData: 0,
        pbData: std::ptr::null_mut(),
    };
    let ok = unsafe {
        CryptUnprotectData(
            &mut in_blob,
            std::ptr::null_mut(),
            std::ptr::null(),
            std::ptr::null_mut(),
            std::ptr::null_mut(),
            // Use machine scope so elevated and non-elevated processes can decrypt.
            CRYPTPROTECT_UI_FORBIDDEN | CRYPTPROTECT_LOCAL_MACHINE,
            &mut out_blob,
        )
    };
    if ok == 0 {
        return Err(anyhow!("CryptUnprotectData failed: {}", unsafe {
            GetLastError()
        }));
    }
    let slice =
        unsafe { std::slice::from_raw_parts(out_blob.pbData, out_blob.cbData as usize) }.to_vec();
    unsafe {
        if !out_blob.pbData.is_null() {
            LocalFree(out_blob.pbData as HLOCAL);
        }
    }
    Ok(slice)
}

View on GitHub (pinned to 339751715c)