|
| 1 | +import cpp |
| 2 | +import WindowsCng |
| 3 | + |
| 4 | +//TODO: Verify NCrypt calls (parameters) & find all other APIs that should be included (i.e. decrypt, etc.) |
| 5 | + |
| 6 | +predicate isExprAlgIdForBCryptOpenAlgorithmProvider(Expr e){ |
| 7 | + exists( FunctionCall call | |
| 8 | + // BCryptOpenAlgorithmProvider 2nd argument specifies the algorithm to be used |
| 9 | + e = call.getArgument(1) |
| 10 | + and |
| 11 | + call.getTarget().hasGlobalName("BCryptOpenAlgorithmProvider") |
| 12 | + ) |
| 13 | +} |
| 14 | + |
| 15 | +predicate isExprAlgHandleForBCryptGenerateOrImportKeyPair(Expr e){ |
| 16 | + exists( FunctionCall call | |
| 17 | + e = call.getArgument(0) |
| 18 | + and |
| 19 | + ( call.getTarget().hasGlobalName("BCryptImportKeyPair") or |
| 20 | + call.getTarget().hasGlobalName("BCryptGenerateKeyPair")) |
| 21 | + ) |
| 22 | +} |
| 23 | + |
| 24 | +predicate isExprOutKeyHandleForBCryptGenerateOrImportKeyPair(Expr e){ |
| 25 | + exists( FunctionCall call | |
| 26 | + ( e = call.getArgument(3) and call.getTarget().hasGlobalName("BCryptImportKeyPair") )or |
| 27 | + ( e = call.getArgument(1) and call.getTarget().hasGlobalName("BCryptGenerateKeyPair") ) |
| 28 | + ) |
| 29 | +} |
| 30 | + |
| 31 | +predicate isExprKeyHandleForBCryptSignHash(Expr e){ |
| 32 | + exists( FunctionCall call | |
| 33 | + e = call.getArgument(0) |
| 34 | + and |
| 35 | + call.getTarget().hasGlobalName("BCryptSignHash") |
| 36 | + ) |
| 37 | +} |
| 38 | + |
| 39 | +class BCryptSignHashArgumentSink extends BCryptOpenAlgorithmProviderSink { |
| 40 | + BCryptSignHashArgumentSink() { |
| 41 | + isExprKeyHandleForBCryptSignHash(this.asExpr()) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +class BCryptOpenAlgorithmProviderPqcVulnerableAlgorithmsSource extends BCryptOpenAlgorithmProviderSource { |
| 46 | + BCryptOpenAlgorithmProviderPqcVulnerableAlgorithmsSource() { |
| 47 | + this.asExpr() instanceof StringLiteral and |
| 48 | + ( |
| 49 | + this.asExpr().getValue() in ["DH", "DSA", "ECDSA", "ECDH"] |
| 50 | + or this.asExpr().getValue().matches("ECDH%") |
| 51 | + or this.asExpr().getValue().matches("RSA%") |
| 52 | + ) |
| 53 | + } |
| 54 | + |
| 55 | +} |
| 56 | + |
| 57 | +predicate isAdditionalTaintStepOpenAlgolrithmProviderToGenerateOrImportKeyPair(DataFlow::Node node1, DataFlow::Node node2) |
| 58 | +{ |
| 59 | + isExprAlgIdForBCryptOpenAlgorithmProvider(node1.asExpr()) and |
| 60 | + isExprAlgHandleForBCryptGenerateOrImportKeyPair(node2.asExpr()) |
| 61 | +} |
| 62 | + |
| 63 | + |
| 64 | +predicate isAdditionalTaintStepWithinGenerateOrImportKeyPair(DataFlow::Node node1, DataFlow::Node node2) |
| 65 | +{ |
| 66 | + isExprAlgHandleForBCryptGenerateOrImportKeyPair(node1.asExpr()) and |
| 67 | + isExprOutKeyHandleForBCryptGenerateOrImportKeyPair(node2.asExpr()) |
| 68 | +} |
| 69 | + |
| 70 | +predicate isAdditionalTaintStepGenerateOrImportKeyPairToSignHash(DataFlow::Node node1, DataFlow::Node node2) |
| 71 | +{ |
| 72 | + isExprOutKeyHandleForBCryptGenerateOrImportKeyPair(node1.asExpr()) and |
| 73 | + isExprKeyHandleForBCryptSignHash(node2.asExpr()) |
| 74 | +} |
| 75 | + |
| 76 | +predicate isWindowsCngAsymmetricKeyAdditionalTaintStep(DataFlow::Node node1, DataFlow::Node node2) { |
| 77 | + isAdditionalTaintStepOpenAlgolrithmProviderToGenerateOrImportKeyPair(node1, node2) |
| 78 | + or isAdditionalTaintStepWithinGenerateOrImportKeyPair(node1, node2) |
| 79 | + or isAdditionalTaintStepGenerateOrImportKeyPairToSignHash(node1, node2) |
| 80 | +} |
| 81 | + |
0 commit comments