-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clear security sensitive data after usage in c.s.j.p.win32.Crypt32Uti…
…l#cryptUnprotectData and #cryptUnprotectData
- Loading branch information
Dmytro Sheyko
committed
Aug 10, 2021
1 parent
7027e33
commit 48fe9bc
Showing
3 changed files
with
176 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
...b/platform/test/com/sun/jna/platform/win32/Crypt32UtilClearSecuritySensitiveDataTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package com.sun.jna.platform.win32; | ||
|
||
import java.lang.ref.WeakReference; | ||
import java.lang.reflect.Field; | ||
import java.util.Arrays; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.ErrorCollector; | ||
|
||
import com.sun.jna.Memory; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
|
||
/** | ||
* https://github.com/java-native-access/jna/issues/1362 | ||
*/ | ||
public class Crypt32UtilClearSecuritySensitiveDataTest { | ||
@Rule public ErrorCollector errors = new ErrorCollector(); | ||
Field fieldMemory_HEAD; | ||
Field fieldLinkedReference_next; | ||
Field fieldLinkedReference_prev; | ||
|
||
@Before | ||
public void setUp() throws NoSuchFieldException, ClassNotFoundException { | ||
fieldMemory_HEAD = Memory.class.getDeclaredField("HEAD"); | ||
fieldMemory_HEAD.setAccessible(true); | ||
Class<?> classLinkedReference = Class.forName("com.sun.jna.Memory$LinkedReference"); | ||
fieldLinkedReference_next = classLinkedReference.getDeclaredField("next"); | ||
fieldLinkedReference_next.setAccessible(true); | ||
fieldLinkedReference_prev = classLinkedReference.getDeclaredField("prev"); | ||
fieldLinkedReference_prev.setAccessible(true); | ||
} | ||
|
||
boolean stillHover(byte[] sample) throws IllegalAccessException { | ||
Object head = fieldMemory_HEAD.get(null); | ||
return stillHover(sample, head, fieldLinkedReference_next) || stillHover(sample, head, fieldLinkedReference_prev); | ||
} | ||
|
||
boolean stillHover(byte[] sample, Object head, Field fieldNext) throws IllegalAccessException { | ||
Object next = head; | ||
do { | ||
Object curr = next; | ||
Memory memory = (Memory) ((WeakReference<?>) curr).get(); | ||
byte[] array = memory.getByteArray(0, (int) memory.size()); | ||
if (Arrays.equals(array, sample)) { | ||
return true; | ||
} | ||
next = fieldNext.get(curr); | ||
} while (next != null); | ||
return false; | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
Memory.disposeAll(); | ||
} | ||
|
||
@Test | ||
public void testEncryption() throws IllegalAccessException { | ||
byte[] original = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, }; | ||
Crypt32Util.cryptProtectData(original, null, WinCrypt.CRYPTPROTECT_UI_FORBIDDEN, "", null); | ||
|
||
errors.checkThat("original is still hover", false, is(stillHover(original))); | ||
} | ||
|
||
@Test | ||
public void testDecryption() throws IllegalAccessException { | ||
byte[] original = { 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, }; | ||
byte[] encrypted = Crypt32Util.cryptProtectData(original, null, WinCrypt.CRYPTPROTECT_UI_FORBIDDEN, "", null); | ||
Crypt32Util.cryptUnprotectData(encrypted, null, WinCrypt.CRYPTPROTECT_UI_FORBIDDEN, null); | ||
|
||
errors.checkThat("original is still hover", false, is(stillHover(original))); | ||
errors.checkThat("encrypted is still hover", false, is(stillHover(encrypted))); | ||
} | ||
|
||
@Test | ||
public void testEncryptionWithEntropy() throws IllegalAccessException { | ||
byte[] original = { 25, 26, 27, 28, 29, 30, }; | ||
byte[] entropy = { 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, }; | ||
Crypt32Util.cryptProtectData(original, entropy, WinCrypt.CRYPTPROTECT_UI_FORBIDDEN, "", null); | ||
|
||
errors.checkThat("original is still hover", false, is(stillHover(original))); | ||
errors.checkThat("entropy is still hover", false, is(stillHover(entropy))); | ||
} | ||
|
||
@Test | ||
public void testDecryptionWithEntropy() throws IllegalAccessException { | ||
byte[] original = { 31, 32, 33, 34, }; | ||
byte[] entropy = { 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, }; | ||
byte[] encrypted = Crypt32Util.cryptProtectData(original, entropy, WinCrypt.CRYPTPROTECT_UI_FORBIDDEN, "", null); | ||
Crypt32Util.cryptUnprotectData(encrypted, entropy, WinCrypt.CRYPTPROTECT_UI_FORBIDDEN, null); | ||
|
||
errors.checkThat("original is still hover", false, is(stillHover(original))); | ||
errors.checkThat("entropy is still hover", false, is(stillHover(entropy))); | ||
errors.checkThat("encrypted is still hover", false, is(stillHover(encrypted))); | ||
} | ||
|
||
@Test | ||
public void testUnsuccessfulDecryption() throws IllegalAccessException { | ||
byte[] original = { 35, 36, 37, 38, 39, 40, 41, }; | ||
try { | ||
Crypt32Util.cryptUnprotectData(original, null, WinCrypt.CRYPTPROTECT_UI_FORBIDDEN, null); | ||
errors.addError(new AssertionError("Win32Exception is expected")); | ||
} catch (Win32Exception e) { | ||
// ok, expected | ||
} | ||
|
||
errors.checkThat("original is still hover", false, is(stillHover(original))); | ||
} | ||
|
||
@Test | ||
public void testUnsuccessfulDecryptionBadEntropy() throws IllegalAccessException { | ||
byte[] original = { 42, 43, }; | ||
byte[] entropy0 = { 44, 45, 46, }; | ||
byte[] entropy1 = { 47, 48, 49, 50, }; | ||
byte[] encrypted = Crypt32Util.cryptProtectData(original, entropy0, WinCrypt.CRYPTPROTECT_UI_FORBIDDEN, "", null); | ||
try { | ||
Crypt32Util.cryptUnprotectData(encrypted, entropy1, WinCrypt.CRYPTPROTECT_UI_FORBIDDEN, null); | ||
errors.addError(new AssertionError("Win32Exception is expected")); | ||
} catch (Win32Exception e) { | ||
// ok, expected | ||
} | ||
|
||
errors.checkThat("original is still hover", false, is(stillHover(original))); | ||
errors.checkThat("entropy0 is still hover", false, is(stillHover(entropy0))); | ||
errors.checkThat("entropy1 is still hover", false, is(stillHover(entropy1))); | ||
errors.checkThat("encrypted is still hover", false, is(stillHover(encrypted))); | ||
} | ||
} |