Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/rsa/pss/SerializedPSSKey.java
41153 views
1
/*
2
* Copyright (c) 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
import static javax.crypto.Cipher.PRIVATE_KEY;
25
import static javax.crypto.Cipher.PUBLIC_KEY;
26
import java.io.ByteArrayInputStream;
27
import java.io.ByteArrayOutputStream;
28
import java.io.IOException;
29
import java.io.ObjectInputStream;
30
import java.io.ObjectOutputStream;
31
import java.security.InvalidKeyException;
32
import java.security.Key;
33
import java.security.KeyFactory;
34
import java.security.KeyPair;
35
import java.security.KeyPairGenerator;
36
import java.security.MessageDigest;
37
import java.security.NoSuchAlgorithmException;
38
import java.security.NoSuchProviderException;
39
import java.security.PrivateKey;
40
import java.security.PublicKey;
41
import java.security.Signature;
42
import java.security.interfaces.RSAPrivateCrtKey;
43
import java.security.interfaces.RSAPublicKey;
44
import java.security.spec.InvalidKeySpecException;
45
import java.security.spec.MGF1ParameterSpec;
46
import java.security.spec.PKCS8EncodedKeySpec;
47
import java.security.spec.PSSParameterSpec;
48
import java.security.spec.RSAKeyGenParameterSpec;
49
import java.security.spec.RSAPrivateCrtKeySpec;
50
import java.security.spec.RSAPublicKeySpec;
51
import java.security.spec.X509EncodedKeySpec;
52
import java.util.Arrays;
53
54
/**
55
* @test @bug 8242335
56
* @summary Test RSASSA-PSS serialized keys
57
* @run main SerializedPSSKey
58
*/
59
public class SerializedPSSKey {
60
61
private static final String ALGO = "RSASSA-PSS";
62
private static final String OID = "1.2.840.113549.1.1.10";
63
private static final String PROVIDER = "SunRsaSign";
64
private static final int KEY_SIZE = 2048;
65
private static final byte[] DATA = "Test".getBytes();
66
/**
67
* Digest algorithms to test w/ RSASSA-PSS signature algorithms
68
*/
69
private static final String[] DIGEST_ALG = {
70
"SHA-1", "SHA-224", "SHA-256", "SHA-384",
71
"SHA-512", "SHA-512/224", "SHA-512/256"
72
};
73
74
public static void main(String[] args) throws Exception {
75
76
for (String algo : new String[]{ALGO, OID}) {
77
KeyPairGenerator kpg = KeyPairGenerator.getInstance(algo, PROVIDER);
78
79
// Algorithm-Independent Initialization
80
kpg.initialize(KEY_SIZE);
81
KeyPair kp = kpg.generateKeyPair();
82
test(algo, kp, null);
83
for (String digest : DIGEST_ALG) {
84
PSSParameterSpec params = genPSSParameter(digest, KEY_SIZE);
85
// RSAKeyGenParameterSpec#1 Initialization
86
kpg.initialize(new RSAKeyGenParameterSpec(KEY_SIZE,
87
RSAKeyGenParameterSpec.F4, params));
88
KeyPair kp2 = kpg.generateKeyPair();
89
test(algo, kp2, params);
90
}
91
System.out.println("Successful with : " + algo);
92
}
93
}
94
95
private static void test(String algo, KeyPair orig, PSSParameterSpec params)
96
throws Exception {
97
98
Key[] privs = manipulateKey(algo, PRIVATE_KEY, orig.getPrivate());
99
Key[] pubs = manipulateKey(algo, PUBLIC_KEY, orig.getPublic());
100
for (Key pri : privs) {
101
for (Key pub : pubs) {
102
testSerialize(orig, new KeyPair(
103
(PublicKey) pub, (PrivateKey) pri));
104
if (params != null) {
105
testSignature(algo, (PublicKey) pub,
106
(PrivateKey) pri, params);
107
}
108
}
109
}
110
}
111
112
private static void testSignature(String algo, PublicKey pub,
113
PrivateKey priv, PSSParameterSpec params) throws Exception {
114
115
Signature sig = Signature.getInstance(algo, PROVIDER);
116
sig.setParameter(params);
117
sig.initSign(priv);
118
sig.update(DATA);
119
byte[] signature = sig.sign();
120
121
sig.initVerify(pub);
122
sig.update(DATA);
123
if (!sig.verify(signature)) {
124
throw new RuntimeException("Signature verification failed");
125
}
126
// Re-verify the signature with another Signature instance
127
Signature sig1 = Signature.getInstance(algo, PROVIDER);
128
sig1.setParameter(params);
129
sig1.initVerify(pub);
130
sig1.update(DATA);
131
if (!sig1.verify(signature)) {
132
throw new RuntimeException("Signature verification failed");
133
}
134
}
135
136
private static Key[] manipulateKey(String algo, int type, Key key)
137
throws NoSuchAlgorithmException, InvalidKeySpecException,
138
NoSuchProviderException, InvalidKeyException {
139
140
KeyFactory kf = KeyFactory.getInstance(algo, PROVIDER);
141
switch (type) {
142
case PUBLIC_KEY:
143
return new Key[]{
144
kf.generatePublic(kf.getKeySpec(key, RSAPublicKeySpec.class)),
145
kf.generatePublic(new X509EncodedKeySpec(key.getEncoded())),
146
kf.generatePublic(new RSAPublicKeySpec(
147
((RSAPublicKey) key).getModulus(),
148
((RSAPublicKey) key).getPublicExponent(),
149
((RSAPublicKey) key).getParams())),
150
kf.translateKey(key)
151
};
152
case PRIVATE_KEY:
153
RSAPrivateCrtKey crtKey = (RSAPrivateCrtKey) key;
154
return new Key[]{
155
kf.generatePrivate(
156
kf.getKeySpec(key, RSAPrivateCrtKeySpec.class)),
157
kf.generatePrivate(new PKCS8EncodedKeySpec(key.getEncoded())),
158
kf.generatePrivate(new RSAPrivateCrtKeySpec(
159
crtKey.getModulus(),
160
crtKey.getPublicExponent(),
161
crtKey.getPrivateExponent(),
162
crtKey.getPrimeP(),
163
crtKey.getPrimeQ(),
164
crtKey.getPrimeExponentP(),
165
crtKey.getPrimeExponentQ(),
166
crtKey.getCrtCoefficient(),
167
crtKey.getParams()
168
)),
169
kf.translateKey(key)
170
};
171
}
172
throw new RuntimeException("We shouldn't reach here");
173
}
174
175
private static PSSParameterSpec genPSSParameter(String digest, int keySize)
176
throws NoSuchAlgorithmException {
177
178
int dgLen = MessageDigest.getInstance(digest).getDigestLength();
179
// pick a salt length based on the key length and digestAlgo
180
int saltLength = keySize / 8 - dgLen - 2;
181
if (saltLength < 0) {
182
System.out.printf("keysize: %s, digestLen: %s%n", keySize / 8, dgLen);
183
return null;
184
}
185
return new PSSParameterSpec(digest, "MGF1", new MGF1ParameterSpec(digest),
186
saltLength, PSSParameterSpec.TRAILER_FIELD_BC);
187
}
188
189
/**
190
* Compare original KeyPair with transformed ones.
191
*/
192
private static void testKeyEquals(KeyPair orig, PublicKey pubKey,
193
PrivateKey priKey) {
194
195
if (!orig.getPrivate().equals(priKey)
196
&& orig.getPrivate().hashCode() != priKey.hashCode()
197
&& !Arrays.equals(orig.getPrivate().getEncoded(),
198
priKey.getEncoded())) {
199
throw new RuntimeException(
200
"PrivateKey is not equal with transformed one");
201
}
202
if (!orig.getPublic().equals(pubKey)
203
&& orig.getPublic().hashCode() != pubKey.hashCode()
204
&& !Arrays.equals(orig.getPublic().getEncoded(),
205
pubKey.getEncoded())) {
206
throw new RuntimeException(
207
"PublicKey is not equal with transformed one");
208
}
209
}
210
211
/**
212
* Test serialization of KeyPair and Keys it holds.
213
*/
214
private static void testSerialize(KeyPair orig, KeyPair transformed)
215
throws Exception {
216
217
testKeyEquals(orig, transformed.getPublic(), transformed.getPrivate());
218
PrivateKey serializedPrivate = deserializedCopy(transformed.getPrivate(),
219
PrivateKey.class);
220
PublicKey serializedPublic = deserializedCopy(transformed.getPublic(),
221
PublicKey.class);
222
testKeyEquals(orig, serializedPublic, serializedPrivate);
223
// Verify Serialized KeyPair instance.
224
KeyPair copy = deserializedCopy(transformed, KeyPair.class);
225
testKeyEquals(orig, copy.getPublic(), copy.getPrivate());
226
}
227
228
private static <T extends Object> T deserializedCopy(T orig, Class<T> type)
229
throws IOException, ClassNotFoundException {
230
return deserialize(serialize(orig), type);
231
}
232
233
/**
234
* Deserialize the Key object.
235
*/
236
private static <T extends Object> T deserialize(byte[] serialized,
237
Class<T> type) throws IOException, ClassNotFoundException {
238
239
T key = null;
240
try (ByteArrayInputStream bis = new ByteArrayInputStream(serialized);
241
ObjectInputStream ois = new ObjectInputStream(bis)) {
242
key = (T) ois.readObject();
243
}
244
return key;
245
}
246
247
/**
248
* Serialize the given Key object.
249
*/
250
private static <T extends Object> byte[] serialize(T key)
251
throws IOException {
252
253
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
254
ObjectOutputStream oos = new ObjectOutputStream(bos)) {
255
oos.writeObject(key);
256
return bos.toByteArray();
257
}
258
}
259
260
}
261
262