1
1
import javax .crypto .KeyGenerator ;
2
2
import java .security .KeyPairGenerator ;
3
+ import java .security .AlgorithmParameterGenerator ;
3
4
4
5
import java .security .spec .ECGenParameterSpec ;
5
6
import java .security .spec .RSAKeyGenParameterSpec ;
@@ -30,8 +31,8 @@ public void keySizeTesting() throws java.security.NoSuchAlgorithmException, java
30
31
keyGen4 .init (size2 ); // $ hasInsufficientKeySize
31
32
32
33
/* Test variables passed to another method */
33
- KeyGenerator keyGen = KeyGenerator .getInstance ("AES" ); // MISSING: test KeyGenerator variable as argument
34
- testSymmetricVariable (size2 , keyGen ); // test with variable as key size
34
+ KeyGenerator keyGen5 = KeyGenerator .getInstance ("AES" ); // MISSING: test KeyGenerator variable as argument
35
+ testSymmetricVariable (size2 , keyGen5 ); // test with variable as key size
35
36
testSymmetricInt (64 ); // test with int literal as key size
36
37
}
37
38
@@ -62,9 +63,13 @@ public void keySizeTesting() throws java.security.NoSuchAlgorithmException, java
62
63
keyPairGen6 .initialize (size2 ); // $ hasInsufficientKeySize
63
64
64
65
/* Test variables passed to another method */
65
- KeyPairGenerator keyPairGen = KeyPairGenerator .getInstance ("RSA" ); // MISSING: test KeyGenerator variable as argument
66
- testAsymmetricNonEcVariable (size2 , keyPairGen ); // test with variable as key size
66
+ KeyPairGenerator keyPairGen7 = KeyPairGenerator .getInstance ("RSA" ); // MISSING: test KeyGenerator variable as argument
67
+ testAsymmetricNonEcVariable (size2 , keyPairGen7 ); // test with variable as key size
67
68
testAsymmetricNonEcInt (1024 ); // test with int literal as key size
69
+
70
+ /* Test getting key size as return value of another method */
71
+ KeyPairGenerator keyPairGen8 = KeyPairGenerator .getInstance ("RSA" );
72
+ keyPairGen8 .initialize (getRSAKeySize ()); // $ hasInsufficientKeySize
68
73
}
69
74
70
75
// DSA (Asymmetric): minimum recommended key size is 2048
@@ -82,6 +87,10 @@ public void keySizeTesting() throws java.security.NoSuchAlgorithmException, java
82
87
83
88
KeyPairGenerator keyPairGen4 = KeyPairGenerator .getInstance ("DSA" );
84
89
keyPairGen4 .initialize (new DSAGenParameterSpec (1024 , 0 )); // $ hasInsufficientKeySize
90
+
91
+ /* Test `AlgorithmParameterGenerator` */
92
+ AlgorithmParameterGenerator paramGen = AlgorithmParameterGenerator .getInstance ("DSA" );
93
+ paramGen .init (1024 ); // $ hasInsufficientKeySize
85
94
}
86
95
87
96
// DH (Asymmetric): minimum recommended key size is 2048
@@ -99,6 +108,10 @@ public void keySizeTesting() throws java.security.NoSuchAlgorithmException, java
99
108
100
109
KeyPairGenerator keyPairGen4 = KeyPairGenerator .getInstance ("DH" );
101
110
keyPairGen4 .initialize (new DHGenParameterSpec (1024 , 0 )); // $ hasInsufficientKeySize
111
+
112
+ /* Test `AlgorithmParameterGenerator` */
113
+ AlgorithmParameterGenerator paramGen = AlgorithmParameterGenerator .getInstance ("DH" );
114
+ paramGen .init (1024 ); // $ hasInsufficientKeySize
102
115
}
103
116
104
117
// EC (Asymmetric): minimum recommended key size is 256
@@ -153,8 +166,11 @@ public void keySizeTesting() throws java.security.NoSuchAlgorithmException, java
153
166
154
167
/* Test variables passed to another method */
155
168
ECGenParameterSpec ecSpec = new ECGenParameterSpec ("secp112r1" ); // $ hasInsufficientKeySize
169
+ testAsymmetricEcSpecVariable (ecSpec ); // test spec as an argument
170
+ int size = 128 ;
156
171
KeyPairGenerator keyPairGen = KeyPairGenerator .getInstance ("EC" ); // MISSING: test KeyGenerator variable as argument
157
- testAsymmetricEC (ecSpec , keyPairGen ); // test spec as an argument
172
+ testAsymmetricEcIntVariable (size , keyPairGen ); // test with variable as key size
173
+ testAsymmetricEcIntLiteral (128 ); // test with int literal as key size
158
174
}
159
175
}
160
176
@@ -180,27 +196,21 @@ public static void testAsymmetricNonEcInt(int keySize) throws java.security.NoSu
180
196
keyPairGen .initialize (keySize ); // $ hasInsufficientKeySize
181
197
}
182
198
183
- public static void testAsymmetricEcVariable (ECGenParameterSpec spec , KeyPairGenerator kpg ) throws java .security .NoSuchAlgorithmException , java .security .InvalidAlgorithmParameterException {
199
+ public static void testAsymmetricEcSpecVariable (ECGenParameterSpec spec ) throws java .security .NoSuchAlgorithmException , java .security .InvalidAlgorithmParameterException {
184
200
KeyPairGenerator keyPairGen = KeyPairGenerator .getInstance ("EC" );
185
201
keyPairGen .initialize (spec ); // sink is above where `spec` variable is initialized
186
-
187
- ECGenParameterSpec ecSpec = new ECGenParameterSpec ("secp112r1" ); // $ hasInsufficientKeySize
188
- kpg .initialize (ecSpec ); // MISSING: test KeyGenerator variable as argument
189
202
}
190
203
191
- public static void testAsymmetricEcInt (int keySize ) throws java .security .NoSuchAlgorithmException , java .security .InvalidAlgorithmParameterException {
204
+ public static void testAsymmetricEcIntVariable (int keySize , KeyPairGenerator kpg ) throws java .security .NoSuchAlgorithmException , java .security .InvalidAlgorithmParameterException {
192
205
KeyPairGenerator keyPairGen = KeyPairGenerator .getInstance ("EC" );
193
206
keyPairGen .initialize (keySize ); // $ hasInsufficientKeySize
207
+ kpg .initialize (128 ); // $ MISSING: hasInsufficientKeySize
194
208
}
195
209
196
- // public static void testVariable(int keySize, KeyGenerator kg) throws java.security.NoSuchAlgorithmException, java.security.InvalidAlgorithmParameterException {
197
- // KeyGenerator keyGen = KeyGenerator.getInstance("AES");
198
- // keyGen.init(keySize); // $ hasInsufficientKeySize
199
-
200
- // // BAD: Key size is less than 2048
201
- // kg.init(64); // $ MISSING: hasInsufficientKeySize
202
- // }
210
+ public static void testAsymmetricEcIntLiteral (int keySize ) throws java .security .NoSuchAlgorithmException , java .security .InvalidAlgorithmParameterException {
211
+ KeyPairGenerator keyPairGen = KeyPairGenerator .getInstance ("EC" );
212
+ keyPairGen .initialize (keySize ); // $ hasInsufficientKeySize
213
+ }
203
214
204
- // public static void testInt(int keySize, KeyGenerator kg) throws java.security.NoSuchAlgorithmException, java.security.InvalidAlgorithmParameterException {
205
- // }
215
+ public int getRSAKeySize (){ return 1024 ; }
206
216
}
0 commit comments