-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Crypt32Util.cryptProtectData fails on zero-length array #1361
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
The API isn't really clear on whether a null pointer or empty array is permitted, and it seems nonsensical to encrypt an empty array. What's the appropriate return value? It's undocumented, and potentially undefined. Seems this could be fixed by length-checking jna/contrib/platform/src/com/sun/jna/platform/win32/Crypt32Util.java Lines 78 to 80 in 5ac6161
Or we could alter jna/contrib/platform/src/com/sun/jna/platform/win32/WinCrypt.java Lines 74 to 76 in 5ac6161
It could also be fixed in user code by length-checking before calling what amounts to a no-op method. |
I think the variant to create a public DATA_BLOB(byte [] data) {
if(data == null || data.length == 0) {
pbData = null;
cbData = 0;
} else {
pbData = new Memory(data.length);
pbData.write(0, data, 0, data.length);
cbData = data.length;
}
allocateMemory();
} What the w32 API will to with that is a totally different matter and also needs testing. |
@matthiasblaesing, thanks for suggestion, I will try to test it. But I would note that 1-byte array is still ok it does not affect the final result because we pass the size of data in |
@matthiasblaesing, unfortunately the approach with
|
Welcome to the wonderful world of
that the second variant of // Allocate memory for the BLOB data.
blob.pbData = Marshal.AllocHGlobal(data.Length);
// Make sure that memory allocation was successful.
if (blob.pbData == IntPtr.Zero)
throw new Exception(
"Unable to allocate data buffer for BLOB structure.");Marshal.AllocHGlobal(data.Length); would fail (assuming |
In any case - it needs to be documentation above the Memory allocation why a 1 byte allocation is done even if the byte[] has zero length. |
corrected pull request: added comment explaining why we allocate 1 byte instead of 0. |
JNA version: 5.8.0
jna-platform.jar (+ jna.jar)
Java Version: 1.8.0_261
java -version
java version "1.8.0_261"
Java(TM) SE Runtime Environment (build 1.8.0_261-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.261-b12, mixed mode)
OS Version: Windows 10
Microsoft Windows [Version 10.0.18363.1556]
Description of the problem
The method
Crypt32Util.cryptProtectData
is not able to encrypt zero-length array. Please note that .NETProtectedData.Protect
and direct Windows APICryptProtectData
accept and encrypt zero-length data perfectly.Code that reproduces the bug
Expected
Actual
The text was updated successfully, but these errors were encountered: