Skip to content

Commit d5ff477

Browse files
committed
Python: Add modeling for pycryptodome PyPI package
1 parent 6e4c627 commit d5ff477

4 files changed

Lines changed: 39 additions & 36 deletions

File tree

python/ql/src/semmle/python/frameworks/Cryptodome.qll

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,28 @@ private module CryptodomeModel {
1919
// ---------------------------------------------------------------------------
2020
// Cryptodome
2121
// ---------------------------------------------------------------------------
22-
/** Gets a reference to the `Cryptodome` module. */
22+
/** Gets a reference to the `Cryptodome`/`Crypto` module. */
2323
private DataFlow::Node cryptodome(DataFlow::TypeTracker t) {
2424
t.start() and
25-
result = DataFlow::importNode("Cryptodome")
25+
result = DataFlow::importNode(["Cryptodome", "Crypto"])
2626
or
2727
exists(DataFlow::TypeTracker t2 | result = cryptodome(t2).track(t2, t))
2828
}
2929

30-
/** Gets a reference to the `Cryptodome` module. */
30+
/** Gets a reference to the `Cryptodome`/`Crypto` module. */
3131
DataFlow::Node cryptodome() { result = cryptodome(DataFlow::TypeTracker::end()) }
3232

33-
/** Provides models for the `Cryptodome` module. */
33+
/** Provides models for the `Cryptodome`/`Crypto` module. */
3434
module Cryptodome {
3535
/**
36-
* Gets a reference to the attribute `attr_name` of the `Cryptodome` module.
36+
* Gets a reference to the attribute `attr_name` of the `Cryptodome`/`Crypto` module.
3737
* WARNING: Only holds for a few predefined attributes.
3838
*/
3939
private DataFlow::Node cryptodome_attr(DataFlow::TypeTracker t, string attr_name) {
4040
attr_name in ["PublicKey"] and
4141
(
4242
t.start() and
43-
result = DataFlow::importNode("Cryptodome" + "." + attr_name)
43+
result = DataFlow::importNode(["Cryptodome", "Crypto"] + "." + attr_name)
4444
or
4545
t.startInAttr(attr_name) and
4646
result = cryptodome()
@@ -64,7 +64,7 @@ private module CryptodomeModel {
6464
}
6565

6666
/**
67-
* Gets a reference to the attribute `attr_name` of the `Cryptodome` module.
67+
* Gets a reference to the attribute `attr_name` of the `Cryptodome`/`Crypto` module.
6868
* WARNING: Only holds for a few predefined attributes.
6969
*/
7070
private DataFlow::Node cryptodome_attr(string attr_name) {
@@ -74,20 +74,20 @@ private module CryptodomeModel {
7474
// -------------------------------------------------------------------------
7575
// Cryptodome.PublicKey
7676
// -------------------------------------------------------------------------
77-
/** Gets a reference to the `Cryptodome.PublicKey` module. */
77+
/** Gets a reference to the `Cryptodome.PublicKey`/`Crypto.PublicKey` module. */
7878
DataFlow::Node publicKey() { result = cryptodome_attr("PublicKey") }
7979

80-
/** Provides models for the `Cryptodome.PublicKey` module */
80+
/** Provides models for the `Cryptodome.PublicKey`/`Crypto.PublicKey` module */
8181
module PublicKey {
8282
/**
83-
* Gets a reference to the attribute `attr_name` of the `Cryptodome.PublicKey` module.
83+
* Gets a reference to the attribute `attr_name` of the `Cryptodome.PublicKey`/`Crypto.PublicKey` module.
8484
* WARNING: Only holds for a few predefined attributes.
8585
*/
8686
private DataFlow::Node publicKey_attr(DataFlow::TypeTracker t, string attr_name) {
8787
attr_name in ["RSA", "DSA", "ECC"] and
8888
(
8989
t.start() and
90-
result = DataFlow::importNode("Cryptodome.PublicKey" + "." + attr_name)
90+
result = DataFlow::importNode(["Cryptodome", "Crypto"] + ".PublicKey" + "." + attr_name)
9191
or
9292
t.startInAttr(attr_name) and
9393
result = publicKey()
@@ -112,7 +112,7 @@ private module CryptodomeModel {
112112
}
113113

114114
/**
115-
* Gets a reference to the attribute `attr_name` of the `Cryptodome.PublicKey` module.
115+
* Gets a reference to the attribute `attr_name` of the `Cryptodome.PublicKey`/`Crypto.PublicKey` module.
116116
* WARNING: Only holds for a few predefined attributes.
117117
*/
118118
private DataFlow::Node publicKey_attr(string attr_name) {
@@ -122,20 +122,21 @@ private module CryptodomeModel {
122122
// -------------------------------------------------------------------------
123123
// Cryptodome.PublicKey.RSA
124124
// -------------------------------------------------------------------------
125-
/** Gets a reference to the `Cryptodome.PublicKey.RSA` module. */
125+
/** Gets a reference to the `Cryptodome.PublicKey.RSA`/`Crypto.PublicKey.RSA` module. */
126126
DataFlow::Node rsa() { result = publicKey_attr("RSA") }
127127

128-
/** Provides models for the `Cryptodome.PublicKey.RSA` module */
128+
/** Provides models for the `Cryptodome.PublicKey.RSA`/`Crypto.PublicKey.RSA` module */
129129
module RSA {
130130
/**
131-
* Gets a reference to the attribute `attr_name` of the `Cryptodome.PublicKey.RSA` module.
131+
* Gets a reference to the attribute `attr_name` of the `Cryptodome.PublicKey.RSA`/`Crypto.PublicKey.RSA` module.
132132
* WARNING: Only holds for a few predefined attributes.
133133
*/
134134
private DataFlow::Node rsa_attr(DataFlow::TypeTracker t, string attr_name) {
135135
attr_name in ["generate"] and
136136
(
137137
t.start() and
138-
result = DataFlow::importNode("Cryptodome.PublicKey.RSA" + "." + attr_name)
138+
result =
139+
DataFlow::importNode(["Cryptodome", "Crypto"] + ".PublicKey.RSA" + "." + attr_name)
139140
or
140141
t.startInAttr(attr_name) and
141142
result = rsa()
@@ -160,34 +161,35 @@ private module CryptodomeModel {
160161
}
161162

162163
/**
163-
* Gets a reference to the attribute `attr_name` of the `Cryptodome.PublicKey.RSA` module.
164+
* Gets a reference to the attribute `attr_name` of the `Cryptodome.PublicKey.RSA`/`Crypto.PublicKey.RSA` module.
164165
* WARNING: Only holds for a few predefined attributes.
165166
*/
166167
private DataFlow::Node rsa_attr(string attr_name) {
167168
result = rsa_attr(DataFlow::TypeTracker::end(), attr_name)
168169
}
169170

170-
/** Gets a reference to the `Cryptodome.PublicKey.RSA.generate` function. */
171+
/** Gets a reference to the `Cryptodome.PublicKey.RSA.generate`/`Crypto.PublicKey.RSA.generate` function. */
171172
DataFlow::Node generate() { result = rsa_attr("generate") }
172173
}
173174

174175
// -------------------------------------------------------------------------
175176
// Cryptodome.PublicKey.DSA
176177
// -------------------------------------------------------------------------
177-
/** Gets a reference to the `Cryptodome.PublicKey.DSA` module. */
178+
/** Gets a reference to the `Cryptodome.PublicKey.DSA`/`Crypto.PublicKey.DSA` module. */
178179
DataFlow::Node dsa() { result = publicKey_attr("DSA") }
179180

180-
/** Provides models for the `Cryptodome.PublicKey.DSA` module */
181+
/** Provides models for the `Cryptodome.PublicKey.DSA`/`Crypto.PublicKey.DSA` module */
181182
module DSA {
182183
/**
183-
* Gets a reference to the attribute `attr_name` of the `Cryptodome.PublicKey.DSA` module.
184+
* Gets a reference to the attribute `attr_name` of the `Cryptodome.PublicKey.DSA`/`Crypto.PublicKey.DSA` module.
184185
* WARNING: Only holds for a few predefined attributes.
185186
*/
186187
private DataFlow::Node dsa_attr(DataFlow::TypeTracker t, string attr_name) {
187188
attr_name in ["generate"] and
188189
(
189190
t.start() and
190-
result = DataFlow::importNode("Cryptodome.PublicKey.DSA" + "." + attr_name)
191+
result =
192+
DataFlow::importNode(["Cryptodome", "Crypto"] + ".PublicKey.DSA" + "." + attr_name)
191193
or
192194
t.startInAttr(attr_name) and
193195
result = dsa()
@@ -212,34 +214,35 @@ private module CryptodomeModel {
212214
}
213215

214216
/**
215-
* Gets a reference to the attribute `attr_name` of the `Cryptodome.PublicKey.DSA` module.
217+
* Gets a reference to the attribute `attr_name` of the `Cryptodome.PublicKey.DSA`/`Crypto.PublicKey.DSA` module.
216218
* WARNING: Only holds for a few predefined attributes.
217219
*/
218220
private DataFlow::Node dsa_attr(string attr_name) {
219221
result = dsa_attr(DataFlow::TypeTracker::end(), attr_name)
220222
}
221223

222-
/** Gets a reference to the `Cryptodome.PublicKey.DSA.generate` function. */
224+
/** Gets a reference to the `Cryptodome.PublicKey.DSA.generate`/`Crypto.PublicKey.DSA.generate` function. */
223225
DataFlow::Node generate() { result = dsa_attr("generate") }
224226
}
225227

226228
// -------------------------------------------------------------------------
227229
// Cryptodome.PublicKey.ECC
228230
// -------------------------------------------------------------------------
229-
/** Gets a reference to the `Cryptodome.PublicKey.ECC` module. */
231+
/** Gets a reference to the `Cryptodome.PublicKey.ECC`/`Crypto.PublicKey.ECC` module. */
230232
DataFlow::Node ecc() { result = publicKey_attr("ECC") }
231233

232-
/** Provides models for the `Cryptodome.PublicKey.ECC` module */
234+
/** Provides models for the `Cryptodome.PublicKey.ECC`/`Crypto.PublicKey.ECC` module */
233235
module ECC {
234236
/**
235-
* Gets a reference to the attribute `attr_name` of the `Cryptodome.PublicKey.ECC` module.
237+
* Gets a reference to the attribute `attr_name` of the `Cryptodome.PublicKey.ECC`/`Crypto.PublicKey.ECC` module.
236238
* WARNING: Only holds for a few predefined attributes.
237239
*/
238240
private DataFlow::Node ecc_attr(DataFlow::TypeTracker t, string attr_name) {
239241
attr_name in ["generate"] and
240242
(
241243
t.start() and
242-
result = DataFlow::importNode("Cryptodome.PublicKey.ECC" + "." + attr_name)
244+
result =
245+
DataFlow::importNode(["Cryptodome", "Crypto"] + ".PublicKey.ECC" + "." + attr_name)
243246
or
244247
t.startInAttr(attr_name) and
245248
result = ecc()
@@ -264,22 +267,22 @@ private module CryptodomeModel {
264267
}
265268

266269
/**
267-
* Gets a reference to the attribute `attr_name` of the `Cryptodome.PublicKey.ECC` module.
270+
* Gets a reference to the attribute `attr_name` of the `Cryptodome.PublicKey.ECC`/`Crypto.PublicKey.ECC` module.
268271
* WARNING: Only holds for a few predefined attributes.
269272
*/
270273
private DataFlow::Node ecc_attr(string attr_name) {
271274
result = ecc_attr(DataFlow::TypeTracker::end(), attr_name)
272275
}
273276

274-
/** Gets a reference to the `Cryptodome.PublicKey.ECC.generate` function. */
277+
/** Gets a reference to the `Cryptodome.PublicKey.ECC.generate`/`Crypto.PublicKey.ECC.generate` function. */
275278
DataFlow::Node generate() { result = ecc_attr("generate") }
276279
}
277280
}
278281
}
279282

280283
// ---------------------------------------------------------------------------
281284
/**
282-
* A call to `Cryptodome.PublicKey.RSA.generate`
285+
* A call to `Cryptodome.PublicKey.RSA.generate`/`Crypto.PublicKey.RSA.generate`
283286
*
284287
* See https://pycryptodome.readthedocs.io/en/latest/src/public_key/rsa.html#Crypto.PublicKey.RSA.generate
285288
*/
@@ -297,7 +300,7 @@ private module CryptodomeModel {
297300
}
298301

299302
/**
300-
* A call to `Cryptodome.PublicKey.DSA.generate`
303+
* A call to `Cryptodome.PublicKey.DSA.generate`/`Crypto.PublicKey.DSA.generate`
301304
*
302305
* See https://pycryptodome.readthedocs.io/en/latest/src/public_key/dsa.html#Crypto.PublicKey.DSA.generate
303306
*/
@@ -315,7 +318,7 @@ private module CryptodomeModel {
315318
}
316319

317320
/**
318-
* A call to `Cryptodome.PublicKey.ECC.generate`
321+
* A call to `Cryptodome.PublicKey.ECC.generate`/`Crypto.PublicKey.ECC.generate`
319322
*
320323
* See https://pycryptodome.readthedocs.io/en/latest/src/public_key/ecc.html#Crypto.PublicKey.ECC.generate
321324
*/

python/ql/test/experimental/library-tests/frameworks/crypto/test_dsa.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from Crypto.Hash import SHA256
77

88

9-
private_key = DSA.generate(2048) # $ MISSING: PublicKeyGeneration keySize=2048
9+
private_key = DSA.generate(2048) # $ PublicKeyGeneration keySize=2048
1010
public_key = private_key.publickey()
1111

1212
# ------------------------------------------------------------------------------

python/ql/test/experimental/library-tests/frameworks/crypto/test_ec.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from Crypto.Hash import SHA256
44

55

6-
private_key = ECC.generate(curve="P-256") # $ MISSING: PublicKeyGeneration keySize=256
6+
private_key = ECC.generate(curve="P-256") # $ PublicKeyGeneration keySize=256
77
public_key = private_key.public_key()
88

99
# ------------------------------------------------------------------------------

python/ql/test/experimental/library-tests/frameworks/crypto/test_rsa.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from Crypto.Signature import pss
66
from Crypto.Hash import SHA256
77

8-
private_key = RSA.generate(2048) # $ MISSING: PublicKeyGeneration keySize=2048
8+
private_key = RSA.generate(2048) # $ PublicKeyGeneration keySize=2048
99
public_key = private_key.publickey()
1010

1111
# ------------------------------------------------------------------------------

0 commit comments

Comments
 (0)