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