Skip to content

Commit de69827

Browse files
Use a full dataflow config rather than local flow
1 parent fe5a61b commit de69827

File tree

4 files changed

+35
-27
lines changed

4 files changed

+35
-27
lines changed

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,20 @@ import java
44
import Encryption
55
import semmle.code.java.dataflow.DataFlow
66

7-
/** Holds if `c` is a call which initialises an RSA cipher without using OAEP padding. */
8-
predicate rsaWithoutOaepCall(CryptoAlgoSpec c) {
9-
exists(CompileTimeConstantExpr specExpr, string spec |
10-
specExpr.getStringValue() = spec and
11-
DataFlow::localExprFlow(specExpr, c.getAlgoSpec()) and
12-
spec.matches("RSA/%") and
13-
not spec.matches("%OAEP%")
14-
)
7+
/** A configuration for finding RSA ciphers initialized without using OAEP padding. */
8+
class RsaWithoutOaepConfig extends DataFlow::Configuration {
9+
RsaWithoutOaepConfig() { this = "RsaWithoutOaepConfig" }
10+
11+
override predicate isSource(DataFlow::Node src) {
12+
exists(CompileTimeConstantExpr specExpr, string spec |
13+
specExpr.getStringValue() = spec and
14+
specExpr = src.asExpr() and
15+
spec.matches("RSA/%") and
16+
not spec.matches("%OAEP%")
17+
)
18+
}
19+
20+
override predicate isSink(DataFlow::Node sink) {
21+
exists(CryptoAlgoSpec cr | sink.asExpr() = cr.getAlgoSpec())
22+
}
1523
}
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @name Use of RSA algorithm without OAEP
33
* @description Using RSA encryption without OAEP padding can lead to a padding oracle attack, weakening the encryption.
4-
* @kind problem
4+
* @kind path-problem
55
* @problem.severity warning
66
* @security-severity 7.5
77
* @precision high
@@ -11,9 +11,10 @@
1111
*/
1212

1313
import java
14-
import semmle.code.java.security.Encryption
1514
import semmle.code.java.security.RsaWithoutOaepQuery
15+
import DataFlow::PathGraph
1616

17-
from CryptoAlgoSpec c
18-
where rsaWithoutOaepCall(c)
19-
select c, "This instance of RSA does not use OAEP padding."
17+
from RsaWithoutOaepConfig conf, DataFlow::Node source, DataFlow::Node sink
18+
where conf.hasFlow(source, sink)
19+
select source, source, sink,
20+
"This specification is used to initialize an RSA cipher without OAEP padding $@.", sink, "here"

java/ql/test/query-tests/security/CWE-780/RsaWithoutOaepTest.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,16 @@
22

33
class RsaWithoutOaep {
44
public void test() throws Exception {
5-
Cipher rsaBad = Cipher.getInstance("RSA/ECB/NoPadding"); // $hasResult
5+
Cipher rsaBad = Cipher.getInstance("RSA/ECB/NoPadding"); // $hasTaintFlow
66

77
Cipher rsaGood = Cipher.getInstance("RSA/ECB/OAEPWithSHA-1AndMGF1Padding");
88
}
9+
10+
public Cipher getCipher(String spec) throws Exception {
11+
return Cipher.getInstance(spec); // $hasTaintFlow
12+
}
13+
14+
public void test2() throws Exception {
15+
Cipher rsa = getCipher("RSA/ECB/NoPadding");
16+
}
917
}
Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
11
import java
22
import TestUtilities.InlineExpectationsTest
3+
import TestUtilities.InlineFlowTest
34
import semmle.code.java.security.RsaWithoutOaepQuery
45

5-
class HasResult extends InlineExpectationsTest {
6-
HasResult() { this = "HasResult" }
6+
class HasFlowTest extends InlineFlowTest {
7+
override DataFlow::Configuration getTaintFlowConfig() { result instanceof RsaWithoutOaepConfig }
78

8-
override string getARelevantTag() { result = "hasResult" }
9-
10-
override predicate hasActualResult(Location location, string element, string tag, string value) {
11-
tag = "hasResult" and
12-
value = "" and
13-
exists(CryptoAlgoSpec c |
14-
rsaWithoutOaepCall(c) and
15-
location = c.getLocation() and
16-
element = c.toString()
17-
)
18-
}
9+
override DataFlow::Configuration getValueFlowConfig() { none() }
1910
}

0 commit comments

Comments
 (0)