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.signature;
|
|
22 |
|
|
23 |
import java.io.ByteArrayInputStream;
|
|
24 |
import java.io.IOException;
|
|
25 |
import java.io.OutputStream;
|
|
26 |
import javax.crypto.SecretKey;
|
|
27 |
import javax.crypto.spec.SecretKeySpec;
|
|
28 |
import javax.xml.parsers.ParserConfigurationException;
|
|
29 |
|
|
30 |
import com.sun.org.apache.xml.internal.security.algorithms.SignatureAlgorithm;
|
|
31 |
import com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException;
|
|
32 |
import com.sun.org.apache.xml.internal.security.c14n.Canonicalizer;
|
|
33 |
import com.sun.org.apache.xml.internal.security.c14n.InvalidCanonicalizerException;
|
|
34 |
import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException;
|
|
35 |
import com.sun.org.apache.xml.internal.security.utils.Constants;
|
|
36 |
import com.sun.org.apache.xml.internal.security.utils.XMLUtils;
|
|
37 |
import com.sun.org.apache.xml.internal.security.transforms.params.InclusiveNamespaces;
|
|
38 |
import org.w3c.dom.Document;
|
|
39 |
import org.w3c.dom.Element;
|
|
40 |
import org.w3c.dom.Node;
|
|
41 |
import org.xml.sax.SAXException;
|
|
42 |
|
|
43 |
/**
|
|
44 |
* Handles <code><ds:SignedInfo></code> elements
|
|
45 |
* This <code>SignedInfo<code> element includes the canonicalization algorithm,
|
1337
|
46 |
* a signature algorithm, and one or more references.
|
|
47 |
*
|
2
|
48 |
* @author Christian Geuer-Pollmann
|
|
49 |
*/
|
|
50 |
public class SignedInfo extends Manifest {
|
|
51 |
|
1337
|
52 |
/** Field _signatureAlgorithm */
|
|
53 |
private SignatureAlgorithm _signatureAlgorithm = null;
|
2
|
54 |
|
1337
|
55 |
/** Field _c14nizedBytes */
|
|
56 |
private byte[] _c14nizedBytes = null;
|
|
57 |
|
|
58 |
private Element c14nMethod;
|
|
59 |
private Element signatureMethod;
|
2
|
60 |
|
1337
|
61 |
/**
|
|
62 |
* Overwrites {@link Manifest#addDocument} because it creates another
|
|
63 |
* Element.
|
|
64 |
*
|
|
65 |
* @param doc the {@link Document} in which <code>XMLsignature</code> will
|
|
66 |
* be placed
|
|
67 |
* @throws XMLSecurityException
|
|
68 |
*/
|
|
69 |
public SignedInfo(Document doc) throws XMLSecurityException {
|
|
70 |
this(doc, XMLSignature.ALGO_ID_SIGNATURE_DSA,
|
|
71 |
Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS);
|
|
72 |
}
|
2
|
73 |
|
1337
|
74 |
/**
|
|
75 |
* Constructs {@link SignedInfo} using given Canonicalization algorithm and
|
|
76 |
* Signature algorithm.
|
|
77 |
*
|
|
78 |
* @param doc <code>SignedInfo</code> is placed in this document
|
|
79 |
* @param signatureMethodURI URI representation of the Digest and
|
|
80 |
* Signature algorithm
|
|
81 |
* @param canonicalizationMethodURI URI representation of the
|
|
82 |
* Canonicalization method
|
|
83 |
* @throws XMLSecurityException
|
|
84 |
*/
|
|
85 |
public SignedInfo(Document doc, String signatureMethodURI,
|
|
86 |
String canonicalizationMethodURI)
|
2
|
87 |
throws XMLSecurityException {
|
1337
|
88 |
this(doc, signatureMethodURI, 0, canonicalizationMethodURI);
|
|
89 |
}
|
2
|
90 |
|
1337
|
91 |
/**
|
|
92 |
* Constructor SignedInfo
|
|
93 |
*
|
|
94 |
* @param doc <code>SignedInfo</code> is placed in this document
|
|
95 |
* @param signatureMethodURI URI representation of the Digest and
|
|
96 |
* Signature algorithm
|
|
97 |
* @param hMACOutputLength
|
|
98 |
* @param canonicalizationMethodURI URI representation of the
|
|
99 |
* Canonicalization method
|
|
100 |
* @throws XMLSecurityException
|
|
101 |
*/
|
|
102 |
public SignedInfo(Document doc, String signatureMethodURI,
|
|
103 |
int hMACOutputLength, String canonicalizationMethodURI)
|
2
|
104 |
throws XMLSecurityException {
|
|
105 |
|
1337
|
106 |
super(doc);
|
2
|
107 |
|
1337
|
108 |
c14nMethod = XMLUtils.createElementInSignatureSpace(this._doc,
|
2
|
109 |
Constants._TAG_CANONICALIZATIONMETHOD);
|
|
110 |
|
1337
|
111 |
c14nMethod.setAttributeNS(null, Constants._ATT_ALGORITHM,
|
|
112 |
canonicalizationMethodURI);
|
|
113 |
this._constructionElement.appendChild(c14nMethod);
|
|
114 |
XMLUtils.addReturnToElement(this._constructionElement);
|
|
115 |
|
|
116 |
if (hMACOutputLength > 0) {
|
2
|
117 |
this._signatureAlgorithm = new SignatureAlgorithm(this._doc,
|
1337
|
118 |
signatureMethodURI, hMACOutputLength);
|
|
119 |
} else {
|
2
|
120 |
this._signatureAlgorithm = new SignatureAlgorithm(this._doc,
|
1337
|
121 |
signatureMethodURI);
|
|
122 |
}
|
2
|
123 |
|
1337
|
124 |
signatureMethod = this._signatureAlgorithm.getElement();
|
|
125 |
this._constructionElement.appendChild(signatureMethod);
|
|
126 |
XMLUtils.addReturnToElement(this._constructionElement);
|
|
127 |
}
|
2
|
128 |
|
1337
|
129 |
/**
|
|
130 |
* @param doc
|
|
131 |
* @param signatureMethodElem
|
|
132 |
* @param canonicalizationMethodElem
|
|
133 |
* @throws XMLSecurityException
|
|
134 |
*/
|
|
135 |
public SignedInfo(Document doc, Element signatureMethodElem,
|
|
136 |
Element canonicalizationMethodElem) throws XMLSecurityException {
|
2
|
137 |
|
1337
|
138 |
super(doc);
|
|
139 |
// Check this?
|
|
140 |
this.c14nMethod = canonicalizationMethodElem;
|
|
141 |
this._constructionElement.appendChild(c14nMethod);
|
|
142 |
XMLUtils.addReturnToElement(this._constructionElement);
|
2
|
143 |
|
1337
|
144 |
this._signatureAlgorithm =
|
|
145 |
new SignatureAlgorithm(signatureMethodElem, null);
|
2
|
146 |
|
1337
|
147 |
signatureMethod = this._signatureAlgorithm.getElement();
|
|
148 |
this._constructionElement.appendChild(signatureMethod);
|
|
149 |
|
|
150 |
XMLUtils.addReturnToElement(this._constructionElement);
|
|
151 |
}
|
2
|
152 |
|
1337
|
153 |
/**
|
|
154 |
* Build a {@link SignedInfo} from an {@link Element}
|
|
155 |
*
|
|
156 |
* @param element <code>SignedInfo</code>
|
|
157 |
* @param baseURI the URI of the resource where the XML instance was stored
|
|
158 |
* @throws XMLSecurityException
|
|
159 |
* @see <A HREF="http://lists.w3.org/Archives/Public/w3c-ietf-xmldsig/2001OctDec/0033.html">Question</A>
|
|
160 |
* @see <A HREF="http://lists.w3.org/Archives/Public/w3c-ietf-xmldsig/2001OctDec/0054.html">Answer</A>
|
|
161 |
*/
|
|
162 |
public SignedInfo(Element element, String baseURI)
|
2
|
163 |
throws XMLSecurityException {
|
|
164 |
|
1337
|
165 |
// Parse the Reference children and Id attribute in the Manifest
|
|
166 |
super(element, baseURI);
|
2
|
167 |
|
1337
|
168 |
/* canonicalize ds:SignedInfo, reparse it into a new document
|
|
169 |
* and replace the original not-canonicalized ds:SignedInfo by
|
|
170 |
* the re-parsed canonicalized one.
|
|
171 |
*/
|
|
172 |
c14nMethod = XMLUtils.getNextElement(element.getFirstChild());
|
|
173 |
String c14nMethodURI = this.getCanonicalizationMethodURI();
|
|
174 |
if (!(c14nMethodURI.equals(Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS) ||
|
|
175 |
c14nMethodURI.equals(Canonicalizer.ALGO_ID_C14N_WITH_COMMENTS) ||
|
|
176 |
c14nMethodURI.equals(Canonicalizer.ALGO_ID_C14N_EXCL_OMIT_COMMENTS) ||
|
|
177 |
c14nMethodURI.equals(Canonicalizer.ALGO_ID_C14N_EXCL_WITH_COMMENTS))) {
|
|
178 |
// the c14n is not a secure one and can rewrite the URIs or like
|
|
179 |
// that reparse the SignedInfo to be sure
|
|
180 |
try {
|
|
181 |
Canonicalizer c14nizer =
|
|
182 |
Canonicalizer.getInstance(this.getCanonicalizationMethodURI());
|
2
|
183 |
|
1337
|
184 |
this._c14nizedBytes =
|
|
185 |
c14nizer.canonicalizeSubtree(this._constructionElement);
|
|
186 |
javax.xml.parsers.DocumentBuilderFactory dbf =
|
|
187 |
javax.xml.parsers.DocumentBuilderFactory.newInstance();
|
|
188 |
dbf.setNamespaceAware(true);
|
|
189 |
javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();
|
|
190 |
org.w3c.dom.Document newdoc =
|
|
191 |
db.parse(new ByteArrayInputStream(this._c14nizedBytes));
|
|
192 |
Node imported =
|
|
193 |
this._doc.importNode(newdoc.getDocumentElement(), true);
|
2
|
194 |
|
1337
|
195 |
this._constructionElement.getParentNode().replaceChild(imported,
|
|
196 |
this._constructionElement);
|
2
|
197 |
|
1337
|
198 |
this._constructionElement = (Element) imported;
|
|
199 |
} catch (ParserConfigurationException ex) {
|
|
200 |
throw new XMLSecurityException("empty", ex);
|
|
201 |
} catch (IOException ex) {
|
|
202 |
throw new XMLSecurityException("empty", ex);
|
|
203 |
} catch (SAXException ex) {
|
|
204 |
throw new XMLSecurityException("empty", ex);
|
|
205 |
}
|
|
206 |
}
|
|
207 |
signatureMethod = XMLUtils.getNextElement(c14nMethod.getNextSibling());
|
|
208 |
this._signatureAlgorithm =
|
|
209 |
new SignatureAlgorithm(signatureMethod, this.getBaseURI());
|
|
210 |
}
|
2
|
211 |
|
|
212 |
/**
|
|
213 |
* Tests core validation process
|
|
214 |
*
|
|
215 |
* @return true if verification was successful
|
|
216 |
* @throws MissingResourceFailureException
|
|
217 |
* @throws XMLSecurityException
|
|
218 |
*/
|
|
219 |
public boolean verify()
|
|
220 |
throws MissingResourceFailureException, XMLSecurityException {
|
|
221 |
return super.verifyReferences(false);
|
|
222 |
}
|
|
223 |
|
|
224 |
/**
|
|
225 |
* Tests core validation process
|
|
226 |
*
|
|
227 |
* @param followManifests defines whether the verification process has to verify referenced <CODE>ds:Manifest</CODE>s, too
|
|
228 |
* @return true if verification was successful
|
|
229 |
* @throws MissingResourceFailureException
|
|
230 |
* @throws XMLSecurityException
|
|
231 |
*/
|
|
232 |
public boolean verify(boolean followManifests)
|
|
233 |
throws MissingResourceFailureException, XMLSecurityException {
|
|
234 |
return super.verifyReferences(followManifests);
|
|
235 |
}
|
|
236 |
|
|
237 |
/**
|
|
238 |
* Returns getCanonicalizedOctetStream
|
|
239 |
*
|
|
240 |
* @return the canonicalization result octedt stream of <code>SignedInfo</code> element
|
|
241 |
* @throws CanonicalizationException
|
|
242 |
* @throws InvalidCanonicalizerException
|
|
243 |
* @throws XMLSecurityException
|
|
244 |
*/
|
|
245 |
public byte[] getCanonicalizedOctetStream()
|
|
246 |
throws CanonicalizationException, InvalidCanonicalizerException,
|
|
247 |
XMLSecurityException {
|
|
248 |
|
|
249 |
if ((this._c14nizedBytes == null)
|
|
250 |
/*&& (this._state == ElementProxy.MODE_SIGN)*/) {
|
|
251 |
Canonicalizer c14nizer =
|
|
252 |
Canonicalizer.getInstance(this.getCanonicalizationMethodURI());
|
|
253 |
|
|
254 |
this._c14nizedBytes =
|
|
255 |
c14nizer.canonicalizeSubtree(this._constructionElement);
|
|
256 |
}
|
|
257 |
|
|
258 |
// make defensive copy
|
|
259 |
byte[] output = new byte[this._c14nizedBytes.length];
|
|
260 |
|
|
261 |
System.arraycopy(this._c14nizedBytes, 0, output, 0, output.length);
|
|
262 |
|
|
263 |
return output;
|
|
264 |
}
|
|
265 |
|
|
266 |
/**
|
|
267 |
* Output the C14n stream to the give outputstream.
|
|
268 |
* @param os
|
|
269 |
* @throws CanonicalizationException
|
|
270 |
* @throws InvalidCanonicalizerException
|
|
271 |
* @throws XMLSecurityException
|
|
272 |
*/
|
|
273 |
public void signInOctectStream(OutputStream os)
|
|
274 |
throws CanonicalizationException, InvalidCanonicalizerException,
|
|
275 |
XMLSecurityException {
|
|
276 |
|
|
277 |
if ((this._c14nizedBytes == null)) {
|
|
278 |
Canonicalizer c14nizer =
|
|
279 |
Canonicalizer.getInstance(this.getCanonicalizationMethodURI());
|
|
280 |
c14nizer.setWriter(os);
|
|
281 |
String inclusiveNamespaces = this.getInclusiveNamespaces();
|
|
282 |
|
|
283 |
if(inclusiveNamespaces == null)
|
|
284 |
c14nizer.canonicalizeSubtree(this._constructionElement);
|
|
285 |
else
|
|
286 |
c14nizer.canonicalizeSubtree(this._constructionElement, inclusiveNamespaces);
|
|
287 |
} else {
|
|
288 |
try {
|
|
289 |
os.write(this._c14nizedBytes);
|
|
290 |
} catch (IOException e) {
|
|
291 |
throw new RuntimeException(""+e);
|
|
292 |
}
|
|
293 |
}
|
|
294 |
}
|
|
295 |
|
|
296 |
/**
|
|
297 |
* Returns the Canonicalization method URI
|
|
298 |
*
|
|
299 |
* @return the Canonicalization method URI
|
|
300 |
*/
|
|
301 |
public String getCanonicalizationMethodURI() {
|
|
302 |
|
1337
|
303 |
|
|
304 |
return c14nMethod.getAttributeNS(null, Constants._ATT_ALGORITHM);
|
2
|
305 |
}
|
|
306 |
|
|
307 |
/**
|
|
308 |
* Returns the Signature method URI
|
|
309 |
*
|
|
310 |
* @return the Signature method URI
|
|
311 |
*/
|
|
312 |
public String getSignatureMethodURI() {
|
|
313 |
|
|
314 |
Element signatureElement = this.getSignatureMethodElement();
|
|
315 |
|
|
316 |
if (signatureElement != null) {
|
|
317 |
return signatureElement.getAttributeNS(null, Constants._ATT_ALGORITHM);
|
|
318 |
}
|
|
319 |
|
|
320 |
return null;
|
|
321 |
}
|
|
322 |
|
|
323 |
/**
|
|
324 |
* Method getSignatureMethodElement
|
|
325 |
* @return gets The SignatureMethod Node.
|
|
326 |
*
|
|
327 |
*/
|
|
328 |
public Element getSignatureMethodElement() {
|
1337
|
329 |
return signatureMethod;
|
2
|
330 |
}
|
|
331 |
|
|
332 |
/**
|
|
333 |
* Creates a SecretKey for the appropriate Mac algorithm based on a
|
|
334 |
* byte[] array password.
|
|
335 |
*
|
|
336 |
* @param secretKeyBytes
|
|
337 |
* @return the secret key for the SignedInfo element.
|
|
338 |
*/
|
|
339 |
public SecretKey createSecretKey(byte[] secretKeyBytes)
|
|
340 |
{
|
|
341 |
|
|
342 |
return new SecretKeySpec(secretKeyBytes,
|
|
343 |
this._signatureAlgorithm
|
|
344 |
.getJCEAlgorithmString());
|
|
345 |
}
|
|
346 |
|
1337
|
347 |
protected SignatureAlgorithm getSignatureAlgorithm() {
|
|
348 |
return _signatureAlgorithm;
|
|
349 |
}
|
2
|
350 |
/**
|
|
351 |
* Method getBaseLocalName
|
|
352 |
* @inheritDoc
|
|
353 |
*
|
|
354 |
*/
|
|
355 |
public String getBaseLocalName() {
|
|
356 |
return Constants._TAG_SIGNEDINFO;
|
|
357 |
}
|
|
358 |
|
|
359 |
public String getInclusiveNamespaces() {
|
|
360 |
|
1337
|
361 |
|
2
|
362 |
|
1337
|
363 |
String c14nMethodURI = c14nMethod.getAttributeNS(null, Constants._ATT_ALGORITHM);
|
2
|
364 |
if(!(c14nMethodURI.equals("http://www.w3.org/2001/10/xml-exc-c14n#") ||
|
|
365 |
c14nMethodURI.equals("http://www.w3.org/2001/10/xml-exc-c14n#WithComments"))) {
|
|
366 |
return null;
|
|
367 |
}
|
|
368 |
|
1337
|
369 |
Element inclusiveElement = XMLUtils.getNextElement(
|
|
370 |
c14nMethod.getFirstChild());
|
2
|
371 |
|
|
372 |
if(inclusiveElement != null)
|
|
373 |
{
|
|
374 |
try
|
|
375 |
{
|
|
376 |
String inclusiveNamespaces = new InclusiveNamespaces(inclusiveElement,
|
|
377 |
InclusiveNamespaces.ExclusiveCanonicalizationNamespace).getInclusiveNamespaces();
|
|
378 |
return inclusiveNamespaces;
|
|
379 |
}
|
|
380 |
catch (XMLSecurityException e)
|
|
381 |
{
|
|
382 |
return null;
|
|
383 |
}
|
|
384 |
}
|
|
385 |
return null;
|
|
386 |
}
|
|
387 |
}
|