|
| 1 | +/** Provides classes and predicates related to insufficient key sizes in Java. */ |
| 2 | + |
| 3 | +private import semmle.code.java.security.Encryption |
| 4 | +private import semmle.code.java.dataflow.DataFlow |
| 5 | + |
| 6 | +/** A source for an insufficient key size. */ |
| 7 | +abstract class InsufficientKeySizeSource extends DataFlow::Node { |
| 8 | + /** Holds if this source has the specified `state`. */ |
| 9 | + predicate hasState(DataFlow::FlowState state) { state instanceof DataFlow::FlowStateEmpty } |
| 10 | +} |
| 11 | + |
| 12 | +/** A sink for an insufficient key size. */ |
| 13 | +abstract class InsufficientKeySizeSink extends DataFlow::Node { |
| 14 | + /** Holds if this sink has the specified `state`. */ |
| 15 | + predicate hasState(DataFlow::FlowState state) { state instanceof DataFlow::FlowStateEmpty } |
| 16 | +} |
| 17 | + |
| 18 | +/** Provides models for asymmetric cryptography. */ |
| 19 | +private module Asymmetric { |
| 20 | + /** Provides models for non-elliptic-curve asymmetric cryptography. */ |
| 21 | + private module NonEllipticCurve { |
| 22 | + /** A source for an insufficient key size used in RSA, DSA, and DH algorithms. */ |
| 23 | + private class Source extends InsufficientKeySizeSource { |
| 24 | + Source() { this.asExpr().(IntegerLiteral).getIntValue() < getMinKeySize() } |
| 25 | + |
| 26 | + override predicate hasState(DataFlow::FlowState state) { state = getMinKeySize().toString() } |
| 27 | + } |
| 28 | + |
| 29 | + /** A sink for an insufficient key size used in RSA, DSA, and DH algorithms. */ |
| 30 | + private class Sink extends InsufficientKeySizeSink { |
| 31 | + Sink() { |
| 32 | + exists(KeyPairGenInit kpgInit, KeyPairGen kpg | |
| 33 | + kpg.getAlgoName().matches(["RSA", "DSA", "DH"]) and |
| 34 | + DataFlow::localExprFlow(kpg, kpgInit.getQualifier()) and |
| 35 | + this.asExpr() = kpgInit.getKeySizeArg() |
| 36 | + ) |
| 37 | + or |
| 38 | + exists(Spec spec | this.asExpr() = spec.getKeySizeArg()) |
| 39 | + } |
| 40 | + |
| 41 | + override predicate hasState(DataFlow::FlowState state) { state = getMinKeySize().toString() } |
| 42 | + } |
| 43 | + |
| 44 | + /** Returns the minimum recommended key size for RSA, DSA, and DH algorithms. */ |
| 45 | + private int getMinKeySize() { result = 2048 } |
| 46 | + |
| 47 | + /** An instance of an RSA, DSA, or DH algorithm specification. */ |
| 48 | + private class Spec extends ClassInstanceExpr { |
| 49 | + Spec() { |
| 50 | + this.getConstructedType() instanceof RsaKeyGenParameterSpec or |
| 51 | + this.getConstructedType() instanceof DsaGenParameterSpec or |
| 52 | + this.getConstructedType() instanceof DhGenParameterSpec |
| 53 | + } |
| 54 | + |
| 55 | + /** Gets the `keysize` argument of this instance. */ |
| 56 | + Argument getKeySizeArg() { result = this.getArgument(0) } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + /** Provides models for elliptic-curve asymmetric cryptography. */ |
| 61 | + private module EllipticCurve { |
| 62 | + /** A source for an insufficient key size used in elliptic curve (EC) algorithms. */ |
| 63 | + private class Source extends InsufficientKeySizeSource { |
| 64 | + Source() { |
| 65 | + this.asExpr().(IntegerLiteral).getIntValue() < getMinKeySize() |
| 66 | + or |
| 67 | + // the below is needed for cases when the key size is embedded in the curve name |
| 68 | + getKeySize(this.asExpr().(StringLiteral).getValue()) < getMinKeySize() |
| 69 | + } |
| 70 | + |
| 71 | + override predicate hasState(DataFlow::FlowState state) { state = getMinKeySize().toString() } |
| 72 | + } |
| 73 | + |
| 74 | + /** A sink for an insufficient key size used in elliptic curve (EC) algorithms. */ |
| 75 | + private class Sink extends InsufficientKeySizeSink { |
| 76 | + Sink() { |
| 77 | + exists(KeyPairGenInit kpgInit, KeyPairGen kpg | |
| 78 | + kpg.getAlgoName().matches("EC%") and |
| 79 | + DataFlow::localExprFlow(kpg, kpgInit.getQualifier()) and |
| 80 | + this.asExpr() = kpgInit.getKeySizeArg() |
| 81 | + ) |
| 82 | + or |
| 83 | + exists(Spec s | this.asExpr() = s.getKeySizeArg()) |
| 84 | + } |
| 85 | + |
| 86 | + override predicate hasState(DataFlow::FlowState state) { state = getMinKeySize().toString() } |
| 87 | + } |
| 88 | + |
| 89 | + /** Returns the minimum recommended key size for elliptic curve (EC) algorithms. */ |
| 90 | + private int getMinKeySize() { result = 256 } |
| 91 | + |
| 92 | + /** Returns the key size from an EC algorithm's curve name string */ |
| 93 | + bindingset[algorithm] |
| 94 | + private int getKeySize(string algorithm) { |
| 95 | + algorithm.matches("sec%") and // specification such as "secp256r1" |
| 96 | + result = algorithm.regexpCapture("sec[p|t](\\d+)[a-zA-Z].*", 1).toInt() |
| 97 | + or |
| 98 | + algorithm.matches("X9.62%") and // specification such as "X9.62 prime192v2" |
| 99 | + result = algorithm.regexpCapture("X9\\.62 .*[a-zA-Z](\\d+)[a-zA-Z].*", 1).toInt() |
| 100 | + or |
| 101 | + algorithm.matches(["prime%", "c2tnb%"]) and // specification such as "prime192v2" |
| 102 | + result = algorithm.regexpCapture(".*[a-zA-Z](\\d+)[a-zA-Z].*", 1).toInt() |
| 103 | + } |
| 104 | + |
| 105 | + /** An instance of an elliptic curve (EC) algorithm specification. */ |
| 106 | + private class Spec extends ClassInstanceExpr { |
| 107 | + Spec() { this.getConstructedType() instanceof EcGenParameterSpec } |
| 108 | + |
| 109 | + /** Gets the `keysize` argument of this instance. */ |
| 110 | + Argument getKeySizeArg() { result = this.getArgument(0) } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * A call to the `initialize` method declared in `java.security.KeyPairGenerator` |
| 116 | + * or to the `init` method declared in `java.security.AlgorithmParameterGenerator`. |
| 117 | + */ |
| 118 | + private class KeyPairGenInit extends MethodAccess { |
| 119 | + KeyPairGenInit() { |
| 120 | + this.getMethod() instanceof KeyPairGeneratorInitMethod or |
| 121 | + this.getMethod() instanceof AlgoParamGeneratorInitMethod |
| 122 | + } |
| 123 | + |
| 124 | + /** Gets the `keysize` argument of this call. */ |
| 125 | + Argument getKeySizeArg() { result = this.getArgument(0) } |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * An instance of a `java.security.KeyPairGenerator` |
| 130 | + * or of a `java.security.AlgorithmParameterGenerator`. |
| 131 | + */ |
| 132 | + private class KeyPairGen extends GeneratorAlgoSpec { |
| 133 | + KeyPairGen() { |
| 134 | + this instanceof JavaSecurityKeyPairGenerator or |
| 135 | + this instanceof JavaSecurityAlgoParamGenerator |
| 136 | + } |
| 137 | + |
| 138 | + override Expr getAlgoSpec() { |
| 139 | + result = |
| 140 | + [ |
| 141 | + this.(JavaSecurityKeyPairGenerator).getAlgoSpec(), |
| 142 | + this.(JavaSecurityAlgoParamGenerator).getAlgoSpec() |
| 143 | + ] |
| 144 | + } |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +/** Provides models for symmetric cryptography. */ |
| 149 | +private module Symmetric { |
| 150 | + /** A source for an insufficient key size used in AES algorithms. */ |
| 151 | + private class Source extends InsufficientKeySizeSource { |
| 152 | + Source() { this.asExpr().(IntegerLiteral).getIntValue() < getMinKeySize() } |
| 153 | + |
| 154 | + override predicate hasState(DataFlow::FlowState state) { state = getMinKeySize().toString() } |
| 155 | + } |
| 156 | + |
| 157 | + /** A sink for an insufficient key size used in AES algorithms. */ |
| 158 | + private class Sink extends InsufficientKeySizeSink { |
| 159 | + Sink() { |
| 160 | + exists(KeyGenInit kgInit, KeyGen kg | |
| 161 | + kg.getAlgoName() = "AES" and |
| 162 | + DataFlow::localExprFlow(kg, kgInit.getQualifier()) and |
| 163 | + this.asExpr() = kgInit.getKeySizeArg() |
| 164 | + ) |
| 165 | + } |
| 166 | + |
| 167 | + override predicate hasState(DataFlow::FlowState state) { state = getMinKeySize().toString() } |
| 168 | + } |
| 169 | + |
| 170 | + /** Returns the minimum recommended key size for AES algorithms. */ |
| 171 | + private int getMinKeySize() { result = 128 } |
| 172 | + |
| 173 | + /** A call to the `init` method declared in `javax.crypto.KeyGenerator`. */ |
| 174 | + private class KeyGenInit extends MethodAccess { |
| 175 | + KeyGenInit() { this.getMethod() instanceof KeyGeneratorInitMethod } |
| 176 | + |
| 177 | + /** Gets the `keysize` argument of this call. */ |
| 178 | + Argument getKeySizeArg() { result = this.getArgument(0) } |
| 179 | + } |
| 180 | + |
| 181 | + /** An instance of a `javax.crypto.KeyGenerator`. */ |
| 182 | + private class KeyGen extends GeneratorAlgoSpec instanceof JavaxCryptoKeyGenerator { |
| 183 | + override Expr getAlgoSpec() { result = JavaxCryptoKeyGenerator.super.getAlgoSpec() } |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +/** An instance of a generator that specifies an encryption algorithm. */ |
| 188 | +abstract private class GeneratorAlgoSpec extends CryptoAlgoSpec { |
| 189 | + /** Returns an uppercase string representing the algorithm name specified by this generator object. */ |
| 190 | + string getAlgoName() { |
| 191 | + result = this.getAlgoSpec().(CompileTimeConstantExpr).getStringValue().toUpperCase() |
| 192 | + } |
| 193 | +} |
0 commit comments