|
| 1 | +/** |
| 2 | + * @name Weak asymmetric key size |
| 3 | + * @description Using RSA keys smaller than 2048 bits does not provide adequate security. |
| 4 | + * @kind problem |
| 5 | + * @problem.severity error |
| 6 | + * @security-severity 7.5 |
| 7 | + * @precision high |
| 8 | + * @id powershell/microsoft/security/weak-asymmetric-key |
| 9 | + * @tags security |
| 10 | + * external/cwe/cwe-326 |
| 11 | + */ |
| 12 | + |
| 13 | +import powershell |
| 14 | +import semmle.code.powershell.ApiGraphs |
| 15 | +import semmle.code.powershell.dataflow.DataFlow |
| 16 | + |
| 17 | +/** |
| 18 | + * Holds if `keySize` is below the minimum RSA key size of 2048 bits. |
| 19 | + */ |
| 20 | +bindingset[keySize] |
| 21 | +predicate isWeakKeySize(int keySize) { keySize > 0 and keySize < 2048 } |
| 22 | + |
| 23 | +/** |
| 24 | + * An RSA.Create(keySize) or RSA::new(keySize) call via the API graph with a weak key size. |
| 25 | + */ |
| 26 | +class WeakRsaCreateCall extends DataFlow::CallNode { |
| 27 | + int keySize; |
| 28 | + |
| 29 | + WeakRsaCreateCall() { |
| 30 | + exists(string method | |
| 31 | + method = ["create", "new"] and |
| 32 | + this = |
| 33 | + API::getTopLevelMember("system") |
| 34 | + .getMember("security") |
| 35 | + .getMember("cryptography") |
| 36 | + .getMember(["rsa", "rsacryptoserviceprovider"]) |
| 37 | + .getMember(method) |
| 38 | + .asCall() |
| 39 | + ) and |
| 40 | + keySize = this.getAnArgument().asExpr().getExpr().(ConstExpr).getValueString().toInt() and |
| 41 | + isWeakKeySize(keySize) |
| 42 | + } |
| 43 | + |
| 44 | + int getKeySize() { result = keySize } |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * A New-Object RSACryptoServiceProvider(keySize) with a weak key size. |
| 49 | + */ |
| 50 | +class WeakRsaCspCreation extends DataFlow::ObjectCreationNode { |
| 51 | + int keySize; |
| 52 | + |
| 53 | + WeakRsaCspCreation() { |
| 54 | + exists(string objName | |
| 55 | + objName = |
| 56 | + this.getExprNode().getExpr().(CallExpr).getAnArgument().getValue().asString().toLowerCase() and |
| 57 | + objName = |
| 58 | + [ |
| 59 | + "system.security.cryptography.rsacryptoserviceprovider", |
| 60 | + "rsacryptoserviceprovider" |
| 61 | + ] |
| 62 | + ) and |
| 63 | + exists(DataFlow::Node arg | |
| 64 | + arg = this.getAnArgument() and |
| 65 | + keySize = arg.asExpr().getExpr().(ConstExpr).getValueString().toInt() and |
| 66 | + isWeakKeySize(keySize) |
| 67 | + ) |
| 68 | + } |
| 69 | + |
| 70 | + int getKeySize() { result = keySize } |
| 71 | +} |
| 72 | + |
| 73 | +from DataFlow::Node node, int keySize |
| 74 | +where |
| 75 | + exists(WeakRsaCreateCall c | node = c and keySize = c.getKeySize()) |
| 76 | + or |
| 77 | + exists(WeakRsaCspCreation c | node = c and keySize = c.getKeySize()) |
| 78 | +select node, |
| 79 | + "RSA key size " + keySize.toString() + " bits is below the minimum of 2048 bits." |
0 commit comments