|
13 | 13 | import cpp
|
14 | 14 | import semmle.code.cpp.security.Encryption
|
15 | 15 |
|
16 |
| -abstract class InsecureCryptoSpec extends Locatable { |
17 |
| - abstract string description(); |
| 16 | +/** |
| 17 | + * A function which may relate to an insecure encryption algorithm. |
| 18 | + */ |
| 19 | +Function getAnInsecureEncryptionFunction() { |
| 20 | + ( |
| 21 | + isInsecureEncryption(result.getName()) or |
| 22 | + isInsecureEncryption(result.getAParameter().getName()) |
| 23 | + ) and |
| 24 | + exists(result.getACallToThisFunction()) |
18 | 25 | }
|
19 | 26 |
|
20 |
| -Function getAnInsecureFunction() { |
21 |
| - isInsecureEncryption(result.getName()) and |
| 27 | +/** |
| 28 | + * A function with additional evidence it is related to encryption. |
| 29 | + */ |
| 30 | +Function getAdditionalEvidenceFunction() { |
| 31 | + ( |
| 32 | + isEncryptionAdditionalEvidence(result.getName()) or |
| 33 | + isEncryptionAdditionalEvidence(result.getAParameter().getName()) |
| 34 | + ) and |
22 | 35 | exists(result.getACallToThisFunction())
|
23 | 36 | }
|
24 | 37 |
|
25 |
| -class InsecureFunctionCall extends InsecureCryptoSpec, FunctionCall { |
26 |
| - InsecureFunctionCall() { |
27 |
| - // the function name suggests it relates to an insecure crypto algorithm. |
28 |
| - this.getTarget() = getAnInsecureFunction() |
29 |
| - } |
30 |
| - |
31 |
| - override string description() { result = "function call" } |
32 |
| - |
33 |
| - override string toString() { result = FunctionCall.super.toString() } |
34 |
| - |
35 |
| - override Location getLocation() { result = FunctionCall.super.getLocation() } |
| 38 | +/** |
| 39 | + * A macro which may relate to an insecure encryption algorithm. |
| 40 | + */ |
| 41 | +Macro getAnInsecureEncryptionMacro() { |
| 42 | + isInsecureEncryption(result.getName()) and |
| 43 | + exists(result.getAnInvocation()) |
36 | 44 | }
|
37 | 45 |
|
38 |
| -Macro getAnInsecureMacro() { |
39 |
| - isInsecureEncryption(result.getName()) and |
| 46 | +/** |
| 47 | + * A macro with additional evidence it is related to encryption. |
| 48 | + */ |
| 49 | +Macro getAdditionalEvidenceMacro() { |
| 50 | + isEncryptionAdditionalEvidence(result.getName()) and |
40 | 51 | exists(result.getAnInvocation())
|
41 | 52 | }
|
42 | 53 |
|
43 |
| -class InsecureMacroSpec extends InsecureCryptoSpec, MacroInvocation { |
44 |
| - InsecureMacroSpec() { |
45 |
| - // the macro name suggests it relates to an insecure crypto algorithm. |
46 |
| - this.getMacro() = getAnInsecureMacro() and |
47 |
| - // the macro invocation generates something. |
48 |
| - exists(this.getAGeneratedElement().(ControlFlowNode)) and |
49 |
| - // exclude expressions controlling ifs/switches (as they may not be used). |
50 |
| - not any(IfStmt c).getCondition().getAChild*() = this.getAGeneratedElement() and |
51 |
| - not any(SwitchCase c).getExpr().getAChild*() = this.getAGeneratedElement() and |
52 |
| - // exclude expressions in array initializers (as they may not be used). |
53 |
| - not any(AggregateLiteral i).getAChild*() = this.getAGeneratedElement() |
| 54 | +/** |
| 55 | + * A function call we have a high confidence is related to use of an insecure |
| 56 | + * encryption algorithm. |
| 57 | + */ |
| 58 | +class InsecureFunctionCall extends FunctionCall { |
| 59 | + InsecureFunctionCall() { |
| 60 | + // find use of an insecure algorithm name |
| 61 | + ( |
| 62 | + getTarget() = getAnInsecureEncryptionFunction() |
| 63 | + or |
| 64 | + exists(MacroInvocation mi | |
| 65 | + mi.getAGeneratedElement() = this.getAChild*() and |
| 66 | + mi.getMacro() = getAnInsecureEncryptionMacro() |
| 67 | + ) |
| 68 | + ) and |
| 69 | + // find additional evidence that this function is related to encryption. |
| 70 | + ( |
| 71 | + getTarget() = getAdditionalEvidenceFunction() |
| 72 | + or |
| 73 | + exists(MacroInvocation mi | |
| 74 | + mi.getAGeneratedElement() = this.getAChild*() and |
| 75 | + mi.getMacro() = getAdditionalEvidenceMacro() |
| 76 | + ) |
| 77 | + ) |
54 | 78 | }
|
55 | 79 |
|
56 |
| - override string description() { result = "macro invocation" } |
57 |
| - |
58 |
| - override string toString() { result = MacroInvocation.super.toString() } |
59 |
| - |
60 |
| - override Location getLocation() { result = MacroInvocation.super.getLocation() } |
| 80 | + string description() { result = "function call" } |
61 | 81 | }
|
62 | 82 |
|
63 |
| -from InsecureCryptoSpec c |
| 83 | +from InsecureFunctionCall c |
64 | 84 | select c, "This " + c.description() + " specifies a broken or weak cryptographic algorithm."
|
0 commit comments