Skip to content

Commit b9b0037

Browse files
committed
Crypto: Comment todo for observed missing modeled case. Tests for weak and unknown KDF iteration count.
1 parent 3f36b09 commit b9b0037

File tree

6 files changed

+94
-0
lines changed

6 files changed

+94
-0
lines changed

java/ql/lib/experimental/quantum/JCA.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,8 @@ module JCAModel {
697697
abstract DataFlow::Node getInputNode();
698698
}
699699

700+
// TODO: for all parametert specs, I think they can be set through the constructor
701+
// and through setter methods
700702
class IvParameterSpecInstance extends NonceParameterInstantiation {
701703
IvParameterSpecInstance() {
702704
super.getConstructedType().hasQualifiedName("javax.crypto.spec", "IvParameterSpec")
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import java.io.FileInputStream;
2+
import java.io.IOException;
3+
import java.security.MessageDigest;
4+
import java.security.SecureRandom;
5+
import java.util.Arrays;
6+
import java.util.Base64;
7+
import java.util.Properties;
8+
import javax.crypto.Mac;
9+
import javax.crypto.SecretKey;
10+
import javax.crypto.SecretKeyFactory;
11+
import javax.crypto.spec.PBEKeySpec;
12+
import javax.crypto.spec.SecretKeySpec;
13+
14+
public class Test {
15+
16+
public static byte[] generateSalt(int length) {
17+
SecureRandom random = new SecureRandom();
18+
byte[] salt = new byte[length];
19+
random.nextBytes(salt);
20+
return salt;
21+
}
22+
23+
/**
24+
* PBKDF2 derivation with a very low iteration count.
25+
*
26+
* SAST/CBOM: - Parent: PBKDF2. - Iteration count is only 10, which is far
27+
* below acceptable security standards. - Flagged as insecure.
28+
*/
29+
public void pbkdf2LowIteration(String password) throws Exception {
30+
byte[] salt = generateSalt(16);
31+
int iterationCount = 10; // $Source
32+
PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), salt, iterationCount, 256); // $Alert[java/quantum/weak-kdf-iteration-count]
33+
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
34+
byte[] key = factory.generateSecret(spec).getEncoded();
35+
}
36+
37+
/**
38+
* PBKDF2 derivation with a very low iteration count.
39+
*
40+
* SAST/CBOM: - Parent: PBKDF2. - Iteration count is only 10, which is far
41+
* below acceptable security standards. - Flagged as insecure.
42+
*/
43+
public void pbkdf2LowIteration(String password, int iterationCount) throws Exception { // $Source
44+
byte[] salt = generateSalt(16);
45+
PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), salt, iterationCount, 256); // $Alert[java/quantum/unknown-kdf-iteration-count]
46+
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
47+
byte[] key = factory.generateSecret(spec).getEncoded();
48+
}
49+
50+
/**
51+
* PBKDF2 derivation with a high iteration count.
52+
*
53+
* SAST/CBOM: - Parent: PBKDF2. - Uses 1,000,000 iterations; this is secure
54+
* but may impact performance.
55+
*/
56+
public void pbkdf2HighIteration(String password) throws Exception {
57+
byte[] salt = generateSalt(16);
58+
int iterationCount = 1_000_000;
59+
PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), salt, iterationCount, 256);
60+
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
61+
byte[] key = factory.generateSecret(spec).getEncoded();
62+
}
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#select
2+
| Test.java:47:22:47:49 | KeyDerivation | Key derivation operation with unknown iteration: $@ | Test.java:43:53:43:70 | iterationCount | iterationCount |
3+
testFailures
4+
| Test.java:45:94:45:145 | // $Alert[java/quantum/unknown-kdf-iteration-count] | Missing result: Alert[java/quantum/unknown-kdf-iteration-count] |
5+
| Test.java:47:22:47:49 | Key derivation operation with unknown iteration: $@ | Unexpected result: Alert |
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
query: experimental/quantum/Examples/UnknownKDFIterationCount.ql
2+
postprocess:
3+
- utils/test/PrettyPrintModels.ql
4+
- utils/test/InlineExpectationsTestQuery.ql
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#select
2+
| Test.java:32:72:32:85 | iterationCount | Test.java:31:30:31:31 | 10 : Number | Test.java:32:72:32:85 | iterationCount | Key derivation operation configures iteration count below 100k: $@ | Test.java:31:30:31:31 | 10 | 10 |
3+
edges
4+
| Test.java:31:30:31:31 | 10 : Number | Test.java:32:72:32:85 | iterationCount | provenance | |
5+
| Test.java:43:53:43:70 | iterationCount : Number | Test.java:45:72:45:85 | iterationCount | provenance | |
6+
| Test.java:58:30:58:38 | 1_000_000 : Number | Test.java:59:72:59:85 | iterationCount | provenance | |
7+
nodes
8+
| Test.java:31:30:31:31 | 10 : Number | semmle.label | 10 : Number |
9+
| Test.java:32:72:32:85 | iterationCount | semmle.label | iterationCount |
10+
| Test.java:43:53:43:70 | iterationCount : Number | semmle.label | iterationCount : Number |
11+
| Test.java:45:72:45:85 | iterationCount | semmle.label | iterationCount |
12+
| Test.java:58:30:58:38 | 1_000_000 : Number | semmle.label | 1_000_000 : Number |
13+
| Test.java:59:72:59:85 | iterationCount | semmle.label | iterationCount |
14+
subpaths
15+
testFailures
16+
| Test.java:43:92:43:102 | // $Source | Missing result: Source |
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
query: experimental/quantum/Examples/WeakKDFIterationCount.ql
2+
postprocess:
3+
- utils/test/PrettyPrintModels.ql
4+
- utils/test/InlineExpectationsTestQuery.ql

0 commit comments

Comments
 (0)