Skip to content

Commit 4b241d7

Browse files
committed
Crypto: adding initial weak hash query overhaul and tests, but no expected file yet.
1 parent 08abdb8 commit 4b241d7

File tree

4 files changed

+60
-7
lines changed

4 files changed

+60
-7
lines changed

java/ql/src/experimental/quantum/Examples/WeakHash.ql

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,21 @@ from Crypto::HashAlgorithmNode alg, Crypto::HashType htype, string msg
1717
where
1818
htype = alg.getHashType() and
1919
(
20-
(htype != Crypto::SHA2() and htype != Crypto::SHA2()) and
21-
msg = "Use of unapproved hash algorithm or API " + htype.toString() + "."
20+
(htype != Crypto::SHA2() and htype != Crypto::SHA3()) and
21+
msg = "Use of unapproved hash algorithm or API: " + htype.toString() + "."
2222
or
2323
(htype = Crypto::SHA2() or htype = Crypto::SHA3()) and
2424
not exists(alg.getDigestLength()) and
2525
msg =
2626
"Use of approved hash algorithm or API type " + htype.toString() + " but unknown digest size."
2727
or
28-
(htype = Crypto::SHA2() or htype = Crypto::SHA3()) and
29-
alg.getDigestLength() < 256 and
30-
msg =
31-
"Use of approved hash algorithm or API type " + htype.toString() + " but weak digest size (" +
32-
alg.getDigestLength() + ")."
28+
exists(int digestLength |
29+
digestLength = alg.getDigestLength() and
30+
(htype = Crypto::SHA2() or htype = Crypto::SHA3()) and
31+
digestLength < 256 and
32+
msg =
33+
"Use of approved hash algorithm or API type " + htype.toString() + " but weak digest size ("
34+
+ digestLength + ")."
35+
)
3336
)
3437
select alg, msg
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
query: experimental/quantum/Examples/WeakHash.ql
2+
postprocess:
3+
- utils/test/PrettyPrintModels.ql
4+
- utils/test/InlineExpectationsTestQuery.ql
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package test.cwe327.semmle.tests;
2+
3+
import java.util.Properties;
4+
import java.io.FileInputStream;
5+
import java.io.IOException;
6+
import java.security.MessageDigest;
7+
import java.security.NoSuchAlgorithmException;
8+
9+
public class WeakHashing {
10+
void hashing() throws NoSuchAlgorithmException, IOException {
11+
java.util.Properties props = new java.util.Properties();
12+
props.load(new FileInputStream("example.properties"));
13+
14+
// BAD: Using a weak hashing algorithm even with a secure default
15+
MessageDigest bad = MessageDigest.getInstance(props.getProperty("hashAlg1"));
16+
17+
// BAD: Using a weak hashing algorithm even with a secure default
18+
MessageDigest bad2 = MessageDigest.getInstance(props.getProperty("hashAlg1", "SHA-256"));
19+
20+
// BAD: Using a strong hashing algorithm but with a weak default
21+
MessageDigest bad3 = MessageDigest.getInstance(props.getProperty("hashAlg2", "MD5"));
22+
23+
// BAD: Property does not exist and default (used value) is unknown
24+
MessageDigest bad4 = MessageDigest.getInstance(props.getProperty("non-existent_property", "non-existent_default"));
25+
26+
// GOOD: Using a strong hashing algorithm
27+
MessageDigest ok = MessageDigest.getInstance(props.getProperty("hashAlg2"));
28+
29+
// BAD?: Property does not exist (considered unknown) and but default is secure
30+
MessageDigest ok2 = MessageDigest.getInstance(props.getProperty("non-existent-property", "SHA-256"));
31+
32+
// GOOD: Using a strong hashing algorithm
33+
MessageDigest ok3 = MessageDigest.getInstance("SHA3-512");
34+
35+
// GOOD: Using a strong hashing algorithm
36+
MessageDigest ok4 = MessageDigest.getInstance("SHA384");
37+
38+
props.load(new FileInputStream("unobserved-file.properties"));
39+
40+
// BAD: "hashalg1" is not visible since the file isn't known, this is an 'unknown' hash
41+
// False positive/negative
42+
MessageDigest bad5 = MessageDigest.getInstance(props.getProperty("hashAlg1", "SHA-256"));
43+
}
44+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
hashAlg1=MD5
2+
hashAlg2=SHA-256

0 commit comments

Comments
 (0)