Skip to content

Commit 4b76564

Browse files
committed
Add MaybeBrokenCryptoAlgorithmQuery
1 parent e4f47ec commit 4b76564

File tree

2 files changed

+64
-50
lines changed

2 files changed

+64
-50
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* Provides classes and a taint-tracking configuration to reason about the use of potentially insecure cryptographic algorithms.
3+
*/
4+
5+
import java
6+
private import semmle.code.java.security.Encryption
7+
private import semmle.code.java.dataflow.TaintTracking
8+
private import semmle.code.java.dispatch.VirtualDispatch
9+
10+
private class ShortStringLiteral extends StringLiteral {
11+
ShortStringLiteral() { this.getValue().length() < 100 }
12+
}
13+
14+
/**
15+
* A string literal that may refer to an insecure cryptographic algorithm.
16+
*/
17+
class InsecureAlgoLiteral extends ShortStringLiteral {
18+
InsecureAlgoLiteral() {
19+
// Algorithm identifiers should be at least two characters.
20+
this.getValue().length() > 1 and
21+
exists(string s | s = this.getValue() |
22+
not s.regexpMatch(getSecureAlgorithmRegex()) and
23+
// Exclude results covered by another query.
24+
not s.regexpMatch(getInsecureAlgorithmRegex())
25+
)
26+
}
27+
}
28+
29+
private predicate objectToString(MethodAccess ma) {
30+
exists(ToStringMethod m |
31+
m = ma.getMethod() and
32+
m.getDeclaringType() instanceof TypeObject and
33+
DataFlow::exprNode(ma.getQualifier()).getTypeBound().getErasure() instanceof TypeObject
34+
)
35+
}
36+
37+
private class StringContainer extends RefType {
38+
StringContainer() {
39+
this instanceof TypeString or
40+
this instanceof StringBuildingType or
41+
this.hasQualifiedName("java.util", "StringTokenizer") or
42+
this.(Array).getComponentType() instanceof StringContainer
43+
}
44+
}
45+
46+
/**
47+
* A taint-tracking configuration to reason about the use of potentially insecure cryptographic algorithms.
48+
*/
49+
module InsecureCryptoConfig implements DataFlow::ConfigSig {
50+
predicate isSource(DataFlow::Node n) { n.asExpr() instanceof InsecureAlgoLiteral }
51+
52+
predicate isSink(DataFlow::Node n) { exists(CryptoAlgoSpec c | n.asExpr() = c.getAlgoSpec()) }
53+
54+
predicate isBarrier(DataFlow::Node n) {
55+
objectToString(n.asExpr()) or
56+
not n.getType().getErasure() instanceof StringContainer
57+
}
58+
}
59+
60+
/**
61+
* Taint-tracking flow for use of potentially insecure cryptographic algorithms.
62+
*/
63+
module InsecureCryptoFlow = TaintTracking::Global<InsecureCryptoConfig>;

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

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -13,56 +13,7 @@
1313

1414
import java
1515
import semmle.code.java.security.Encryption
16-
import semmle.code.java.dataflow.TaintTracking
17-
import DataFlow
18-
import semmle.code.java.dispatch.VirtualDispatch
19-
20-
private class ShortStringLiteral extends StringLiteral {
21-
ShortStringLiteral() { this.getValue().length() < 100 }
22-
}
23-
24-
class InsecureAlgoLiteral extends ShortStringLiteral {
25-
InsecureAlgoLiteral() {
26-
// Algorithm identifiers should be at least two characters.
27-
this.getValue().length() > 1 and
28-
exists(string s | s = this.getValue() |
29-
not s.regexpMatch(getSecureAlgorithmRegex()) and
30-
// Exclude results covered by another query.
31-
not s.regexpMatch(getInsecureAlgorithmRegex())
32-
)
33-
}
34-
}
35-
36-
predicate objectToString(MethodAccess ma) {
37-
exists(ToStringMethod m |
38-
m = ma.getMethod() and
39-
m.getDeclaringType() instanceof TypeObject and
40-
exprNode(ma.getQualifier()).getTypeBound().getErasure() instanceof TypeObject
41-
)
42-
}
43-
44-
class StringContainer extends RefType {
45-
StringContainer() {
46-
this instanceof TypeString or
47-
this instanceof StringBuildingType or
48-
this.hasQualifiedName("java.util", "StringTokenizer") or
49-
this.(Array).getComponentType() instanceof StringContainer
50-
}
51-
}
52-
53-
module InsecureCryptoConfig implements ConfigSig {
54-
predicate isSource(Node n) { n.asExpr() instanceof InsecureAlgoLiteral }
55-
56-
predicate isSink(Node n) { exists(CryptoAlgoSpec c | n.asExpr() = c.getAlgoSpec()) }
57-
58-
predicate isBarrier(Node n) {
59-
objectToString(n.asExpr()) or
60-
not n.getType().getErasure() instanceof StringContainer
61-
}
62-
}
63-
64-
module InsecureCryptoFlow = TaintTracking::Global<InsecureCryptoConfig>;
65-
16+
import semmle.code.java.security.MaybeBrokenCryptoAlgorithmQuery
6617
import InsecureCryptoFlow::PathGraph
6718

6819
from

0 commit comments

Comments
 (0)