Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/security/Signature/Offsets.java
38812 views
1
/*
2
* Copyright (c) 2015, 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 java.security.*;
25
import java.security.spec.*;
26
import jdk.testlibrary.RandomFactory;
27
28
/*
29
* @test
30
* @bug 8050374 8181048 8146293
31
* @key randomness
32
* @summary This test validates signature verification
33
* Signature.verify(byte[], int, int). The test uses RandomFactory to
34
* get random set of clear text data to sign. After the signature
35
* generation, the test tries to verify signature with the above API
36
* and passing in different signature offset (0, 33, 66, 99).
37
* @library /lib/testlibrary
38
* @run main Offsets SUN NONEwithDSA
39
* @run main Offsets SUN SHA1withDSA
40
* @run main Offsets SUN SHA224withDSA
41
* @run main Offsets SUN SHA256withDSA
42
* @run main Offsets SunRsaSign SHA224withRSA
43
* @run main Offsets SunRsaSign SHA256withRSA
44
* @run main Offsets SunRsaSign SHA384withRSA
45
* @run main Offsets SunRsaSign SHA512withRSA
46
* @run main Offsets SunRsaSign SHA512/224withRSA
47
* @run main Offsets SunRsaSign SHA512/256withRSA
48
*/
49
public class Offsets {
50
51
private final int size;
52
private final byte[] cleartext;
53
private final PublicKey pubkey;
54
private final Signature signature;
55
private final byte[] signed;
56
57
private Offsets(Signature signature, PublicKey pubkey, PrivateKey privkey,
58
int size, byte[] cleartext) throws InvalidKeyException,
59
SignatureException {
60
System.out.println("Testing signature " + signature.getAlgorithm());
61
this.pubkey = pubkey;
62
this.signature = signature;
63
this.size = size;
64
this.cleartext = cleartext;
65
66
String sigAlg = signature.getAlgorithm();
67
signature.initSign(privkey);
68
signature.update(cleartext, 0, size);
69
signed = signature.sign();
70
}
71
72
int getDataSize() {
73
return size;
74
}
75
76
int getSignatureLength() {
77
return signed.length;
78
}
79
80
byte[] shiftSignData(int offset) {
81
byte[] testSignData = new byte[offset + signed.length];
82
System.arraycopy(signed, 0, testSignData, offset,
83
signed.length);
84
return testSignData;
85
}
86
87
boolean verifySignature(byte[] sigData, int sigOffset, int sigLength,
88
int updateOffset, int updateLength)
89
throws InvalidKeyException, SignatureException {
90
signature.initVerify(pubkey);
91
signature.update(cleartext, updateOffset, updateLength);
92
return signature.verify(sigData, sigOffset, sigLength);
93
}
94
95
static Offsets init(String provider, String algorithm)
96
throws NoSuchAlgorithmException, NoSuchProviderException,
97
InvalidKeyException, SignatureException {
98
// fill the cleartext data with random bytes
99
byte[] cleartext = new byte[100];
100
RandomFactory.getRandom().nextBytes(cleartext);
101
102
// NONEwith requires input to be of 20 bytes
103
int size = algorithm.contains("NONEwith") ? 20 : 100;
104
105
// create signature instance
106
Signature signature = Signature.getInstance(algorithm, provider);
107
108
String keyAlgo;
109
int keySize = 2048;
110
if (algorithm.contains("RSA")) {
111
keyAlgo = "RSA";
112
} else if (algorithm.contains("ECDSA")) {
113
keyAlgo = "EC";
114
keySize = 256;
115
} else if (algorithm.contains("DSA")) {
116
keyAlgo = "DSA";
117
if (algorithm.startsWith("SHAwith") ||
118
algorithm.startsWith("SHA1with")) {
119
keySize = 1024;
120
}
121
} else {
122
throw new RuntimeException("Test doesn't support this signature "
123
+ "algorithm: " + algorithm);
124
}
125
126
KeyPairGenerator kpg = KeyPairGenerator.getInstance(keyAlgo, provider);
127
kpg.initialize(keySize);
128
KeyPair kp = kpg.generateKeyPair();
129
PublicKey pubkey = kp.getPublic();
130
PrivateKey privkey = kp.getPrivate();
131
132
return new Offsets(signature, pubkey, privkey, size, cleartext);
133
}
134
135
public static void main(String[] args) throws NoSuchAlgorithmException,
136
InvalidKeyException, SignatureException {
137
if (args.length < 2) {
138
throw new RuntimeException("Wrong parameters");
139
}
140
141
boolean result = true;
142
try {
143
Offsets test = init(args[0], args[1]);
144
145
// We are trying 3 different offsets, data size has nothing to do
146
// with signature length
147
for (int chunk = 3; chunk > 0; chunk--) {
148
int signOffset = test.getDataSize() / chunk;
149
150
System.out.println("Running test with offset " + signOffset);
151
byte[] signData = test.shiftSignData(signOffset);
152
153
boolean success = test.verifySignature(signData, signOffset,
154
test.getSignatureLength(), 0, test.getDataSize());
155
156
if (success) {
157
System.out.println("Successfully verified with offset "
158
+ signOffset);
159
} else {
160
System.out.println("Verification failed with offset "
161
+ signOffset);
162
result = false;
163
}
164
}
165
166
// save signature to offset 0
167
byte[] signData = test.shiftSignData(0);
168
169
// Negative tests
170
171
// Test signature offset 0.
172
// Wrong test data will be passed to update,
173
// so signature verification should fail.
174
for (int chunk = 3; chunk > 0; chunk--) {
175
int dataOffset = (test.getDataSize() - 1) / chunk;
176
boolean success;
177
try {
178
success = test.verifySignature(signData, 0,
179
test.getSignatureLength(), dataOffset,
180
(test.getDataSize() - dataOffset));
181
} catch (SignatureException e) {
182
// Since we are trying different data size, it can throw
183
// SignatureException
184
success = false;
185
}
186
187
if (!success) {
188
System.out.println("Signature verification failed "
189
+ "as expected, with data offset " + dataOffset
190
+ " and length "
191
+ (test.getDataSize() - dataOffset));
192
} else {
193
System.out.println("Signature verification "
194
+ "should not succeed, with data offset "
195
+ dataOffset + " and length "
196
+ (test.getDataSize() - dataOffset));
197
result = false;
198
}
199
}
200
201
// Tests with manipulating offset and length
202
result &= Offsets.checkFailure(test, signData, -1,
203
test.getSignatureLength());
204
205
result &= Offsets.checkFailure(test, signData, 0,
206
test.getSignatureLength() - 1);
207
208
result &= Offsets.checkFailure(test, signData,
209
test.getSignatureLength() + 1, test.getSignatureLength());
210
211
result &= Offsets.checkFailure(test, signData, 0,
212
test.getSignatureLength() + 1);
213
214
result &= Offsets.checkFailure(test, signData, 0, 0);
215
216
result &= Offsets.checkFailure(test, signData, 0, -1);
217
218
result &= Offsets.checkFailure(test, signData,
219
2147483646, test.getSignatureLength());
220
221
result &= Offsets.checkFailure(test, null, 0,
222
test.getSignatureLength());
223
} catch (NoSuchProviderException nspe) {
224
System.out.println("No such provider: " + nspe);
225
}
226
227
if (!result) {
228
throw new RuntimeException("Some test cases failed");
229
}
230
}
231
232
static boolean checkFailure(Offsets test, byte[] signData, int offset,
233
int length) {
234
boolean success;
235
try {
236
success = test.verifySignature(signData, offset, length, 0,
237
test.getDataSize());
238
} catch (IllegalArgumentException | SignatureException e) {
239
System.out.println("Expected exception: " + e);
240
success = false;
241
} catch (InvalidKeyException e) {
242
System.out.println("Unexpected exception: " + e);
243
return false;
244
}
245
246
if (!success) {
247
System.out.println("Signature verification failed as expected, "
248
+ "with signature offset " + offset + " and length "
249
+ length);
250
return true;
251
} else {
252
System.out.println("Signature verification should not succeed, "
253
+ "with signature offset " + offset + " and length "
254
+ length);
255
return false;
256
}
257
}
258
259
}
260
261