|
1 | 1 | /** |
2 | 2 | * @name Use of legacy cryptographic algorithm |
3 | 3 | * @id tob/cpp/use-of-legacy-algorithm |
4 | | - * @description Detects potential calls to legacy cryptographic algorithms |
| 4 | + * @description Detects potential instantiations of legacy cryptographic algorithms |
5 | 5 | * @kind problem |
6 | 6 | * @tags correctness crypto |
7 | 7 | * @problem.severity warning |
|
11 | 11 |
|
12 | 12 | import cpp |
13 | 13 |
|
14 | | -from FunctionCall call |
| 14 | +from FunctionCall call, string functionName, string cipherName |
15 | 15 | where |
16 | | - call.getTarget() |
17 | | - .getQualifiedName() |
18 | | - .toLowerCase() |
19 | | - .matches([ |
20 | | - // Hash functions |
21 | | - "%md2%", "%md4%", "%md5%", "%ripemd%", "%sha1%", "%whirlpool%", "%streebog%", |
22 | | - // KDFs |
23 | | - "%pbkdf1%", |
24 | | - // Symmetric ciphers |
25 | | - "%arcfour%", "%blowfish%", "%kasumi%", "%magma%", "%rc2%", "%rc4%", "%tdea%" |
26 | | - ]) |
| 16 | + functionName = call.getTarget() |
| 17 | + .getQualifiedName() |
| 18 | + .toLowerCase() |
| 19 | + and |
| 20 | + ( |
| 21 | + exists(string cn | |
| 22 | + cn in [ |
| 23 | + "MD2", "MD4", "MD5", "RIPEMD", "SHA1", "Whirlpool", "Streebog", |
| 24 | + "PBKDF1", |
| 25 | + "ArcFour", "Blowfish", "CAST", "IDEA", "Kasumi", |
| 26 | + "Magma", "RC2", "RC4", "TDEA" |
| 27 | + ] |
| 28 | + and cipherName = cn |
| 29 | + and functionName.matches("%" + cn.toLowerCase() + "%") |
| 30 | + ) |
| 31 | + /* match DES, but avoid false positives by not matching common terms containing it: |
| 32 | + nodes |
| 33 | + modes |
| 34 | + codes |
| 35 | + describe |
| 36 | + description |
| 37 | + descriptor |
| 38 | + design |
| 39 | + descend |
| 40 | + destroy |
| 41 | + */ |
| 42 | + or cipherName = "DES" and functionName.regexpMatch(".*(?<!no|mo|co)des(?!cri(be|ption|ptor)|ign|cend|troy).*") |
| 43 | + ) |
27 | 44 | select call.getLocation(), |
28 | | - "Potential use of legacy cryptographic algorithm " + call.getTarget().getQualifiedName() + |
29 | | - " detected" |
| 45 | + "Potential use of legacy cryptographic algorithm " + cipherName + " detected in function name " + call.getTarget().getQualifiedName() |
0 commit comments