|
| 1 | +/** |
| 2 | + * Provides classes and predicates for identifying expressions that are use crypto API Next Generation (CNG). |
| 3 | + */ |
| 4 | + |
| 5 | +import cpp |
| 6 | +private import semmle.code.cpp.dataflow.new.DataFlow |
| 7 | + |
| 8 | +/** |
| 9 | + * Dataflow that detects a call to BCryptSetProperty pszProperty = ChainingMode (CNG) |
| 10 | + */ |
| 11 | +module CngBCryptSetPropertyParamtoKChainingModeConfiguration implements DataFlow::ConfigSig { |
| 12 | + predicate isSource(DataFlow::Node source) { |
| 13 | + source.asExpr().getValue().toString().matches("ChainingMode") |
| 14 | + } |
| 15 | + |
| 16 | + predicate isSink(DataFlow::Node sink) { |
| 17 | + exists(FunctionCall call | |
| 18 | + // BCryptSetProperty 2nd argument specifies the key parameter to set |
| 19 | + sink.asExpr() = call.getArgument(1) and |
| 20 | + call.getTarget().hasGlobalName("BCryptSetProperty") |
| 21 | + ) |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +module CngBCryptSetPropertyParamtoKChainingMode = |
| 26 | + DataFlow::Global<CngBCryptSetPropertyParamtoKChainingModeConfiguration>; |
| 27 | + |
| 28 | +/** |
| 29 | + * A function call to BCryptSetProperty pszProperty = ChainingMode (CNG) |
| 30 | + */ |
| 31 | +class CngBCryptSetPropertyParamtoKChainingMode extends FunctionCall { |
| 32 | + CngBCryptSetPropertyParamtoKChainingMode() { |
| 33 | + exists(Expr var | |
| 34 | + CngBCryptSetPropertyParamtoKChainingMode::flow(DataFlow::exprNode(var), |
| 35 | + DataFlow::exprNode(this.getArgument(1))) |
| 36 | + ) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +predicate isChaniningModeCbc(DataFlow::Node source) { |
| 41 | + // Verify if algorithm is in the approved list. |
| 42 | + exists(string s | s = source.asExpr().getValue().toString() | |
| 43 | + s.regexpMatch("ChainingMode[A-Za-z0-9/]+") and |
| 44 | + // Property Strings |
| 45 | + // BCRYPT_CHAIN_MODE_NA L"ChainingModeN/A" - The algorithm does not support chaining |
| 46 | + // BCRYPT_CHAIN_MODE_CBC L"ChainingModeCBC" - Microsoft-Only: Only mode allowed by Crypto Board from this list (CBC-MAC) |
| 47 | + // BCRYPT_CHAIN_MODE_ECB L"ChainingModeECB" - Generally not recommended for usage in cryptographic protocols at all |
| 48 | + // BCRYPT_CHAIN_MODE_CFB L"ChainingModeCFB" - Microsoft-Only: Banned, usage requires Crypto Board review |
| 49 | + // BCRYPT_CHAIN_MODE_CCM L"ChainingModeCCM" - Microsoft-Only: Banned, usage requires Crypto Board review |
| 50 | + // BCRYPT_CHAIN_MODE_GCM L"ChainingModeGCM" - Microsoft-Only: Only for TLS, other usage requires Crypto Board review |
| 51 | + not s.matches("ChainingModeCBC") |
| 52 | + ) |
| 53 | +} |
| 54 | + |
| 55 | +module CngBCryptSetPropertyChainingBannedModeConfiguration implements DataFlow::ConfigSig { |
| 56 | + predicate isSource(DataFlow::Node source) { isChaniningModeCbc(source) } |
| 57 | + |
| 58 | + predicate isSink(DataFlow::Node sink) { |
| 59 | + exists(CngBCryptSetPropertyParamtoKChainingMode call | |
| 60 | + // BCryptOpenAlgorithmProvider 3rd argument sets the chaining mode value |
| 61 | + sink.asExpr() = call.getArgument(2) |
| 62 | + ) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +module CngBCryptSetPropertyChainingBannedMode = |
| 67 | + DataFlow::Global<CngBCryptSetPropertyChainingBannedModeConfiguration>; |
| 68 | + |
| 69 | +module CngBCryptSetPropertyChainingBannedModeIndirectParameterConfiguration implements |
| 70 | + DataFlow::ConfigSig |
| 71 | +{ |
| 72 | + predicate isSource(DataFlow::Node source) { isChaniningModeCbc(source) } |
| 73 | + |
| 74 | + predicate isSink(DataFlow::Node sink) { |
| 75 | + exists(CngBCryptSetPropertyParamtoKChainingMode call | |
| 76 | + // CryptSetKeyParam 3rd argument specifies the mode (KP_MODE) |
| 77 | + sink.asIndirectExpr() = call.getArgument(2) |
| 78 | + ) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +module CngBCryptSetPropertyChainingBannedModeIndirectParameter = |
| 83 | + DataFlow::Global<CngBCryptSetPropertyChainingBannedModeIndirectParameterConfiguration>; |
| 84 | + |
| 85 | +// CNG-specific DataFlow configuration |
| 86 | +module BCryptOpenAlgorithmProviderBannedHashConfiguration implements DataFlow::ConfigSig { |
| 87 | + // NOTE: Unlike the CAPI scenario, CNG will use this method to load and initialize |
| 88 | + // a cryptographic provider for any type of algorithm,not only hash. |
| 89 | + // Therefore, we have to take a banned-list instead of approved list approach. |
| 90 | + // |
| 91 | + predicate isSource(DataFlow::Node source) { |
| 92 | + // Verify if algorithm is marked as banned. |
| 93 | + source.asExpr().getValue().toString().matches("MD_") |
| 94 | + or |
| 95 | + source.asExpr().getValue().toString().matches("SHA1") |
| 96 | + } |
| 97 | + |
| 98 | + predicate isSink(DataFlow::Node sink) { |
| 99 | + exists(FunctionCall call | |
| 100 | + // BCryptOpenAlgorithmProvider 2nd argument specifies the algorithm to be used |
| 101 | + sink.asExpr() = call.getArgument(1) and |
| 102 | + call.getTarget().hasGlobalName("BCryptOpenAlgorithmProvider") |
| 103 | + ) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +module BCryptOpenAlgorithmProviderBannedHash = |
| 108 | + DataFlow::Global<BCryptOpenAlgorithmProviderBannedHashConfiguration>; |
| 109 | + |
| 110 | +// CNG-specific DataFlow configuration |
| 111 | +module BCryptOpenAlgorithmProviderBannedEncryptionConfiguration implements DataFlow::ConfigSig { |
| 112 | + // NOTE: Unlike the CAPI scenario, CNG will use this method to load and initialize |
| 113 | + // a cryptographic provider for any type of algorithm,not only encryption. |
| 114 | + // Therefore, we have to take a banned-list instead of approved list approach. |
| 115 | + // |
| 116 | + predicate isSource(DataFlow::Node source) { |
| 117 | + // Verify if algorithm is marked as banned. |
| 118 | + source.asExpr().getValue().toString().matches("RC_") or |
| 119 | + source.asExpr().getValue().toString().matches("DES") or |
| 120 | + source.asExpr().getValue().toString().matches("DESX") or |
| 121 | + source.asExpr().getValue().toString().matches("3DES") or |
| 122 | + source.asExpr().getValue().toString().matches("3DES_112") or |
| 123 | + source.asExpr().getValue().toString().matches("AES_GMAC") or // Microsoft Only: Requires Cryptoboard review |
| 124 | + source.asExpr().getValue().toString().matches("AES_CMAC") // Microsoft Only: Requires Cryptoboard review |
| 125 | + } |
| 126 | + |
| 127 | + predicate isSink(DataFlow::Node sink) { |
| 128 | + exists(FunctionCall call | |
| 129 | + // BCryptOpenAlgorithmProvider 2nd argument specifies the algorithm to be used |
| 130 | + sink.asExpr() = call.getArgument(1) and |
| 131 | + call.getTarget().hasGlobalName("BCryptOpenAlgorithmProvider") |
| 132 | + ) |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +module BCryptOpenAlgorithmProviderBannedEncryption = |
| 137 | + DataFlow::Global<BCryptOpenAlgorithmProviderBannedEncryptionConfiguration>; |
0 commit comments