/
InvalidKeySize.ql
61 lines (49 loc) · 1.75 KB
/
InvalidKeySize.ql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* @name Invalid key size
* @id tob/cpp/invalid-key-size
* @description Tests if keys passed to EVP_EncryptInit and EVP_EncryptInit_ex have the same size as the key size of the cipher used
* @kind problem
* @tags correctness crypto
* @problem.severity warning
* @precision medium
* @group cryptography
*/
import cpp
import semmle.code.cpp.dataflow.new.DataFlow
import trailofbits.crypto.libraries
class Key extends Variable {
FunctionCall init;
Key() {
DataFlow::localFlow(
DataFlow::exprNode(this.getAnAccess()),
DataFlow::exprNode(init.getArgument(init.getTarget().(EVP_EncryptInit).getKey()))
) or
DataFlow::localFlow(
DataFlow::exprNode(this.getAnAccess()),
DataFlow::exprNode(init.getArgument(init.getTarget().(EVP_EncryptInit_ex).getKey()))
)
}
EVP_CIPHER getACipher() {
DataFlow::localFlow(DataFlow::exprNode(result), DataFlow::exprNode(init.getArgument(init.getTarget().(EVP_EncryptInit).getCipher()))) or
DataFlow::localFlow(DataFlow::exprNode(result), DataFlow::exprNode(init.getArgument(init.getTarget().(EVP_EncryptInit_ex).getCipher())))
}
FunctionCall getInitCall() { result = init }
int getSize() { result = this.getUnderlyingType().getSize() }
predicate correctKeySize(EVP_CIPHER cipher) {
cipher.getKeySize() = this.getSize()
}
// Avoid matching on pointers where the key size is not known.
predicate isArray() {
this.getType() instanceof ArrayType
}
}
from
Key key,
EVP_CIPHER cipher
where
cipher = key.getACipher() and
key.isArray() and
not key.correctKeySize(cipher)
select
key.getInitCall().getLocation(),
"Key size (" + key.getSize() + " bytes) does not match the expected key size for the encryption algorithm (" + cipher.getKeySize() + " bytes)"