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