@@ -15,42 +15,38 @@ abstract class InsufficientKeySizeSink extends DataFlow::Node {
15
15
predicate hasState ( DataFlow:: FlowState state ) { state instanceof DataFlow:: FlowStateEmpty }
16
16
}
17
17
18
+ /** Provides models for asymmetric cryptography. */
18
19
private module Asymmetric {
20
+ /** Provides models for non-elliptic-curve asymmetric cryptography. */
19
21
private module NonEllipticCurve {
20
22
/** 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 ( ) }
25
25
26
- override predicate hasState ( DataFlow:: FlowState state ) {
27
- state = getMinAsymNonEcKeySize ( ) .toString ( )
28
- }
26
+ override predicate hasState ( DataFlow:: FlowState state ) { state = getMinKeySize ( ) .toString ( ) }
29
27
}
30
28
31
29
/** 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 ( )
38
36
)
39
37
or
40
- exists ( AsymmetricNonEcSpec spec | this .asExpr ( ) = spec .getKeySizeArg ( ) )
38
+ exists ( Spec spec | this .asExpr ( ) = spec .getKeySizeArg ( ) )
41
39
}
42
40
43
- override predicate hasState ( DataFlow:: FlowState state ) {
44
- state = getMinAsymNonEcKeySize ( ) .toString ( )
45
- }
41
+ override predicate hasState ( DataFlow:: FlowState state ) { state = getMinKeySize ( ) .toString ( ) }
46
42
}
47
43
48
44
/** Returns the minimum recommended key size for RSA, DSA, and DH algorithms. */
49
- private int getMinAsymNonEcKeySize ( ) { result = 2048 }
45
+ private int getMinKeySize ( ) { result = 2048 }
50
46
51
47
/** 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 ( ) {
54
50
this .getConstructedType ( ) instanceof RsaKeyGenParameterSpec or
55
51
this .getConstructedType ( ) instanceof DsaGenParameterSpec or
56
52
this .getConstructedType ( ) instanceof DhGenParameterSpec
@@ -61,44 +57,41 @@ private module Asymmetric {
61
57
}
62
58
}
63
59
60
+ /** Provides models for elliptic-curve asymmetric cryptography. */
64
61
private module EllipticCurve {
65
62
/** 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 ( )
69
66
or
70
67
// 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 ( )
72
69
}
73
70
74
- override predicate hasState ( DataFlow:: FlowState state ) {
75
- state = getMinAsymEcKeySize ( ) .toString ( )
76
- }
71
+ override predicate hasState ( DataFlow:: FlowState state ) { state = getMinKeySize ( ) .toString ( ) }
77
72
}
78
73
79
74
/** 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 ( )
86
81
)
87
82
or
88
- exists ( AsymmetricEcSpec s | this .asExpr ( ) = s .getKeySizeArg ( ) )
83
+ exists ( Spec s | this .asExpr ( ) = s .getKeySizeArg ( ) )
89
84
}
90
85
91
- override predicate hasState ( DataFlow:: FlowState state ) {
92
- state = getMinAsymEcKeySize ( ) .toString ( )
93
- }
86
+ override predicate hasState ( DataFlow:: FlowState state ) { state = getMinKeySize ( ) .toString ( ) }
94
87
}
95
88
96
89
/** Returns the minimum recommended key size for elliptic curve (EC) algorithms. */
97
- private int getMinAsymEcKeySize ( ) { result = 256 }
90
+ private int getMinKeySize ( ) { result = 256 }
98
91
99
92
/** Returns the key size from an EC algorithm's curve name string */
100
93
bindingset [ algorithm]
101
- private int getEcKeySize ( string algorithm ) {
94
+ private int getKeySize ( string algorithm ) {
102
95
algorithm .matches ( "sec%" ) and // specification such as "secp256r1"
103
96
result = algorithm .regexpCapture ( "sec[p|t](\\d+)[a-zA-Z].*" , 1 ) .toInt ( )
104
97
or
@@ -110,8 +103,8 @@ private module Asymmetric {
110
103
}
111
104
112
105
/** 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 }
115
108
116
109
/** Gets the `keysize` argument of this instance. */
117
110
Argument getKeySizeArg ( ) { result = this .getArgument ( 0 ) }
@@ -122,8 +115,8 @@ private module Asymmetric {
122
115
* A call to the `initialize` method declared in `java.security.KeyPairGenerator`
123
116
* or to the `init` method declared in `java.security.AlgorithmParameterGenerator`.
124
117
*/
125
- private class AsymmetricInitMethodAccess extends MethodAccess {
126
- AsymmetricInitMethodAccess ( ) {
118
+ private class KeyPairGenInit extends MethodAccess {
119
+ KeyPairGenInit ( ) {
127
120
this .getMethod ( ) instanceof KeyPairGeneratorInitMethod or
128
121
this .getMethod ( ) instanceof AlgoParamGeneratorInitMethod
129
122
}
@@ -136,8 +129,8 @@ private module Asymmetric {
136
129
* An instance of a `java.security.KeyPairGenerator`
137
130
* or of a `java.security.AlgorithmParameterGenerator`.
138
131
*/
139
- private class AsymmetricKeyGenerator extends AlgoGeneratorObject {
140
- AsymmetricKeyGenerator ( ) {
132
+ private class KeyPairGen extends GeneratorAlgoSpec {
133
+ KeyPairGen ( ) {
141
134
this instanceof JavaSecurityKeyPairGenerator or
142
135
this instanceof JavaSecurityAlgoParamGenerator
143
136
}
@@ -152,46 +145,47 @@ private module Asymmetric {
152
145
}
153
146
}
154
147
148
+ /** Provides models for symmetric cryptography. */
155
149
private module Symmetric {
156
150
/** 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 ( ) }
159
153
160
- override predicate hasState ( DataFlow:: FlowState state ) { state = getMinSymKeySize ( ) .toString ( ) }
154
+ override predicate hasState ( DataFlow:: FlowState state ) { state = getMinKeySize ( ) .toString ( ) }
161
155
}
162
156
163
157
/** 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 |
167
161
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 ( )
170
164
)
171
165
}
172
166
173
- override predicate hasState ( DataFlow:: FlowState state ) { state = getMinSymKeySize ( ) .toString ( ) }
167
+ override predicate hasState ( DataFlow:: FlowState state ) { state = getMinKeySize ( ) .toString ( ) }
174
168
}
175
169
176
170
/** Returns the minimum recommended key size for AES algorithms. */
177
- private int getMinSymKeySize ( ) { result = 128 }
171
+ private int getMinKeySize ( ) { result = 128 }
178
172
179
173
/** 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 }
182
176
183
177
/** Gets the `keysize` argument of this call. */
184
178
Argument getKeySizeArg ( ) { result = this .getArgument ( 0 ) }
185
179
}
186
180
187
181
/** An instance of a `javax.crypto.KeyGenerator`. */
188
- private class SymmetricKeyGenerator extends AlgoGeneratorObject instanceof JavaxCryptoKeyGenerator {
182
+ private class KeyGen extends GeneratorAlgoSpec instanceof JavaxCryptoKeyGenerator {
189
183
override Expr getAlgoSpec ( ) { result = JavaxCryptoKeyGenerator .super .getAlgoSpec ( ) }
190
184
}
191
185
}
192
186
193
187
/** An instance of a generator that specifies an encryption algorithm. */
194
- abstract private class AlgoGeneratorObject extends CryptoAlgoSpec {
188
+ abstract private class GeneratorAlgoSpec extends CryptoAlgoSpec {
195
189
/** Returns an uppercase string representing the algorithm name specified by this generator object. */
196
190
string getAlgoName ( ) { result = this .getAlgoSpec ( ) .( StringLiteral ) .getValue ( ) .toUpperCase ( ) }
197
191
}
0 commit comments