Skip to content

Commit 860e8f3

Browse files
Better signatures in java/non-constant-time-crypto-comparison
1 parent 622c7ee commit 860e8f3

File tree

1 file changed

+27
-26
lines changed

1 file changed

+27
-26
lines changed

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

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -32,38 +32,44 @@ abstract private class ProduceCryptoCall extends MethodAccess {
3232
/** A method call that produces a MAC. */
3333
private class ProduceMacCall extends ProduceCryptoCall {
3434
ProduceMacCall() {
35-
getMethod().hasQualifiedName("javax.crypto", "Mac", "doFinal") and
35+
getMethod().getDeclaringType().hasQualifiedName("javax.crypto", "Mac") and
3636
(
37-
getMethod().getReturnType() instanceof Array and this = output
37+
getMethod().hasStringSignature(["doFinal()", "doFinal(byte[])"]) and this = output
3838
or
39-
getMethod().getParameterType(0) instanceof Array and getArgument(0) = output
39+
getMethod().hasStringSignature("doFinal(byte[], int)") and getArgument(0) = output
4040
)
4141
}
4242
}
4343

4444
/** A method call that produces a signature. */
4545
private class ProduceSignatureCall extends ProduceCryptoCall {
4646
ProduceSignatureCall() {
47-
getMethod().hasQualifiedName("java.security", "Signature", "sign") and
47+
getMethod().getDeclaringType().hasQualifiedName("java.security", "Signature") and
4848
(
49-
getMethod().getReturnType() instanceof Array and this = output
49+
getMethod().hasStringSignature("sign()") and this = output
5050
or
51-
getMethod().getParameterType(0) instanceof Array and getArgument(0) = output
51+
getMethod().hasStringSignature("sign(byte[], int, int)") and getArgument(0) = output
5252
)
5353
}
5454
}
5555

5656
/** A method call that produces a ciphertext. */
5757
private class ProduceCiphertextCall extends ProduceCryptoCall {
5858
ProduceCiphertextCall() {
59-
getMethod().hasQualifiedName("javax.crypto", "Cipher", "doFinal") and
59+
getMethod().getDeclaringType().hasQualifiedName("javax.crypto", "Cipher") and
6060
(
61-
getMethod().getReturnType() instanceof Array and this = output
61+
getMethod().hasStringSignature(["doFinal()", "doFinal(byte[])", "doFinal(byte[], int, int)"]) and
62+
this = output
6263
or
63-
getMethod().getParameterType([0, 3]) instanceof Array and getArgument([0, 3]) = output
64+
getMethod().hasStringSignature("doFinal(byte[], int)") and getArgument(0) = output
6465
or
65-
getMethod().getParameterType(1) instanceof ByteBuffer and
66-
getArgument(1) = output
66+
getMethod()
67+
.hasStringSignature([
68+
"doFinal(byte[], int, int, byte[])", "doFinal(byte[], int, int, byte[], int)"
69+
]) and
70+
getArgument(3) = output
71+
or
72+
getMethod().hasStringSignature("doFinal(ByteBuffer, ByteBuffer)") and getArgument(1) = output
6773
)
6874
}
6975
}
@@ -88,14 +94,12 @@ private class UserInputInCryptoOperationConfig extends TaintTracking2::Configura
8894
call.getQualifier() = toNode.asExpr() and
8995
call.getArgument(0) = fromNode.asExpr()
9096
|
91-
(
92-
m.hasQualifiedName("java.security", "Signature", "update")
93-
or
94-
m.hasQualifiedName("javax.crypto", ["Mac", "Cipher"], "update")
95-
or
96-
m.hasQualifiedName("javax.crypto", ["Mac", "Cipher"], "doFinal") and
97-
not m.hasStringSignature("doFinal(byte[],int)")
98-
)
97+
m.hasQualifiedName("java.security", "Signature", "update")
98+
or
99+
m.hasQualifiedName("javax.crypto", ["Mac", "Cipher"], "update")
100+
or
101+
m.hasQualifiedName("javax.crypto", ["Mac", "Cipher"], "doFinal") and
102+
not m.hasStringSignature("doFinal(byte[], int)")
99103
)
100104
}
101105
}
@@ -179,13 +183,10 @@ private class NonConstantTimeComparisonSink extends DataFlow::Node {
179183
anotherParameter = call.getQualifier()
180184
)
181185
or
182-
exists(NonConstantTimeComparisonCall call |
183-
call.getAnArgument() = this.asExpr() and
184-
(
185-
this.asExpr() = call.getArgument(0) and anotherParameter = call.getArgument(1)
186-
or
187-
this.asExpr() = call.getArgument(1) and anotherParameter = call.getArgument(0)
188-
)
186+
exists(NonConstantTimeComparisonCall call | call.getAnArgument() = this.asExpr() |
187+
this.asExpr() = call.getArgument(0) and anotherParameter = call.getArgument(1)
188+
or
189+
this.asExpr() = call.getArgument(1) and anotherParameter = call.getArgument(0)
189190
)
190191
) and
191192
not looksLikeConstant(anotherParameter)

0 commit comments

Comments
 (0)