-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathInsufficientKeySize.ql
More file actions
70 lines (62 loc) · 2.43 KB
/
InsufficientKeySize.ql
File metadata and controls
70 lines (62 loc) · 2.43 KB
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
62
63
64
65
66
67
68
69
70
/**
* @name Use of a cryptographic algorithm with insufficient key size
* @description Using cryptographic algorithms with too small a key size can
* allow an attacker to compromise security.
* @kind path-problem
* @problem.severity error
* @security-severity 7.5
* @precision high
* @id cpp/insufficient-key-size
* @tags security
* external/cwe/cwe-326
*/
import cpp
import semmle.code.cpp.ir.dataflow.DataFlow
import semmle.code.cpp.ir.IR
import KeyStrengthFlow::PathGraph
// Gets the recommended minimum key size (in bits) of `func`, the name of an encryption function that accepts a key size as parameter `paramIndex`
int getMinimumKeyStrength(string func, int paramIndex) {
func =
[
"EVP_PKEY_CTX_set_dsa_paramgen_bits", "DSA_generate_parameters_ex",
"EVP_PKEY_CTX_set_rsa_keygen_bits", "RSA_generate_key_ex", "RSA_generate_key_fips",
"EVP_PKEY_CTX_set_dh_paramgen_prime_len", "DH_generate_parameters_ex"
] and
paramIndex = 1 and
result = 2048
}
module KeyStrengthFlowConfig implements DataFlow::ConfigSig {
predicate isSource(DataFlow::Node node) {
exists(int bits |
node.asInstruction().(IntegerConstantInstruction).getValue().toInt() = bits and
bits < getMinimumKeyStrength(_, _) and
bits > 0 // exclude sentinel values
)
}
predicate isSink(DataFlow::Node node) {
exists(FunctionCall fc, string name, int param |
node.asExpr() = fc.getArgument(param) and
fc.getTarget().hasGlobalName(name) and
exists(getMinimumKeyStrength(name, param))
)
}
predicate observeDiffInformedIncrementalMode() { any() }
Location getASelectedSinkLocation(DataFlow::Node sink) {
exists(FunctionCall fc | result = fc.getLocation() | sink.asExpr() = fc.getArgument(_))
}
}
module KeyStrengthFlow = DataFlow::Global<KeyStrengthFlowConfig>;
from
KeyStrengthFlow::PathNode source, KeyStrengthFlow::PathNode sink, FunctionCall fc, int param,
string name, int minimumBits, int bits
where
KeyStrengthFlow::flowPath(source, sink) and
sink.getNode().asExpr() = fc.getArgument(param) and
fc.getTarget().hasGlobalName(name) and
minimumBits = getMinimumKeyStrength(name, param) and
bits = source.getNode().asInstruction().(ConstantValueInstruction).getValue().toInt() and
bits < minimumBits and
bits != 0
select fc, source, sink,
"The key size $@ is less than the recommended key size of " + minimumBits.toString() + " bits.",
source, bits.toString()