diff --git a/languages/cpp/custom/src/WeakCryptoRandomness/WeakCryptoRandomness.qll b/languages/cpp/custom/src/WeakCryptoRandomness/WeakCryptoRandomness.qll new file mode 100644 index 00000000..c0d4707a --- /dev/null +++ b/languages/cpp/custom/src/WeakCryptoRandomness/WeakCryptoRandomness.qll @@ -0,0 +1,88 @@ +/** + * Provides extensible predicates and classes for modeling cryptographic algorithm + * selection, RSA padding selection, and non-cryptographic pseudo-random number + * generators in C and C++ APIs such as the Windows CryptoAPI (`wincrypt.h`). + * + * Models are supplied as data extensions (see `weak-crypto-randomness.model.yml`). + */ + +import cpp +import semmle.code.cpp.dataflow.new.DataFlow + +/** + * Holds if argument `algorithmArg` (0-based) of the global function `function` + * selects a cryptographic algorithm, for example the `Algid` argument of + * `CryptCreateHash`. + */ +extensible predicate cryptoAlgorithmArgumentModel(string function, int algorithmArg); + +/** + * Holds if the macro or enum constant named `constant` selects the cryptographic + * algorithm `algorithm`, which is of kind `kind`. Supported kinds are + * `weak-cipher`, `weak-hash` and `rsa`. + */ +extensible predicate cryptoAlgorithmConstantModel(string constant, string algorithm, string kind); + +/** + * Holds if the global function `function` encrypts or decrypts with the key + * passed as argument `keyArg`, and the padding mode is selected by the flags + * passed as argument `flagsArg`, where setting the bits in `oaepFlag` selects + * OAEP padding. + */ +extensible predicate rsaPaddingFlagsModel(string function, int keyArg, int flagsArg, int oaepFlag); + +/** + * Holds if the global (or `std`) function `function` is a statistical, + * non-cryptographic pseudo-random number generator. + */ +extensible predicate weakPrngFunctionModel(string function); + +/** + * An expression that is the expansion of a macro, or an access of an enum + * constant, that is modeled as selecting a cryptographic algorithm. + */ +class CryptoAlgorithmConstant extends Expr { + string name; + string algorithm; + string kind; + + CryptoAlgorithmConstant() { + cryptoAlgorithmConstantModel(name, algorithm, kind) and + ( + exists(MacroInvocation mi | mi.getMacroName() = name and mi.getExpr() = this) + or + this.(EnumConstantAccess).getTarget().getName() = name + ) + } + + /** Gets the name of the macro or enum constant. */ + string getConstantName() { result = name } + + /** Gets the name of the selected algorithm. */ + string getAlgorithm() { result = algorithm } + + /** Gets the kind of the selected algorithm. */ + string getKind() { result = kind } +} + +/** + * Holds if `call` selects the cryptographic algorithm specified by `constant` + * through a modeled algorithm argument. + */ +predicate selectsCryptoAlgorithm(FunctionCall call, CryptoAlgorithmConstant constant) { + exists(string function, int algorithmArg | + cryptoAlgorithmArgumentModel(function, algorithmArg) and + call.getTarget().hasGlobalName(function) and + DataFlow::localExprFlow(constant, call.getArgument(algorithmArg)) + ) +} + +/** A call to a function modeled as a non-cryptographic pseudo-random number generator. */ +class WeakPrngCall extends FunctionCall { + WeakPrngCall() { + exists(string function | + weakPrngFunctionModel(function) and + this.getTarget().hasGlobalOrStdName(function) + ) + } +} diff --git a/languages/cpp/custom/src/WeakCryptoRandomness/weak-crypto-randomness.model.yml b/languages/cpp/custom/src/WeakCryptoRandomness/weak-crypto-randomness.model.yml new file mode 100644 index 00000000..88c365e3 --- /dev/null +++ b/languages/cpp/custom/src/WeakCryptoRandomness/weak-crypto-randomness.model.yml @@ -0,0 +1,52 @@ +extensions: + # Windows CryptoAPI (wincrypt.h): arguments that select an `ALG_ID`. + - addsTo: + pack: languages-cpp-custom-src + extensible: cryptoAlgorithmArgumentModel + data: + - ["CryptCreateHash", 1] + - ["CryptDeriveKey", 1] + - ["CryptGenKey", 1] + + # Windows CryptoAPI (wincrypt.h): `CALG_*` algorithm identifiers. + - addsTo: + pack: languages-cpp-custom-src + extensible: cryptoAlgorithmConstantModel + data: + - ["CALG_DES", "DES", "weak-cipher"] + - ["CALG_DESX", "DESX", "weak-cipher"] + - ["CALG_3DES", "3DES", "weak-cipher"] + - ["CALG_3DES_112", "3DES", "weak-cipher"] + - ["CALG_RC2", "RC2", "weak-cipher"] + - ["CALG_RC4", "RC4", "weak-cipher"] + - ["CALG_RC5", "RC5", "weak-cipher"] + - ["CALG_MD2", "MD2", "weak-hash"] + - ["CALG_MD4", "MD4", "weak-hash"] + - ["CALG_MD5", "MD5", "weak-hash"] + - ["CALG_SHA", "SHA-1", "weak-hash"] + - ["CALG_SHA1", "SHA-1", "weak-hash"] + - ["CALG_RSA_KEYX", "RSA", "rsa"] + - ["CALG_RSA_SIGN", "RSA", "rsa"] + + # Windows CryptoAPI (wincrypt.h): `CRYPT_OAEP` (0x40) selects OAEP padding for RSA keys. + - addsTo: + pack: languages-cpp-custom-src + extensible: rsaPaddingFlagsModel + data: + - ["CryptEncrypt", 0, 3, 64] + - ["CryptDecrypt", 0, 3, 64] + + # C standard library and POSIX statistical pseudo-random number generators. + - addsTo: + pack: languages-cpp-custom-src + extensible: weakPrngFunctionModel + data: + - ["rand"] + - ["rand_r"] + - ["random"] + - ["drand48"] + - ["erand48"] + - ["lrand48"] + - ["nrand48"] + - ["mrand48"] + - ["jrand48"] diff --git a/languages/cpp/custom/src/WeakPrng/WeakPrng.ql b/languages/cpp/custom/src/WeakPrng/WeakPrng.ql new file mode 100644 index 00000000..6b7de47d --- /dev/null +++ b/languages/cpp/custom/src/WeakPrng/WeakPrng.ql @@ -0,0 +1,22 @@ +/** + * @name Use of a non-cryptographic pseudo-random number generator + * @description Statistical pseudo-random number generators such as `rand()` produce + * predictable output and must not be used where unpredictability is + * required for security. + * @kind problem + * @problem.severity warning + * @security-severity 7.8 + * @precision low + * @id cpp/custom/weak-prng + * @tags security + * external/cwe/cwe-338 + */ + +import cpp +import WeakCryptoRandomness.WeakCryptoRandomness + +from WeakPrngCall call +where not call instanceof ExprInVoidContext +select call, + "Call to " + call.getTarget().getName() + + ", a non-cryptographic pseudo-random number generator whose output is predictable." diff --git a/languages/cpp/custom/src/WinCryptRsaWithoutOaep/WinCryptRsaWithoutOaep.ql b/languages/cpp/custom/src/WinCryptRsaWithoutOaep/WinCryptRsaWithoutOaep.ql new file mode 100644 index 00000000..dc65f778 --- /dev/null +++ b/languages/cpp/custom/src/WinCryptRsaWithoutOaep/WinCryptRsaWithoutOaep.ql @@ -0,0 +1,50 @@ +/** + * @name Use of RSA without OAEP padding with the Windows CryptoAPI + * @description Encrypting or decrypting with an RSA key without the `CRYPT_OAEP` + * flag uses PKCS #1 v1.5 padding, which is vulnerable to adaptive + * chosen-ciphertext (Bleichenbacher) attacks. + * @kind problem + * @problem.severity error + * @security-severity 7.5 + * @precision high + * @id cpp/custom/wincrypt-rsa-without-oaep + * @tags security + * external/cwe/cwe-780 + */ + +import cpp +import WeakCryptoRandomness.WeakCryptoRandomness + +predicate keyCreationFlowsTo(FunctionCall keyCreation, FunctionCall crypt, int keyArg) { + exists(DataFlow::Node keyDefinition | + ( + keyCreation.getTarget().hasGlobalName("CryptGenKey") and + keyDefinition.asDefiningArgument() = keyCreation.getArgument(3) + or + keyCreation.getTarget().hasGlobalName("CryptDeriveKey") and + keyDefinition.asDefiningArgument() = keyCreation.getArgument(4) + ) and + DataFlow::localFlow(keyDefinition, DataFlow::exprNode(crypt.getArgument(keyArg))) + ) +} + +from + FunctionCall crypt, FunctionCall keyCreation, CryptoAlgorithmConstant rsa, string function, + int keyArg, int flagsArg, int oaepFlag +where + rsaPaddingFlagsModel(function, keyArg, flagsArg, oaepFlag) and + crypt.getTarget().hasGlobalName(function) and + keyCreationFlowsTo(keyCreation, crypt, keyArg) and + crypt.getArgument(flagsArg).getValue().toInt().bitAnd(oaepFlag) = 0 and + selectsCryptoAlgorithm(keyCreation, rsa) and + rsa.getKind() = "rsa" and + not exists(FunctionCall otherKeyCreation | + otherKeyCreation != keyCreation and + keyCreationFlowsTo(otherKeyCreation, crypt, keyArg) + ) and + not ( + function = "CryptEncrypt" and + crypt.getArgument(4).getValue().toInt() = 0 + ) +select crypt, "Call to " + function + " uses an RSA key (created by $@) without OAEP padding.", + keyCreation, keyCreation.getTarget().getName() diff --git a/languages/cpp/custom/src/WinCryptWeakAlgorithm/WinCryptWeakAlgorithm.ql b/languages/cpp/custom/src/WinCryptWeakAlgorithm/WinCryptWeakAlgorithm.ql new file mode 100644 index 00000000..ffaff3d6 --- /dev/null +++ b/languages/cpp/custom/src/WinCryptWeakAlgorithm/WinCryptWeakAlgorithm.ql @@ -0,0 +1,31 @@ +/** + * @name Use of a weak cryptographic algorithm with the Windows CryptoAPI + * @description Selecting a broken or risky cipher (such as DES, 3DES, RC2 or RC4) + * or a broken hash algorithm (such as MD5 or SHA-1) when creating a + * CryptoAPI key or hash allows an attacker to compromise security. + * @kind problem + * @problem.severity error + * @security-severity 7.5 + * @precision high + * @id cpp/custom/wincrypt-weak-algorithm + * @tags security + * external/cwe/cwe-327 + * external/cwe/cwe-328 + */ + +import cpp +import WeakCryptoRandomness.WeakCryptoRandomness + +from FunctionCall call, CryptoAlgorithmConstant constant, string description +where + selectsCryptoAlgorithm(call, constant) and + ( + constant.getKind() = "weak-cipher" and + description = "broken or risky cipher " + constant.getAlgorithm() + or + constant.getKind() = "weak-hash" and + description = "broken hash algorithm " + constant.getAlgorithm() + ) +select call, + "Call to " + call.getTarget().getName() + " uses the " + description + " (selected by $@).", + constant, constant.getConstantName() diff --git a/languages/cpp/custom/src/qlpack.yml b/languages/cpp/custom/src/qlpack.yml index d9fd1e11..f87d034f 100644 --- a/languages/cpp/custom/src/qlpack.yml +++ b/languages/cpp/custom/src/qlpack.yml @@ -3,3 +3,5 @@ version: 0.0.2 library: false dependencies: codeql/cpp-all: "*" +dataExtensions: + - "**/*.model.yml" diff --git a/languages/cpp/custom/test/WeakPrng/WeakPrng.expected b/languages/cpp/custom/test/WeakPrng/WeakPrng.expected new file mode 100644 index 00000000..13325654 --- /dev/null +++ b/languages/cpp/custom/test/WeakPrng/WeakPrng.expected @@ -0,0 +1,2 @@ +| test.c:9:14:9:17 | call to rand | Call to rand, a non-cryptographic pseudo-random number generator whose output is predictable. | +| test.c:14:10:14:15 | call to random | Call to random, a non-cryptographic pseudo-random number generator whose output is predictable. | diff --git a/languages/cpp/custom/test/WeakPrng/WeakPrng.qlref b/languages/cpp/custom/test/WeakPrng/WeakPrng.qlref new file mode 100644 index 00000000..bac3b1f1 --- /dev/null +++ b/languages/cpp/custom/test/WeakPrng/WeakPrng.qlref @@ -0,0 +1,2 @@ +query: WeakPrng/WeakPrng.ql +postprocess: utils/test/InlineExpectationsTestQuery.ql diff --git a/languages/cpp/custom/test/WeakPrng/test.c b/languages/cpp/custom/test/WeakPrng/test.c new file mode 100644 index 00000000..c31d7cb6 --- /dev/null +++ b/languages/cpp/custom/test/WeakPrng/test.c @@ -0,0 +1,26 @@ +#include "../WinCryptStubs/wincrypt.h" + +int rand(void); +long random(void); +void srand(unsigned int seed); +void printIntLine(int value); + +void rand_bad(void) { + int data = rand(); // $ Alert + printIntLine(data); +} + +long random_bad(void) { + return random(); // $ Alert +} + +void discarded_rand_good(void) { + srand(42); + rand(); +} + +void crypt_gen_random_good(HCRYPTPROV hProv) { + unsigned int data; + CryptGenRandom(hProv, sizeof(data), (BYTE *)&data); + printIntLine(data); +} diff --git a/languages/cpp/custom/test/WinCryptRsaWithoutOaep/WinCryptRsaWithoutOaep.expected b/languages/cpp/custom/test/WinCryptRsaWithoutOaep/WinCryptRsaWithoutOaep.expected new file mode 100644 index 00000000..0c424636 --- /dev/null +++ b/languages/cpp/custom/test/WinCryptRsaWithoutOaep/WinCryptRsaWithoutOaep.expected @@ -0,0 +1,3 @@ +| test.c:6:3:6:14 | call to CryptEncrypt | Call to CryptEncrypt uses an RSA key (created by $@) without OAEP padding. | test.c:5:3:5:16 | call to CryptDeriveKey | CryptDeriveKey | +| test.c:12:3:12:14 | call to CryptDecrypt | Call to CryptDecrypt uses an RSA key (created by $@) without OAEP padding. | test.c:11:3:11:13 | call to CryptGenKey | CryptGenKey | +| test.c:52:3:52:14 | call to CryptEncrypt | Call to CryptEncrypt uses an RSA key (created by $@) without OAEP padding. | test.c:50:3:50:13 | call to CryptGenKey | CryptGenKey | diff --git a/languages/cpp/custom/test/WinCryptRsaWithoutOaep/WinCryptRsaWithoutOaep.qlref b/languages/cpp/custom/test/WinCryptRsaWithoutOaep/WinCryptRsaWithoutOaep.qlref new file mode 100644 index 00000000..80ca2390 --- /dev/null +++ b/languages/cpp/custom/test/WinCryptRsaWithoutOaep/WinCryptRsaWithoutOaep.qlref @@ -0,0 +1,2 @@ +query: WinCryptRsaWithoutOaep/WinCryptRsaWithoutOaep.ql +postprocess: utils/test/InlineExpectationsTestQuery.ql diff --git a/languages/cpp/custom/test/WinCryptRsaWithoutOaep/test.c b/languages/cpp/custom/test/WinCryptRsaWithoutOaep/test.c new file mode 100644 index 00000000..8539031e --- /dev/null +++ b/languages/cpp/custom/test/WinCryptRsaWithoutOaep/test.c @@ -0,0 +1,59 @@ +#include "../WinCryptStubs/wincrypt.h" + +void rsa_encrypt_bad(HCRYPTPROV hProv, HCRYPTHASH hHash, BYTE *payload, DWORD *payloadLen) { + HCRYPTKEY hKey; + CryptDeriveKey(hProv, CALG_RSA_SIGN, hHash, 0, &hKey); + CryptEncrypt(hKey, 0, 1, 0, payload, payloadLen, 200); // $ Alert +} + +void rsa_decrypt_bad(HCRYPTPROV hProv, BYTE *payload, DWORD *payloadLen) { + HCRYPTKEY hKey; + CryptGenKey(hProv, CALG_RSA_KEYX, 0, &hKey); + CryptDecrypt(hKey, 0, 1, 0, payload, payloadLen); // $ Alert +} + +void rsa_encrypt_good(HCRYPTPROV hProv, HCRYPTHASH hHash, BYTE *payload, DWORD *payloadLen) { + HCRYPTKEY hKey; + CryptDeriveKey(hProv, CALG_RSA_SIGN, hHash, 0, &hKey); + CryptEncrypt(hKey, 0, 1, CRYPT_OAEP, payload, payloadLen, 200); +} + +void rsa_decrypt_good(HCRYPTPROV hProv, BYTE *payload, DWORD *payloadLen) { + HCRYPTKEY hKey; + CryptGenKey(hProv, CALG_RSA_KEYX, 0, &hKey); + CryptDecrypt(hKey, 0, 1, CRYPT_OAEP, payload, payloadLen); +} + +void aes_encrypt_good(HCRYPTPROV hProv, HCRYPTHASH hHash, BYTE *payload, DWORD *payloadLen) { + HCRYPTKEY hKey; + CryptDeriveKey(hProv, CALG_AES_256, hHash, 0, &hKey); + CryptEncrypt(hKey, 0, 1, 0, payload, payloadLen, 200); +} + +void rsa_then_aes_encrypt_good(HCRYPTPROV hProv, HCRYPTHASH hHash, BYTE *payload, DWORD *payloadLen) { + HCRYPTKEY hKey; + CryptGenKey(hProv, CALG_RSA_KEYX, 0, &hKey); + CryptDeriveKey(hProv, CALG_AES_256, hHash, 0, &hKey); + CryptEncrypt(hKey, 0, 1, 0, payload, payloadLen, 200); +} + +void aes_then_rsa_encrypt_good(HCRYPTPROV hProv, HCRYPTHASH hHash, BYTE *payload, DWORD *payloadLen) { + HCRYPTKEY hKey; + CryptDeriveKey(hProv, CALG_AES_256, hHash, 0, &hKey); + CryptEncrypt(hKey, 0, 1, 0, payload, payloadLen, 200); + CryptGenKey(hProv, CALG_RSA_KEYX, 0, &hKey); +} + +void copied_rsa_handle_bad(HCRYPTPROV hProv, BYTE *payload, DWORD *payloadLen) { + HCRYPTKEY rsaKey; + HCRYPTKEY copiedKey; + CryptGenKey(hProv, CALG_RSA_KEYX, 0, &rsaKey); + copiedKey = rsaKey; + CryptEncrypt(copiedKey, 0, 1, 0, payload, payloadLen, 200); // $ Alert +} + +void rsa_encrypt_sizing_good(HCRYPTPROV hProv, BYTE *payload, DWORD *payloadLen) { + HCRYPTKEY hKey; + CryptGenKey(hProv, CALG_RSA_KEYX, 0, &hKey); + CryptEncrypt(hKey, 0, 1, 0, 0, payloadLen, 200); +} diff --git a/languages/cpp/custom/test/WinCryptStubs/wincrypt.h b/languages/cpp/custom/test/WinCryptStubs/wincrypt.h new file mode 100644 index 00000000..413afc9e --- /dev/null +++ b/languages/cpp/custom/test/WinCryptStubs/wincrypt.h @@ -0,0 +1,49 @@ +/* Minimal stand-in for the Windows CryptoAPI header (wincrypt.h), using the same values. */ +#ifndef WINCRYPT_STUB_H +#define WINCRYPT_STUB_H + +typedef unsigned long DWORD; +typedef int BOOL; +typedef unsigned char BYTE; +typedef unsigned int ALG_ID; +typedef unsigned long ULONG_PTR; +typedef ULONG_PTR HCRYPTPROV; +typedef ULONG_PTR HCRYPTKEY; +typedef ULONG_PTR HCRYPTHASH; + +#define ALG_CLASS_SIGNATURE (1 << 13) +#define ALG_CLASS_DATA_ENCRYPT (3 << 13) +#define ALG_CLASS_HASH (4 << 13) +#define ALG_CLASS_KEY_EXCHANGE (5 << 13) +#define ALG_TYPE_ANY (0) +#define ALG_TYPE_RSA (2 << 9) +#define ALG_TYPE_BLOCK (3 << 9) +#define ALG_TYPE_STREAM (4 << 9) +#define ALG_SID_MD5 3 +#define ALG_SID_SHA1 4 +#define ALG_SID_SHA_256 12 +#define ALG_SID_DES 1 +#define ALG_SID_3DES 3 +#define ALG_SID_RC4 1 +#define ALG_SID_AES_256 16 +#define ALG_SID_RSA_ANY 0 +#define CALG_MD5 (ALG_CLASS_HASH | ALG_TYPE_ANY | ALG_SID_MD5) +#define CALG_SHA1 (ALG_CLASS_HASH | ALG_TYPE_ANY | ALG_SID_SHA1) +#define CALG_SHA_256 (ALG_CLASS_HASH | ALG_TYPE_ANY | ALG_SID_SHA_256) +#define CALG_DES (ALG_CLASS_DATA_ENCRYPT | ALG_TYPE_BLOCK | ALG_SID_DES) +#define CALG_3DES (ALG_CLASS_DATA_ENCRYPT | ALG_TYPE_BLOCK | ALG_SID_3DES) +#define CALG_RC4 (ALG_CLASS_DATA_ENCRYPT | ALG_TYPE_STREAM | ALG_SID_RC4) +#define CALG_AES_256 (ALG_CLASS_DATA_ENCRYPT | ALG_TYPE_BLOCK | ALG_SID_AES_256) +#define CALG_RSA_SIGN (ALG_CLASS_SIGNATURE | ALG_TYPE_RSA | ALG_SID_RSA_ANY) +#define CALG_RSA_KEYX (ALG_CLASS_KEY_EXCHANGE | ALG_TYPE_RSA | ALG_SID_RSA_ANY) +#define CRYPT_OAEP 0x00000040 + +BOOL CryptCreateHash(HCRYPTPROV hProv, ALG_ID Algid, HCRYPTKEY hKey, DWORD dwFlags, HCRYPTHASH *phHash); +BOOL CryptHashData(HCRYPTHASH hHash, const BYTE *pbData, DWORD dwDataLen, DWORD dwFlags); +BOOL CryptDeriveKey(HCRYPTPROV hProv, ALG_ID Algid, HCRYPTHASH hBaseData, DWORD dwFlags, HCRYPTKEY *phKey); +BOOL CryptGenKey(HCRYPTPROV hProv, ALG_ID Algid, DWORD dwFlags, HCRYPTKEY *phKey); +BOOL CryptEncrypt(HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final, DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen, DWORD dwBufLen); +BOOL CryptDecrypt(HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final, DWORD dwFlags, BYTE *pbData, DWORD *pdwDataLen); +BOOL CryptGenRandom(HCRYPTPROV hProv, DWORD dwLen, BYTE *pbBuffer); + +#endif diff --git a/languages/cpp/custom/test/WinCryptWeakAlgorithm/WinCryptWeakAlgorithm.expected b/languages/cpp/custom/test/WinCryptWeakAlgorithm/WinCryptWeakAlgorithm.expected new file mode 100644 index 00000000..8396bc87 --- /dev/null +++ b/languages/cpp/custom/test/WinCryptWeakAlgorithm/WinCryptWeakAlgorithm.expected @@ -0,0 +1,5 @@ +| test.c:5:3:5:16 | call to CryptDeriveKey | Call to CryptDeriveKey uses the broken or risky cipher DES (selected by $@). | test.c:5:25:5:32 | ... \| ... | CALG_DES | +| test.c:11:3:11:13 | call to CryptGenKey | Call to CryptGenKey uses the broken or risky cipher RC4 (selected by $@). | test.c:11:22:11:29 | ... \| ... | CALG_RC4 | +| test.c:17:3:17:13 | call to CryptGenKey | Call to CryptGenKey uses the broken or risky cipher 3DES (selected by $@). | test.c:16:16:16:24 | ... \| ... | CALG_3DES | +| test.c:22:3:22:17 | call to CryptCreateHash | Call to CryptCreateHash uses the broken hash algorithm MD5 (selected by $@). | test.c:22:26:22:33 | ... \| ... | CALG_MD5 | +| test.c:28:3:28:17 | call to CryptCreateHash | Call to CryptCreateHash uses the broken hash algorithm SHA-1 (selected by $@). | test.c:28:26:28:34 | ... \| ... | CALG_SHA1 | diff --git a/languages/cpp/custom/test/WinCryptWeakAlgorithm/WinCryptWeakAlgorithm.qlref b/languages/cpp/custom/test/WinCryptWeakAlgorithm/WinCryptWeakAlgorithm.qlref new file mode 100644 index 00000000..ddd7ae42 --- /dev/null +++ b/languages/cpp/custom/test/WinCryptWeakAlgorithm/WinCryptWeakAlgorithm.qlref @@ -0,0 +1,2 @@ +query: WinCryptWeakAlgorithm/WinCryptWeakAlgorithm.ql +postprocess: utils/test/InlineExpectationsTestQuery.ql diff --git a/languages/cpp/custom/test/WinCryptWeakAlgorithm/test.c b/languages/cpp/custom/test/WinCryptWeakAlgorithm/test.c new file mode 100644 index 00000000..5a25f8c8 --- /dev/null +++ b/languages/cpp/custom/test/WinCryptWeakAlgorithm/test.c @@ -0,0 +1,41 @@ +#include "../WinCryptStubs/wincrypt.h" + +void des_bad(HCRYPTPROV hProv, HCRYPTHASH hHash, BYTE *data, DWORD *len) { + HCRYPTKEY hKey; + CryptDeriveKey(hProv, CALG_DES, hHash, 0, &hKey); // $ Alert + CryptDecrypt(hKey, 0, 1, 0, data, len); +} + +void rc4_bad(HCRYPTPROV hProv) { + HCRYPTKEY hKey; + CryptGenKey(hProv, CALG_RC4, 0, &hKey); // $ Alert +} + +void triple_des_via_variable_bad(HCRYPTPROV hProv) { + HCRYPTKEY hKey; + ALG_ID alg = CALG_3DES; + CryptGenKey(hProv, alg, 0, &hKey); // $ Alert +} + +void md5_bad(HCRYPTPROV hProv, BYTE *password, DWORD passwordLen) { + HCRYPTHASH hHash; + CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash); // $ Alert + CryptHashData(hHash, password, passwordLen, 0); +} + +void sha1_bad(HCRYPTPROV hProv) { + HCRYPTHASH hHash; + CryptCreateHash(hProv, CALG_SHA1, 0, 0, &hHash); // $ Alert +} + +void aes_good(HCRYPTPROV hProv, HCRYPTHASH hHash, BYTE *data, DWORD *len) { + HCRYPTKEY hKey; + CryptDeriveKey(hProv, CALG_AES_256, hHash, 0, &hKey); + CryptDecrypt(hKey, 0, 1, 0, data, len); +} + +void sha256_good(HCRYPTPROV hProv, BYTE *password, DWORD passwordLen) { + HCRYPTHASH hHash; + CryptCreateHash(hProv, CALG_SHA_256, 0, 0, &hHash); + CryptHashData(hHash, password, passwordLen, 0); +}