Skip to content

Commit 4e69081

Browse files
Support multi-dimensional arrays
1 parent df0f9ee commit 4e69081

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

java/ql/src/experimental/semmle/code/java/security/StaticInitializationVectorQuery.qll

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@ import semmle.code.java.dataflow.TaintTracking
33
import semmle.code.java.dataflow.TaintTracking2
44

55
/**
6-
* Holds if `array` is initialized only with constants, for example,
7-
* `new byte[8]` or `new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }`.
6+
* Holds if `array` is initialized only with constants.
87
*/
98
private predicate initializedWithConstants(ArrayCreationExpr array) {
9+
// creating an array without an initializer, for example `new byte[8]`
1010
not exists(array.getInit())
1111
or
12-
forex(Expr element | element = array.getInit().getAChildExpr() |
12+
// creating a multidimensional array with an initializer like `{ new byte[8], new byte[16] }`
13+
array.getInit().getAnInit().getAChildExpr() instanceof IntegerLiteral
14+
or
15+
// creating an array wit an initializer like `new byte[] { 1, 2 }`
16+
forex(Expr element | element = array.getInit().getAnInit() |
1317
element instanceof CompileTimeConstantExpr
1418
)
1519
}

java/ql/test/experimental/query-tests/security/CWE-1204/StaticInitializationVector.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,54 @@ public byte[] encryptWithStaticIvByteArray(byte[] key, byte[] plaintext) throws
5050
return cipher.doFinal();
5151
}
5252

53+
// BAD: AES-GCM with static IV from a multidimensional byte array
54+
public byte[] encryptWithOneOfStaticIvs01(byte[] key, byte[] plaintext) throws Exception {
55+
byte[][] staticIvs = new byte[][] {
56+
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5 },
57+
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 42 }
58+
};
59+
60+
GCMParameterSpec ivSpec = new GCMParameterSpec(128, staticIvs[1]);
61+
SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
62+
63+
Cipher cipher = Cipher.getInstance("AES/GCM/PKCS5PADDING");
64+
cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec); // $staticInitializationVector
65+
cipher.update(plaintext);
66+
return cipher.doFinal();
67+
}
68+
69+
// BAD: AES-GCM with static IV from a multidimensional byte array
70+
public byte[] encryptWithOneOfStaticIvs02(byte[] key, byte[] plaintext) throws Exception {
71+
byte[][] staticIvs = new byte[][] {
72+
new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5 },
73+
new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 42 }
74+
};
75+
76+
GCMParameterSpec ivSpec = new GCMParameterSpec(128, staticIvs[1]);
77+
SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
78+
79+
Cipher cipher = Cipher.getInstance("AES/GCM/PKCS5PADDING");
80+
cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec); // $staticInitializationVector
81+
cipher.update(plaintext);
82+
return cipher.doFinal();
83+
}
84+
85+
// BAD: AES-GCM with static IV from a multidimensional byte array
86+
public byte[] encryptWithOneOfStaticZeroIvs(byte[] key, byte[] plaintext) throws Exception {
87+
byte[][] ivs = new byte[][] {
88+
new byte[8],
89+
new byte[16]
90+
};
91+
92+
GCMParameterSpec ivSpec = new GCMParameterSpec(128, ivs[1]);
93+
SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
94+
95+
Cipher cipher = Cipher.getInstance("AES/GCM/PKCS5PADDING");
96+
cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec); // $staticInitializationVector
97+
cipher.update(plaintext);
98+
return cipher.doFinal();
99+
}
100+
53101
// GOOD: AES-GCM with a random IV
54102
public byte[] encryptWithRandomIv(byte[] key, byte[] plaintext) throws Exception {
55103
byte[] iv = new byte[16];

0 commit comments

Comments
 (0)