Skip to content

Commit 1d3216b

Browse files
committed
Bug fix in InvalidKeySize.ql
1 parent 0817fc9 commit 1d3216b

3 files changed

Lines changed: 41 additions & 16 deletions

File tree

cpp/src/crypto/InvalidKeySize.ql

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @name Invalid key size
33
* @id tob/cpp/invalid-key-size
4-
* @description Tests if keys passed to EncryptInit_ex have the same size as the key size of the cipher used
4+
* @description Tests if keys passed to EVP_EncryptInit and EVP_EncryptInit_ex have the same size as the key size of the cipher used
55
* @kind problem
66
* @tags correctness crypto
77
* @problem.severity warning
@@ -41,9 +41,21 @@ class Key extends Variable {
4141
cipher.getKeySize() = this.getSize()
4242
}
4343

44-
44+
// Avoid matching on pointers where the key size is not known.
45+
predicate isArray() {
46+
this.getType() instanceof ArrayType
47+
}
4548
}
4649

47-
from Key key, EVP_CIPHER cipher
48-
where cipher = key.getACipher() and not key.correctKeySize(cipher)
49-
select key.getInitCall().getLocation(), "Key size (" + key.getSize() + " bytes) does not match the expected key size for the encryption algorithm (" + cipher.getKeySize() + " bytes)"
50+
51+
52+
from
53+
Key key,
54+
EVP_CIPHER cipher
55+
where
56+
cipher = key.getACipher() and
57+
key.isArray() and
58+
not key.correctKeySize(cipher)
59+
select
60+
key.getInitCall().getLocation(),
61+
"Key size (" + key.getSize() + " bytes) does not match the expected key size for the encryption algorithm (" + cipher.getKeySize() + " bytes)"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
| test.c:15:5:15:22 | test.c:15:5:15:22 | Key size (31 bytes) does not match the expected key size for the encryption algorithm (32 bytes) |
1+
| test.c:9:3:9:20 | test.c:9:3:9:20 | Key size (16 bytes) does not match the expected key size for the encryption algorithm (32 bytes) |
Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,31 @@
11
#include "../../../include/openssl/evp.h"
22
#include "../../../include/openssl/rand.h"
33

4-
int main(void)
5-
{
6-
unsigned char key[31]; // should be 32 for 256 bit key
4+
void found(EVP_CIPHER_CTX *ctx) {
5+
unsigned char key[16]; // should be 32 for 256 bit key
6+
unsigned char *iv = (unsigned char *)"0123456789012345";
77

8-
const EVP_CIPHER *c = EVP_aes_128_cbc();
8+
RAND_bytes(key, sizeof(key));
9+
EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv);
10+
}
911

10-
int rc = RAND_bytes(key, sizeof(key));
12+
void notFound(EVP_CIPHER_CTX *ctx, unsigned char *key) {
13+
unsigned char *iv = (unsigned char *)"0123456789012345";
1114

12-
/* A 128 bit IV */
13-
unsigned char *iv = (unsigned char *)"0123456789012345";
15+
EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv);
16+
}
1417

15-
EVP_EncryptInit_ex(EVP_CIPHER_CTX_new(), EVP_aes_256_cbc(), NULL, key, iv);
18+
int main(void) {
19+
{
20+
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
21+
found(ctx);
22+
}
23+
{
24+
unsigned char key[16]; // should be 32 for 256 bit key
25+
RAND_bytes(key, sizeof(key));
26+
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
27+
notFound(ctx, key);
28+
}
1629

17-
return 0;
18-
}
30+
return 0;
31+
}

0 commit comments

Comments
 (0)