Skip to content

Commit 1bfdfc9

Browse files
Jami CogswellJami Cogswell
authored andcommitted
shorten class/predicate names
1 parent 1e80fa1 commit 1bfdfc9

File tree

1 file changed

+53
-59
lines changed

1 file changed

+53
-59
lines changed

java/ql/lib/semmle/code/java/security/InsufficientKeySize.qll

Lines changed: 53 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -15,42 +15,38 @@ abstract class InsufficientKeySizeSink extends DataFlow::Node {
1515
predicate hasState(DataFlow::FlowState state) { state instanceof DataFlow::FlowStateEmpty }
1616
}
1717

18+
/** Provides models for asymmetric cryptography. */
1819
private module Asymmetric {
20+
/** Provides models for non-elliptic-curve asymmetric cryptography. */
1921
private module NonEllipticCurve {
2022
/** A source for an insufficient key size used in RSA, DSA, and DH algorithms. */
21-
private class AsymmetricNonEcSource extends InsufficientKeySizeSource {
22-
AsymmetricNonEcSource() {
23-
this.asExpr().(IntegerLiteral).getIntValue() < getMinAsymNonEcKeySize()
24-
}
23+
private class Source extends InsufficientKeySizeSource {
24+
Source() { this.asExpr().(IntegerLiteral).getIntValue() < getMinKeySize() }
2525

26-
override predicate hasState(DataFlow::FlowState state) {
27-
state = getMinAsymNonEcKeySize().toString()
28-
}
26+
override predicate hasState(DataFlow::FlowState state) { state = getMinKeySize().toString() }
2927
}
3028

3129
/** A sink for an insufficient key size used in RSA, DSA, and DH algorithms. */
32-
private class AsymmetricNonEcSink extends InsufficientKeySizeSink {
33-
AsymmetricNonEcSink() {
34-
exists(AsymmetricInitMethodAccess ma, AsymmetricKeyGenerator kg |
35-
kg.getAlgoName().matches(["RSA", "DSA", "DH"]) and
36-
DataFlow::localExprFlow(kg, ma.getQualifier()) and
37-
this.asExpr() = ma.getKeySizeArg()
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()
3836
)
3937
or
40-
exists(AsymmetricNonEcSpec spec | this.asExpr() = spec.getKeySizeArg())
38+
exists(Spec spec | this.asExpr() = spec.getKeySizeArg())
4139
}
4240

43-
override predicate hasState(DataFlow::FlowState state) {
44-
state = getMinAsymNonEcKeySize().toString()
45-
}
41+
override predicate hasState(DataFlow::FlowState state) { state = getMinKeySize().toString() }
4642
}
4743

4844
/** Returns the minimum recommended key size for RSA, DSA, and DH algorithms. */
49-
private int getMinAsymNonEcKeySize() { result = 2048 }
45+
private int getMinKeySize() { result = 2048 }
5046

5147
/** An instance of an RSA, DSA, or DH algorithm specification. */
52-
private class AsymmetricNonEcSpec extends ClassInstanceExpr {
53-
AsymmetricNonEcSpec() {
48+
private class Spec extends ClassInstanceExpr {
49+
Spec() {
5450
this.getConstructedType() instanceof RsaKeyGenParameterSpec or
5551
this.getConstructedType() instanceof DsaGenParameterSpec or
5652
this.getConstructedType() instanceof DhGenParameterSpec
@@ -61,44 +57,41 @@ private module Asymmetric {
6157
}
6258
}
6359

60+
/** Provides models for elliptic-curve asymmetric cryptography. */
6461
private module EllipticCurve {
6562
/** A source for an insufficient key size used in elliptic curve (EC) algorithms. */
66-
private class AsymmetricEcSource extends InsufficientKeySizeSource {
67-
AsymmetricEcSource() {
68-
this.asExpr().(IntegerLiteral).getIntValue() < getMinAsymEcKeySize()
63+
private class Source extends InsufficientKeySizeSource {
64+
Source() {
65+
this.asExpr().(IntegerLiteral).getIntValue() < getMinKeySize()
6966
or
7067
// the below is needed for cases when the key size is embedded in the curve name
71-
getEcKeySize(this.asExpr().(StringLiteral).getValue()) < getMinAsymEcKeySize()
68+
getKeySize(this.asExpr().(StringLiteral).getValue()) < getMinKeySize()
7269
}
7370

74-
override predicate hasState(DataFlow::FlowState state) {
75-
state = getMinAsymEcKeySize().toString()
76-
}
71+
override predicate hasState(DataFlow::FlowState state) { state = getMinKeySize().toString() }
7772
}
7873

7974
/** A sink for an insufficient key size used in elliptic curve (EC) algorithms. */
80-
private class AsymmetricEcSink extends InsufficientKeySizeSink {
81-
AsymmetricEcSink() {
82-
exists(AsymmetricInitMethodAccess ma, AsymmetricKeyGenerator kg |
83-
kg.getAlgoName().matches("EC%") and
84-
DataFlow::localExprFlow(kg, ma.getQualifier()) and
85-
this.asExpr() = ma.getKeySizeArg()
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()
8681
)
8782
or
88-
exists(AsymmetricEcSpec s | this.asExpr() = s.getKeySizeArg())
83+
exists(Spec s | this.asExpr() = s.getKeySizeArg())
8984
}
9085

91-
override predicate hasState(DataFlow::FlowState state) {
92-
state = getMinAsymEcKeySize().toString()
93-
}
86+
override predicate hasState(DataFlow::FlowState state) { state = getMinKeySize().toString() }
9487
}
9588

9689
/** Returns the minimum recommended key size for elliptic curve (EC) algorithms. */
97-
private int getMinAsymEcKeySize() { result = 256 }
90+
private int getMinKeySize() { result = 256 }
9891

9992
/** Returns the key size from an EC algorithm's curve name string */
10093
bindingset[algorithm]
101-
private int getEcKeySize(string algorithm) {
94+
private int getKeySize(string algorithm) {
10295
algorithm.matches("sec%") and // specification such as "secp256r1"
10396
result = algorithm.regexpCapture("sec[p|t](\\d+)[a-zA-Z].*", 1).toInt()
10497
or
@@ -110,8 +103,8 @@ private module Asymmetric {
110103
}
111104

112105
/** An instance of an elliptic curve (EC) algorithm specification. */
113-
private class AsymmetricEcSpec extends ClassInstanceExpr {
114-
AsymmetricEcSpec() { this.getConstructedType() instanceof EcGenParameterSpec }
106+
private class Spec extends ClassInstanceExpr {
107+
Spec() { this.getConstructedType() instanceof EcGenParameterSpec }
115108

116109
/** Gets the `keysize` argument of this instance. */
117110
Argument getKeySizeArg() { result = this.getArgument(0) }
@@ -122,8 +115,8 @@ private module Asymmetric {
122115
* A call to the `initialize` method declared in `java.security.KeyPairGenerator`
123116
* or to the `init` method declared in `java.security.AlgorithmParameterGenerator`.
124117
*/
125-
private class AsymmetricInitMethodAccess extends MethodAccess {
126-
AsymmetricInitMethodAccess() {
118+
private class KeyPairGenInit extends MethodAccess {
119+
KeyPairGenInit() {
127120
this.getMethod() instanceof KeyPairGeneratorInitMethod or
128121
this.getMethod() instanceof AlgoParamGeneratorInitMethod
129122
}
@@ -136,8 +129,8 @@ private module Asymmetric {
136129
* An instance of a `java.security.KeyPairGenerator`
137130
* or of a `java.security.AlgorithmParameterGenerator`.
138131
*/
139-
private class AsymmetricKeyGenerator extends AlgoGeneratorObject {
140-
AsymmetricKeyGenerator() {
132+
private class KeyPairGen extends GeneratorAlgoSpec {
133+
KeyPairGen() {
141134
this instanceof JavaSecurityKeyPairGenerator or
142135
this instanceof JavaSecurityAlgoParamGenerator
143136
}
@@ -152,46 +145,47 @@ private module Asymmetric {
152145
}
153146
}
154147

148+
/** Provides models for symmetric cryptography. */
155149
private module Symmetric {
156150
/** A source for an insufficient key size used in AES algorithms. */
157-
private class SymmetricSource extends InsufficientKeySizeSource {
158-
SymmetricSource() { this.asExpr().(IntegerLiteral).getIntValue() < getMinSymKeySize() }
151+
private class Source extends InsufficientKeySizeSource {
152+
Source() { this.asExpr().(IntegerLiteral).getIntValue() < getMinKeySize() }
159153

160-
override predicate hasState(DataFlow::FlowState state) { state = getMinSymKeySize().toString() }
154+
override predicate hasState(DataFlow::FlowState state) { state = getMinKeySize().toString() }
161155
}
162156

163157
/** A sink for an insufficient key size used in AES algorithms. */
164-
private class SymmetricSink extends InsufficientKeySizeSink {
165-
SymmetricSink() {
166-
exists(SymmetricInitMethodAccess ma, SymmetricKeyGenerator kg |
158+
private class Sink extends InsufficientKeySizeSink {
159+
Sink() {
160+
exists(KeyGenInit kgInit, KeyGen kg |
167161
kg.getAlgoName() = "AES" and
168-
DataFlow::localExprFlow(kg, ma.getQualifier()) and
169-
this.asExpr() = ma.getKeySizeArg()
162+
DataFlow::localExprFlow(kg, kgInit.getQualifier()) and
163+
this.asExpr() = kgInit.getKeySizeArg()
170164
)
171165
}
172166

173-
override predicate hasState(DataFlow::FlowState state) { state = getMinSymKeySize().toString() }
167+
override predicate hasState(DataFlow::FlowState state) { state = getMinKeySize().toString() }
174168
}
175169

176170
/** Returns the minimum recommended key size for AES algorithms. */
177-
private int getMinSymKeySize() { result = 128 }
171+
private int getMinKeySize() { result = 128 }
178172

179173
/** A call to the `init` method declared in `javax.crypto.KeyGenerator`. */
180-
private class SymmetricInitMethodAccess extends MethodAccess {
181-
SymmetricInitMethodAccess() { this.getMethod() instanceof KeyGeneratorInitMethod }
174+
private class KeyGenInit extends MethodAccess {
175+
KeyGenInit() { this.getMethod() instanceof KeyGeneratorInitMethod }
182176

183177
/** Gets the `keysize` argument of this call. */
184178
Argument getKeySizeArg() { result = this.getArgument(0) }
185179
}
186180

187181
/** An instance of a `javax.crypto.KeyGenerator`. */
188-
private class SymmetricKeyGenerator extends AlgoGeneratorObject instanceof JavaxCryptoKeyGenerator {
182+
private class KeyGen extends GeneratorAlgoSpec instanceof JavaxCryptoKeyGenerator {
189183
override Expr getAlgoSpec() { result = JavaxCryptoKeyGenerator.super.getAlgoSpec() }
190184
}
191185
}
192186

193187
/** An instance of a generator that specifies an encryption algorithm. */
194-
abstract private class AlgoGeneratorObject extends CryptoAlgoSpec {
188+
abstract private class GeneratorAlgoSpec extends CryptoAlgoSpec {
195189
/** Returns an uppercase string representing the algorithm name specified by this generator object. */
196190
string getAlgoName() { result = this.getAlgoSpec().(StringLiteral).getValue().toUpperCase() }
197191
}

0 commit comments

Comments
 (0)