author | wetmore |
Tue, 20 Jun 2017 12:57:42 -0700 | |
changeset 45665 | 6f21cd7ec80e |
parent 42991 | 174ff3aebaf7 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
42991
174ff3aebaf7
8172003: getInstance() with unknown provider throws NPE
apetcher
parents:
39827
diff
changeset
|
2 |
* Copyright (c) 2005, 2017, 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
/* |
|
26 |
* $Id: TransformService.java,v 1.6.4.1 2005/09/15 12:42:11 mullan Exp $ |
|
27 |
*/ |
|
28 |
package javax.xml.crypto.dsig; |
|
29 |
||
30 |
import java.security.InvalidAlgorithmParameterException; |
|
31 |
import java.security.NoSuchAlgorithmException; |
|
32 |
import java.security.NoSuchProviderException; |
|
33 |
import java.security.Provider; |
|
34 |
import java.security.Provider.Service; |
|
35 |
import java.security.Security; |
|
36 |
import java.util.*; |
|
37 |
import javax.xml.crypto.MarshalException; |
|
38 |
import javax.xml.crypto.XMLStructure; |
|
39 |
import javax.xml.crypto.XMLCryptoContext; |
|
40 |
import javax.xml.crypto.dsig.spec.TransformParameterSpec; |
|
41 |
||
42 |
||
43 |
/** |
|
44 |
* A Service Provider Interface for transform and canonicalization algorithms. |
|
45 |
* |
|
46 |
* <p>Each instance of <code>TransformService</code> supports a specific |
|
47 |
* transform or canonicalization algorithm and XML mechanism type. To create a |
|
48 |
* <code>TransformService</code>, call one of the static |
|
49 |
* {@link #getInstance getInstance} methods, passing in the algorithm URI and |
|
50 |
* XML mechanism type desired, for example: |
|
51 |
* |
|
52 |
* <blockquote><code> |
|
53 |
* TransformService ts = TransformService.getInstance(Transform.XPATH2, "DOM"); |
|
54 |
* </code></blockquote> |
|
55 |
* |
|
56 |
* <p><code>TransformService</code> implementations are registered and loaded |
|
57 |
* using the {@link java.security.Provider} mechanism. Each |
|
58 |
* <code>TransformService</code> service provider implementation should include |
|
59 |
* a <code>MechanismType</code> service attribute that identifies the XML |
|
60 |
* mechanism type that it supports. If the attribute is not specified, |
|
61 |
* "DOM" is assumed. For example, a service provider that supports the |
|
62 |
* XPath Filter 2 Transform and DOM mechanism would be specified in the |
|
63 |
* <code>Provider</code> subclass as: |
|
64 |
* <pre> |
|
65 |
* put("TransformService." + Transform.XPATH2, |
|
66 |
* "org.example.XPath2TransformService"); |
|
67 |
* put("TransformService." + Transform.XPATH2 + " MechanismType", "DOM"); |
|
68 |
* </pre> |
|
69 |
* <code>TransformService</code> implementations that support the DOM |
|
70 |
* mechanism type must abide by the DOM interoperability requirements defined |
|
71 |
* in the |
|
45665 | 72 |
* {@extLink security_guide_xmldsig_rqmts DOM Mechanism Requirements} section |
73 |
* of the API overview. See the |
|
74 |
* {@extLink security_guide_xmldsig_provider Service Providers} section of |
|
75 |
* the API overview for a list of standard mechanism types. |
|
2 | 76 |
* <p> |
77 |
* Once a <code>TransformService</code> has been created, it can be used |
|
78 |
* to process <code>Transform</code> or <code>CanonicalizationMethod</code> |
|
79 |
* objects. If the <code>Transform</code> or <code>CanonicalizationMethod</code> |
|
80 |
* exists in XML form (for example, when validating an existing |
|
81 |
* <code>XMLSignature</code>), the {@link #init(XMLStructure, XMLCryptoContext)} |
|
82 |
* method must be first called to initialize the transform and provide document |
|
83 |
* context (even if there are no parameters). Alternatively, if the |
|
84 |
* <code>Transform</code> or <code>CanonicalizationMethod</code> is being |
|
85 |
* created from scratch, the {@link #init(TransformParameterSpec)} method |
|
86 |
* is called to initialize the transform with parameters and the |
|
87 |
* {@link #marshalParams marshalParams} method is called to marshal the |
|
88 |
* parameters to XML and provide the transform with document context. Finally, |
|
89 |
* the {@link #transform transform} method is called to perform the |
|
90 |
* transformation. |
|
91 |
* <p> |
|
92 |
* <b>Concurrent Access</b> |
|
93 |
* <p>The static methods of this class are guaranteed to be thread-safe. |
|
94 |
* Multiple threads may concurrently invoke the static methods defined in this |
|
95 |
* class with no ill effects. |
|
96 |
* |
|
97 |
* <p>However, this is not true for the non-static methods defined by this |
|
98 |
* class. Unless otherwise documented by a specific provider, threads that |
|
99 |
* need to access a single <code>TransformService</code> instance |
|
100 |
* concurrently should synchronize amongst themselves and provide the |
|
101 |
* necessary locking. Multiple threads each manipulating a different |
|
102 |
* <code>TransformService</code> instance need not synchronize. |
|
103 |
* |
|
104 |
* @author Sean Mullan |
|
105 |
* @author JSR 105 Expert Group |
|
106 |
* @since 1.6 |
|
107 |
*/ |
|
108 |
public abstract class TransformService implements Transform { |
|
109 |
||
110 |
private String algorithm; |
|
111 |
private String mechanism; |
|
112 |
private Provider provider; |
|
113 |
||
114 |
/** |
|
115 |
* Default constructor, for invocation by subclasses. |
|
116 |
*/ |
|
117 |
protected TransformService() {} |
|
118 |
||
119 |
/** |
|
120 |
* Returns a <code>TransformService</code> that supports the specified |
|
121 |
* algorithm URI (ex: {@link Transform#XPATH2}) and mechanism type |
|
122 |
* (ex: DOM). |
|
123 |
* |
|
124 |
* <p>This method uses the standard JCA provider lookup mechanism to |
|
125 |
* locate and instantiate a <code>TransformService</code> implementation |
|
126 |
* of the desired algorithm and <code>MechanismType</code> service |
|
127 |
* attribute. It traverses the list of registered security |
|
128 |
* <code>Provider</code>s, starting with the most preferred |
|
129 |
* <code>Provider</code>. A new <code>TransformService</code> object |
|
130 |
* from the first <code>Provider</code> that supports the specified |
|
131 |
* algorithm and mechanism type is returned. |
|
132 |
* |
|
133 |
* <p> Note that the list of registered providers may be retrieved via |
|
134 |
* the {@link Security#getProviders() Security.getProviders()} method. |
|
135 |
* |
|
33241
27eb2d6abda9
8133151: Preferred provider configuration for JCE
ascarpino
parents:
25859
diff
changeset
|
136 |
* @implNote |
27eb2d6abda9
8133151: Preferred provider configuration for JCE
ascarpino
parents:
25859
diff
changeset
|
137 |
* The JDK Reference Implementation additionally uses the |
37348
9ccec3170d5e
8152205: jdk.security.provider.preferred is ambiguously documented
ascarpino
parents:
33241
diff
changeset
|
138 |
* {@code jdk.security.provider.preferred} |
9ccec3170d5e
8152205: jdk.security.provider.preferred is ambiguously documented
ascarpino
parents:
33241
diff
changeset
|
139 |
* {@link Security#getProperty(String) Security} property to determine |
33241
27eb2d6abda9
8133151: Preferred provider configuration for JCE
ascarpino
parents:
25859
diff
changeset
|
140 |
* the preferred provider order for the specified algorithm. This |
27eb2d6abda9
8133151: Preferred provider configuration for JCE
ascarpino
parents:
25859
diff
changeset
|
141 |
* may be different than the order of providers returned by |
27eb2d6abda9
8133151: Preferred provider configuration for JCE
ascarpino
parents:
25859
diff
changeset
|
142 |
* {@link Security#getProviders() Security.getProviders()}. |
27eb2d6abda9
8133151: Preferred provider configuration for JCE
ascarpino
parents:
25859
diff
changeset
|
143 |
* |
2 | 144 |
* @param algorithm the URI of the algorithm |
145 |
* @param mechanismType the type of the XML processing mechanism and |
|
146 |
* representation |
|
147 |
* @return a new <code>TransformService</code> |
|
148 |
* @throws NullPointerException if <code>algorithm</code> or |
|
149 |
* <code>mechanismType</code> is <code>null</code> |
|
150 |
* @throws NoSuchAlgorithmException if no <code>Provider</code> supports a |
|
151 |
* <code>TransformService</code> implementation for the specified |
|
152 |
* algorithm and mechanism type |
|
153 |
* @see Provider |
|
154 |
*/ |
|
155 |
public static TransformService getInstance |
|
156 |
(String algorithm, String mechanismType) |
|
157 |
throws NoSuchAlgorithmException { |
|
158 |
if (mechanismType == null || algorithm == null) { |
|
159 |
throw new NullPointerException(); |
|
160 |
} |
|
161 |
boolean dom = false; |
|
162 |
if (mechanismType.equals("DOM")) { |
|
163 |
dom = true; |
|
164 |
} |
|
39827 | 165 |
|
166 |
Provider[] provs = Security.getProviders(); |
|
167 |
for (Provider p : provs) { |
|
168 |
Service s = p.getService("TransformService", algorithm); |
|
169 |
if (s != null) { |
|
170 |
String value = s.getAttribute("MechanismType"); |
|
171 |
if ((value == null && dom) || |
|
172 |
(value != null && value.equals(mechanismType))) { |
|
173 |
Object obj = s.newInstance(null); |
|
174 |
if (obj instanceof TransformService) { |
|
175 |
TransformService ts = (TransformService) obj; |
|
176 |
ts.algorithm = algorithm; |
|
177 |
ts.mechanism = mechanismType; |
|
178 |
ts.provider = p; |
|
179 |
return ts; |
|
180 |
} |
|
181 |
} |
|
2 | 182 |
} |
183 |
} |
|
184 |
throw new NoSuchAlgorithmException |
|
185 |
(algorithm + " algorithm and " + mechanismType |
|
186 |
+ " mechanism not available"); |
|
187 |
} |
|
188 |
||
189 |
/** |
|
190 |
* Returns a <code>TransformService</code> that supports the specified |
|
191 |
* algorithm URI (ex: {@link Transform#XPATH2}) and mechanism type |
|
192 |
* (ex: DOM) as supplied by the specified provider. Note that the specified |
|
193 |
* <code>Provider</code> object does not have to be registered in the |
|
194 |
* provider list. |
|
195 |
* |
|
196 |
* @param algorithm the URI of the algorithm |
|
197 |
* @param mechanismType the type of the XML processing mechanism and |
|
198 |
* representation |
|
199 |
* @param provider the <code>Provider</code> object |
|
200 |
* @return a new <code>TransformService</code> |
|
201 |
* @throws NullPointerException if <code>provider</code>, |
|
202 |
* <code>algorithm</code>, or <code>mechanismType</code> is |
|
203 |
* <code>null</code> |
|
204 |
* @throws NoSuchAlgorithmException if a <code>TransformService</code> |
|
205 |
* implementation for the specified algorithm and mechanism type is not |
|
206 |
* available from the specified <code>Provider</code> object |
|
207 |
* @see Provider |
|
208 |
*/ |
|
209 |
public static TransformService getInstance |
|
210 |
(String algorithm, String mechanismType, Provider provider) |
|
211 |
throws NoSuchAlgorithmException { |
|
212 |
if (mechanismType == null || algorithm == null || provider == null) { |
|
213 |
throw new NullPointerException(); |
|
214 |
} |
|
215 |
||
216 |
boolean dom = false; |
|
217 |
if (mechanismType.equals("DOM")) { |
|
218 |
dom = true; |
|
219 |
} |
|
39827 | 220 |
Service s = provider.getService("TransformService", algorithm); |
221 |
if (s != null) { |
|
222 |
String value = s.getAttribute("MechanismType"); |
|
223 |
if ((value == null && dom) || |
|
224 |
(value != null && value.equals(mechanismType))) { |
|
225 |
Object obj = s.newInstance(null); |
|
226 |
if (obj instanceof TransformService) { |
|
227 |
TransformService ts = (TransformService) obj; |
|
228 |
ts.algorithm = algorithm; |
|
229 |
ts.mechanism = mechanismType; |
|
230 |
ts.provider = provider; |
|
231 |
return ts; |
|
232 |
} |
|
233 |
} |
|
2 | 234 |
} |
235 |
throw new NoSuchAlgorithmException |
|
236 |
(algorithm + " algorithm and " + mechanismType |
|
39827 | 237 |
+ " mechanism not available from " + provider.getName()); |
2 | 238 |
} |
239 |
||
240 |
/** |
|
241 |
* Returns a <code>TransformService</code> that supports the specified |
|
242 |
* algorithm URI (ex: {@link Transform#XPATH2}) and mechanism type |
|
243 |
* (ex: DOM) as supplied by the specified provider. The specified provider |
|
244 |
* must be registered in the security provider list. |
|
245 |
* |
|
246 |
* <p>Note that the list of registered providers may be retrieved via |
|
247 |
* the {@link Security#getProviders() Security.getProviders()} method. |
|
248 |
* |
|
249 |
* @param algorithm the URI of the algorithm |
|
250 |
* @param mechanismType the type of the XML processing mechanism and |
|
251 |
* representation |
|
252 |
* @param provider the string name of the provider |
|
253 |
* @return a new <code>TransformService</code> |
|
254 |
* @throws NoSuchProviderException if the specified provider is not |
|
255 |
* registered in the security provider list |
|
256 |
* @throws NullPointerException if <code>provider</code>, |
|
257 |
* <code>mechanismType</code>, or <code>algorithm</code> is |
|
258 |
* <code>null</code> |
|
259 |
* @throws NoSuchAlgorithmException if a <code>TransformService</code> |
|
260 |
* implementation for the specified algorithm and mechanism type is not |
|
261 |
* available from the specified provider |
|
262 |
* @see Provider |
|
263 |
*/ |
|
264 |
public static TransformService getInstance |
|
265 |
(String algorithm, String mechanismType, String provider) |
|
266 |
throws NoSuchAlgorithmException, NoSuchProviderException { |
|
267 |
if (mechanismType == null || algorithm == null || provider == null) { |
|
268 |
throw new NullPointerException(); |
|
269 |
} else if (provider.length() == 0) { |
|
270 |
throw new NoSuchProviderException(); |
|
271 |
} |
|
272 |
boolean dom = false; |
|
273 |
if (mechanismType.equals("DOM")) { |
|
274 |
dom = true; |
|
275 |
} |
|
39827 | 276 |
Provider p = Security.getProvider(provider); |
42991
174ff3aebaf7
8172003: getInstance() with unknown provider throws NPE
apetcher
parents:
39827
diff
changeset
|
277 |
if (p == null) { |
174ff3aebaf7
8172003: getInstance() with unknown provider throws NPE
apetcher
parents:
39827
diff
changeset
|
278 |
throw new NoSuchProviderException("No such provider: " + |
174ff3aebaf7
8172003: getInstance() with unknown provider throws NPE
apetcher
parents:
39827
diff
changeset
|
279 |
provider); |
174ff3aebaf7
8172003: getInstance() with unknown provider throws NPE
apetcher
parents:
39827
diff
changeset
|
280 |
} |
39827 | 281 |
Service s = p.getService("TransformService", algorithm); |
282 |
if (s != null) { |
|
283 |
String value = s.getAttribute("MechanismType"); |
|
284 |
if ((value == null && dom) || |
|
285 |
(value != null && value.equals(mechanismType))) { |
|
286 |
Object obj = s.newInstance(null); |
|
287 |
if (obj instanceof TransformService) { |
|
288 |
TransformService ts = (TransformService) obj; |
|
289 |
ts.algorithm = algorithm; |
|
290 |
ts.mechanism = mechanismType; |
|
291 |
ts.provider = p; |
|
292 |
return ts; |
|
293 |
} |
|
294 |
} |
|
2 | 295 |
} |
296 |
throw new NoSuchAlgorithmException |
|
297 |
(algorithm + " algorithm and " + mechanismType |
|
39827 | 298 |
+ " mechanism not available from " + provider); |
2 | 299 |
} |
300 |
||
11276
6a7de6dddc18
7118546: fix warnings in javax.xml.crypto, javax.script
smarks
parents:
5506
diff
changeset
|
301 |
private static class MechanismMapEntry implements Map.Entry<String,String> { |
2 | 302 |
private final String mechanism; |
303 |
private final String algorithm; |
|
304 |
private final String key; |
|
305 |
MechanismMapEntry(String algorithm, String mechanism) { |
|
306 |
this.algorithm = algorithm; |
|
307 |
this.mechanism = mechanism; |
|
308 |
this.key = "TransformService." + algorithm + " MechanismType"; |
|
309 |
} |
|
310 |
public boolean equals(Object o) { |
|
311 |
if (!(o instanceof Map.Entry)) { |
|
312 |
return false; |
|
313 |
} |
|
11276
6a7de6dddc18
7118546: fix warnings in javax.xml.crypto, javax.script
smarks
parents:
5506
diff
changeset
|
314 |
Map.Entry<?,?> e = (Map.Entry<?,?>) o; |
2 | 315 |
return (getKey()==null ? |
316 |
e.getKey()==null : getKey().equals(e.getKey())) && |
|
317 |
(getValue()==null ? |
|
318 |
e.getValue()==null : getValue().equals(e.getValue())); |
|
319 |
} |
|
11276
6a7de6dddc18
7118546: fix warnings in javax.xml.crypto, javax.script
smarks
parents:
5506
diff
changeset
|
320 |
public String getKey() { |
2 | 321 |
return key; |
322 |
} |
|
11276
6a7de6dddc18
7118546: fix warnings in javax.xml.crypto, javax.script
smarks
parents:
5506
diff
changeset
|
323 |
public String getValue() { |
2 | 324 |
return mechanism; |
325 |
} |
|
11276
6a7de6dddc18
7118546: fix warnings in javax.xml.crypto, javax.script
smarks
parents:
5506
diff
changeset
|
326 |
public String setValue(String value) { |
2 | 327 |
throw new UnsupportedOperationException(); |
328 |
} |
|
329 |
public int hashCode() { |
|
330 |
return (getKey()==null ? 0 : getKey().hashCode()) ^ |
|
331 |
(getValue()==null ? 0 : getValue().hashCode()); |
|
332 |
} |
|
333 |
} |
|
334 |
||
335 |
/** |
|
336 |
* Returns the mechanism type supported by this <code>TransformService</code>. |
|
337 |
* |
|
338 |
* @return the mechanism type |
|
339 |
*/ |
|
340 |
public final String getMechanismType() { |
|
341 |
return mechanism; |
|
342 |
} |
|
343 |
||
344 |
/** |
|
345 |
* Returns the URI of the algorithm supported by this |
|
346 |
* <code>TransformService</code>. |
|
347 |
* |
|
348 |
* @return the algorithm URI |
|
349 |
*/ |
|
350 |
public final String getAlgorithm() { |
|
351 |
return algorithm; |
|
352 |
} |
|
353 |
||
354 |
/** |
|
355 |
* Returns the provider of this <code>TransformService</code>. |
|
356 |
* |
|
357 |
* @return the provider |
|
358 |
*/ |
|
359 |
public final Provider getProvider() { |
|
360 |
return provider; |
|
361 |
} |
|
362 |
||
363 |
/** |
|
364 |
* Initializes this <code>TransformService</code> with the specified |
|
365 |
* parameters. |
|
366 |
* |
|
367 |
* <p>If the parameters exist in XML form, the |
|
368 |
* {@link #init(XMLStructure, XMLCryptoContext)} method should be used to |
|
369 |
* initialize the <code>TransformService</code>. |
|
370 |
* |
|
371 |
* @param params the algorithm parameters (may be <code>null</code> if |
|
372 |
* not required or optional) |
|
373 |
* @throws InvalidAlgorithmParameterException if the specified parameters |
|
374 |
* are invalid for this algorithm |
|
375 |
*/ |
|
376 |
public abstract void init(TransformParameterSpec params) |
|
377 |
throws InvalidAlgorithmParameterException; |
|
378 |
||
379 |
/** |
|
380 |
* Marshals the algorithm-specific parameters. If there are no parameters |
|
381 |
* to be marshalled, this method returns without throwing an exception. |
|
382 |
* |
|
383 |
* @param parent a mechanism-specific structure containing the parent |
|
384 |
* node that the marshalled parameters should be appended to |
|
385 |
* @param context the <code>XMLCryptoContext</code> containing |
|
386 |
* additional context (may be <code>null</code> if not applicable) |
|
387 |
* @throws ClassCastException if the type of <code>parent</code> or |
|
388 |
* <code>context</code> is not compatible with this |
|
389 |
* <code>TransformService</code> |
|
390 |
* @throws NullPointerException if <code>parent</code> is <code>null</code> |
|
391 |
* @throws MarshalException if the parameters cannot be marshalled |
|
392 |
*/ |
|
393 |
public abstract void marshalParams |
|
394 |
(XMLStructure parent, XMLCryptoContext context) |
|
395 |
throws MarshalException; |
|
396 |
||
397 |
/** |
|
398 |
* Initializes this <code>TransformService</code> with the specified |
|
399 |
* parameters and document context. |
|
400 |
* |
|
401 |
* @param parent a mechanism-specific structure containing the parent |
|
402 |
* structure |
|
403 |
* @param context the <code>XMLCryptoContext</code> containing |
|
404 |
* additional context (may be <code>null</code> if not applicable) |
|
405 |
* @throws ClassCastException if the type of <code>parent</code> or |
|
406 |
* <code>context</code> is not compatible with this |
|
407 |
* <code>TransformService</code> |
|
408 |
* @throws NullPointerException if <code>parent</code> is <code>null</code> |
|
409 |
* @throws InvalidAlgorithmParameterException if the specified parameters |
|
410 |
* are invalid for this algorithm |
|
411 |
*/ |
|
412 |
public abstract void init(XMLStructure parent, XMLCryptoContext context) |
|
413 |
throws InvalidAlgorithmParameterException; |
|
414 |
} |