Skip to content

Commit 62039b8

Browse files
committed
add cryptographic key model to the crypto-js library
1 parent 028799d commit 62039b8

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

javascript/ql/lib/semmle/javascript/frameworks/CryptoLibraries.qll

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,35 @@ private module CryptoJS {
330330
)
331331
}
332332
}
333+
334+
private class CreateKey extends CryptographicKeyCreation, DataFlow::CallNode {
335+
string algorithm;
336+
int optionArg;
337+
338+
CreateKey() {
339+
// var key = CryptoJS.PBKDF2(password, salt, { keySize: 8 });
340+
this =
341+
getAlgorithmExpr(any(CryptographicAlgorithm algo | algo.getName() = algorithm)).getACall() and
342+
optionArg = 2
343+
or
344+
// var key = CryptoJS.algo.PBKDF2.create({ keySize: 8 });
345+
this =
346+
DataFlow::moduleMember("crypto-js", "algo")
347+
.getAPropertyRead(algorithm)
348+
.getAMethodCall("create") and
349+
optionArg = 0
350+
}
351+
352+
override CryptographicAlgorithm getAlgorithm() { result.matchesName(algorithm) }
353+
354+
override int getSize() {
355+
result = getOptionArgument(optionArg, "keySize").getIntValue() * 32 // size is in words
356+
or
357+
result = getArgument(optionArg).getIntValue() * 32 // size is in words
358+
}
359+
360+
override predicate isSymmetricKey() { any() }
361+
}
333362
}
334363

335364
/**

javascript/ql/lib/semmle/javascript/security/CryptoAlgorithms.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ private module AlgorithmNames {
4646
name = ["ARGON2", "PBKDF2", "BCRYPT", "SCRYPT"]
4747
}
4848

49-
predicate isWeakPasswordHashingAlgorithm(string name) { none() }
49+
predicate isWeakPasswordHashingAlgorithm(string name) { name = "EVPKDF" }
5050
}
5151

5252
private import AlgorithmNames
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
| tst.js:3:14:3:71 | crypto. ... 1024 }) | Creation of an asymmetric RSA key uses 1024 bits, which is below 2048 and considered breakable. |
22
| tst.js:7:14:7:59 | crypto. ... : 64 }) | Creation of an symmetric key uses 64 bits, which is below 128 and considered breakable. |
3+
| tst.js:14:14:14:56 | CryptoJ ... e: 2 }) | Creation of an symmetric PBKDF2 key uses 64 bits, which is below 128 and considered breakable. |
4+
| tst.js:15:14:15:60 | CryptoJ ... e: 2 }) | Creation of an symmetric PBKDF2 key uses 64 bits, which is below 128 and considered breakable. |
5+
| tst.js:16:14:16:60 | CryptoJ ... e: 2 }) | Creation of an symmetric EVPKDF key uses 64 bits, which is below 128 and considered breakable. |

javascript/ql/test/query-tests/Security/CWE-326/tst.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,11 @@ const good1 = crypto.generateKeyPairSync("rsa", { modulusLength: 4096 }); // OK
66

77
const bad2 = crypto.generateKeySync("hmac", { length: 64 }); // NOT OK
88

9-
const good2 = crypto.generateKeySync("aes", { length: 256 }); // OK
9+
const good2 = crypto.generateKeySync("aes", { length: 256 }); // OK
10+
11+
var CryptoJS = require("crypto-js");
12+
13+
const bad3 = CryptoJS.algo.PBKDF2.create({ keySize: 2 }); // NOT OK
14+
const bad4 = CryptoJS.PBKDF2(password, salt, { keySize: 2 }); // NOT OK
15+
const bad5 = CryptoJS.EvpKDF(password, salt, { keySize: 2 }); // NOT OK
16+
const bad4 = CryptoJS.PBKDF2(password, salt, { keySize: 8 }); // OK

0 commit comments

Comments
 (0)