-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Java: Add MaDs for java.crypto.KDF
#20345
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
16fbe8d
Java: add dataflow test for newly added KDF API
IdrissRio f52a427
Java: Add MaDs for `java.crypto.KDF`
IdrissRio 311690c
Java: accept new test results
IdrissRio 89e080c
Java: Add new change note
IdrissRio 55ff71b
Java: Address review comment. Fix dataflow model
IdrissRio 3aba4d3
Java: Add test showing missing model for `thenExpand`
IdrissRio 728a4af
Java: Add model for `thenExpand` and accept new results
IdrissRio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,4 @@ | ||
| --- | ||
| category: minorAnalysis | ||
| --- | ||
| * Added taint flow model for `java.crypto.KDF`. |
This file contains hidden or 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 hidden or 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
87 changes: 87 additions & 0 deletions
87
java/ql/test/library-tests/dataflow/kdf/KDFDataflowTest.java
This file contains hidden or 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,87 @@ | ||
| import javax.crypto.KDF; | ||
| import javax.crypto.spec.HKDFParameterSpec; | ||
|
|
||
| public class KDFDataflowTest { | ||
| public static String source(String label) { | ||
| return "tainted"; | ||
| } | ||
|
|
||
| public static void sink(Object o) {} | ||
|
|
||
| public static void main(String[] args) throws Exception { | ||
| String userInput = source(""); | ||
| byte[] taintedBytes = userInput.getBytes(); | ||
|
|
||
| testBuilderPattern(taintedBytes); | ||
| testSeparateBuilder(taintedBytes); | ||
| testKDFWithSalt(taintedBytes); | ||
| testStaticParameterSpec(taintedBytes); | ||
| testCleanUsage(); | ||
| } | ||
|
|
||
| public static void testBuilderPattern(byte[] taintedIKM) throws Exception { | ||
| HKDFParameterSpec.Builder builder = HKDFParameterSpec.ofExtract(); | ||
| builder.addIKM(taintedIKM); | ||
| HKDFParameterSpec spec = builder.thenExpand("info".getBytes(), 32); | ||
|
|
||
| KDF kdf = KDF.getInstance("HKDF-SHA256"); | ||
| byte[] result = kdf.deriveData(spec); | ||
| sink(result); // $ hasTaintFlow | ||
| } | ||
|
|
||
| public static void testSeparateBuilder(byte[] taintedIKM) throws Exception { | ||
| HKDFParameterSpec.Builder builder1 = HKDFParameterSpec.ofExtract(); | ||
| HKDFParameterSpec.Builder builder2 = builder1.addIKM(taintedIKM); | ||
| HKDFParameterSpec spec = builder2.thenExpand("info".getBytes(), 32); | ||
|
|
||
| KDF kdf = KDF.getInstance("HKDF-SHA256"); | ||
| byte[] result = kdf.deriveData(spec); | ||
| sink(result); // $ hasTaintFlow | ||
| } | ||
|
|
||
| public static void testKDFWithSalt(byte[] taintedIKM) throws Exception { | ||
| HKDFParameterSpec.Builder builder = HKDFParameterSpec.ofExtract(); | ||
| builder.addIKM(taintedIKM); | ||
| builder.addSalt("sensitive-salt".getBytes()); | ||
| HKDFParameterSpec spec = builder.thenExpand("info".getBytes(), 32); | ||
|
|
||
| KDF kdf = KDF.getInstance("HKDF-SHA256"); | ||
| byte[] result = kdf.deriveData(spec); | ||
| sink(result); // $ hasTaintFlow | ||
| } | ||
|
|
||
| public static void testStaticParameterSpec(byte[] taintedIKM) throws Exception { | ||
| javax.crypto.spec.SecretKeySpec secretKey = new javax.crypto.spec.SecretKeySpec(taintedIKM, "AES"); | ||
| HKDFParameterSpec spec = HKDFParameterSpec.expandOnly( | ||
| secretKey, "info".getBytes(), 32); | ||
|
|
||
| KDF kdf = KDF.getInstance("HKDF-SHA256"); | ||
| byte[] result = kdf.deriveData(spec); | ||
| sink(result); // $ hasTaintFlow | ||
| } | ||
|
|
||
| public static void testCleanUsage() throws Exception { | ||
| byte[] cleanKeyMaterial = "static-key-material".getBytes(); | ||
|
|
||
| HKDFParameterSpec.Builder builder = HKDFParameterSpec.ofExtract(); | ||
| builder.addIKM(cleanKeyMaterial); | ||
| HKDFParameterSpec spec = builder.thenExpand("info".getBytes(), 32); | ||
|
|
||
| KDF kdf = KDF.getInstance("HKDF-SHA256"); | ||
| byte[] cleanResult = kdf.deriveData(spec); | ||
| sink(cleanResult); // Safe - no taint | ||
| } | ||
|
|
||
| public static void testThenExpand(byte[] cleanIKM) throws Exception { | ||
| String userInput = source(""); | ||
| byte[] taintedInfo = userInput.getBytes(); | ||
|
|
||
| HKDFParameterSpec.Builder builder = HKDFParameterSpec.ofExtract(); | ||
| builder.addIKM(cleanIKM); | ||
| HKDFParameterSpec spec = builder.thenExpand(taintedInfo, 32); | ||
|
|
||
| KDF kdf = KDF.getInstance("HKDF-SHA256"); | ||
| byte[] result = kdf.deriveData(spec); | ||
| sink(result); // $ hasTaintFlow | ||
| } | ||
| } |
This file contains hidden or 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 @@ | ||
| //semmle-extractor-options: --javac-args --enable-preview --release 25 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.