|
| 1 | +/** |
| 2 | + * @name Weak encryption: Insufficient key size |
| 3 | + * @description Finds uses of encryption algorithms with too small a key size |
| 4 | + * @kind problem |
| 5 | + * @id java/insufficient-key-size |
| 6 | + * @tags security |
| 7 | + * external/cwe/cwe-326 |
| 8 | + */ |
| 9 | + |
| 10 | +import java |
| 11 | +import semmle.code.java.security.Encryption |
| 12 | +import semmle.code.java.dataflow.TaintTracking |
| 13 | + |
| 14 | +/** The Java class `javax.crypto.KeyGenerator`. */ |
| 15 | +class KeyGenerator extends RefType { |
| 16 | + KeyGenerator() { this.hasQualifiedName("javax.crypto", "KeyGenerator") } |
| 17 | +} |
| 18 | + |
| 19 | +/** The Java class `javax.crypto.KeyGenerator`. */ |
| 20 | +class KeyPairGenerator extends RefType { |
| 21 | + KeyPairGenerator() { this.hasQualifiedName("java.security", "KeyPairGenerator") } |
| 22 | +} |
| 23 | + |
| 24 | +/** The Java class `java.security.spec.ECGenParameterSpec`. */ |
| 25 | +class ECGenParameterSpec extends RefType { |
| 26 | + ECGenParameterSpec() { this.hasQualifiedName("java.security.spec", "ECGenParameterSpec") } |
| 27 | +} |
| 28 | + |
| 29 | +/** The `init` method declared in `javax.crypto.KeyGenerator`. */ |
| 30 | +class KeyGeneratorInitMethod extends Method { |
| 31 | + KeyGeneratorInitMethod() { |
| 32 | + getDeclaringType() instanceof KeyGenerator and |
| 33 | + hasName("init") |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +/** The `initialize` method declared in `java.security.KeyPairGenerator`. */ |
| 38 | +class KeyPairGeneratorInitMethod extends Method { |
| 39 | + KeyPairGeneratorInitMethod() { |
| 40 | + getDeclaringType() instanceof KeyPairGenerator and |
| 41 | + hasName("initialize") |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +/** Taint configuration tracking flow from a key generator to a `init` method call. */ |
| 46 | +class CryptoKeyGeneratorConfiguration extends TaintTracking::Configuration { |
| 47 | + CryptoKeyGeneratorConfiguration() { this = "CryptoKeyGeneratorConfiguration" } |
| 48 | + |
| 49 | + override predicate isSource(DataFlow::Node source) { |
| 50 | + exists(JavaxCryptoKeyGenerator jcg | jcg = source.asExpr()) |
| 51 | + } |
| 52 | + |
| 53 | + override predicate isSink(DataFlow::Node sink) { |
| 54 | + exists(MethodAccess ma | |
| 55 | + ma.getMethod() instanceof KeyGeneratorInitMethod and |
| 56 | + sink.asExpr() = ma.getQualifier() |
| 57 | + ) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +/** Taint configuration tracking flow from a keypair generator to a `initialize` method call. */ |
| 62 | +class KeyPairGeneratorConfiguration extends TaintTracking::Configuration { |
| 63 | + KeyPairGeneratorConfiguration() { this = "KeyPairGeneratorConfiguration" } |
| 64 | + |
| 65 | + override predicate isSource(DataFlow::Node source) { |
| 66 | + exists(JavaSecurityKeyPairGenerator jkg | jkg = source.asExpr()) |
| 67 | + } |
| 68 | + |
| 69 | + override predicate isSink(DataFlow::Node sink) { |
| 70 | + exists(MethodAccess ma | |
| 71 | + ma.getMethod() instanceof KeyPairGeneratorInitMethod and |
| 72 | + sink.asExpr() = ma.getQualifier() |
| 73 | + ) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +/** Holds if an AES `KeyGenerator` is initialized with an insufficient key size. */ |
| 78 | +predicate incorrectUseOfAES(MethodAccess ma, string msg) { |
| 79 | + ma.getMethod() instanceof KeyGeneratorInitMethod and |
| 80 | + exists( |
| 81 | + JavaxCryptoKeyGenerator jcg, CryptoKeyGeneratorConfiguration cc, DataFlow::PathNode source, |
| 82 | + DataFlow::PathNode dest |
| 83 | + | |
| 84 | + jcg.getAlgoSpec().(StringLiteral).getValue() = "AES" and |
| 85 | + source.getNode().asExpr() = jcg and |
| 86 | + dest.getNode().asExpr() = ma.getQualifier() and |
| 87 | + cc.hasFlowPath(source, dest) |
| 88 | + ) and |
| 89 | + ma.getArgument(0).(IntegerLiteral).getIntValue() < 128 and |
| 90 | + msg = "Key size should be at least 128 bits for AES encryption." |
| 91 | +} |
| 92 | + |
| 93 | +/** Holds if a DSA `KeyPairGenerator` is initialized with an insufficient key size. */ |
| 94 | +predicate incorrectUseOfDSA(MethodAccess ma, string msg) { |
| 95 | + ma.getMethod() instanceof KeyPairGeneratorInitMethod and |
| 96 | + exists( |
| 97 | + JavaSecurityKeyPairGenerator jpg, KeyPairGeneratorConfiguration kc, DataFlow::PathNode source, |
| 98 | + DataFlow::PathNode dest |
| 99 | + | |
| 100 | + jpg.getAlgoSpec().(StringLiteral).getValue() = "DSA" and |
| 101 | + source.getNode().asExpr() = jpg and |
| 102 | + dest.getNode().asExpr() = ma.getQualifier() and |
| 103 | + kc.hasFlowPath(source, dest) |
| 104 | + ) and |
| 105 | + ma.getArgument(0).(IntegerLiteral).getIntValue() < 2048 and |
| 106 | + msg = "Key size should be at least 2048 bits for DSA encryption." |
| 107 | +} |
| 108 | + |
| 109 | +/** Holds if a RSA `KeyPairGenerator` is initialized with an insufficient key size. */ |
| 110 | +predicate incorrectUseOfRSA(MethodAccess ma, string msg) { |
| 111 | + ma.getMethod() instanceof KeyPairGeneratorInitMethod and |
| 112 | + exists( |
| 113 | + JavaSecurityKeyPairGenerator jpg, KeyPairGeneratorConfiguration kc, DataFlow::PathNode source, |
| 114 | + DataFlow::PathNode dest |
| 115 | + | |
| 116 | + jpg.getAlgoSpec().(StringLiteral).getValue() = "RSA" and |
| 117 | + source.getNode().asExpr() = jpg and |
| 118 | + dest.getNode().asExpr() = ma.getQualifier() and |
| 119 | + kc.hasFlowPath(source, dest) |
| 120 | + ) and |
| 121 | + ma.getArgument(0).(IntegerLiteral).getIntValue() < 2048 and |
| 122 | + msg = "Key size should be at least 2048 bits for RSA encryption." |
| 123 | +} |
| 124 | + |
| 125 | +/** Holds if an EC `KeyPairGenerator` is initialized with an insufficient key size. */ |
| 126 | +predicate incorrectUseOfEC(MethodAccess ma, string msg) { |
| 127 | + ma.getMethod() instanceof KeyPairGeneratorInitMethod and |
| 128 | + exists( |
| 129 | + JavaSecurityKeyPairGenerator jpg, KeyPairGeneratorConfiguration kc, DataFlow::PathNode source, |
| 130 | + DataFlow::PathNode dest, ClassInstanceExpr cie |
| 131 | + | |
| 132 | + jpg.getAlgoSpec().(StringLiteral).getValue().matches("EC%") and //ECC variants such as ECDH and ECDSA |
| 133 | + source.getNode().asExpr() = jpg and |
| 134 | + dest.getNode().asExpr() = ma.getQualifier() and |
| 135 | + kc.hasFlowPath(source, dest) and |
| 136 | + exists(VariableAssign va | |
| 137 | + ma.getArgument(0).(VarAccess).getVariable() = va.getDestVar() and |
| 138 | + va.getSource() = cie and |
| 139 | + cie.getArgument(0) |
| 140 | + .(StringLiteral) |
| 141 | + .getRepresentedString() |
| 142 | + .regexpCapture(".*[a-zA-Z]+([0-9]+)[a-zA-Z]+.*", 1) |
| 143 | + .toInt() < 224 |
| 144 | + ) |
| 145 | + ) and |
| 146 | + msg = "Key size should be at least 224 bits for EC encryption." |
| 147 | +} |
| 148 | + |
| 149 | +from Expr e, string msg |
| 150 | +where |
| 151 | + incorrectUseOfAES(e, msg) or |
| 152 | + incorrectUseOfDSA(e, msg) or |
| 153 | + incorrectUseOfRSA(e, msg) or |
| 154 | + incorrectUseOfEC(e, msg) |
| 155 | +select e, msg |
0 commit comments