Skip to content

Commit e65a54b

Browse files
committed
Add BrokenCryptoAlgorithmQuery
1 parent 4b76564 commit e65a54b

File tree

2 files changed

+45
-34
lines changed

2 files changed

+45
-34
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/** Provides to taint-tracking configuration to reason about the use of broken or risky cryptographic algorithms. */
2+
3+
import java
4+
import semmle.code.java.security.Encryption
5+
import semmle.code.java.dataflow.TaintTracking
6+
7+
private class ShortStringLiteral extends StringLiteral {
8+
ShortStringLiteral() { this.getValue().length() < 100 }
9+
}
10+
11+
/**
12+
* A string literal that may refer to a broken or risky cryptographic algorithm.
13+
*/
14+
class BrokenAlgoLiteral extends ShortStringLiteral {
15+
BrokenAlgoLiteral() {
16+
this.getValue().regexpMatch(getInsecureAlgorithmRegex()) and
17+
// Exclude German and French sentences.
18+
not this.getValue().regexpMatch(".*\\p{IsLowercase} des \\p{IsLetter}.*")
19+
}
20+
}
21+
22+
/**
23+
* A taint-tracking configuration to reason about the use of broken or risky cryptographic algorithms.
24+
*/
25+
module InsecureCryptoConfig implements DataFlow::ConfigSig {
26+
predicate isSource(DataFlow::Node n) { n.asExpr() instanceof BrokenAlgoLiteral }
27+
28+
predicate isSink(DataFlow::Node n) { exists(CryptoAlgoSpec c | n.asExpr() = c.getAlgoSpec()) }
29+
30+
predicate isBarrier(DataFlow::Node node) {
31+
node.getType() instanceof PrimitiveType or node.getType() instanceof BoxedType
32+
}
33+
}
34+
35+
/**
36+
* Taint-tracking flow for use of broken or risky cryptographic algorithms.
37+
*/
38+
module InsecureCryptoFlow = TaintTracking::Global<InsecureCryptoConfig>;

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

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,42 +12,15 @@
1212
*/
1313

1414
import java
15-
import semmle.code.java.security.Encryption
16-
import semmle.code.java.dataflow.TaintTracking
17-
import DataFlow
18-
19-
private class ShortStringLiteral extends StringLiteral {
20-
ShortStringLiteral() { this.getValue().length() < 100 }
21-
}
22-
23-
class BrokenAlgoLiteral extends ShortStringLiteral {
24-
BrokenAlgoLiteral() {
25-
this.getValue().regexpMatch(getInsecureAlgorithmRegex()) and
26-
// Exclude German and French sentences.
27-
not this.getValue().regexpMatch(".*\\p{IsLowercase} des \\p{IsLetter}.*")
28-
}
29-
}
30-
31-
module InsecureCryptoConfig implements ConfigSig {
32-
predicate isSource(Node n) { n.asExpr() instanceof BrokenAlgoLiteral }
33-
34-
predicate isSink(Node n) { exists(CryptoAlgoSpec c | n.asExpr() = c.getAlgoSpec()) }
35-
36-
predicate isBarrier(DataFlow::Node node) {
37-
node.getType() instanceof PrimitiveType or node.getType() instanceof BoxedType
38-
}
39-
}
40-
41-
module InsecureCryptoFlow = TaintTracking::Global<InsecureCryptoConfig>;
42-
15+
import semmle.code.java.security.BrokenCryptoAlgorithmQuery
4316
import InsecureCryptoFlow::PathGraph
4417

4518
from
46-
InsecureCryptoFlow::PathNode source, InsecureCryptoFlow::PathNode sink, CryptoAlgoSpec c,
47-
BrokenAlgoLiteral s
19+
InsecureCryptoFlow::PathNode source, InsecureCryptoFlow::PathNode sink, CryptoAlgoSpec spec,
20+
BrokenAlgoLiteral algo
4821
where
49-
sink.getNode().asExpr() = c.getAlgoSpec() and
50-
source.getNode().asExpr() = s and
22+
sink.getNode().asExpr() = spec.getAlgoSpec() and
23+
source.getNode().asExpr() = algo and
5124
InsecureCryptoFlow::flowPath(source, sink)
52-
select c, source, sink, "Cryptographic algorithm $@ is weak and should not be used.", s,
53-
s.getValue()
25+
select spec, source, sink, "Cryptographic algorithm $@ is weak and should not be used.", algo,
26+
algo.getValue()

0 commit comments

Comments
 (0)