|
| 1 | +/** |
| 2 | + * Provides classes modeling security-relevant aspects of the `rsa` PyPI package. |
| 3 | + * See https://stuvel.eu/python-rsa-doc/. |
| 4 | + */ |
| 5 | + |
| 6 | +private import python |
| 7 | +private import semmle.python.dataflow.new.DataFlow |
| 8 | +private import semmle.python.dataflow.new.TaintTracking |
| 9 | +private import semmle.python.Concepts |
| 10 | +private import semmle.python.ApiGraphs |
| 11 | + |
| 12 | +/** |
| 13 | + * Provides models for the `rsa` PyPI package. |
| 14 | + * See https://stuvel.eu/python-rsa-doc/. |
| 15 | + */ |
| 16 | +private module Rsa { |
| 17 | + /** |
| 18 | + * A call to `rsa.newkeys` |
| 19 | + * |
| 20 | + * See https://stuvel.eu/python-rsa-doc/reference.html#rsa.newkeys |
| 21 | + */ |
| 22 | + class RsaNewkeysCall extends Cryptography::PublicKey::KeyGeneration::RsaRange, |
| 23 | + DataFlow::CallCfgNode { |
| 24 | + RsaNewkeysCall() { this = API::moduleImport("rsa").getMember("newkeys").getACall() } |
| 25 | + |
| 26 | + override DataFlow::Node getKeySizeArg() { |
| 27 | + result in [this.getArg(0), this.getArgByName("nbits")] |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * A call to `rsa.encrypt` |
| 33 | + * |
| 34 | + * See https://stuvel.eu/python-rsa-doc/reference.html#rsa.encrypt |
| 35 | + */ |
| 36 | + class RsaEncryptCall extends Cryptography::CryptographicOperation::Range, DataFlow::CallCfgNode { |
| 37 | + RsaEncryptCall() { this = API::moduleImport("rsa").getMember("encrypt").getACall() } |
| 38 | + |
| 39 | + override Cryptography::CryptographicAlgorithm getAlgorithm() { result.getName() = "RSA" } |
| 40 | + |
| 41 | + override DataFlow::Node getAnInput() { |
| 42 | + result in [this.getArg(0), this.getArgByName("message")] |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * A call to `rsa.decrypt` |
| 48 | + * |
| 49 | + * See https://stuvel.eu/python-rsa-doc/reference.html#rsa.decrypt |
| 50 | + */ |
| 51 | + class RsaDecryptCall extends Cryptography::CryptographicOperation::Range, DataFlow::CallCfgNode { |
| 52 | + RsaDecryptCall() { this = API::moduleImport("rsa").getMember("decrypt").getACall() } |
| 53 | + |
| 54 | + override Cryptography::CryptographicAlgorithm getAlgorithm() { result.getName() = "RSA" } |
| 55 | + |
| 56 | + override DataFlow::Node getAnInput() { result in [this.getArg(0), this.getArgByName("crypto")] } |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * A call to `rsa.sign`, which both hashes and signs in the input message. |
| 61 | + * |
| 62 | + * See https://stuvel.eu/python-rsa-doc/reference.html#rsa.sign |
| 63 | + */ |
| 64 | + class RsaSignCall extends Cryptography::CryptographicOperation::Range, DataFlow::CallCfgNode { |
| 65 | + RsaSignCall() { this = API::moduleImport("rsa").getMember("sign").getACall() } |
| 66 | + |
| 67 | + override Cryptography::CryptographicAlgorithm getAlgorithm() { |
| 68 | + // signature part |
| 69 | + result.getName() = "RSA" |
| 70 | + or |
| 71 | + // hashing part |
| 72 | + exists(StrConst str, DataFlow::Node hashNameArg | |
| 73 | + hashNameArg in [this.getArg(2), this.getArgByName("hash_method")] and |
| 74 | + DataFlow::exprNode(str).(DataFlow::LocalSourceNode).flowsTo(hashNameArg) and |
| 75 | + result.matchesName(str.getText()) |
| 76 | + ) |
| 77 | + } |
| 78 | + |
| 79 | + override DataFlow::Node getAnInput() { |
| 80 | + result in [this.getArg(0), this.getArgByName("message")] |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * A call to `rsa.verify` |
| 86 | + * |
| 87 | + * See https://stuvel.eu/python-rsa-doc/reference.html#rsa.verify |
| 88 | + */ |
| 89 | + class RsaVerifyCall extends Cryptography::CryptographicOperation::Range, DataFlow::CallCfgNode { |
| 90 | + RsaVerifyCall() { this = API::moduleImport("rsa").getMember("verify").getACall() } |
| 91 | + |
| 92 | + override Cryptography::CryptographicAlgorithm getAlgorithm() { |
| 93 | + // note that technically there is also a hashing operation going on but we don't |
| 94 | + // know what algorithm is used up front, since it is encoded in the signature |
| 95 | + result.getName() = "RSA" |
| 96 | + } |
| 97 | + |
| 98 | + override DataFlow::Node getAnInput() { |
| 99 | + result in [this.getArg(0), this.getArgByName("message")] |
| 100 | + or |
| 101 | + result in [this.getArg(1), this.getArgByName("signature")] |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * A call to `rsa.compute_hash` |
| 107 | + * |
| 108 | + * See https://stuvel.eu/python-rsa-doc/reference.html#rsa.compute_hash |
| 109 | + */ |
| 110 | + class RsaComputeHashCall extends Cryptography::CryptographicOperation::Range, |
| 111 | + DataFlow::CallCfgNode { |
| 112 | + RsaComputeHashCall() { this = API::moduleImport("rsa").getMember("compute_hash").getACall() } |
| 113 | + |
| 114 | + override Cryptography::CryptographicAlgorithm getAlgorithm() { |
| 115 | + exists(StrConst str, DataFlow::Node hashNameArg | |
| 116 | + hashNameArg in [this.getArg(1), this.getArgByName("method_name")] and |
| 117 | + DataFlow::exprNode(str).(DataFlow::LocalSourceNode).flowsTo(hashNameArg) and |
| 118 | + result.matchesName(str.getText()) |
| 119 | + ) |
| 120 | + } |
| 121 | + |
| 122 | + override DataFlow::Node getAnInput() { |
| 123 | + result in [this.getArg(0), this.getArgByName("message")] |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * A call to `rsa.sign_hash` |
| 129 | + * |
| 130 | + * See https://stuvel.eu/python-rsa-doc/reference.html#rsa.sign_hash |
| 131 | + */ |
| 132 | + class RsaSignHashCall extends Cryptography::CryptographicOperation::Range, DataFlow::CallCfgNode { |
| 133 | + RsaSignHashCall() { this = API::moduleImport("rsa").getMember("sign_hash").getACall() } |
| 134 | + |
| 135 | + override Cryptography::CryptographicAlgorithm getAlgorithm() { result.getName() = "RSA" } |
| 136 | + |
| 137 | + override DataFlow::Node getAnInput() { |
| 138 | + result in [this.getArg(0), this.getArgByName("hash_value")] |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments