Skip to content

Commit 624795c

Browse files
authored
Merge pull request #8059 from rdmarsh2/rdmarsh2/cpp/insufficient-key-strength
C++: new query for insufficient key strength
2 parents 71e393c + 280fdbf commit 624795c

File tree

7 files changed

+162
-0
lines changed

7 files changed

+162
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
void encrypt_with_openssl(EVP_PKEY_CTX *ctx) {
2+
3+
// BAD: only 1024 bits for an RSA key
4+
EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, 1024);
5+
6+
// GOOD: 2048 bits for an RSA key
7+
EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, 2048);
8+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
<overview>
6+
<p>Using cryptographic algorithms with a small key size can leave data vulnerable to being decrypted.</p>
7+
8+
<p>Many cryptographic algorithms provided by cryptography libraries can be configured with key sizes that are
9+
vulnerable to brute force attacks. Using such a key size means that an attacker may be able to easily decrypt the
10+
encrypted data.</p>
11+
12+
</overview>
13+
<recommendation>
14+
15+
<p>Ensure that you use a strong, modern cryptographic algorithm. Use at least AES-128 or RSA-2048.</p>
16+
17+
</recommendation>
18+
<example>
19+
20+
<p>The following code shows an example of using the <code>openssl</code> library to generate an RSA key.
21+
When creating a key, you must specify which key size to use. The first example uses 1024 bits, which is not
22+
considered sufficient. The second example uses 2048 bits, which is currently considered sufficient.</p>
23+
24+
<sample src="InsufficientKeySize.c" />
25+
26+
</example>
27+
<references>
28+
29+
<li>NIST, FIPS 140 Annex a: <a href="http://csrc.nist.gov/publications/fips/fips140-2/fips1402annexa.pdf">
30+
Approved Security Functions</a>.</li>
31+
<li>NIST, SP 800-131A: <a href="https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-131Ar2.pdf">
32+
Transitions: Recommendation for Transitioning the Use of Cryptographic Algorithms and Key Lengths</a>.</li>
33+
34+
<!-- LocalWords: CWE
35+
-->
36+
37+
</references>
38+
</qhelp>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* @name Use of a cryptographic algorithm with insufficient key size
3+
* @description Using cryptographic algorithms with too small a key size can
4+
* allow an attacker to compromise security.
5+
* @kind path-problem
6+
* @problem.severity error
7+
* @precision high
8+
* @id cpp/insufficient-key-size
9+
* @tags security
10+
* external/cwe/cwe-326
11+
*/
12+
13+
import cpp
14+
import semmle.code.cpp.ir.dataflow.DataFlow
15+
import semmle.code.cpp.ir.IR
16+
import DataFlow::PathGraph
17+
18+
// Gets the recommended minimum key size (in bits) of `func`, the name of an encryption function that accepts a key size as parameter `paramIndex`
19+
int getMinimumKeyStrength(string func, int paramIndex) {
20+
func =
21+
[
22+
"EVP_PKEY_CTX_set_dsa_paramgen_bits", "DSA_generate_parameters_ex",
23+
"EVP_PKEY_CTX_set_rsa_keygen_bits", "RSA_generate_key_ex", "RSA_generate_key_fips",
24+
"EVP_PKEY_CTX_set_dh_paramgen_prime_len", "DH_generate_parameters_ex"
25+
] and
26+
paramIndex = 1 and
27+
result = 2048
28+
}
29+
30+
class KeyStrengthFlow extends DataFlow::Configuration {
31+
KeyStrengthFlow() { this = "KeyStrengthFlow" }
32+
33+
override predicate isSource(DataFlow::Node node) {
34+
exists(int bits |
35+
node.asInstruction().(IntegerConstantInstruction).getValue().toInt() = bits and
36+
bits < getMinimumKeyStrength(_, _) and
37+
bits > 0 // exclude sentinel values
38+
)
39+
}
40+
41+
override predicate isSink(DataFlow::Node node) {
42+
exists(FunctionCall fc, string name, int param |
43+
node.asExpr() = fc.getArgument(param) and
44+
fc.getTarget().hasGlobalName(name) and
45+
exists(getMinimumKeyStrength(name, param))
46+
)
47+
}
48+
}
49+
50+
from
51+
DataFlow::PathNode source, DataFlow::PathNode sink, KeyStrengthFlow conf, FunctionCall fc,
52+
int param, string name, int minimumBits, int bits
53+
where
54+
conf.hasFlowPath(source, sink) and
55+
sink.getNode().asExpr() = fc.getArgument(param) and
56+
fc.getTarget().hasGlobalName(name) and
57+
minimumBits = getMinimumKeyStrength(name, param) and
58+
bits = source.getNode().asInstruction().(ConstantValueInstruction).getValue().toInt() and
59+
bits < minimumBits and
60+
bits != 0
61+
select fc, source, sink,
62+
"The key size $@ is less than the recommended key size of " + minimumBits.toString() + " bits.",
63+
source, bits.toString()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: newQuery
3+
---
4+
* A new `cpp/insufficient-key-size` query has been added to the default query suite for C/C++. The query finds uses of certain cryptographic algorithms where the key size is too small to provide adequate encryption strength.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
edges
2+
nodes
3+
| test.cpp:34:45:34:48 | 1024 | semmle.label | 1024 |
4+
| test.cpp:35:49:35:52 | 1024 | semmle.label | 1024 |
5+
| test.cpp:37:43:37:46 | 1024 | semmle.label | 1024 |
6+
subpaths
7+
#select
8+
| test.cpp:34:5:34:38 | call to EVP_PKEY_CTX_set_dsa_paramgen_bits | test.cpp:34:45:34:48 | 1024 | test.cpp:34:45:34:48 | 1024 | The key size $@ is less than the recommended key size of 2048 bits. | test.cpp:34:45:34:48 | 1024 | 1024 |
9+
| test.cpp:35:5:35:42 | call to EVP_PKEY_CTX_set_dh_paramgen_prime_len | test.cpp:35:49:35:52 | 1024 | test.cpp:35:49:35:52 | 1024 | The key size $@ is less than the recommended key size of 2048 bits. | test.cpp:35:49:35:52 | 1024 | 1024 |
10+
| test.cpp:37:5:37:36 | call to EVP_PKEY_CTX_set_rsa_keygen_bits | test.cpp:37:43:37:46 | 1024 | test.cpp:37:43:37:46 | 1024 | The key size $@ is less than the recommended key size of 2048 bits. | test.cpp:37:43:37:46 | 1024 | 1024 |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Security/CWE/CWE-326/InsufficientKeySize.ql
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
3+
typedef int EVP_CIPHER;
4+
typedef int EVP_MD;
5+
6+
const EVP_CIPHER *EVP_aes_128_ctr();
7+
const EVP_CIPHER *EVP_aes_192_ctr();
8+
const EVP_CIPHER *EVP_aes_256_ctr();
9+
10+
const EVP_MD *EVP_sha224();
11+
const EVP_MD *EVP_sha256();
12+
const EVP_MD *EVP_sha384();
13+
const EVP_MD *EVP_sha512();
14+
15+
16+
class EVP_PKEY_CTX;
17+
18+
// int is a curve ID rather than a bit width
19+
int EVP_PKEY_CTX_set_ec_paramgen_curve_nid(EVP_PKEY_CTX*, int);
20+
21+
int EVP_PKEY_CTX_set_dsa_paramgen_bits(EVP_PKEY_CTX*, int);
22+
int EVP_PKEY_CTX_set_dh_paramgen_prime_len(EVP_PKEY_CTX*, int);
23+
24+
// RSA sets bits per-key rather than with parameters
25+
int EVP_PKEY_CTX_set_rsa_keygen_bits(EVP_PKEY_CTX*, int);
26+
27+
void test1(EVP_PKEY_CTX *ctx) {
28+
EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, 2048);
29+
EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx, 2048);
30+
// RSA sets bits per-key rather than with parameters
31+
EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, 2048);
32+
33+
// low key sizes
34+
EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, 1024);
35+
EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx, 1024);
36+
// RSA sets bits per-key rather than with parameters
37+
EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, 1024);
38+
}

0 commit comments

Comments
 (0)