Skip to content

Protect locally stored Acount Code on disk with system api - #3508

Open
Mabeeck wants to merge 19 commits into
PixelGuys:masterfrom
Mabeeck:windows-CryptProtectData
Open

Protect locally stored Acount Code on disk with system api#3508
Mabeeck wants to merge 19 commits into
PixelGuys:masterfrom
Mabeeck:windows-CryptProtectData

Conversation

@Mabeeck

@Mabeeck Mabeeck commented Aug 11, 2026

Copy link
Copy Markdown

This PR adds the main.protect.protect and main.protect.unprotect functions and integrates them into the PasswordEncodedAccountCode class.

protect Takes an allocator and a slice of bytes as arguments. The function returns a different slice of bytes that has been allocated with the provided allocator and can be passed to unprotect to get back the original slice of bytes. The function can fail, if the platform Cubyz is currently running on does not have an implementation yet (in which case the error will be error.Unsupported) or the syscall fails for some undisclosed reason (Windows), in which case the error will be error.syserr.
unprotect Takes an allocator and a slice of bytes that has been previously generated by protect as arguments. The function returns a different slice of bytes that has been allocated with the provided allocator and is equivalent in value to the slice of bytes that was passed to protect in order to produce the provided slice. The function can return error.Invalid if the provided input was protected on a different device; can no longer be unprotected for some reason; the current platform does not have an implementation. If something unexpected happened the function will fail with error.syserr.
canProtect Takes no arguments and returns a boolean indicating weather the protection functions have an implementation on the current platform.

Currently the protection functions only support Windows. They can later be easily expanded to support Linux as well.

Adds a protected attribute to PasswordEncodedAccountCode that indicates weather a call to unprotect is needed, before the AccountCode can be decrypted.
Functions to initialize PasswordEncodedAccountCode now take a shouldProtect boolean argument that when set to true will protect the function with the native system api, if available. Setting it to false will prevent usage of the protection api.

Contributes to #2551

Due to line 411 in authentication.zig and some other reasons I have decided to rework this implementation to make use of a `protected` boolean attribute instead of creating tons of extra EncodingTypes.
Obey linter and migrate to protected attribute.
protect() and unprotect() now error on unsupported platforms. I felt that it would be bad, if someone assumed that a call to unprotect would always error on bad input or that a call to protect would always encrypt the data.
These previously false assumptions are now true.
@Mabeeck Mabeeck changed the title Add a protect() function Protect locally stored Acount Code on disk with system api Aug 12, 2026
Yes, I blame microslop for using an uppercase in their docs.
@Mabeeck
Mabeeck force-pushed the windows-CryptProtectData branch 2 times, most recently from 60f2598 to 92890da Compare August 12, 2026 19:19
Apparently errorcode 13 is for strings too short and errorcode 87 is for otherwise gibberish.

The real reason I made this commit is that some github service was down when my last test ran and the only way to rerun it is to make another commit.
@Mabeeck
Mabeeck marked this pull request as ready for review August 12, 2026 19:56
@Wunka Wunka moved this to High Priority in PRs to review Aug 14, 2026
Comment thread src/protect.zig Outdated
Comment thread src/protect.zig Outdated
Comment thread src/protect.zig Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should not be at the root. main.protect is quite ambiguous.
I'd suggest to put it in main.network.autentication.protect, to emphasize that it belongs thematically to the authentication system.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think it should still be put into a separate file, since it will likely grow as we add support for the other operating systems.

Comment thread src/protect.zig Outdated
Comment thread src/protect.zig Outdated
Comment thread src/protect.zig Outdated
Comment thread src/protect.zig Outdated
Comment thread src/protect.zig Outdated
Comment thread src/protect.zig Outdated
Comment thread src/network/authentication.zig Outdated
Comment thread src/gui/windows/authentication/encrypt_with_password.zig Outdated
Comment thread src/protect.zig Outdated
@IntegratedQuantum IntegratedQuantum moved this from High Priority to In review in PRs to review Aug 16, 2026
@Mabeeck

Mabeeck commented Aug 17, 2026

Copy link
Copy Markdown
Author

Done

Comment on lines +296 to +298
pub inline fn canProtect() bool {
return Impl.canProtect;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
pub inline fn canProtect() bool {
return Impl.canProtect;
}
pub const canProtect: bool = Impl.canProtect;

return Impl.unprotect(allocator, data);
}

const NoImpl = struct {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

technically these are namespaces (no fields), so they should follow our naming scheme for them: no_impl windows_impl impl

}
}

test "Protect fails on unsupported platforms" {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Note that these currently do not run in the CI. I made an issue for this: #3535

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In review

Development

Successfully merging this pull request may close these issues.

3 participants