2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2003, 2005, Oracle and/or its affiliates. All rights reserved.
|
2
|
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 |
*
|
5506
|
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.
|
2
|
22 |
*/
|
|
23 |
|
|
24 |
/*
|
|
25 |
* @test
|
|
26 |
* @bug 4856968
|
|
27 |
* @summary make sure getInstance() works correctly, including failover
|
|
28 |
* and delayed provider selection for Signatures
|
|
29 |
* @author Andreas Sterbenz
|
|
30 |
*/
|
|
31 |
|
|
32 |
import java.util.*;
|
|
33 |
|
|
34 |
import java.security.*;
|
|
35 |
import java.security.cert.*;
|
|
36 |
|
|
37 |
public class GetInstance {
|
|
38 |
|
|
39 |
private static void same(Provider p1, Provider p2) throws Exception {
|
|
40 |
if (p1 != p2) {
|
|
41 |
throw new Exception("Wrong provider");
|
|
42 |
}
|
|
43 |
}
|
|
44 |
|
|
45 |
public static void main(String[] args) throws Exception {
|
|
46 |
long start = System.currentTimeMillis();
|
|
47 |
|
|
48 |
Provider foo = new FooProvider();
|
|
49 |
Provider bar = new BarProvider();
|
|
50 |
Provider baz = new BazProvider();
|
|
51 |
|
|
52 |
Security.addProvider(foo);
|
|
53 |
Security.addProvider(bar);
|
|
54 |
Security.addProvider(baz);
|
|
55 |
|
|
56 |
System.out.println("Testing MessageDigest.getInstance()...");
|
|
57 |
MessageDigest m;
|
|
58 |
m = MessageDigest.getInstance("foo");
|
|
59 |
m = MessageDigest.getInstance("foo", "foo");
|
|
60 |
m = MessageDigest.getInstance("foo", foo);
|
|
61 |
|
|
62 |
System.out.println("Testing Signature.getInstance() for SPI...");
|
|
63 |
Signature sig;
|
|
64 |
PrivateKey privateKey = new FooPrivateKey();
|
|
65 |
sig = Signature.getInstance("foo");
|
|
66 |
same(foo, sig.getProvider());
|
|
67 |
sig = Signature.getInstance("foo");
|
|
68 |
sig.initSign(privateKey);
|
|
69 |
same(foo, sig.getProvider());
|
|
70 |
sig = Signature.getInstance("foo", "foo");
|
|
71 |
sig.initSign(privateKey);
|
|
72 |
same(foo, sig.getProvider());
|
|
73 |
sig = Signature.getInstance("foo", foo);
|
|
74 |
sig.initSign(privateKey);
|
|
75 |
same(foo, sig.getProvider());
|
|
76 |
|
|
77 |
System.out.println("Testing Signature.getInstance() for Signature...");
|
|
78 |
sig = Signature.getInstance("fuu");
|
|
79 |
same(foo, sig.getProvider());
|
|
80 |
sig = Signature.getInstance("fuu");
|
|
81 |
sig.initSign(privateKey);
|
|
82 |
same(foo, sig.getProvider());
|
|
83 |
sig = Signature.getInstance("fuu", "foo");
|
|
84 |
sig.initSign(privateKey);
|
|
85 |
same(foo, sig.getProvider());
|
|
86 |
sig = Signature.getInstance("fuu", foo);
|
|
87 |
sig.initSign(privateKey);
|
|
88 |
same(foo, sig.getProvider());
|
|
89 |
|
|
90 |
System.out.println("Testing CertStore.getInstance()...");
|
|
91 |
CertStoreParameters params = new CollectionCertStoreParameters(Collections.EMPTY_LIST);
|
|
92 |
CertStore cs;
|
|
93 |
cs = CertStore.getInstance("foo", params);
|
|
94 |
cs = CertStore.getInstance("foo", params, "foo");
|
|
95 |
cs = CertStore.getInstance("foo", params, foo);
|
|
96 |
|
|
97 |
System.out.println("Testing failover...");
|
|
98 |
m = MessageDigest.getInstance("bar");
|
|
99 |
same(m.getProvider(), baz);
|
|
100 |
sig = Signature.getInstance("bar");
|
|
101 |
same(sig.getProvider(), baz);
|
|
102 |
cs = CertStore.getInstance("bar", params);
|
|
103 |
same(cs.getProvider(), baz);
|
|
104 |
|
|
105 |
System.out.println("Testing Signature delayed provider selection...");
|
|
106 |
sig = Signature.getInstance("baz");
|
|
107 |
sig.initVerify(new FooPublicKey());
|
|
108 |
same(sig.getProvider(), baz);
|
|
109 |
|
|
110 |
Provider.Service s = foo.getService("CertStore", "foo");
|
|
111 |
s.newInstance(null);
|
|
112 |
s.newInstance(params);
|
|
113 |
try {
|
|
114 |
s.newInstance(0);
|
|
115 |
throw new Exception("call should not succeed");
|
|
116 |
} catch (NoSuchAlgorithmException e) {
|
|
117 |
e.printStackTrace();
|
|
118 |
Throwable cause = e.getCause();
|
|
119 |
if (cause instanceof InvalidParameterException == false) {
|
|
120 |
throw new Exception("incorrect exception");
|
|
121 |
}
|
|
122 |
}
|
|
123 |
|
|
124 |
long stop = System.currentTimeMillis();
|
|
125 |
System.out.println("Done (" + (stop - start) + " ms).");
|
|
126 |
}
|
|
127 |
|
|
128 |
public static class FooProvider extends Provider {
|
|
129 |
FooProvider() {
|
|
130 |
super("foo", 1.0d, "none");
|
|
131 |
put("MessageDigest.foo", "GetInstance$FooDigest");
|
|
132 |
put("CertStore.foo", "GetInstance$FooStore");
|
|
133 |
put("Signature.foo", "GetInstance$FooSignatureSpi");
|
|
134 |
|
|
135 |
put("Signature.fuu", "GetInstance$BazSignature");
|
|
136 |
|
|
137 |
// throws InvalidKeyException, skipped in delayed provider selection
|
|
138 |
put("Signature.baz", "GetInstance$BazSignatureSpi");
|
|
139 |
}
|
|
140 |
}
|
|
141 |
|
|
142 |
public static class BarProvider extends Provider {
|
|
143 |
BarProvider() {
|
|
144 |
super("bar", 1.0d, "none");
|
|
145 |
// all entries invalid for failover
|
|
146 |
put("MessageDigest.bar", "GetInstance$FooKey");
|
|
147 |
put("Signature.bar", "GetInstance$FooKey");
|
|
148 |
put("Certstore.bar", "GetInstance$FooKey");
|
|
149 |
|
|
150 |
// not an SPI, skipped in delayed provider selection
|
|
151 |
put("Signature.baz", "GetInstance$BazSignature");
|
|
152 |
}
|
|
153 |
}
|
|
154 |
|
|
155 |
public static class BazProvider extends Provider {
|
|
156 |
BazProvider() {
|
|
157 |
super("baz", 1.0d, "none");
|
|
158 |
put("MessageDigest.bar", "GetInstance$FooDigest");
|
|
159 |
put("CertStore.bar", "GetInstance$FooStore");
|
|
160 |
put("Signature.bar", "GetInstance$FooSignatureSpi");
|
|
161 |
|
|
162 |
put("Signature.baz", "GetInstance$FooSignatureSpi");
|
|
163 |
}
|
|
164 |
}
|
|
165 |
|
|
166 |
public static class FooDigest extends MessageDigestSpi {
|
|
167 |
public byte[] engineDigest() { return new byte[0]; }
|
|
168 |
public void engineReset() {}
|
|
169 |
public void engineUpdate(byte input) {}
|
|
170 |
public void engineUpdate(byte[] b, int ofs, int len) {}
|
|
171 |
}
|
|
172 |
|
|
173 |
public static class FooStore extends CertStoreSpi {
|
|
174 |
public FooStore(CertStoreParameters params) throws InvalidAlgorithmParameterException { super(params); }
|
|
175 |
public Collection engineGetCertificates(CertSelector sel) { return Collections.EMPTY_LIST; }
|
|
176 |
public Collection engineGetCRLs(CRLSelector sel) { return Collections.EMPTY_LIST; }
|
|
177 |
}
|
|
178 |
|
|
179 |
public static class BaseSignatureSpi extends SignatureSpi {
|
|
180 |
protected void engineInitVerify(PublicKey publicKey) throws InvalidKeyException {
|
|
181 |
}
|
|
182 |
protected void engineInitSign(PrivateKey privateKey) throws InvalidKeyException {
|
|
183 |
}
|
|
184 |
protected void engineUpdate(byte b) throws SignatureException { }
|
|
185 |
protected void engineUpdate(byte[] b, int off, int len) throws SignatureException { }
|
|
186 |
protected byte[] engineSign() throws SignatureException {
|
|
187 |
return new byte[0];
|
|
188 |
}
|
|
189 |
protected boolean engineVerify(byte[] sigBytes) throws SignatureException {
|
|
190 |
return false;
|
|
191 |
}
|
|
192 |
protected void engineSetParameter(String param, Object value) throws InvalidParameterException {
|
|
193 |
}
|
|
194 |
protected Object engineGetParameter(String param) throws InvalidParameterException {
|
|
195 |
return null;
|
|
196 |
}
|
|
197 |
}
|
|
198 |
|
|
199 |
public static class BaseSignature extends Signature {
|
|
200 |
BaseSignature(String s) {
|
|
201 |
super(s);
|
|
202 |
}
|
|
203 |
protected void engineInitVerify(PublicKey publicKey) throws InvalidKeyException {
|
|
204 |
//
|
|
205 |
}
|
|
206 |
protected void engineInitSign(PrivateKey privateKey) throws InvalidKeyException { }
|
|
207 |
protected void engineUpdate(byte b) throws SignatureException { }
|
|
208 |
protected void engineUpdate(byte[] b, int off, int len) throws SignatureException { }
|
|
209 |
protected byte[] engineSign() throws SignatureException {
|
|
210 |
return new byte[0];
|
|
211 |
}
|
|
212 |
protected boolean engineVerify(byte[] sigBytes) throws SignatureException {
|
|
213 |
return false;
|
|
214 |
}
|
|
215 |
protected void engineSetParameter(String param, Object value) throws InvalidParameterException {
|
|
216 |
}
|
|
217 |
protected Object engineGetParameter(String param) throws InvalidParameterException {
|
|
218 |
return null;
|
|
219 |
}
|
|
220 |
}
|
|
221 |
|
|
222 |
public static abstract class FooKey implements Key {
|
|
223 |
public String getFormat() { return null; }
|
|
224 |
public byte[] getEncoded() { return null; }
|
|
225 |
public String getAlgorithm() { return "foo"; }
|
|
226 |
}
|
|
227 |
|
|
228 |
public static class FooPrivateKey extends FooKey implements PrivateKey { }
|
|
229 |
|
|
230 |
public static class FooPublicKey extends FooKey implements PublicKey { }
|
|
231 |
|
|
232 |
public static class FooSignatureSpi extends BaseSignatureSpi {
|
|
233 |
public FooSignatureSpi() {
|
|
234 |
super();
|
|
235 |
System.out.println("FooSignatureSpi constructor");
|
|
236 |
}
|
|
237 |
}
|
|
238 |
|
|
239 |
public static class BazSignatureSpi extends BaseSignatureSpi {
|
|
240 |
public BazSignatureSpi() {
|
|
241 |
super();
|
|
242 |
System.out.println("BazSignatureSpi constructor");
|
|
243 |
}
|
|
244 |
protected void engineInitVerify(PublicKey publicKey) throws InvalidKeyException {
|
|
245 |
throw new InvalidKeyException("verify not supported");
|
|
246 |
}
|
|
247 |
}
|
|
248 |
|
|
249 |
public static class BazSignature extends BaseSignature {
|
|
250 |
public BazSignature() {
|
|
251 |
super("baz");
|
|
252 |
System.out.println("BazSignature constructor");
|
|
253 |
}
|
|
254 |
}
|
|
255 |
|
|
256 |
}
|