|
| 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 `java.security.spec.ECGenParameterSpec`. */ |
| 15 | +class ECGenParameterSpec extends RefType { |
| 16 | + ECGenParameterSpec() { this.hasQualifiedName("java.security.spec", "ECGenParameterSpec") } |
| 17 | +} |
| 18 | + |
| 19 | +/** The `init` method declared in `javax.crypto.KeyGenerator`. */ |
| 20 | +class KeyGeneratorInitMethod extends Method { |
| 21 | + KeyGeneratorInitMethod() { |
| 22 | + getDeclaringType() instanceof KeyGenerator and |
| 23 | + hasName("init") |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +/** The `initialize` method declared in `java.security.KeyPairGenerator`. */ |
| 28 | +class KeyPairGeneratorInitMethod extends Method { |
| 29 | + KeyPairGeneratorInitMethod() { |
| 30 | + getDeclaringType() instanceof KeyPairGenerator and |
| 31 | + hasName("initialize") |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +/** Returns the key size in the EC algorithm string */ |
| 36 | +bindingset[algorithm] |
| 37 | +int getECKeySize(string algorithm) { |
| 38 | + algorithm.matches("sec%") and // specification such as "secp256r1" |
| 39 | + result = algorithm.regexpCapture("sec[p|t](\\d+)[a-zA-Z].*", 1).toInt() |
| 40 | + or |
| 41 | + algorithm.matches("X9.62%") and //specification such as "X9.62 prime192v2" |
| 42 | + result = algorithm.regexpCapture("X9\\.62 .*[a-zA-Z](\\d+)[a-zA-Z].*", 1).toInt() |
| 43 | + or |
| 44 | + (algorithm.matches("prime%") or algorithm.matches("c2tnb%")) and //specification such as "prime192v2" |
| 45 | + result = algorithm.regexpCapture(".*[a-zA-Z](\\d+)[a-zA-Z].*", 1).toInt() |
| 46 | +} |
| 47 | + |
| 48 | +/** Taint configuration tracking flow from a key generator to a `init` method call. */ |
| 49 | +class KeyGeneratorInitConfiguration extends TaintTracking::Configuration { |
| 50 | + KeyGeneratorInitConfiguration() { this = "KeyGeneratorInitConfiguration" } |
| 51 | + |
| 52 | + override predicate isSource(DataFlow::Node source) { |
| 53 | + exists(JavaxCryptoKeyGenerator jcg | jcg = source.asExpr()) |
| 54 | + } |
| 55 | + |
| 56 | + override predicate isSink(DataFlow::Node sink) { |
| 57 | + exists(MethodAccess ma | |
| 58 | + ma.getMethod() instanceof KeyGeneratorInitMethod and |
| 59 | + sink.asExpr() = ma.getQualifier() |
| 60 | + ) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +/** Taint configuration tracking flow from a keypair generator to a `initialize` method call. */ |
| 65 | +class KeyPairGeneratorInitConfiguration extends TaintTracking::Configuration { |
| 66 | + KeyPairGeneratorInitConfiguration() { this = "KeyPairGeneratorInitConfiguration" } |
| 67 | + |
| 68 | + override predicate isSource(DataFlow::Node source) { |
| 69 | + exists(JavaSecurityKeyPairGenerator jkg | jkg = source.asExpr()) |
| 70 | + } |
| 71 | + |
| 72 | + override predicate isSink(DataFlow::Node sink) { |
| 73 | + exists(MethodAccess ma | |
| 74 | + ma.getMethod() instanceof KeyPairGeneratorInitMethod and |
| 75 | + sink.asExpr() = ma.getQualifier() |
| 76 | + ) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +/** Holds if a symmetric `KeyGenerator` implementing encryption algorithm `type` and initialized by `ma` uses an insufficient key size. `msg` provides a human-readable description of the problem. */ |
| 81 | +bindingset[type] |
| 82 | +predicate hasShortSymmetricKey(MethodAccess ma, string msg, string type) { |
| 83 | + ma.getMethod() instanceof KeyGeneratorInitMethod and |
| 84 | + exists( |
| 85 | + JavaxCryptoKeyGenerator jcg, KeyGeneratorInitConfiguration cc, DataFlow::PathNode source, |
| 86 | + DataFlow::PathNode dest |
| 87 | + | |
| 88 | + jcg.getAlgoSpec().(StringLiteral).getValue() = type and |
| 89 | + source.getNode().asExpr() = jcg and |
| 90 | + dest.getNode().asExpr() = ma.getQualifier() and |
| 91 | + cc.hasFlowPath(source, dest) |
| 92 | + ) and |
| 93 | + ma.getArgument(0).(IntegerLiteral).getIntValue() < 128 and |
| 94 | + msg = "Key size should be at least 128 bits for " + type + " encryption." |
| 95 | +} |
| 96 | + |
| 97 | +/** Holds if an AES `KeyGenerator` initialized by `ma` uses an insufficient key size. `msg` provides a human-readable description of the problem. */ |
| 98 | +predicate hasShortAESKey(MethodAccess ma, string msg) { hasShortSymmetricKey(ma, msg, "AES") } |
| 99 | + |
| 100 | +/** Holds if an asymmetric `KeyPairGenerator` implementing encryption algorithm `type` and initialized by `ma` uses an insufficient key size. `msg` provides a human-readable description of the problem. */ |
| 101 | +bindingset[type] |
| 102 | +predicate hasShortAsymmetricKeyPair(MethodAccess ma, string msg, string type) { |
| 103 | + ma.getMethod() instanceof KeyPairGeneratorInitMethod and |
| 104 | + exists( |
| 105 | + JavaSecurityKeyPairGenerator jpg, KeyPairGeneratorInitConfiguration kc, |
| 106 | + DataFlow::PathNode source, DataFlow::PathNode dest |
| 107 | + | |
| 108 | + jpg.getAlgoSpec().(StringLiteral).getValue().toUpperCase() = type and |
| 109 | + source.getNode().asExpr() = jpg and |
| 110 | + dest.getNode().asExpr() = ma.getQualifier() and |
| 111 | + kc.hasFlowPath(source, dest) |
| 112 | + ) and |
| 113 | + ma.getArgument(0).(IntegerLiteral).getIntValue() < 2048 and |
| 114 | + msg = "Key size should be at least 2048 bits for " + type + " encryption." |
| 115 | +} |
| 116 | + |
| 117 | +/** Holds if a DSA `KeyPairGenerator` initialized by `ma` uses an insufficient key size. `msg` provides a human-readable description of the problem. */ |
| 118 | +predicate hasShortDSAKeyPair(MethodAccess ma, string msg) { |
| 119 | + hasShortAsymmetricKeyPair(ma, msg, "DSA") or hasShortAsymmetricKeyPair(ma, msg, "DH") |
| 120 | +} |
| 121 | + |
| 122 | +/** Holds if a RSA `KeyPairGenerator` initialized by `ma` uses an insufficient key size. `msg` provides a human-readable description of the problem. */ |
| 123 | +predicate hasShortRSAKeyPair(MethodAccess ma, string msg) { |
| 124 | + hasShortAsymmetricKeyPair(ma, msg, "RSA") |
| 125 | +} |
| 126 | + |
| 127 | +/** Holds if an EC `KeyPairGenerator` initialized by `ma` uses an insufficient key size. `msg` provides a human-readable description of the problem. */ |
| 128 | +predicate hasShortECKeyPair(MethodAccess ma, string msg) { |
| 129 | + ma.getMethod() instanceof KeyPairGeneratorInitMethod and |
| 130 | + exists( |
| 131 | + JavaSecurityKeyPairGenerator jpg, KeyPairGeneratorInitConfiguration kc, |
| 132 | + DataFlow::PathNode source, DataFlow::PathNode dest, ClassInstanceExpr cie |
| 133 | + | |
| 134 | + jpg.getAlgoSpec().(StringLiteral).getValue().matches("EC%") and // ECC variants such as ECDH and ECDSA |
| 135 | + source.getNode().asExpr() = jpg and |
| 136 | + dest.getNode().asExpr() = ma.getQualifier() and |
| 137 | + kc.hasFlowPath(source, dest) and |
| 138 | + DataFlow::localExprFlow(cie, ma.getArgument(0)) and |
| 139 | + ma.getArgument(0).getType() instanceof ECGenParameterSpec and |
| 140 | + getECKeySize(cie.getArgument(0).(StringLiteral).getRepresentedString()) < 256 |
| 141 | + ) and |
| 142 | + msg = "Key size should be at least 256 bits for EC encryption." |
| 143 | +} |
| 144 | + |
| 145 | +from Expr e, string msg |
| 146 | +where |
| 147 | + hasShortAESKey(e, msg) or |
| 148 | + hasShortDSAKeyPair(e, msg) or |
| 149 | + hasShortRSAKeyPair(e, msg) or |
| 150 | + hasShortECKeyPair(e, msg) |
| 151 | +select e, msg |
0 commit comments