author | darcy |
Thu, 07 Jul 2016 18:21:21 -0700 | |
changeset 39502 | 682792016aec |
parent 30791 | 425c19d93347 |
child 44116 | fa73683c2c10 |
permissions | -rw-r--r-- |
30791 | 1 |
/* |
39502
682792016aec
8161022: Mark java/security/SignedObject/Chain.java as failing intermittently
darcy
parents:
30791
diff
changeset
|
2 |
* Copyright (c) 2015, 2016, 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 |
||
24 |
import java.security.Signature; |
|
25 |
import java.security.SignedObject; |
|
26 |
import java.security.KeyPairGenerator; |
|
27 |
import java.security.KeyPair; |
|
28 |
import java.security.NoSuchProviderException; |
|
29 |
import java.security.PrivateKey; |
|
30 |
import java.security.PublicKey; |
|
31 |
import java.util.Arrays; |
|
32 |
||
33 |
/* |
|
34 |
* @test |
|
35 |
* @bug 8050374 |
|
39502
682792016aec
8161022: Mark java/security/SignedObject/Chain.java as failing intermittently
darcy
parents:
30791
diff
changeset
|
36 |
* @key intermittent |
30791 | 37 |
* @summary Verify a chain of signed objects |
38 |
*/ |
|
39 |
public class Chain { |
|
40 |
||
41 |
static enum KeyAlg { |
|
42 |
RSA("RSA"), |
|
43 |
DSA("DSA"), |
|
44 |
EC("EC"); |
|
45 |
||
46 |
final String name; |
|
47 |
||
48 |
KeyAlg(String alg) { |
|
49 |
this.name = alg; |
|
50 |
} |
|
51 |
} |
|
52 |
||
53 |
static enum Provider { |
|
54 |
Default("default"), |
|
55 |
SunRsaSign("SunRsaSign"), |
|
56 |
Sun("SUN"), |
|
57 |
SunEC("SunEC"), |
|
58 |
SunJSSE("SunJSSE"), |
|
59 |
SunMSCAPI("SunMSCAPI"); |
|
60 |
||
61 |
final String name; |
|
62 |
||
63 |
Provider(String name) { |
|
64 |
this.name = name; |
|
65 |
} |
|
66 |
} |
|
67 |
||
68 |
static enum SigAlg { |
|
69 |
MD2withRSA("MD2withRSA"), |
|
70 |
MD5withRSA("md5withRSA"), |
|
71 |
||
72 |
SHA1withDSA("SHA1withDSA"), |
|
73 |
SHA224withDSA("SHA224withDSA"), |
|
74 |
SHA256withDSA("SHA256withDSA"), |
|
75 |
||
76 |
SHA1withRSA("Sha1withrSA"), |
|
77 |
SHA224withRSA("SHA224withRSA"), |
|
78 |
SHA256withRSA("SHA256withRSA"), |
|
79 |
SHA384withRSA("SHA384withRSA"), |
|
80 |
SHA512withRSA("SHA512withRSA"), |
|
81 |
||
82 |
SHA1withECDSA("SHA1withECDSA"), |
|
83 |
SHA256withECDSA("SHA256withECDSA"), |
|
84 |
SHA224withECDSA("SHA224withECDSA"), |
|
85 |
SHA384withECDSA("SHA384withECDSA"), |
|
86 |
SHA512withECDSA("SHA512withECDSA"), |
|
87 |
||
88 |
MD5andSHA1withRSA("MD5andSHA1withRSA"); |
|
89 |
||
90 |
final String name; |
|
91 |
||
92 |
SigAlg(String name) { |
|
93 |
this.name = name; |
|
94 |
} |
|
95 |
} |
|
96 |
||
97 |
static class Test { |
|
98 |
final Provider provider; |
|
99 |
final KeyAlg keyAlg; |
|
100 |
final SigAlg sigAlg; |
|
101 |
||
102 |
Test(SigAlg sigAlg, KeyAlg keyAlg, Provider privider) { |
|
103 |
this.provider = privider; |
|
104 |
this.keyAlg = keyAlg; |
|
105 |
this.sigAlg = sigAlg; |
|
106 |
} |
|
107 |
} |
|
108 |
||
109 |
private static final Test[] tests = { |
|
110 |
new Test(SigAlg.SHA1withDSA, KeyAlg.DSA, Provider.Default), |
|
111 |
new Test(SigAlg.MD2withRSA, KeyAlg.RSA, Provider.Default), |
|
112 |
new Test(SigAlg.MD5withRSA, KeyAlg.RSA, Provider.Default), |
|
113 |
new Test(SigAlg.SHA1withRSA, KeyAlg.RSA, Provider.Default), |
|
114 |
new Test(SigAlg.SHA1withDSA, KeyAlg.DSA, Provider.Sun), |
|
115 |
new Test(SigAlg.SHA224withDSA, KeyAlg.DSA, Provider.Sun), |
|
116 |
new Test(SigAlg.SHA256withDSA, KeyAlg.DSA, Provider.Sun), |
|
117 |
}; |
|
118 |
||
119 |
private static final String str = "to-be-signed"; |
|
120 |
private static final int N = 3; |
|
121 |
||
122 |
public static void main(String argv[]) { |
|
123 |
boolean result = Arrays.stream(tests).allMatch((test) -> runTest(test)); |
|
124 |
if(result) { |
|
125 |
System.out.println("All tests passed"); |
|
126 |
} else { |
|
127 |
throw new RuntimeException("Some tests failed"); |
|
128 |
} |
|
129 |
} |
|
130 |
||
131 |
static boolean runTest(Test test) { |
|
132 |
System.out.format("Test: provider = %s, signature algorithm = %s, " |
|
133 |
+ "key algorithm = %s\n", |
|
134 |
test.provider, test.sigAlg, test.keyAlg); |
|
135 |
try { |
|
136 |
// Generate all private/public key pairs |
|
137 |
PrivateKey[] privKeys = new PrivateKey[N]; |
|
138 |
PublicKey[] pubKeys = new PublicKey[N]; |
|
139 |
PublicKey[] anotherPubKeys = new PublicKey[N]; |
|
140 |
KeyPairGenerator kpg = KeyPairGenerator.getInstance( |
|
141 |
test.keyAlg.name); |
|
142 |
for (int j=0; j < N; j++) { |
|
143 |
KeyPair kp = kpg.genKeyPair(); |
|
144 |
KeyPair anotherKp = kpg.genKeyPair(); |
|
145 |
privKeys[j] = kp.getPrivate(); |
|
146 |
pubKeys[j] = kp.getPublic(); |
|
147 |
anotherPubKeys[j] = anotherKp.getPublic(); |
|
148 |
||
149 |
if (Arrays.equals(pubKeys[j].getEncoded(), |
|
150 |
anotherPubKeys[j].getEncoded())) { |
|
151 |
System.out.println("Failed: it should not get " |
|
152 |
+ "the same pair of public key"); |
|
153 |
return false; |
|
154 |
} |
|
155 |
} |
|
156 |
||
157 |
Signature signature; |
|
158 |
if (test.provider != Provider.Default) { |
|
159 |
signature = Signature.getInstance(test.sigAlg.name, |
|
160 |
test.provider.name); |
|
161 |
} else { |
|
162 |
signature = Signature.getInstance(test.sigAlg.name); |
|
163 |
} |
|
164 |
||
165 |
// Create a chain of signed objects |
|
166 |
SignedObject[] objects = new SignedObject[N]; |
|
167 |
objects[0] = new SignedObject(str, privKeys[0], signature); |
|
168 |
for (int j = 1; j < N; j++) { |
|
169 |
objects[j] = new SignedObject(objects[j - 1], privKeys[j], |
|
170 |
signature); |
|
171 |
} |
|
172 |
||
173 |
// Verify the chain |
|
174 |
int n = objects.length - 1; |
|
175 |
SignedObject object = objects[n]; |
|
176 |
do { |
|
177 |
if (!object.verify(pubKeys[n], signature)) { |
|
178 |
System.out.println("Failed: verification failed, n = " + n); |
|
179 |
return false; |
|
180 |
} |
|
181 |
||
182 |
if (object.verify(anotherPubKeys[n], signature)) { |
|
183 |
System.out.println("Failed: verification should not " |
|
184 |
+ "succeed with wrong public key, n = " + n); |
|
185 |
return false; |
|
186 |
} |
|
187 |
||
188 |
object = (SignedObject) object.getObject(); |
|
189 |
n--; |
|
190 |
} while (n > 0); |
|
191 |
||
192 |
System.out.println("signed data: " + object.getObject()); |
|
193 |
if (!str.equals(object.getObject())) { |
|
194 |
System.out.println("Failed: signed data is not equal to " |
|
195 |
+ "original one"); |
|
196 |
return false; |
|
197 |
} |
|
198 |
||
199 |
System.out.println("Test passed"); |
|
200 |
return true; |
|
201 |
} catch (NoSuchProviderException nspe) { |
|
202 |
if (test.provider == Provider.SunMSCAPI |
|
203 |
&& !System.getProperty("os.name").startsWith("Windows")) { |
|
204 |
System.out.println("SunMSCAPI is available only on Windows: " |
|
205 |
+ nspe); |
|
206 |
return true; |
|
207 |
} |
|
208 |
System.out.println("Unexpected exception: " + nspe); |
|
209 |
return false; |
|
210 |
} catch (Exception e) { |
|
211 |
System.out.println("Unexpected exception: " + e); |
|
212 |
e.printStackTrace(System.out); |
|
213 |
return false; |
|
214 |
} |
|
215 |
} |
|
216 |
} |
|
217 |