Skip to content

Commit 29de0c6

Browse files
Jami CogswellJami Cogswell
authored andcommitted
make one config for asymm with flow states; seems to work...
1 parent 3e8748e commit 29de0c6

3 files changed

Lines changed: 91 additions & 8 deletions

File tree

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

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,84 @@
22

33
import semmle.code.java.security.Encryption
44
import semmle.code.java.dataflow.DataFlow
5+
import semmle.code.java.dataflow.TaintTracking
6+
7+
//import semmle.code.java.dataflow.internal.DataFlowImplCommonPublic
8+
//import semmle.code.java.dataflow.FlowSources
9+
//import semmle.code.java.dataflow.internal.DataFlowNodes
10+
/**
11+
* An Asymmetric (RSA, DSA, DH) key length data flow tracking configuration.
12+
*/
13+
class AsymmetricKeyTrackingConfiguration extends DataFlow::Configuration {
14+
AsymmetricKeyTrackingConfiguration() { this = "AsymmetricKeyTrackingConfiguration" }
15+
16+
override predicate isSource(DataFlow::Node source, DataFlow::FlowState state) {
17+
//state instanceof DataFlow::FlowStateEmpty and
18+
source.asExpr().(IntegerLiteral).getIntValue() < 2048 and state = "2048"
19+
or
20+
source.asExpr().(IntegerLiteral).getIntValue() < 256 and state = "256"
21+
or
22+
getECKeySize(source.asExpr().(StringLiteral).getValue()) < 256 and state = "256" // need this for the cases when the key size is embedded in the curve name.
23+
}
24+
25+
override predicate isSink(DataFlow::Node sink, DataFlow::FlowState state) {
26+
exists(MethodAccess ma, JavaSecurityKeyPairGenerator jpg |
27+
ma.getMethod() instanceof KeyPairGeneratorInitMethod and
28+
(
29+
jpg.getAlgoSpec().(StringLiteral).getValue().toUpperCase().matches(["RSA", "DSA", "DH"]) and
30+
DataFlow::localExprFlow(jpg, ma.getQualifier()) and
31+
sink.asExpr() = ma.getArgument(0) and
32+
//ma.getArgument(0).(LocalSourceNode).flowsTo(sink) and
33+
//ma.getArgument(0).(CompileTimeConstantExpr).getIntValue() < 2048 and
34+
state = "2048"
35+
)
36+
or
37+
jpg.getAlgoSpec().(StringLiteral).getValue().toUpperCase().matches("EC%") and
38+
DataFlow::localExprFlow(jpg, ma.getQualifier()) and
39+
sink.asExpr() = ma.getArgument(0) and
40+
//ma.getArgument(0).(CompileTimeConstantExpr).getIntValue() < 256 and
41+
state = "256"
42+
)
43+
or
44+
// TODO: combine below three for less duplicated code
45+
exists(ClassInstanceExpr rsaKeyGenParamSpec |
46+
rsaKeyGenParamSpec.getConstructedType() instanceof RsaKeyGenParameterSpec and
47+
sink.asExpr() = rsaKeyGenParamSpec.getArgument(0) and
48+
state = "2048"
49+
)
50+
or
51+
exists(ClassInstanceExpr dsaGenParamSpec |
52+
dsaGenParamSpec.getConstructedType() instanceof DsaGenParameterSpec and
53+
sink.asExpr() = dsaGenParamSpec.getArgument(0) and
54+
state = "2048"
55+
)
56+
or
57+
exists(ClassInstanceExpr dhGenParamSpec |
58+
dhGenParamSpec.getConstructedType() instanceof DhGenParameterSpec and
59+
sink.asExpr() = dhGenParamSpec.getArgument(0) and
60+
state = "2048"
61+
)
62+
or
63+
exists(ClassInstanceExpr ecGenParamSpec |
64+
ecGenParamSpec.getConstructedType() instanceof EcGenParameterSpec and
65+
sink.asExpr() = ecGenParamSpec.getArgument(0) and
66+
state = "256"
67+
)
68+
}
69+
70+
override predicate isAdditionalFlowStep(
71+
DataFlow::Node node1, DataFlow::FlowState state1, DataFlow::Node node2,
72+
DataFlow::FlowState state2
73+
) {
74+
exists(IntegerLiteral intLiteral |
75+
state1 = "" and
76+
state2 = intLiteral.toString() and
77+
node1.asExpr() = intLiteral and
78+
node2.asExpr() = intLiteral
79+
//intLiteral.toString().toInt() = 64 // test viability of this craziness
80+
)
81+
}
82+
}
583

684
/**
785
* An Asymmetric (RSA, DSA, DH) key length data flow tracking configuration.

java/ql/src/Security/CWE/CWE-326/InsufficientKeySize.ql

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ import DataFlow::PathGraph
1717

1818
from DataFlow::PathNode source, DataFlow::PathNode sink
1919
where
20-
exists(AsymmetricNonECKeyTrackingConfiguration config1 | config1.hasFlowPath(source, sink)) or
21-
exists(AsymmetricECKeyTrackingConfiguration config2 | config2.hasFlowPath(source, sink)) or
20+
exists(AsymmetricKeyTrackingConfiguration config1 | config1.hasFlowPath(source, sink))
21+
or
22+
// exists(AsymmetricNonECKeyTrackingConfiguration config1 | config1.hasFlowPath(source, sink)) or
23+
// exists(AsymmetricECKeyTrackingConfiguration config2 | config2.hasFlowPath(source, sink)) or
2224
exists(SymmetricKeyTrackingConfiguration config3 | config3.hasFlowPath(source, sink))
2325
select sink.getNode(), source, sink, "This $@ is too small.", source.getNode(), "key size"

java/ql/test/query-tests/security/CWE-326/InsufficientKeySizeTest.ql

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import java
22
import TestUtilities.InlineExpectationsTest
33
import semmle.code.java.security.InsufficientKeySizeQuery
4+
import DataFlow::PathGraph
45

56
class InsufficientKeySizeTest extends InlineExpectationsTest {
67
InsufficientKeySizeTest() { this = "InsufficientKeySize" }
@@ -9,13 +10,15 @@ class InsufficientKeySizeTest extends InlineExpectationsTest {
910

1011
override predicate hasActualResult(Location location, string element, string tag, string value) {
1112
tag = "hasInsufficientKeySize" and
12-
exists(DataFlow::Node source, DataFlow::Node sink |
13-
exists(AsymmetricNonECKeyTrackingConfiguration config1 | config1.hasFlow(source, sink)) or
14-
exists(AsymmetricECKeyTrackingConfiguration config2 | config2.hasFlow(source, sink)) or
15-
exists(SymmetricKeyTrackingConfiguration config3 | config3.hasFlow(source, sink))
13+
exists(DataFlow::PathNode source, DataFlow::PathNode sink |
14+
exists(AsymmetricKeyTrackingConfiguration config1 | config1.hasFlowPath(source, sink))
15+
or
16+
// exists(AsymmetricNonECKeyTrackingConfiguration config1 | config1.hasFlowPath(source, sink)) or
17+
// exists(AsymmetricECKeyTrackingConfiguration config2 | config2.hasFlowPath(source, sink)) or
18+
exists(SymmetricKeyTrackingConfiguration config3 | config3.hasFlowPath(source, sink))
1619
|
17-
sink.getLocation() = location and
18-
element = sink.toString() and
20+
sink.getNode().getLocation() = location and
21+
element = sink.getNode().toString() and
1922
value = ""
2023
)
2124
}

0 commit comments

Comments
 (0)