author | mullan |
Mon, 26 Sep 2011 17:20:45 -0700 | |
changeset 10694 | cf59e2badd14 |
parent 8814 | f899ad8ada82 |
child 18240 | cda839ac048f |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2 |
* reserved comment block |
|
3 |
* DO NOT REMOVE OR ALTER! |
|
4 |
*/ |
|
5 |
/* |
|
6 |
* Copyright 1999-2004 The Apache Software Foundation. |
|
7 |
* |
|
8 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
9 |
* you may not use this file except in compliance with the License. |
|
10 |
* You may obtain a copy of the License at |
|
11 |
* |
|
12 |
* http://www.apache.org/licenses/LICENSE-2.0 |
|
13 |
* |
|
14 |
* Unless required by applicable law or agreed to in writing, software |
|
15 |
* distributed under the License is distributed on an "AS IS" BASIS, |
|
16 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
17 |
* See the License for the specific language governing permissions and |
|
18 |
* limitations under the License. |
|
19 |
* |
|
20 |
*/ |
|
21 |
package com.sun.org.apache.xml.internal.security.transforms; |
|
22 |
||
23 |
import java.io.IOException; |
|
24 |
import java.io.OutputStream; |
|
25 |
import java.security.AccessController; |
|
26 |
import java.security.PrivilegedAction; |
|
1337 | 27 |
import java.util.HashMap; |
10694
cf59e2badd14
7088502: Security libraries don't build with javac -Werror
mullan
parents:
8814
diff
changeset
|
28 |
import java.util.Map; |
2 | 29 |
import javax.xml.parsers.ParserConfigurationException; |
30 |
||
31 |
import com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException; |
|
32 |
import com.sun.org.apache.xml.internal.security.c14n.InvalidCanonicalizerException; |
|
33 |
import com.sun.org.apache.xml.internal.security.exceptions.AlgorithmAlreadyRegisteredException; |
|
34 |
import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException; |
|
35 |
import com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput; |
|
36 |
import com.sun.org.apache.xml.internal.security.utils.Constants; |
|
37 |
import com.sun.org.apache.xml.internal.security.utils.HelperNodeList; |
|
38 |
import com.sun.org.apache.xml.internal.security.utils.SignatureElementProxy; |
|
1337 | 39 |
import com.sun.org.apache.xml.internal.security.utils.XMLUtils; |
2 | 40 |
import org.w3c.dom.Document; |
41 |
import org.w3c.dom.Element; |
|
42 |
import org.w3c.dom.NodeList; |
|
43 |
import org.xml.sax.SAXException; |
|
44 |
||
45 |
/** |
|
46 |
* Implements the behaviour of the <code>ds:Transform</code> element. |
|
47 |
* |
|
1337 | 48 |
* This <code>Transform</code>(Factory) class acts as the Factory and Proxy of |
49 |
* the implementing class that supports the functionality of <a |
|
2 | 50 |
* href=http://www.w3.org/TR/xmldsig-core/#sec-TransformAlg>a Transform |
51 |
* algorithm</a>. |
|
52 |
* Implements the Factory and Proxy pattern for ds:Transform algorithms. |
|
53 |
* |
|
54 |
* @author Christian Geuer-Pollmann |
|
55 |
* @see Transforms |
|
56 |
* @see TransformSpi |
|
57 |
* |
|
58 |
*/ |
|
59 |
public final class Transform extends SignatureElementProxy { |
|
60 |
||
1337 | 61 |
/** {@link java.util.logging} logging facility */ |
2 | 62 |
static java.util.logging.Logger log = |
63 |
java.util.logging.Logger.getLogger(Transform.class.getName()); |
|
64 |
||
1337 | 65 |
/** Field _alreadyInitialized */ |
66 |
private static boolean alreadyInitialized = false; |
|
2 | 67 |
|
1337 | 68 |
/** All available Transform classes are registered here */ |
10694
cf59e2badd14
7088502: Security libraries don't build with javac -Werror
mullan
parents:
8814
diff
changeset
|
69 |
private static Map<String, Class<?>> transformClassHash = null; |
2 | 70 |
|
10694
cf59e2badd14
7088502: Security libraries don't build with javac -Werror
mullan
parents:
8814
diff
changeset
|
71 |
private static Map<String, TransformSpi> transformSpiHash = new HashMap<String, TransformSpi>(); |
1337 | 72 |
|
73 |
private TransformSpi transformSpi = null; |
|
2 | 74 |
|
1337 | 75 |
/** |
76 |
* Constructs {@link Transform} |
|
77 |
* |
|
78 |
* @param doc the {@link Document} in which <code>Transform</code> will be |
|
79 |
* placed |
|
80 |
* @param algorithmURI URI representation of |
|
81 |
* <code>Transform algorithm</code> which will be specified as parameter of |
|
82 |
* {@link #getInstance(Document, String)}, when generated. </br> |
|
83 |
* @param contextNodes the child node list of <code>Transform</code> element |
|
84 |
* @throws InvalidTransformException |
|
85 |
*/ |
|
86 |
public Transform(Document doc, String algorithmURI, NodeList contextNodes) |
|
87 |
throws InvalidTransformException { |
|
2 | 88 |
|
1337 | 89 |
super(doc); |
2 | 90 |
|
1337 | 91 |
this._constructionElement.setAttributeNS |
92 |
(null, Constants._ATT_ALGORITHM, algorithmURI); |
|
2 | 93 |
|
1337 | 94 |
transformSpi = getTransformSpi(algorithmURI); |
95 |
if (transformSpi == null) { |
|
2 | 96 |
Object exArgs[] = { algorithmURI }; |
97 |
throw new InvalidTransformException( |
|
98 |
"signature.Transform.UnknownTransform", exArgs); |
|
1337 | 99 |
} |
2 | 100 |
|
1337 | 101 |
if (log.isLoggable(java.util.logging.Level.FINE)) { |
102 |
log.log(java.util.logging.Level.FINE, "Create URI \"" + algorithmURI + "\" class \"" |
|
103 |
+ transformSpi.getClass() + "\""); |
|
104 |
log.log(java.util.logging.Level.FINE, "The NodeList is " + contextNodes); |
|
105 |
} |
|
2 | 106 |
|
1337 | 107 |
// give it to the current document |
108 |
if (contextNodes != null) { |
|
109 |
for (int i = 0; i < contextNodes.getLength(); i++) { |
|
110 |
this._constructionElement.appendChild |
|
111 |
(contextNodes.item(i).cloneNode(true)); |
|
2 | 112 |
} |
1337 | 113 |
} |
114 |
} |
|
2 | 115 |
|
1337 | 116 |
/** |
117 |
* This constructor can only be called from the {@link Transforms} object, |
|
118 |
* so it's protected. |
|
119 |
* |
|
120 |
* @param element <code>ds:Transform</code> element |
|
121 |
* @param BaseURI the URI of the resource where the XML instance was stored |
|
122 |
* @throws InvalidTransformException |
|
123 |
* @throws TransformationException |
|
124 |
* @throws XMLSecurityException |
|
125 |
*/ |
|
126 |
public Transform(Element element, String BaseURI) |
|
127 |
throws InvalidTransformException, TransformationException, |
|
128 |
XMLSecurityException { |
|
2 | 129 |
|
1337 | 130 |
super(element, BaseURI); |
2 | 131 |
|
1337 | 132 |
// retrieve Algorithm Attribute from ds:Transform |
133 |
String algorithmURI = element.getAttributeNS(null, Constants._ATT_ALGORITHM); |
|
2 | 134 |
|
1337 | 135 |
if (algorithmURI == null || algorithmURI.length() == 0) { |
136 |
Object exArgs[] = { Constants._ATT_ALGORITHM, |
|
137 |
Constants._TAG_TRANSFORM }; |
|
138 |
throw new TransformationException("xml.WrongContent", exArgs); |
|
139 |
} |
|
2 | 140 |
|
1337 | 141 |
transformSpi = getTransformSpi(algorithmURI); |
142 |
if (transformSpi == null) { |
|
143 |
Object exArgs[] = { algorithmURI }; |
|
144 |
throw new InvalidTransformException( |
|
145 |
"signature.Transform.UnknownTransform", exArgs); |
|
146 |
} |
|
147 |
} |
|
2 | 148 |
|
1337 | 149 |
/** |
150 |
* Generates a Transform object that implements the specified |
|
151 |
* <code>Transform algorithm</code> URI. |
|
152 |
* |
|
153 |
* @param algorithmURI <code>Transform algorithm</code> URI representation, |
|
154 |
* such as specified in |
|
155 |
* <a href=http://www.w3.org/TR/xmldsig-core/#sec-TransformAlg>Transform algorithm </a> |
|
156 |
* @param doc the proxy {@link Document} |
|
157 |
* @return <code>{@link Transform}</code> object |
|
158 |
* @throws InvalidTransformException |
|
159 |
*/ |
|
160 |
public static Transform getInstance( |
|
161 |
Document doc, String algorithmURI) throws InvalidTransformException { |
|
162 |
return getInstance(doc, algorithmURI, (NodeList) null); |
|
163 |
} |
|
2 | 164 |
|
1337 | 165 |
/** |
166 |
* Generates a Transform object that implements the specified |
|
167 |
* <code>Transform algorithm</code> URI. |
|
168 |
* |
|
169 |
* @param algorithmURI <code>Transform algorithm</code> URI representation, |
|
170 |
* such as specified in |
|
171 |
* <a href=http://www.w3.org/TR/xmldsig-core/#sec-TransformAlg>Transform algorithm </a> |
|
172 |
* @param contextChild the child element of <code>Transform</code> element |
|
173 |
* @param doc the proxy {@link Document} |
|
174 |
* @return <code>{@link Transform}</code> object |
|
175 |
* @throws InvalidTransformException |
|
176 |
*/ |
|
177 |
public static Transform getInstance( |
|
178 |
Document doc, String algorithmURI, Element contextChild) |
|
179 |
throws InvalidTransformException { |
|
2 | 180 |
|
1337 | 181 |
HelperNodeList contextNodes = new HelperNodeList(); |
2 | 182 |
|
1337 | 183 |
XMLUtils.addReturnToElement(doc, contextNodes); |
184 |
contextNodes.appendChild(contextChild); |
|
185 |
XMLUtils.addReturnToElement(doc, contextNodes); |
|
2 | 186 |
|
1337 | 187 |
return getInstance(doc, algorithmURI, contextNodes); |
188 |
} |
|
2 | 189 |
|
1337 | 190 |
/** |
191 |
* Generates a Transform object that implements the specified |
|
192 |
* <code>Transform algorithm</code> URI. |
|
193 |
* |
|
194 |
* @param algorithmURI <code>Transform algorithm</code> URI form, such as |
|
195 |
* specified in <a href=http://www.w3.org/TR/xmldsig-core/#sec-TransformAlg> |
|
196 |
* Transform algorithm </a> |
|
197 |
* @param contextNodes the child node list of <code>Transform</code> element |
|
198 |
* @param doc the proxy {@link Document} |
|
199 |
* @return <code>{@link Transform}</code> object |
|
200 |
* @throws InvalidTransformException |
|
201 |
*/ |
|
202 |
public static Transform getInstance( |
|
203 |
Document doc, String algorithmURI, NodeList contextNodes) |
|
204 |
throws InvalidTransformException { |
|
205 |
return new Transform(doc, algorithmURI, contextNodes); |
|
206 |
} |
|
2 | 207 |
|
1337 | 208 |
/** |
209 |
* Initalizes for this {@link Transform}. |
|
210 |
*/ |
|
211 |
public static void init() { |
|
212 |
if (!alreadyInitialized) { |
|
10694
cf59e2badd14
7088502: Security libraries don't build with javac -Werror
mullan
parents:
8814
diff
changeset
|
213 |
transformClassHash = new HashMap<String,Class<?>>(10); |
8814
f899ad8ada82
6994263: Untrusted code can replace JRE's XML DSig Transform or C14N algorithm implementations
mullan
parents:
1337
diff
changeset
|
214 |
// make sure builtin algorithms are all registered first |
f899ad8ada82
6994263: Untrusted code can replace JRE's XML DSig Transform or C14N algorithm implementations
mullan
parents:
1337
diff
changeset
|
215 |
com.sun.org.apache.xml.internal.security.Init.init(); |
1337 | 216 |
alreadyInitialized = true; |
217 |
} |
|
218 |
} |
|
2 | 219 |
|
1337 | 220 |
/** |
221 |
* Registers implementing class of the Transform algorithm with algorithmURI |
|
222 |
* |
|
223 |
* @param algorithmURI algorithmURI URI representation of |
|
224 |
* <code>Transform algorithm</code> will be specified as parameter of |
|
225 |
* {@link #getInstance(Document, String)}, when generate. </br> |
|
226 |
* @param implementingClass <code>implementingClass</code> the implementing |
|
227 |
* class of {@link TransformSpi} |
|
228 |
* @throws AlgorithmAlreadyRegisteredException if specified algorithmURI |
|
229 |
* is already registered |
|
230 |
*/ |
|
231 |
public static void register(String algorithmURI, String implementingClass) |
|
232 |
throws AlgorithmAlreadyRegisteredException { |
|
2 | 233 |
|
1337 | 234 |
// are we already registered? |
10694
cf59e2badd14
7088502: Security libraries don't build with javac -Werror
mullan
parents:
8814
diff
changeset
|
235 |
Class<? extends TransformSpi> registeredClass = getImplementingClass(algorithmURI); |
1337 | 236 |
if ((registeredClass != null) ) { |
2 | 237 |
Object exArgs[] = { algorithmURI, registeredClass }; |
238 |
throw new AlgorithmAlreadyRegisteredException( |
|
239 |
"algorithm.alreadyRegistered", exArgs); |
|
1337 | 240 |
} |
2 | 241 |
|
8814
f899ad8ada82
6994263: Untrusted code can replace JRE's XML DSig Transform or C14N algorithm implementations
mullan
parents:
1337
diff
changeset
|
242 |
ClassLoader cl = Thread.currentThread().getContextClassLoader(); |
2 | 243 |
|
1337 | 244 |
try { |
245 |
transformClassHash.put |
|
246 |
(algorithmURI, Class.forName(implementingClass, true, cl)); |
|
247 |
} catch (ClassNotFoundException e) { |
|
248 |
throw new RuntimeException(e); |
|
249 |
} |
|
250 |
} |
|
251 |
||
252 |
/** |
|
253 |
* Returns the URI representation of Transformation algorithm |
|
254 |
* |
|
255 |
* @return the URI representation of Transformation algorithm |
|
256 |
*/ |
|
257 |
public String getURI() { |
|
258 |
return this._constructionElement.getAttributeNS |
|
259 |
(null, Constants._ATT_ALGORITHM); |
|
260 |
} |
|
2 | 261 |
|
1337 | 262 |
/** |
263 |
* Transforms the input, and generates {@link XMLSignatureInput} as output. |
|
264 |
* |
|
265 |
* @param input input {@link XMLSignatureInput} which can supplied Octet |
|
266 |
* Stream and NodeSet as Input of Transformation |
|
267 |
* @return the {@link XMLSignatureInput} class as the result of |
|
268 |
* transformation |
|
269 |
* @throws CanonicalizationException |
|
270 |
* @throws IOException |
|
271 |
* @throws InvalidCanonicalizerException |
|
272 |
* @throws TransformationException |
|
273 |
*/ |
|
274 |
public XMLSignatureInput performTransform(XMLSignatureInput input) |
|
275 |
throws IOException, CanonicalizationException, |
|
276 |
InvalidCanonicalizerException, TransformationException { |
|
2 | 277 |
|
1337 | 278 |
XMLSignatureInput result = null; |
2 | 279 |
|
1337 | 280 |
try { |
281 |
result = transformSpi.enginePerformTransform(input, this); |
|
282 |
} catch (ParserConfigurationException ex) { |
|
283 |
Object exArgs[] = { this.getURI(), "ParserConfigurationException" }; |
|
284 |
throw new CanonicalizationException( |
|
285 |
"signature.Transform.ErrorDuringTransform", exArgs, ex); |
|
286 |
} catch (SAXException ex) { |
|
287 |
Object exArgs[] = { this.getURI(), "SAXException" }; |
|
288 |
throw new CanonicalizationException( |
|
289 |
"signature.Transform.ErrorDuringTransform", exArgs, ex); |
|
290 |
} |
|
2 | 291 |
|
1337 | 292 |
return result; |
293 |
} |
|
2 | 294 |
|
1337 | 295 |
/** |
296 |
* Transforms the input, and generates {@link XMLSignatureInput} as output. |
|
297 |
* |
|
298 |
* @param input input {@link XMLSignatureInput} which can supplied Octet |
|
299 |
* Stream and NodeSet as Input of Transformation |
|
300 |
* @param os where to output the result of the last transformation |
|
301 |
* @return the {@link XMLSignatureInput} class as the result of |
|
302 |
* transformation |
|
303 |
* @throws CanonicalizationException |
|
304 |
* @throws IOException |
|
305 |
* @throws InvalidCanonicalizerException |
|
306 |
* @throws TransformationException |
|
307 |
*/ |
|
308 |
public XMLSignatureInput performTransform(XMLSignatureInput input, |
|
309 |
OutputStream os) throws IOException, CanonicalizationException, |
|
310 |
InvalidCanonicalizerException, TransformationException { |
|
2 | 311 |
|
1337 | 312 |
XMLSignatureInput result = null; |
2 | 313 |
|
1337 | 314 |
try { |
315 |
result = transformSpi.enginePerformTransform(input, os, this); |
|
316 |
} catch (ParserConfigurationException ex) { |
|
317 |
Object exArgs[] = { this.getURI(), "ParserConfigurationException" }; |
|
318 |
throw new CanonicalizationException( |
|
319 |
"signature.Transform.ErrorDuringTransform", exArgs, ex); |
|
320 |
} catch (SAXException ex) { |
|
321 |
Object exArgs[] = { this.getURI(), "SAXException" }; |
|
322 |
throw new CanonicalizationException( |
|
323 |
"signature.Transform.ErrorDuringTransform", exArgs, ex); |
|
324 |
} |
|
2 | 325 |
|
1337 | 326 |
return result; |
327 |
} |
|
2 | 328 |
|
1337 | 329 |
/** |
330 |
* Method getImplementingClass |
|
331 |
* |
|
332 |
* @param URI |
|
333 |
* @return The name of the class implementing the URI. |
|
334 |
*/ |
|
10694
cf59e2badd14
7088502: Security libraries don't build with javac -Werror
mullan
parents:
8814
diff
changeset
|
335 |
@SuppressWarnings("unchecked") |
cf59e2badd14
7088502: Security libraries don't build with javac -Werror
mullan
parents:
8814
diff
changeset
|
336 |
private static Class<? extends TransformSpi> getImplementingClass(String URI) { |
cf59e2badd14
7088502: Security libraries don't build with javac -Werror
mullan
parents:
8814
diff
changeset
|
337 |
return (Class<? extends TransformSpi>)transformClassHash.get(URI); |
1337 | 338 |
} |
2 | 339 |
|
1337 | 340 |
private static TransformSpi getTransformSpi(String URI) |
341 |
throws InvalidTransformException { |
|
342 |
try { |
|
10694
cf59e2badd14
7088502: Security libraries don't build with javac -Werror
mullan
parents:
8814
diff
changeset
|
343 |
TransformSpi value = transformSpiHash.get(URI); |
1337 | 344 |
if (value != null) { |
10694
cf59e2badd14
7088502: Security libraries don't build with javac -Werror
mullan
parents:
8814
diff
changeset
|
345 |
return value; |
1337 | 346 |
} |
10694
cf59e2badd14
7088502: Security libraries don't build with javac -Werror
mullan
parents:
8814
diff
changeset
|
347 |
Class<? extends TransformSpi> cl = getImplementingClass(URI); |
1337 | 348 |
if (cl != null) { |
10694
cf59e2badd14
7088502: Security libraries don't build with javac -Werror
mullan
parents:
8814
diff
changeset
|
349 |
TransformSpi tr = cl.newInstance(); |
1337 | 350 |
transformSpiHash.put(URI, tr); |
351 |
return tr; |
|
352 |
} |
|
353 |
} catch (InstantiationException ex) { |
|
354 |
Object exArgs[] = { URI }; |
|
355 |
throw new InvalidTransformException( |
|
356 |
"signature.Transform.UnknownTransform", exArgs, ex); |
|
357 |
} catch (IllegalAccessException ex) { |
|
358 |
Object exArgs[] = { URI }; |
|
359 |
throw new InvalidTransformException( |
|
360 |
"signature.Transform.UnknownTransform", exArgs, ex); |
|
361 |
} |
|
362 |
return null; |
|
363 |
} |
|
2 | 364 |
|
1337 | 365 |
/** @inheritDoc */ |
366 |
public String getBaseLocalName() { |
|
367 |
return Constants._TAG_TRANSFORM; |
|
368 |
} |
|
2 | 369 |
} |