Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/security/misc/TestDefaultRandom.java
41149 views
1
/*
2
* Copyright (c) 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 8260274
27
* @run main/othervm TestDefaultRandom APG 1
28
* @run main/othervm TestDefaultRandom APG 2
29
* @run main/othervm TestDefaultRandom KPG 1
30
* @run main/othervm TestDefaultRandom KPG 2
31
* @run main/othervm TestDefaultRandom CIP 1
32
* @run main/othervm TestDefaultRandom CIP 2
33
* @run main/othervm TestDefaultRandom CIP 3
34
* @run main/othervm TestDefaultRandom CIP 4
35
* @run main/othervm TestDefaultRandom KA 1
36
* @run main/othervm TestDefaultRandom KA 2
37
* @run main/othervm TestDefaultRandom KG 1
38
* @run main/othervm TestDefaultRandom KG 2
39
* @summary Ensure the default SecureRandom impl is used as the javadoc
40
* spec stated when none supplied.
41
*/
42
import java.lang.reflect.InvocationTargetException;
43
import java.util.List;
44
import java.util.Map;
45
import java.security.*;
46
import java.security.cert.Certificate;
47
import java.security.spec.AlgorithmParameterSpec;
48
import java.security.spec.DSAGenParameterSpec;
49
import java.security.spec.RSAKeyGenParameterSpec;
50
import javax.crypto.Cipher;
51
import javax.crypto.KeyAgreement;
52
import javax.crypto.KeyGenerator;
53
import javax.crypto.SecretKey;
54
import javax.crypto.spec.SecretKeySpec;
55
import javax.crypto.spec.IvParameterSpec;
56
57
public class TestDefaultRandom {
58
59
public static void main(String[] argv) throws Exception {
60
if (argv.length != 2) {
61
throw new RuntimeException("Error: missing test parameters");
62
}
63
64
switch (argv[0]) {
65
case "APG" ->
66
check(AlgorithmParameterGenerator.getInstance("DSA"), argv[1]);
67
case "KPG" ->
68
check(KeyPairGenerator.getInstance("RSA"), argv[1]);
69
case "CIP" ->
70
check(Cipher.getInstance("AES/CBC/NoPadding"), argv[1]);
71
case "KA" -> check(KeyAgreement.getInstance("DH"), argv[1]);
72
case "KG" -> check(KeyGenerator.getInstance("AES"), argv[1]);
73
default -> throw new RuntimeException
74
("Error: unsupported test type");
75
}
76
}
77
78
private static void check(Object obj, String testNum) {
79
if (obj == null) throw new NullPointerException();
80
81
SampleProvider prov = new SampleProvider();
82
Security.insertProviderAt(prov, 1);
83
84
int b4Cnt = SampleProvider.count;
85
86
System.out.println("before, count = " + b4Cnt);
87
// Note that the arguments may not be valid, they just need to be
88
// non-null to trigger the call for checking if the default
89
// SecureRandom impl is used
90
try {
91
if (obj instanceof AlgorithmParameterGenerator) {
92
AlgorithmParameterGenerator apg =
93
(AlgorithmParameterGenerator) obj;
94
switch (testNum) {
95
case "1" -> apg.init(123);
96
case "2" -> apg.init((AlgorithmParameterSpec) null);
97
default -> throw new RuntimeException
98
("Error: invalid test#");
99
}
100
} else if (obj instanceof KeyPairGenerator) {
101
KeyPairGenerator kpg = (KeyPairGenerator) obj;
102
switch (testNum) {
103
case "1" -> kpg.initialize(123);
104
case "2" -> kpg.initialize((AlgorithmParameterSpec) null);
105
default -> throw new RuntimeException
106
("Error: invalid test#");
107
}
108
} else if (obj instanceof Cipher) {
109
Cipher c = (Cipher) obj;
110
switch (testNum) {
111
case "1" -> c.init(Cipher.ENCRYPT_MODE, (Key) null);
112
case "2" -> c.init(Cipher.ENCRYPT_MODE, (Key) null,
113
(AlgorithmParameterSpec) null);
114
case "3" -> c.init(Cipher.ENCRYPT_MODE, (Key) null,
115
(AlgorithmParameters) null);
116
case "4" -> c.init(Cipher.ENCRYPT_MODE, (Certificate)null);
117
default -> throw new RuntimeException
118
("Error: invalid test#");
119
}
120
} else if (obj instanceof KeyAgreement) {
121
KeyAgreement ka = (KeyAgreement) obj;
122
switch (testNum) {
123
case "1" -> ka.init((Key) null);
124
case "2" -> ka.init((Key) null, (AlgorithmParameterSpec)
125
null);
126
default -> throw new RuntimeException
127
("Error: invalid test#");
128
}
129
} else if (obj instanceof KeyGenerator) {
130
KeyGenerator kg = (KeyGenerator) obj;
131
switch (testNum) {
132
case "1" -> kg.init(123);
133
case "2" -> kg.init((AlgorithmParameterSpec) null);
134
default -> throw new RuntimeException
135
("Error: invalid test#");
136
}
137
} else {
138
throw new RuntimeException("Error: Unsupported type");
139
}
140
} catch (GeneralSecurityException | InvalidParameterException e) {
141
// expected; ignore
142
}
143
System.out.println("after, count = " + SampleProvider.count);
144
if (SampleProvider.count == b4Cnt) {
145
throw new RuntimeException("Test Failed");
146
}
147
}
148
149
private static class SampleProvider extends Provider {
150
151
static int count = 0;
152
static String SR_ALGO = "Custom";
153
154
SampleProvider() {
155
super("Sample", "1.0", "test provider with custom SR impl");
156
putService(new SampleService(this, "SecureRandom", SR_ALGO,
157
"SampleSecureRandom.class" /* stub class name */,
158
null, null));
159
}
160
161
private static class SampleService extends Service {
162
163
SampleService(Provider p, String type, String alg, String cn,
164
List<String> aliases, Map<String,String> attrs) {
165
super(p, type, alg, cn, aliases, attrs);
166
}
167
168
@Override
169
public Object newInstance(Object param)
170
throws NoSuchAlgorithmException {
171
String alg = getAlgorithm();
172
String type = getType();
173
174
if (type.equals("SecureRandom") && alg.equals(SR_ALGO)) {
175
SampleProvider.count++;
176
return new CustomSR();
177
} else {
178
// should never happen
179
throw new NoSuchAlgorithmException("No support for " + alg);
180
}
181
}
182
}
183
184
private static class CustomSR extends SecureRandomSpi {
185
@Override
186
protected void engineSetSeed(byte[] seed) {
187
}
188
189
@Override
190
protected void engineNextBytes(byte[] bytes) {
191
}
192
193
@Override
194
protected byte[] engineGenerateSeed(int numBytes) {
195
return new byte[numBytes];
196
}
197
}
198
}
199
}
200
201