Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/security/KeyAgreement/NegativeTest.java
41149 views
1
/*
2
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/*
25
* @test
26
* @bug 8200219
27
* @summary Negative tests for Key related Test with DiffieHellman, ECDH, XDH.
28
* It Tests,
29
* Use modified encoding while generating Public/Private Keys
30
* Short, long, unsupported keysize
31
* Invalid Algo names including Null
32
* Invalid provider names including Null
33
* Invalid curve names
34
* Invalid spec usage
35
* Arguments order <KeyExchangeAlgorithm> <Provider> <KeyGenAlgorithm>
36
* <keySize> <Curve*>
37
* @library /test/lib
38
* @run main NegativeTest DiffieHellman SunJCE DiffieHellman 1024
39
* @run main NegativeTest ECDH SunEC EC 256
40
* @run main NegativeTest XDH SunEC XDH 255 X25519
41
* @run main NegativeTest XDH SunEC XDH 448 X448
42
*/
43
import java.math.BigInteger;
44
import java.security.InvalidAlgorithmParameterException;
45
import java.security.InvalidParameterException;
46
import java.security.KeyFactory;
47
import java.security.KeyPair;
48
import java.security.KeyPairGenerator;
49
import java.security.NoSuchAlgorithmException;
50
import java.security.NoSuchProviderException;
51
import java.security.Security;
52
import java.security.spec.InvalidKeySpecException;
53
import java.security.spec.NamedParameterSpec;
54
import java.security.spec.KeySpec;
55
import java.security.spec.PKCS8EncodedKeySpec;
56
import java.security.spec.X509EncodedKeySpec;
57
import java.security.spec.XECPrivateKeySpec;
58
import java.security.spec.XECPublicKeySpec;
59
import java.util.Arrays;
60
import java.util.HexFormat;
61
import javax.crypto.KeyAgreement;
62
63
public class NegativeTest {
64
65
public static void main(String[] args) throws Exception {
66
67
String kaAlgo = args[0];
68
String provider = args[1];
69
String kpgAlgo = args[2];
70
int keySize = Integer.parseInt(args[3]);
71
String kpgInit = (args.length > 4) ? args[4] : args[2];
72
testModifiedKeyEncodingTest(provider, kpgAlgo, kpgInit);
73
testInvalidKeyLen(provider, kaAlgo, kpgAlgo, kpgInit);
74
testInvalidKpgAlgo(provider, kaAlgo, keySize);
75
testInvalidKaAlgo(provider, kpgAlgo, keySize);
76
testInvalidProvider(kaAlgo, kpgAlgo, keySize);
77
if (!kaAlgo.equals("DiffieHellman")) {
78
testNamedParameter(provider, kpgAlgo);
79
}
80
if (kaAlgo.equals("XDH")) {
81
testInvalidSpec(provider, kpgAlgo, kpgInit);
82
testInCompatibleSpec(provider, kpgAlgo, kpgInit);
83
}
84
}
85
86
/**
87
* Generate keyPair based on KeyPairGenerator algorithm.
88
*/
89
private static KeyPair genKeyPair(String provider, String kpgAlgo,
90
String kpgInit) throws Exception {
91
92
KeyPairGenerator kpg = KeyPairGenerator.getInstance(kpgAlgo,
93
Security.getProvider(provider));
94
switch (kpgInit) {
95
case "DiffieHellman":
96
kpg.initialize(512);
97
break;
98
case "EC":
99
kpg.initialize(256);
100
break;
101
case "X25519":
102
kpg.initialize(255);
103
break;
104
case "X448":
105
kpg.initialize(448);
106
break;
107
default:
108
throw new RuntimeException("Invalid Algo name " + kpgInit);
109
}
110
return kpg.generateKeyPair();
111
}
112
113
private static void testModifiedKeyEncodingTest(String provider,
114
String kpgAlgo, String kpgInit) throws Exception {
115
116
KeyFactory kf = KeyFactory.getInstance(kpgAlgo, provider);
117
KeyPair kp = genKeyPair(provider, kpgAlgo, kpgInit);
118
// Test modified PrivateKey encoding
119
byte[] encoded = kp.getPrivate().getEncoded();
120
byte[] modified = modifyEncoded(encoded);
121
PKCS8EncodedKeySpec priSpec = new PKCS8EncodedKeySpec(modified);
122
try {
123
// Generate PrivateKey with modified encoding
124
kf.generatePrivate(priSpec);
125
throw new RuntimeException(
126
"testModifiedKeyTest should fail but passed.");
127
} catch (InvalidKeySpecException e) {
128
System.out.printf("Expected InvalidKeySpecException for invalid "
129
+ "PrivateKey %s%n and modified encoding: %s, %s%n",
130
HexFormat.of().withUpperCase().formatHex(encoded),
131
HexFormat.of().withUpperCase().formatHex(modified), e.getMessage());
132
}
133
// Test modified PublicKey encoding
134
encoded = kp.getPublic().getEncoded();
135
modified = modifyEncoded(encoded);
136
X509EncodedKeySpec pubSpec = new X509EncodedKeySpec(modified);
137
try {
138
// Generate PublicKey with modified encoding
139
kf.generatePublic(pubSpec);
140
throw new RuntimeException(
141
"testModifiedKeyTest should fail but passed.");
142
} catch (InvalidKeySpecException e) {
143
System.out.printf("Expected InvalidKeySpecException for invalid "
144
+ "PublicKey %s%n and modified encoding: %s, %s%n",
145
HexFormat.of().withUpperCase().formatHex(encoded),
146
HexFormat.of().withUpperCase().formatHex(modified), e.getMessage());
147
}
148
}
149
150
/**
151
* Test with all Invalid key length.
152
*/
153
private static void testInvalidKeyLen(String provider, String kaAlgo,
154
String kpgAlgo, String kpgInit) throws Exception {
155
156
for (int keySize : selectInvalidKeylength(kpgInit)) {
157
try {
158
startKeyAgreement(provider, kaAlgo, kpgAlgo, keySize);
159
throw new RuntimeException(
160
"testInvalidKeyLen should fail but passed.");
161
} catch (InvalidParameterException e) {
162
System.out.printf("Expected InvalidParameterException for "
163
+ "keyLength: %s, %s%n", keySize, e.getMessage());
164
}
165
}
166
}
167
168
/**
169
* Test with all Invalid KeyPairGenerator algorithms.
170
*/
171
private static void testInvalidKpgAlgo(String provider, String algo,
172
int keySize) throws Exception {
173
174
for (String kpgAlgo : new String[]{null, " ", "", "NoSuchAlgorithm"}) {
175
try {
176
startKeyAgreement(provider, algo, kpgAlgo, keySize);
177
throw new RuntimeException(
178
"testInvalidKpgAlgo should fail but passed.");
179
} catch (NoSuchAlgorithmException e) {
180
System.out.printf("Expected NoSuchAlgorithmException for "
181
+ "KeyAgreement algo: %s, %s%n",
182
kpgAlgo, e.getMessage());
183
} catch (NullPointerException e) {
184
if (kpgAlgo == null) {
185
System.out.printf("Expected NullPointerException for "
186
+ "KeyPairGenerator algo: %s, %s%n",
187
kpgAlgo, e.getMessage());
188
continue;
189
}
190
throw new RuntimeException(
191
"Unknown failure in testInvalidKpgAlgo.");
192
}
193
}
194
}
195
196
/**
197
* Test with all Invalid KeyAgreement algorithms.
198
*/
199
private static void testInvalidKaAlgo(String provider, String kpgAlgo,
200
int keySize) throws Exception {
201
202
for (String algo : new String[]{null, " ", "", "NoSuchAlgorithm"}) {
203
try {
204
startKeyAgreement(provider, algo, kpgAlgo, keySize);
205
throw new RuntimeException(
206
"testInvalidKaAlgo should fail but passed.");
207
} catch (NoSuchAlgorithmException e) {
208
System.out.printf("Expected NoSuchAlgorithmException for "
209
+ "KeyAgreement algo: %s, %s%n", algo, e.getMessage());
210
} catch (NullPointerException e) {
211
if (algo == null) {
212
System.out.printf("Expected NullPointerException for "
213
+ "KeyAgreement algo: %s, %s%n",
214
algo, e.getMessage());
215
continue;
216
}
217
throw new RuntimeException(
218
"Unknown failure in testInvalidKaAlgo.");
219
}
220
}
221
}
222
223
/**
224
* Test with all Invalid Provider names.
225
*/
226
private static void testInvalidProvider(String kaAlgo, String kpgAlgo,
227
int keySize) throws Exception {
228
229
for (String provider : new String[]{null, " ", "", "NoSuchProvider"}) {
230
try {
231
startKeyAgreement(provider, kaAlgo, kpgAlgo, keySize);
232
throw new RuntimeException(
233
"testInvalidProvider should fail but passed.");
234
} catch (NoSuchProviderException e) {
235
System.out.printf("Expected NoSuchProviderException for "
236
+ "Provider: %s, %s%n", provider, e.getMessage());
237
} catch (IllegalArgumentException e) {
238
System.out.printf("Expected IllegalArgumentException for "
239
+ "Provider: %s, %s%n", provider, e.getMessage());
240
}
241
}
242
}
243
244
/**
245
* Test for (in)valid curve names as argument to NamedParameterSpec
246
*/
247
private static void testNamedParameter(String provider, String kpgAlgo)
248
throws Exception {
249
250
for (String name : new String[]{null, " ", "", "NoSuchCurve"}) {
251
try {
252
NamedParameterSpec spec = new NamedParameterSpec(name);
253
KeyPairGenerator kpg
254
= KeyPairGenerator.getInstance(kpgAlgo, provider);
255
kpg.initialize(spec);
256
kpg.generateKeyPair();
257
throw new RuntimeException(
258
"testNamedParameter should fail but passed.");
259
} catch (NullPointerException e) {
260
if (name == null) {
261
System.out.printf("Expected NullPointerException for "
262
+ "NamedParameterSpec name: %s, %s%n",
263
name, e.getMessage());
264
continue;
265
}
266
throw new RuntimeException(
267
"Unknown failure in testNamedParameter.");
268
} catch (InvalidAlgorithmParameterException e) {
269
System.out.printf("Expected InvalidAlgorithmParameterException"
270
+ " for NamedParameterSpec name: %s, %s%n",
271
name, e.getMessage());
272
}
273
}
274
}
275
276
/**
277
* Test to generate Public/Private keys using (in)valid coordinate/scalar.
278
*/
279
private static void testInvalidSpec(String provider,
280
String kpgAlgo, String curve) throws Exception {
281
282
NamedParameterSpec spec = new NamedParameterSpec(curve);
283
KeyFactory kf = KeyFactory.getInstance(kpgAlgo, provider);
284
int validLen = curve.equalsIgnoreCase("X448") ? 56 : 32;
285
for (byte[] scalarBytes : new byte[][]{null, new byte[]{},
286
new byte[32], new byte[56], new byte[65535]}) {
287
try {
288
KeySpec privateSpec = new XECPrivateKeySpec(spec, scalarBytes);
289
kf.generatePrivate(privateSpec);
290
if (scalarBytes.length != validLen) {
291
throw new RuntimeException(String.format("testInvalidSpec "
292
+ "should fail but passed when Scalar bytes length "
293
+ "!= %s for curve %s", validLen, curve));
294
}
295
} catch (NullPointerException e) {
296
if (scalarBytes == null) {
297
System.out.printf("Expected NullPointerException for "
298
+ "scalar: %s, %s%n", scalarBytes, e.getMessage());
299
continue;
300
}
301
throw new RuntimeException(e);
302
} catch (InvalidKeySpecException e) {
303
if (scalarBytes.length != validLen) {
304
System.out.printf("Expected InvalidKeySpecException for "
305
+ "scalar length %s and curve %s: %s%n",
306
scalarBytes.length, curve, e.getMessage());
307
continue;
308
}
309
throw new RuntimeException(e);
310
}
311
}
312
for (BigInteger coordinate : new BigInteger[]{null, BigInteger.ZERO,
313
BigInteger.ONE, new BigInteger("2").pow(255),
314
new BigInteger("2").pow(448)}) {
315
try {
316
KeySpec publicSpec = new XECPublicKeySpec(spec, coordinate);
317
kf.generatePublic(publicSpec);
318
} catch (NullPointerException e) {
319
if (coordinate == null) {
320
System.out.printf("Expected NullPointerException for "
321
+ "coordinate : %s, %s%n", coordinate,
322
e.getMessage());
323
continue;
324
}
325
throw new RuntimeException(e);
326
}
327
}
328
}
329
330
private static void testInCompatibleSpec(String provider,
331
String kpgAlgo, String curve) throws Exception {
332
333
int validLen = curve.equalsIgnoreCase("X448") ? 56 : 32;
334
NamedParameterSpec spec = new NamedParameterSpec(curve);
335
KeyFactory kf = KeyFactory.getInstance(kpgAlgo, provider);
336
KeySpec privateSpec = new XECPrivateKeySpec(spec, new byte[validLen]);
337
KeySpec publicSpec = new XECPublicKeySpec(spec, BigInteger.ONE);
338
try {
339
kf.generatePrivate(publicSpec);
340
throw new RuntimeException(
341
"testInCompatibleSpec should fail but passed.");
342
} catch (InvalidKeySpecException e) {
343
System.out.printf("Expected XECPublicKeySpec to XECPrivateKeySpec :"
344
+ " %s%n", e.getMessage());
345
}
346
try {
347
kf.generatePublic(privateSpec);
348
throw new RuntimeException(
349
"testInCompatibleSpec should fail but passed.");
350
} catch (InvalidKeySpecException e) {
351
System.out.printf("Expected XECPrivateKeySpec to XECPublicKeySpec :"
352
+ " %s%n", e.getMessage());
353
}
354
}
355
356
/**
357
* Perform KeyAgreement operation.
358
*/
359
private static void startKeyAgreement(String provider, String kaAlgo,
360
String kpgAlgo, int keySize) throws Exception {
361
362
KeyPairGenerator kpg = KeyPairGenerator.getInstance(kpgAlgo, provider);
363
kpg.initialize(keySize);
364
KeyPair kp = kpg.generateKeyPair();
365
KeyAgreement ka = KeyAgreement.getInstance(kaAlgo, provider);
366
ka.init(kp.getPrivate());
367
ka.doPhase(kp.getPublic(), true);
368
ka.generateSecret();
369
}
370
371
/**
372
* Return manipulated encoded bytes.
373
*/
374
private static byte[] modifyEncoded(byte[] encoded) {
375
376
byte[] copy = Arrays.copyOf(encoded, encoded.length);
377
for (int i = 0; i < copy.length; i++) {
378
copy[i] = (byte) ~copy[i];
379
}
380
return copy;
381
}
382
383
/**
384
* Select invalid key sizes for different Key generation algorithms.
385
*/
386
private static int[] selectInvalidKeylength(String kpgInit) {
387
388
int[] keySize = new int[]{};
389
switch (kpgInit) {
390
case "DiffieHellman":
391
keySize = new int[]{256, 513, 1023, 2176, 4032, 6400, 8200};
392
break;
393
case "EC":
394
keySize = new int[]{100, 300};
395
break;
396
case "X25519":
397
keySize = new int[]{100, 300};
398
break;
399
case "X448":
400
keySize = new int[]{100, 500};
401
break;
402
default:
403
throw new RuntimeException("Invalid Algo name " + kpgInit);
404
}
405
return keySize;
406
}
407
}
408
409