Skip to content

Commit c359852

Browse files
Consider only Cipher.ENCRYPT_MODE in NonConstantTimeCryptoComparison.ql
1 parent 1f2a9cd commit c359852

File tree

1 file changed

+45
-18
lines changed

1 file changed

+45
-18
lines changed

java/ql/src/experimental/Security/CWE/CWE-208/NonConstantTimeCryptoComparison.ql

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,10 @@ import java
1515
import semmle.code.java.controlflow.Guards
1616
import semmle.code.java.dataflow.TaintTracking
1717
import semmle.code.java.dataflow.TaintTracking2
18+
import semmle.code.java.dataflow.DataFlow3
1819
import semmle.code.java.dataflow.FlowSources
1920
import DataFlow::PathGraph
2021

21-
private class ByteBuffer extends Class {
22-
ByteBuffer() { hasQualifiedName("java.nio", "ByteBuffer") }
23-
}
24-
2522
/** A method call that produces cryptographic result. */
2623
abstract private class ProduceCryptoCall extends MethodAccess {
2724
Expr output;
@@ -54,23 +51,51 @@ private class ProduceSignatureCall extends ProduceCryptoCall {
5451
}
5552
}
5653

54+
/**
55+
* A config that tracks data flow from initializing a cipher for encryption
56+
* to producing a ciphertext using this cipher.
57+
*/
58+
private class InitializeEncryptorConfig extends DataFlow3::Configuration {
59+
InitializeEncryptorConfig() { this = "InitializeEncryptorConfig" }
60+
61+
override predicate isSource(DataFlow::Node source) {
62+
exists(MethodAccess ma |
63+
ma.getMethod().hasQualifiedName("javax.crypto", "Cipher", "init") and
64+
ma.getArgument(0).(VarAccess).getVariable().hasName("ENCRYPT_MODE") and
65+
ma.getQualifier() = source.asExpr()
66+
)
67+
}
68+
69+
override predicate isSink(DataFlow::Node sink) {
70+
exists(MethodAccess ma |
71+
ma.getMethod().hasQualifiedName("javax.crypto", "Cipher", "doFinal") and
72+
ma.getQualifier() = sink.asExpr()
73+
)
74+
}
75+
}
76+
5777
/** A method call that produces a ciphertext. */
5878
private class ProduceCiphertextCall extends ProduceCryptoCall {
5979
ProduceCiphertextCall() {
60-
getMethod().getDeclaringType().hasQualifiedName("javax.crypto", "Cipher") and
61-
(
62-
getMethod().hasStringSignature(["doFinal()", "doFinal(byte[])", "doFinal(byte[], int, int)"]) and
63-
this = output
64-
or
65-
getMethod().hasStringSignature("doFinal(byte[], int)") and getArgument(0) = output
66-
or
67-
getMethod()
68-
.hasStringSignature([
69-
"doFinal(byte[], int, int, byte[])", "doFinal(byte[], int, int, byte[], int)"
70-
]) and
71-
getArgument(3) = output
72-
or
73-
getMethod().hasStringSignature("doFinal(ByteBuffer, ByteBuffer)") and getArgument(1) = output
80+
exists(Method m | m = this.getMethod() |
81+
m.getDeclaringType().hasQualifiedName("javax.crypto", "Cipher") and
82+
(
83+
m.hasStringSignature(["doFinal()", "doFinal(byte[])", "doFinal(byte[], int, int)"]) and
84+
this = output
85+
or
86+
m.hasStringSignature("doFinal(byte[], int)") and getArgument(0) = output
87+
or
88+
m.hasStringSignature([
89+
"doFinal(byte[], int, int, byte[])", "doFinal(byte[], int, int, byte[], int)"
90+
]) and
91+
getArgument(3) = output
92+
or
93+
m.hasStringSignature("doFinal(ByteBuffer, ByteBuffer)") and
94+
getArgument(1) = output
95+
)
96+
) and
97+
exists(InitializeEncryptorConfig config |
98+
config.hasFlowTo(DataFlow3::exprNode(this.getQualifier()))
7499
)
75100
}
76101
}
@@ -168,6 +193,7 @@ private class CryptoOperationSource extends DataFlow::Node {
168193
}
169194
}
170195

196+
/** Methods that use a non-constant-time algorithm for comparing inputs. */
171197
private class NonConstantTimeEqualsCall extends MethodAccess {
172198
NonConstantTimeEqualsCall() {
173199
getMethod()
@@ -176,6 +202,7 @@ private class NonConstantTimeEqualsCall extends MethodAccess {
176202
}
177203
}
178204

205+
/** Static methods that use a non-constant-time algorithm for comparing inputs. */
179206
private class NonConstantTimeComparisonCall extends StaticMethodAccess {
180207
NonConstantTimeComparisonCall() {
181208
getMethod().hasQualifiedName("java.util", "Arrays", ["equals", "deepEquals"]) or

0 commit comments

Comments
 (0)