Skip to content

Commit f931dab

Browse files
committed
C++: Improve the cpp/cleartext-* query examples by using libsodium rather than pseudocode.
1 parent ffc61ae commit f931dab

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed
Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,31 @@
1-
void writeCredentials() {
2-
char *password = "cleartext password";
3-
FILE* file = fopen("credentials.txt", "w");
4-
1+
#include <sodium.h>
2+
#include <stdio.h>
3+
#include <string.h>
4+
5+
void writeCredentialsBad(FILE *file, const char *cleartextCredentials) {
56
// BAD: write password to disk in cleartext
6-
fputs(password, file);
7-
8-
// GOOD: encrypt password first
9-
char *encrypted = encrypt(password);
10-
fputs(encrypted, file);
7+
fputs(cleartextCredentials, file);
118
}
129

10+
int writeCredentialsGood(FILE *file, const char *cleartextCredentials, const unsigned char *key, const unsigned char *nonce) {
11+
size_t credentialsLen = strlen(cleartextCredentials);
12+
size_t ciphertext_len = crypto_secretbox_MACBYTES + credentialsLen;
13+
unsigned char *ciphertext = malloc(ciphertext_len);
14+
if (!ciphertext) {
15+
logError();
16+
return -1;
17+
}
18+
19+
// encrypt the password first
20+
if (crypto_secretbox_easy(ciphertext, (const unsigned char *)cleartextCredentials, credentialsLen, nonce, key) != 0) {
21+
free(ciphertext);
22+
logError();
23+
return -1;
24+
}
25+
26+
// GOOD: write encrypted password to disk
27+
fwrite(ciphertext, 1, ciphertext_len, file);
28+
29+
free(ciphertext);
30+
return 0;
31+
}

cpp/ql/src/Security/CWE/CWE-311/CleartextStorage.inc.qhelp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,20 @@ cleartext.</p>
1919
<example>
2020

2121
<p>The following example shows two ways of storing user credentials in a file. In the 'BAD' case,
22-
the credentials are simply stored in cleartext. In the 'GOOD' case, the credentials are encrypted before
22+
the credentials are simply stored in cleartext. In the 'GOOD' case, the credentials are encrypted before
2323
storing them.</p>
2424

2525
<sample src="CleartextStorage.c" />
2626

27+
<p>Note that for the good example to work we need to link against the encryption library (in this case libsodium),
28+
initialize it with a call to <code>sodium_init</code>, and create the key and nonce with
29+
<code>crypto_secretbox_keygen</code> and <code>randombytes_buf</code> respectively. We also need to store those
30+
details securely so they can be used for decryption.</p>
31+
2732
</example>
2833
<references>
2934

30-
<li>M. Dowd, J. McDonald and J. Schuhm, <i>The Art of Software Security Assessment</i>, 1st Edition, Chapter 2 - 'Common Vulnerabilities of Encryption', p. 43. Addison Wesley, 2006.</li>
35+
<li>M. Dowd, J. McDonald and J. Schuhm, <i>The Art of Software Security Assessment</i>, 1st Edition, Chapter 2 - 'Common Vulnerabilities of Encryption', p. 43. Addison Wesley, 2006.</li>
3136
<li>M. Howard and D. LeBlanc, <i>Writing Secure Code</i>, 2nd Edition, Chapter 9 - 'Protecting Secret Data', p. 299. Microsoft, 2002.</li>
3237

3338

0 commit comments

Comments
 (0)