Skip to content

Commit 2288eab

Browse files
committed
Java: Refactor BrokenCryptoAlgorithm, MaybeBrokenCryptoAlgorithm
1 parent 5093589 commit 2288eab

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

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

+11-10
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import java
1515
import semmle.code.java.security.Encryption
1616
import semmle.code.java.dataflow.TaintTracking
1717
import DataFlow
18-
import PathGraph
1918

2019
private class ShortStringLiteral extends StringLiteral {
2120
ShortStringLiteral() { getValue().length() < 100 }
@@ -29,24 +28,26 @@ class BrokenAlgoLiteral extends ShortStringLiteral {
2928
}
3029
}
3130

32-
class InsecureCryptoConfiguration extends TaintTracking::Configuration {
33-
InsecureCryptoConfiguration() { this = "BrokenCryptoAlgortihm::InsecureCryptoConfiguration" }
31+
module InsecureCryptoConfiguration implements ConfigSig {
32+
predicate isSource(Node n) { n.asExpr() instanceof BrokenAlgoLiteral }
3433

35-
override predicate isSource(Node n) { n.asExpr() instanceof BrokenAlgoLiteral }
34+
predicate isSink(Node n) { exists(CryptoAlgoSpec c | n.asExpr() = c.getAlgoSpec()) }
3635

37-
override predicate isSink(Node n) { exists(CryptoAlgoSpec c | n.asExpr() = c.getAlgoSpec()) }
38-
39-
override predicate isSanitizer(DataFlow::Node node) {
36+
predicate isBarrier(DataFlow::Node node) {
4037
node.getType() instanceof PrimitiveType or node.getType() instanceof BoxedType
4138
}
4239
}
4340

41+
module InsecureCryptoFlow = TaintTracking::Make<InsecureCryptoConfiguration>;
42+
43+
import InsecureCryptoFlow::PathGraph
44+
4445
from
45-
PathNode source, PathNode sink, CryptoAlgoSpec c, BrokenAlgoLiteral s,
46-
InsecureCryptoConfiguration conf
46+
InsecureCryptoFlow::PathNode source, InsecureCryptoFlow::PathNode sink, CryptoAlgoSpec c,
47+
BrokenAlgoLiteral s
4748
where
4849
sink.getNode().asExpr() = c.getAlgoSpec() and
4950
source.getNode().asExpr() = s and
50-
conf.hasFlowPath(source, sink)
51+
InsecureCryptoFlow::hasFlowPath(source, sink)
5152
select c, source, sink, "Cryptographic algorithm $@ is weak and should not be used.", s,
5253
s.getValue()

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

+11-10
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import semmle.code.java.security.Encryption
1616
import semmle.code.java.dataflow.TaintTracking
1717
import DataFlow
1818
import semmle.code.java.dispatch.VirtualDispatch
19-
import PathGraph
2019

2120
private class ShortStringLiteral extends StringLiteral {
2221
ShortStringLiteral() { this.getValue().length() < 100 }
@@ -51,26 +50,28 @@ class StringContainer extends RefType {
5150
}
5251
}
5352

54-
class InsecureCryptoConfiguration extends TaintTracking::Configuration {
55-
InsecureCryptoConfiguration() { this = "InsecureCryptoConfiguration" }
53+
module InsecureCryptoConfiguration implements ConfigSig {
54+
predicate isSource(Node n) { n.asExpr() instanceof InsecureAlgoLiteral }
5655

57-
override predicate isSource(Node n) { n.asExpr() instanceof InsecureAlgoLiteral }
56+
predicate isSink(Node n) { exists(CryptoAlgoSpec c | n.asExpr() = c.getAlgoSpec()) }
5857

59-
override predicate isSink(Node n) { exists(CryptoAlgoSpec c | n.asExpr() = c.getAlgoSpec()) }
60-
61-
override predicate isSanitizer(Node n) {
58+
predicate isBarrier(Node n) {
6259
objectToString(n.asExpr()) or
6360
not n.getType().getErasure() instanceof StringContainer
6461
}
6562
}
6663

64+
module InsecureCryptoFlow = TaintTracking::Make<InsecureCryptoConfiguration>;
65+
66+
import InsecureCryptoFlow::PathGraph
67+
6768
from
68-
PathNode source, PathNode sink, CryptoAlgoSpec c, InsecureAlgoLiteral s,
69-
InsecureCryptoConfiguration conf
69+
InsecureCryptoFlow::PathNode source, InsecureCryptoFlow::PathNode sink, CryptoAlgoSpec c,
70+
InsecureAlgoLiteral s
7071
where
7172
sink.getNode().asExpr() = c.getAlgoSpec() and
7273
source.getNode().asExpr() = s and
73-
conf.hasFlowPath(source, sink)
74+
InsecureCryptoFlow::hasFlowPath(source, sink)
7475
select c, source, sink,
7576
"Cryptographic algorithm $@ may not be secure, consider using a different algorithm.", s,
7677
s.getValue()

0 commit comments

Comments
 (0)