author | mullan |
Fri, 21 Nov 2014 15:23:36 -0500 | |
changeset 27747 | 3a271dc8b758 |
parent 25859 | 3317bb8137f4 |
child 32003 | acb12269398a |
permissions | -rw-r--r-- |
2 | 1 |
/* |
1337 | 2 |
* reserved comment block |
3 |
* DO NOT REMOVE OR ALTER! |
|
2 | 4 |
*/ |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
5 |
/** |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
6 |
* Licensed to the Apache Software Foundation (ASF) under one |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
7 |
* or more contributor license agreements. See the NOTICE file |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
8 |
* distributed with this work for additional information |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
9 |
* regarding copyright ownership. The ASF licenses this file |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
10 |
* to you under the Apache License, Version 2.0 (the |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
11 |
* "License"); you may not use this file except in compliance |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
12 |
* with the License. You may obtain a copy of the License at |
1337 | 13 |
* |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
14 |
* http://www.apache.org/licenses/LICENSE-2.0 |
1337 | 15 |
* |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
16 |
* Unless required by applicable law or agreed to in writing, |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
17 |
* software distributed under the License is distributed on an |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
18 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
19 |
* KIND, either express or implied. See the License for the |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
20 |
* specific language governing permissions and limitations |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
21 |
* under the License. |
1337 | 22 |
*/ |
23 |
/* |
|
24970
094bfaa699c3
8046044: Fix raw and unchecked lint warnings in XML Signature Impl
mullan
parents:
23010
diff
changeset
|
24 |
* Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved. |
1337 | 25 |
*/ |
26 |
/* |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
27 |
* $Id: DOMXMLObject.java 1333415 2012-05-03 12:03:51Z coheigea $ |
2 | 28 |
*/ |
29 |
package org.jcp.xml.dsig.internal.dom; |
|
30 |
||
31 |
import javax.xml.crypto.*; |
|
32 |
import javax.xml.crypto.dom.DOMCryptoContext; |
|
33 |
import javax.xml.crypto.dsig.*; |
|
34 |
||
1337 | 35 |
import java.security.Provider; |
2 | 36 |
import java.util.*; |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
37 |
|
18240 | 38 |
import org.w3c.dom.Attr; |
2 | 39 |
import org.w3c.dom.Document; |
40 |
import org.w3c.dom.Element; |
|
41 |
import org.w3c.dom.Node; |
|
42 |
import org.w3c.dom.NodeList; |
|
43 |
||
44 |
/** |
|
45 |
* DOM-based implementation of XMLObject. |
|
46 |
* |
|
47 |
* @author Sean Mullan |
|
48 |
*/ |
|
49 |
public final class DOMXMLObject extends DOMStructure implements XMLObject { |
|
50 |
||
51 |
private final String id; |
|
52 |
private final String mimeType; |
|
53 |
private final String encoding; |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
54 |
private final List<XMLStructure> content; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
55 |
private Element objectElem; |
2 | 56 |
|
57 |
/** |
|
58 |
* Creates an <code>XMLObject</code> from the specified parameters. |
|
59 |
* |
|
60 |
* @param content a list of {@link XMLStructure}s. The list |
|
61 |
* is defensively copied to protect against subsequent modification. |
|
62 |
* May be <code>null</code> or empty. |
|
63 |
* @param id the Id (may be <code>null</code>) |
|
64 |
* @param mimeType the mime type (may be <code>null</code>) |
|
65 |
* @param encoding the encoding (may be <code>null</code>) |
|
66 |
* @return an <code>XMLObject</code> |
|
67 |
* @throws ClassCastException if <code>content</code> contains any |
|
68 |
* entries that are not of type {@link XMLStructure} |
|
69 |
*/ |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
70 |
public DOMXMLObject(List<? extends XMLStructure> content, String id, |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
71 |
String mimeType, String encoding) |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
72 |
{ |
27747 | 73 |
List<XMLStructure> tempList = |
74 |
Collections.checkedList(new ArrayList<XMLStructure>(), |
|
75 |
XMLStructure.class); |
|
76 |
if (content != null) { |
|
77 |
tempList.addAll(content); |
|
2 | 78 |
} |
27747 | 79 |
this.content = Collections.unmodifiableList(tempList); |
2 | 80 |
this.id = id; |
81 |
this.mimeType = mimeType; |
|
82 |
this.encoding = encoding; |
|
83 |
} |
|
84 |
||
85 |
/** |
|
86 |
* Creates an <code>XMLObject</code> from an element. |
|
87 |
* |
|
88 |
* @param objElem an Object element |
|
89 |
* @throws MarshalException if there is an error when unmarshalling |
|
90 |
*/ |
|
1337 | 91 |
public DOMXMLObject(Element objElem, XMLCryptoContext context, |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
92 |
Provider provider) |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
93 |
throws MarshalException |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
94 |
{ |
2 | 95 |
// unmarshal attributes |
96 |
this.encoding = DOMUtils.getAttributeValue(objElem, "Encoding"); |
|
18240 | 97 |
|
98 |
Attr attr = objElem.getAttributeNodeNS(null, "Id"); |
|
99 |
if (attr != null) { |
|
100 |
this.id = attr.getValue(); |
|
101 |
objElem.setIdAttributeNode(attr, true); |
|
102 |
} else { |
|
103 |
this.id = null; |
|
104 |
} |
|
2 | 105 |
this.mimeType = DOMUtils.getAttributeValue(objElem, "MimeType"); |
106 |
||
107 |
NodeList nodes = objElem.getChildNodes(); |
|
108 |
int length = nodes.getLength(); |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
109 |
List<XMLStructure> content = new ArrayList<XMLStructure>(length); |
2 | 110 |
for (int i = 0; i < length; i++) { |
111 |
Node child = nodes.item(i); |
|
112 |
if (child.getNodeType() == Node.ELEMENT_NODE) { |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
113 |
Element childElem = (Element)child; |
2 | 114 |
String tag = childElem.getLocalName(); |
115 |
if (tag.equals("Manifest")) { |
|
1337 | 116 |
content.add(new DOMManifest(childElem, context, provider)); |
2 | 117 |
continue; |
118 |
} else if (tag.equals("SignatureProperties")) { |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
119 |
content.add(new DOMSignatureProperties(childElem, context)); |
2 | 120 |
continue; |
121 |
} else if (tag.equals("X509Data")) { |
|
122 |
content.add(new DOMX509Data(childElem)); |
|
123 |
continue; |
|
124 |
} |
|
125 |
//@@@FIXME: check for other dsig structures |
|
126 |
} |
|
127 |
content.add(new javax.xml.crypto.dom.DOMStructure(child)); |
|
128 |
} |
|
129 |
if (content.isEmpty()) { |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
130 |
this.content = Collections.emptyList(); |
2 | 131 |
} else { |
132 |
this.content = Collections.unmodifiableList(content); |
|
133 |
} |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
134 |
this.objectElem = objElem; |
2 | 135 |
} |
136 |
||
24970
094bfaa699c3
8046044: Fix raw and unchecked lint warnings in XML Signature Impl
mullan
parents:
23010
diff
changeset
|
137 |
public List<XMLStructure> getContent() { |
2 | 138 |
return content; |
139 |
} |
|
140 |
||
141 |
public String getId() { |
|
142 |
return id; |
|
143 |
} |
|
144 |
||
145 |
public String getMimeType() { |
|
146 |
return mimeType; |
|
147 |
} |
|
148 |
||
149 |
public String getEncoding() { |
|
150 |
return encoding; |
|
151 |
} |
|
152 |
||
153 |
public void marshal(Node parent, String dsPrefix, DOMCryptoContext context) |
|
154 |
throws MarshalException { |
|
155 |
Document ownerDoc = DOMUtils.getOwnerDocument(parent); |
|
156 |
||
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
157 |
Element objElem = objectElem != null ? objectElem : null; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
158 |
if (objElem == null) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
159 |
objElem = DOMUtils.createElement(ownerDoc, "Object", |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
160 |
XMLSignature.XMLNS, dsPrefix); |
2 | 161 |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
162 |
// set attributes |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
163 |
DOMUtils.setAttributeID(objElem, "Id", id); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
164 |
DOMUtils.setAttribute(objElem, "MimeType", mimeType); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
165 |
DOMUtils.setAttribute(objElem, "Encoding", encoding); |
2 | 166 |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
167 |
// create and append any elements and mixed content, if necessary |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
168 |
for (XMLStructure object : content) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
169 |
if (object instanceof DOMStructure) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
170 |
((DOMStructure)object).marshal(objElem, dsPrefix, context); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
171 |
} else { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
172 |
javax.xml.crypto.dom.DOMStructure domObject = |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
173 |
(javax.xml.crypto.dom.DOMStructure)object; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
174 |
DOMUtils.appendChild(objElem, domObject.getNode()); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
175 |
} |
2 | 176 |
} |
177 |
} |
|
178 |
||
179 |
parent.appendChild(objElem); |
|
180 |
} |
|
181 |
||
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
182 |
@Override |
2 | 183 |
public boolean equals(Object o) { |
184 |
if (this == o) { |
|
185 |
return true; |
|
186 |
} |
|
187 |
||
188 |
if (!(o instanceof XMLObject)) { |
|
189 |
return false; |
|
190 |
} |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
191 |
XMLObject oxo = (XMLObject)o; |
2 | 192 |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
193 |
boolean idsEqual = (id == null ? oxo.getId() == null |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
194 |
: id.equals(oxo.getId())); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
195 |
boolean encodingsEqual = |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
196 |
(encoding == null ? oxo.getEncoding() == null |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
197 |
: encoding.equals(oxo.getEncoding())); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
198 |
boolean mimeTypesEqual = |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
199 |
(mimeType == null ? oxo.getMimeType() == null |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
200 |
: mimeType.equals(oxo.getMimeType())); |
2 | 201 |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
202 |
List<XMLStructure> oxoContent = oxo.getContent(); |
2 | 203 |
return (idsEqual && encodingsEqual && mimeTypesEqual && |
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
204 |
equalsContent(oxoContent)); |
2 | 205 |
} |
206 |
||
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
207 |
@Override |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
208 |
public int hashCode() { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
209 |
int result = 17; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
210 |
if (id != null) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
211 |
result = 31 * result + id.hashCode(); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
212 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
213 |
if (encoding != null) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
214 |
result = 31 * result + encoding.hashCode(); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
215 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
216 |
if (mimeType != null) { |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
217 |
result = 31 * result + mimeType.hashCode(); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
218 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
219 |
result = 31 * result + content.hashCode(); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
220 |
|
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
221 |
return result; |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
222 |
} |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
223 |
|
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
224 |
private boolean equalsContent(List<XMLStructure> otherContent) { |
2 | 225 |
if (content.size() != otherContent.size()) { |
226 |
return false; |
|
227 |
} |
|
228 |
for (int i = 0, osize = otherContent.size(); i < osize; i++) { |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
229 |
XMLStructure oxs = otherContent.get(i); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
230 |
XMLStructure xs = content.get(i); |
2 | 231 |
if (oxs instanceof javax.xml.crypto.dom.DOMStructure) { |
232 |
if (!(xs instanceof javax.xml.crypto.dom.DOMStructure)) { |
|
233 |
return false; |
|
234 |
} |
|
18780
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
235 |
Node onode = ((javax.xml.crypto.dom.DOMStructure)oxs).getNode(); |
f47b920867e7
8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
18240
diff
changeset
|
236 |
Node node = ((javax.xml.crypto.dom.DOMStructure)xs).getNode(); |
2 | 237 |
if (!DOMUtils.nodesEqual(node, onode)) { |
238 |
return false; |
|
239 |
} |
|
240 |
} else { |
|
241 |
if (!(xs.equals(oxs))) { |
|
242 |
return false; |
|
243 |
} |
|
244 |
} |
|
245 |
} |
|
246 |
||
247 |
return true; |
|
248 |
} |
|
249 |
} |