openai/codex · error · anyhow::Error

CryptProtectData failed

Error message

CryptProtectData failed: {}

What it means

CryptProtectData returned 0 while DPAPI-encrypting data (sandbox credentials) with CRYPTPROTECT_LOCAL_MACHINE | CRYPTPROTECT_UI_FORBIDDEN. Machine-scope protection is used so both elevated and non-elevated processes can decrypt; failure means the machine DPAPI key store is unavailable or the protection request was rejected, with GetLastError identifying the cause.

Solutions

  1. Check the GetLastError code in the message for the DPAPI failure reason
  2. Verify the machine DPAPI keys (C:\ProgramData\Microsoft\Crypto\Protect) are intact and accessible; running elevated can fix key-access denials
  3. If the machine key store is corrupted, use DPAPI repair guidance or regenerate protection keys, then re-run setup
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at codex-rs/windows-sandbox-rs/src/dpapi.rs:39 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/bcfde76809bed029. Report an issue: GitHub.

Appendix: source

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

    let mut in_blob = make_blob(data);
    let mut out_blob = CRYPT_INTEGER_BLOB {
        cbData: 0,
        pbData: std::ptr::null_mut(),
    };
    let ok = unsafe {
        CryptProtectData(
            &mut in_blob,
            std::ptr::null(),
            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!("CryptProtectData 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)
}

#[allow(clippy::unnecessary_mut_passed)]
pub fn unprotect(blob: &[u8]) -> Result<Vec<u8>> {
    let mut in_blob = make_blob(blob);
    let mut out_blob = CRYPT_INTEGER_BLOB {
        cbData: 0,

View on GitHub (pinned to 339751715c)