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 |
|
|
24 |
|
|
25 |
import java.io.IOException;
|
|
26 |
import java.io.OutputStream;
|
|
27 |
|
|
28 |
import com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException;
|
|
29 |
import com.sun.org.apache.xml.internal.security.c14n.Canonicalizer;
|
|
30 |
import com.sun.org.apache.xml.internal.security.c14n.InvalidCanonicalizerException;
|
|
31 |
import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException;
|
|
32 |
import com.sun.org.apache.xml.internal.security.signature.XMLSignatureException;
|
|
33 |
import com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput;
|
|
34 |
import com.sun.org.apache.xml.internal.security.utils.Constants;
|
|
35 |
import com.sun.org.apache.xml.internal.security.utils.SignatureElementProxy;
|
|
36 |
import com.sun.org.apache.xml.internal.security.utils.XMLUtils;
|
|
37 |
import org.w3c.dom.DOMException;
|
|
38 |
import org.w3c.dom.Document;
|
|
39 |
import org.w3c.dom.Element;
|
|
40 |
import org.w3c.dom.NodeList;
|
|
41 |
|
|
42 |
|
|
43 |
/**
|
|
44 |
* Holder of the {@link com.sun.org.apache.xml.internal.security.transforms.Transform} steps to be performed on the data.
|
|
45 |
* The input to the first Transform is the result of dereferencing the <code>URI</code> attribute of the <code>Reference</code> element.
|
|
46 |
* The output from the last Transform is the input for the <code>DigestMethod algorithm</code>
|
|
47 |
*
|
|
48 |
* @author Christian Geuer-Pollmann
|
|
49 |
* @see Transform
|
|
50 |
* @see com.sun.org.apache.xml.internal.security.signature.Reference
|
|
51 |
*/
|
|
52 |
public class Transforms extends SignatureElementProxy {
|
|
53 |
|
|
54 |
/** {@link java.util.logging} logging facility */
|
|
55 |
static java.util.logging.Logger log =
|
|
56 |
java.util.logging.Logger.getLogger(Transforms.class.getName());
|
|
57 |
//J-
|
|
58 |
/** Canonicalization - Required Canonical XML (omits comments) */
|
|
59 |
public static final String TRANSFORM_C14N_OMIT_COMMENTS = Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS;
|
|
60 |
/** Canonicalization - Recommended Canonical XML with Comments */
|
|
61 |
public static final String TRANSFORM_C14N_WITH_COMMENTS = Canonicalizer.ALGO_ID_C14N_WITH_COMMENTS;
|
|
62 |
/** Canonicalization - Required Exclusive Canonicalization (omits comments) */
|
|
63 |
public static final String TRANSFORM_C14N_EXCL_OMIT_COMMENTS = Canonicalizer.ALGO_ID_C14N_EXCL_OMIT_COMMENTS;
|
|
64 |
/** Canonicalization - Recommended Exclusive Canonicalization with Comments */
|
|
65 |
public static final String TRANSFORM_C14N_EXCL_WITH_COMMENTS = Canonicalizer.ALGO_ID_C14N_EXCL_WITH_COMMENTS;
|
|
66 |
/** Transform - Optional XSLT */
|
|
67 |
public static final String TRANSFORM_XSLT = "http://www.w3.org/TR/1999/REC-xslt-19991116";
|
|
68 |
/** Transform - Required base64 decoding */
|
|
69 |
public static final String TRANSFORM_BASE64_DECODE = Constants.SignatureSpecNS + "base64";
|
|
70 |
/** Transform - Recommended XPath */
|
|
71 |
public static final String TRANSFORM_XPATH = "http://www.w3.org/TR/1999/REC-xpath-19991116";
|
|
72 |
/** Transform - Required Enveloped Signature */
|
|
73 |
public static final String TRANSFORM_ENVELOPED_SIGNATURE = Constants.SignatureSpecNS + "enveloped-signature";
|
|
74 |
/** Transform - XPointer */
|
|
75 |
public static final String TRANSFORM_XPOINTER = "http://www.w3.org/TR/2001/WD-xptr-20010108";
|
|
76 |
/** Transform - XPath Filter v2.0 */
|
|
77 |
public static final String TRANSFORM_XPATH2FILTER04 = "http://www.w3.org/2002/04/xmldsig-filter2";
|
|
78 |
/** Transform - XPath Filter */
|
|
79 |
public static final String TRANSFORM_XPATH2FILTER = "http://www.w3.org/2002/06/xmldsig-filter2";
|
|
80 |
/** Transform - XPath Filter CHGP private*/
|
|
81 |
public static final String TRANSFORM_XPATHFILTERCHGP = "http://www.nue.et-inf.uni-siegen.de/~geuer-pollmann/#xpathFilter";
|
|
82 |
//J+
|
|
83 |
Element []transforms;
|
|
84 |
/**
|
|
85 |
* Consturcts {@link Transforms}
|
|
86 |
*
|
|
87 |
* @param doc the {@link Document} in which <code>XMLsignature</code> will be placed
|
|
88 |
*/
|
|
89 |
public Transforms(Document doc) {
|
|
90 |
|
|
91 |
super(doc);
|
|
92 |
|
|
93 |
XMLUtils.addReturnToElement(this._constructionElement);
|
|
94 |
}
|
|
95 |
|
|
96 |
/**
|
|
97 |
* Consturcts {@link Transforms} from {@link Element} which is <code>Transforms</code> Element
|
|
98 |
*
|
|
99 |
* @param element is <code>Transforms</code> element
|
|
100 |
* @param BaseURI the URI where the XML instance was stored
|
|
101 |
* @throws DOMException
|
|
102 |
* @throws InvalidTransformException
|
|
103 |
* @throws TransformationException
|
|
104 |
* @throws XMLSecurityException
|
|
105 |
* @throws XMLSignatureException
|
|
106 |
*/
|
|
107 |
public Transforms(Element element, String BaseURI)
|
|
108 |
throws DOMException, XMLSignatureException,
|
|
109 |
InvalidTransformException, TransformationException,
|
|
110 |
XMLSecurityException {
|
|
111 |
|
|
112 |
super(element, BaseURI);
|
|
113 |
|
|
114 |
int numberOfTransformElems = this.getLength();
|
|
115 |
|
|
116 |
if (numberOfTransformElems == 0) {
|
|
117 |
|
|
118 |
// At least ont Transform element must be present. Bad.
|
|
119 |
Object exArgs[] = { Constants._TAG_TRANSFORM,
|
|
120 |
Constants._TAG_TRANSFORMS };
|
|
121 |
|
|
122 |
throw new TransformationException("xml.WrongContent", exArgs);
|
|
123 |
}
|
|
124 |
}
|
|
125 |
|
|
126 |
/**
|
|
127 |
* Adds the <code>Transform</code> with the specified <code>Transform algorithm URI</code>
|
|
128 |
*
|
|
129 |
* @param transformURI the URI form of transform that indicates which transformation is applied to data
|
|
130 |
* @throws TransformationException
|
|
131 |
*/
|
|
132 |
public void addTransform(String transformURI)
|
|
133 |
throws TransformationException {
|
|
134 |
|
|
135 |
try {
|
|
136 |
if (true)
|
|
137 |
if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Transforms.addTransform(" + transformURI + ")");
|
|
138 |
|
|
139 |
Transform transform = Transform.getInstance(this._doc, transformURI);
|
|
140 |
|
|
141 |
this.addTransform(transform);
|
|
142 |
} catch (InvalidTransformException ex) {
|
|
143 |
throw new TransformationException("empty", ex);
|
|
144 |
}
|
|
145 |
}
|
|
146 |
|
|
147 |
/**
|
|
148 |
* Adds the <code>Transform</code> with the specified <code>Transform algorithm URI</code>
|
|
149 |
*
|
|
150 |
* @param transformURI the URI form of transform that indicates which transformation is applied to data
|
|
151 |
* @param contextElement
|
|
152 |
* @throws TransformationException
|
|
153 |
* @see Transform#getInstance(Document doc, String algorithmURI, Element childElement)
|
|
154 |
*/
|
|
155 |
public void addTransform(String transformURI, Element contextElement)
|
|
156 |
throws TransformationException {
|
|
157 |
|
|
158 |
try {
|
|
159 |
if (true)
|
|
160 |
if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Transforms.addTransform(" + transformURI + ")");
|
|
161 |
|
|
162 |
Transform transform = Transform.getInstance(this._doc, transformURI,
|
|
163 |
contextElement);
|
|
164 |
|
|
165 |
this.addTransform(transform);
|
|
166 |
} catch (InvalidTransformException ex) {
|
|
167 |
throw new TransformationException("empty", ex);
|
|
168 |
}
|
|
169 |
}
|
|
170 |
|
|
171 |
/**
|
|
172 |
* Adds the <code>Transform</code> with the specified <code>Transform algorithm URI</code>
|
|
173 |
*
|
|
174 |
* @param transformURI the URI form of transform that indicates which transformation is applied to data
|
|
175 |
* @param contextNodes
|
|
176 |
* @throws TransformationException
|
|
177 |
* @see Transform#getInstance(Document doc, String algorithmURI, NodeList contextNodes)
|
|
178 |
*/
|
|
179 |
public void addTransform(String transformURI, NodeList contextNodes)
|
|
180 |
throws TransformationException {
|
|
181 |
|
|
182 |
try {
|
|
183 |
Transform transform = Transform.getInstance(this._doc, transformURI,
|
|
184 |
contextNodes);
|
|
185 |
|
|
186 |
this.addTransform(transform);
|
|
187 |
} catch (InvalidTransformException ex) {
|
|
188 |
throw new TransformationException("empty", ex);
|
|
189 |
}
|
|
190 |
}
|
|
191 |
|
|
192 |
/**
|
|
193 |
* Adds a user-provided Transform step.
|
|
194 |
*
|
|
195 |
* @param transform {@link Transform} object
|
|
196 |
*/
|
|
197 |
private void addTransform(Transform transform) {
|
|
198 |
if (true)
|
|
199 |
if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Transforms.addTransform(" + transform.getURI() + ")");
|
|
200 |
|
|
201 |
Element transformElement = transform.getElement();
|
|
202 |
|
|
203 |
this._constructionElement.appendChild(transformElement);
|
|
204 |
XMLUtils.addReturnToElement(this._constructionElement);
|
|
205 |
}
|
|
206 |
|
|
207 |
/**
|
|
208 |
* Applies all included <code>Transform</code>s to xmlSignatureInput and returns the result of these transformations.
|
|
209 |
*
|
|
210 |
* @param xmlSignatureInput the input for the <code>Transform</code>s
|
|
211 |
* @return the result of the <code>Transforms</code>
|
|
212 |
* @throws TransformationException
|
|
213 |
*/
|
|
214 |
public XMLSignatureInput performTransforms(
|
|
215 |
XMLSignatureInput xmlSignatureInput) throws TransformationException {
|
|
216 |
return performTransforms(xmlSignatureInput,null);
|
|
217 |
}
|
|
218 |
|
|
219 |
/**
|
|
220 |
* Applies all included <code>Transform</code>s to xmlSignatureInput and returns the result of these transformations.
|
|
221 |
*
|
|
222 |
* @param xmlSignatureInput the input for the <code>Transform</code>s
|
|
223 |
* @param os where to output the last transformation.
|
|
224 |
* @return the result of the <code>Transforms</code>
|
|
225 |
* @throws TransformationException
|
|
226 |
*/
|
|
227 |
public XMLSignatureInput performTransforms(
|
|
228 |
XMLSignatureInput xmlSignatureInput,OutputStream os) throws TransformationException {
|
|
229 |
|
|
230 |
try {
|
|
231 |
int last=this.getLength()-1;
|
|
232 |
for (int i = 0; i < last; i++) {
|
|
233 |
Transform t = this.item(i);
|
|
234 |
if (true) {
|
|
235 |
if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Preform the (" + i + ")th " + t.getURI() + " transform");
|
|
236 |
}
|
|
237 |
xmlSignatureInput = t.performTransform(xmlSignatureInput);
|
|
238 |
}
|
|
239 |
if (last>=0) {
|
|
240 |
Transform t = this.item(last);
|
|
241 |
xmlSignatureInput = t.performTransform(xmlSignatureInput, os);
|
|
242 |
}
|
|
243 |
|
|
244 |
|
|
245 |
return xmlSignatureInput;
|
|
246 |
} catch (IOException ex) {
|
|
247 |
throw new TransformationException("empty", ex);
|
|
248 |
// } catch (ParserConfigurationException ex) { throw new TransformationException("empty", ex);
|
|
249 |
// } catch (SAXException ex) { throw new TransformationException("empty", ex);
|
|
250 |
} catch (CanonicalizationException ex) {
|
|
251 |
throw new TransformationException("empty", ex);
|
|
252 |
} catch (InvalidCanonicalizerException ex) {
|
|
253 |
throw new TransformationException("empty", ex);
|
|
254 |
}
|
|
255 |
}
|
|
256 |
|
|
257 |
/**
|
|
258 |
* Return the nonnegative number of transformations.
|
|
259 |
*
|
|
260 |
* @return the number of transformations
|
|
261 |
*/
|
|
262 |
public int getLength()
|
|
263 |
{
|
|
264 |
/*Element nscontext = XMLUtils.createDSctx(this._doc, "ds",
|
|
265 |
Constants.SignatureSpecNS);
|
|
266 |
NodeList transformElems =
|
|
267 |
XPathAPI.selectNodeList(this._constructionElement,
|
|
268 |
"./ds:Transform", nscontext);
|
|
269 |
return transformElems.getLength();*/
|
|
270 |
if (transforms==null) {
|
|
271 |
transforms=XMLUtils.selectDsNodes(this._constructionElement.getFirstChild(),
|
|
272 |
"Transform");
|
|
273 |
}
|
|
274 |
return transforms.length;
|
|
275 |
}
|
|
276 |
|
|
277 |
/**
|
|
278 |
* Return the <it>i</it><sup>th</sup> <code>{@link Transform}</code>.
|
|
279 |
* Valid <code>i</code> values are 0 to <code>{@link #getLength}-1</code>.
|
|
280 |
*
|
|
281 |
* @param i index of {@link Transform} to return
|
|
282 |
* @return the <it>i</it><sup>th</sup> transforms
|
|
283 |
* @throws TransformationException
|
|
284 |
*/
|
|
285 |
public Transform item(int i) throws TransformationException {
|
|
286 |
|
|
287 |
try {
|
|
288 |
if (transforms==null) {
|
|
289 |
transforms=XMLUtils.selectDsNodes(this._constructionElement.getFirstChild(),
|
|
290 |
"Transform");
|
|
291 |
}
|
|
292 |
return new Transform(transforms[i], this._baseURI);
|
|
293 |
} catch (XMLSecurityException ex) {
|
|
294 |
throw new TransformationException("empty", ex);
|
|
295 |
}
|
|
296 |
}
|
|
297 |
|
|
298 |
/** @inheritDoc */
|
|
299 |
public String getBaseLocalName() {
|
|
300 |
return Constants._TAG_TRANSFORMS;
|
|
301 |
}
|
|
302 |
}
|