Skip to content

Commit be50696

Browse files
authored
Merge pull request #100 from atorralba/atorralba/java/weak-hashing-suggestion
Java: Generalize MaybeBrokenCryptoAlgorithmQuery.qll
2 parents 8051cfc + 8ad787f commit be50696

File tree

2 files changed

+29
-29
lines changed

2 files changed

+29
-29
lines changed

java/ql/lib/semmle/code/java/security/MaybeBrokenCryptoAlgorithmQuery.qll

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,47 @@ private import semmle.code.java.dataflow.RangeUtils
1010
private import semmle.code.java.dispatch.VirtualDispatch
1111
private import semmle.code.java.frameworks.Properties
1212

13+
/** A reference to an insecure cryptographic algorithm. */
14+
abstract class InsecureAlgorithm extends Expr {
15+
/** Gets the string representation of this insecure cryptographic algorithm. */
16+
abstract string getStringValue();
17+
}
18+
1319
private class ShortStringLiteral extends StringLiteral {
1420
ShortStringLiteral() { this.getValue().length() < 100 }
1521
}
1622

1723
/**
1824
* A string literal that may refer to an insecure cryptographic algorithm.
1925
*/
20-
class InsecureAlgoLiteral extends ShortStringLiteral {
26+
class InsecureAlgoLiteral extends InsecureAlgorithm, ShortStringLiteral {
2127
InsecureAlgoLiteral() {
22-
// Algorithm identifiers should be at least two characters.
23-
this.getValue().length() > 1 and
2428
exists(string s | s = this.getValue() |
29+
// Algorithm identifiers should be at least two characters.
30+
s.length() > 1 and
2531
not s.regexpMatch(getSecureAlgorithmRegex()) and
2632
// Exclude results covered by another query.
2733
not s.regexpMatch(getInsecureAlgorithmRegex())
2834
)
2935
}
36+
37+
override string getStringValue() { result = this.getValue() }
38+
}
39+
40+
/**
41+
* A property access that may refer to an insecure cryptographic algorithm.
42+
*/
43+
class InsecureAlgoProperty extends InsecureAlgorithm, PropertiesGetPropertyMethodCall {
44+
string value;
45+
46+
InsecureAlgoProperty() {
47+
value = this.getPropertyValue() and
48+
// Since properties pairs are not included in the java/weak-cryptographic-algorithm,
49+
// the check for values from properties files can be less strict than `InsecureAlgoLiteral`.
50+
not value.regexpMatch(getSecureAlgorithmRegex())
51+
}
52+
53+
override string getStringValue() { result = value }
3054
}
3155

3256
private predicate objectToString(MethodCall ma) {
@@ -41,17 +65,7 @@ private predicate objectToString(MethodCall ma) {
4165
* A taint-tracking configuration to reason about the use of potentially insecure cryptographic algorithms.
4266
*/
4367
module InsecureCryptoConfig implements DataFlow::ConfigSig {
44-
predicate isSource(DataFlow::Node n) {
45-
n.asExpr() instanceof InsecureAlgoLiteral
46-
or
47-
exists(PropertiesGetPropertyMethodCall mc, string value |
48-
n.asExpr() = mc and value = mc.getPropertyValue()
49-
|
50-
// Since properties pairs are not included in the java/weak-crypto-algorithm,
51-
// The check for values from properties files can be less strict than `InsecureAlgoLiteral`.
52-
not value.regexpMatch(getSecureAlgorithmRegex())
53-
)
54-
}
68+
predicate isSource(DataFlow::Node n) { n.asExpr() instanceof InsecureAlgorithm }
5569

5670
predicate isSink(DataFlow::Node n) { exists(CryptoAlgoSpec c | n.asExpr() = c.getAlgoSpec()) }
5771

java/ql/src/Security/CWE/CWE-327/MaybeBrokenCryptoAlgorithm.ql

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,10 @@ import semmle.code.java.frameworks.Properties
1818
import semmle.code.java.security.MaybeBrokenCryptoAlgorithmQuery
1919
import InsecureCryptoFlow::PathGraph
2020

21-
/**
22-
* Get the string value represented by the given expression.
23-
*
24-
* If the value is a string literal, get the literal value.
25-
* If the value is a call to `java.util.Properties::getProperty`, get the potential values of the property.
26-
*/
27-
string getStringValue(DataFlow::Node algo) {
28-
result = algo.asExpr().(StringLiteral).getValue()
29-
or
30-
exists(string value | value = algo.asExpr().(PropertiesGetPropertyMethodCall).getPropertyValue() |
31-
result = value and not value.regexpMatch(getSecureAlgorithmRegex())
32-
)
33-
}
34-
3521
from InsecureCryptoFlow::PathNode source, InsecureCryptoFlow::PathNode sink, CryptoAlgoSpec c
3622
where
3723
sink.getNode().asExpr() = c.getAlgoSpec() and
3824
InsecureCryptoFlow::flowPath(source, sink)
3925
select c, source, sink,
4026
"Cryptographic algorithm $@ may not be secure, consider using a different algorithm.", source,
41-
getStringValue(source.getNode())
27+
source.getNode().asExpr().(InsecureAlgorithm).getStringValue()

0 commit comments

Comments
 (0)