Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/security/KeyRep/Serial.java
41149 views
1
/*
2
* Copyright (c) 2003, 2020, 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 4532506 4999599
27
* @summary Serializing KeyPair on one VM (Sun),
28
* and Deserializing on another (IBM) fails
29
* @run main/othervm/java.security.policy=Serial.policy Serial
30
*/
31
32
import java.io.*;
33
import java.math.BigInteger;
34
import java.security.*;
35
import javax.crypto.*;
36
import javax.crypto.spec.*;
37
38
public class Serial {
39
40
// providers
41
private static final String SUN = "SUN";
42
private static final String RSA = "SunRsaSign";
43
private static final String JCE = "SunJCE";
44
45
public static void main(String[] args) throws Exception {
46
47
// generate DSA key pair
48
KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA", SUN);
49
kpg.initialize(512);
50
KeyPair dsaKp = kpg.genKeyPair();
51
52
// serialize DSA key pair
53
ByteArrayOutputStream baos = new ByteArrayOutputStream();
54
ObjectOutputStream oos = new ObjectOutputStream(baos);
55
oos.writeObject(dsaKp);
56
oos.close();
57
58
// deserialize DSA key pair
59
ObjectInputStream ois = new ObjectInputStream
60
(new ByteArrayInputStream(baos.toByteArray()));
61
KeyPair dsaKp2 = (KeyPair)ois.readObject();
62
ois.close();
63
64
if (!dsaKp2.getPublic().equals(dsaKp.getPublic()) ||
65
!dsaKp2.getPrivate().equals(dsaKp.getPrivate())) {
66
throw new SecurityException("DSA test failed");
67
}
68
69
// generate RSA key pair
70
kpg = KeyPairGenerator.getInstance("RSA", RSA);
71
kpg.initialize(512);
72
KeyPair rsaKp = kpg.genKeyPair();
73
74
// serialize RSA key pair
75
baos.reset();
76
oos = new ObjectOutputStream(baos);
77
oos.writeObject(rsaKp);
78
oos.close();
79
80
// deserialize RSA key pair
81
ois = new ObjectInputStream
82
(new ByteArrayInputStream(baos.toByteArray()));
83
KeyPair rsaKp2 = (KeyPair)ois.readObject();
84
ois.close();
85
86
if (!rsaKp2.getPublic().equals(rsaKp.getPublic()) ||
87
!rsaKp2.getPrivate().equals(rsaKp.getPrivate())) {
88
throw new SecurityException("RSA test failed");
89
}
90
91
// generate DH key pair
92
kpg = KeyPairGenerator.getInstance("DiffieHellman", JCE);
93
kpg.initialize(new DHParameterSpec(skip1024Modulus, skip1024Base));
94
KeyPair dhKp = kpg.genKeyPair();
95
96
// serialize DH key pair
97
baos.reset();
98
oos = new ObjectOutputStream(baos);
99
oos.writeObject(dhKp);
100
oos.close();
101
102
// deserialize DH key pair
103
ois = new ObjectInputStream
104
(new ByteArrayInputStream(baos.toByteArray()));
105
KeyPair dhKp2 = (KeyPair)ois.readObject();
106
ois.close();
107
108
if (!dhKp2.getPublic().equals(dhKp.getPublic()) ||
109
!dhKp2.getPrivate().equals(dhKp.getPrivate())) {
110
throw new SecurityException("DH test failed");
111
}
112
113
// generate RC5 key
114
SecretKeySpec rc5Key = new SecretKeySpec(new byte[128], "RC5");
115
116
// serialize RC5 key
117
baos.reset();
118
oos = new ObjectOutputStream(baos);
119
oos.writeObject(rc5Key);
120
oos.close();
121
122
// deserialize RC5 key
123
ois = new ObjectInputStream
124
(new ByteArrayInputStream(baos.toByteArray()));
125
SecretKey rc5Key2 = (SecretKey)ois.readObject();
126
ois.close();
127
128
if (!rc5Key.equals(rc5Key2)) {
129
throw new SecurityException("RC5 test failed");
130
}
131
132
// generate PBE key
133
134
// Salt
135
byte[] salt = {
136
(byte)0xc7, (byte)0x73, (byte)0x21, (byte)0x8c,
137
(byte)0x7e, (byte)0xc8, (byte)0xee, (byte)0x99
138
};
139
140
// Iteration count
141
int count = 20;
142
143
// Create PBE parameter set
144
PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, count);
145
146
char[] password = new char[] {'f', 'o', 'o'};
147
PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
148
SecretKeyFactory keyFac =
149
SecretKeyFactory.getInstance("PBEWithMD5AndDES", JCE);
150
SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);
151
152
// serialize PBE key
153
baos.reset();
154
oos = new ObjectOutputStream(baos);
155
oos.writeObject(pbeKey);
156
oos.close();
157
158
// deserialize PBE key
159
ois = new ObjectInputStream
160
(new ByteArrayInputStream(baos.toByteArray()));
161
SecretKey pbeKey2 = (SecretKey)ois.readObject();
162
ois.close();
163
164
if (!pbeKey.equals(pbeKey2)) {
165
throw new SecurityException("PBE test failed");
166
}
167
168
checkKey("AES", 128);
169
checkKey("Blowfish", -1);
170
checkKey("DES", 56);
171
checkKey("DESede", 168);
172
checkKey("HmacMD5", -1);
173
checkKey("HmacSHA1", -1);
174
}
175
176
private static void checkKey(String algorithm, int size) throws Exception {
177
// generate key
178
KeyGenerator kg = KeyGenerator.getInstance(algorithm, JCE);
179
if (size > 0) {
180
kg.init(size);
181
}
182
SecretKey key = kg.generateKey();
183
184
// serialize key
185
ByteArrayOutputStream baos = new ByteArrayOutputStream();
186
ObjectOutputStream oos = new ObjectOutputStream(baos);
187
oos.writeObject(key);
188
oos.close();
189
190
// deserialize key
191
ObjectInputStream ois = new ObjectInputStream
192
(new ByteArrayInputStream(baos.toByteArray()));
193
SecretKey key2 = (SecretKey)ois.readObject();
194
ois.close();
195
196
if (!key.equals(key2)) {
197
throw new SecurityException(algorithm + " test failed");
198
}
199
}
200
201
// The 1024 bit Diffie-Hellman modulus values used by SKIP
202
private static final byte skip1024ModulusBytes[] = {
203
(byte)0xF4, (byte)0x88, (byte)0xFD, (byte)0x58,
204
(byte)0x4E, (byte)0x49, (byte)0xDB, (byte)0xCD,
205
(byte)0x20, (byte)0xB4, (byte)0x9D, (byte)0xE4,
206
(byte)0x91, (byte)0x07, (byte)0x36, (byte)0x6B,
207
(byte)0x33, (byte)0x6C, (byte)0x38, (byte)0x0D,
208
(byte)0x45, (byte)0x1D, (byte)0x0F, (byte)0x7C,
209
(byte)0x88, (byte)0xB3, (byte)0x1C, (byte)0x7C,
210
(byte)0x5B, (byte)0x2D, (byte)0x8E, (byte)0xF6,
211
(byte)0xF3, (byte)0xC9, (byte)0x23, (byte)0xC0,
212
(byte)0x43, (byte)0xF0, (byte)0xA5, (byte)0x5B,
213
(byte)0x18, (byte)0x8D, (byte)0x8E, (byte)0xBB,
214
(byte)0x55, (byte)0x8C, (byte)0xB8, (byte)0x5D,
215
(byte)0x38, (byte)0xD3, (byte)0x34, (byte)0xFD,
216
(byte)0x7C, (byte)0x17, (byte)0x57, (byte)0x43,
217
(byte)0xA3, (byte)0x1D, (byte)0x18, (byte)0x6C,
218
(byte)0xDE, (byte)0x33, (byte)0x21, (byte)0x2C,
219
(byte)0xB5, (byte)0x2A, (byte)0xFF, (byte)0x3C,
220
(byte)0xE1, (byte)0xB1, (byte)0x29, (byte)0x40,
221
(byte)0x18, (byte)0x11, (byte)0x8D, (byte)0x7C,
222
(byte)0x84, (byte)0xA7, (byte)0x0A, (byte)0x72,
223
(byte)0xD6, (byte)0x86, (byte)0xC4, (byte)0x03,
224
(byte)0x19, (byte)0xC8, (byte)0x07, (byte)0x29,
225
(byte)0x7A, (byte)0xCA, (byte)0x95, (byte)0x0C,
226
(byte)0xD9, (byte)0x96, (byte)0x9F, (byte)0xAB,
227
(byte)0xD0, (byte)0x0A, (byte)0x50, (byte)0x9B,
228
(byte)0x02, (byte)0x46, (byte)0xD3, (byte)0x08,
229
(byte)0x3D, (byte)0x66, (byte)0xA4, (byte)0x5D,
230
(byte)0x41, (byte)0x9F, (byte)0x9C, (byte)0x7C,
231
(byte)0xBD, (byte)0x89, (byte)0x4B, (byte)0x22,
232
(byte)0x19, (byte)0x26, (byte)0xBA, (byte)0xAB,
233
(byte)0xA2, (byte)0x5E, (byte)0xC3, (byte)0x55,
234
(byte)0xE9, (byte)0x2F, (byte)0x78, (byte)0xC7
235
};
236
237
// The SKIP 1024 bit modulus
238
private static final BigInteger skip1024Modulus
239
= new BigInteger(1, skip1024ModulusBytes);
240
241
// The base used with the SKIP 1024 bit modulus
242
private static final BigInteger skip1024Base = BigInteger.valueOf(2);
243
}
244
245