2
|
1 |
/*
|
1337
|
2 |
* reserved comment block
|
|
3 |
* DO NOT REMOVE OR ALTER!
|
|
4 |
*/
|
|
5 |
/*
|
|
6 |
* Copyright 2005 The Apache Software Foundation.
|
2
|
7 |
*
|
1337
|
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
|
2
|
11 |
*
|
1337
|
12 |
* http://www.apache.org/licenses/LICENSE-2.0
|
2
|
13 |
*
|
1337
|
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.
|
2
|
19 |
*
|
|
20 |
*/
|
1337
|
21 |
/*
|
5506
|
22 |
* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
|
1337
|
23 |
*/
|
2
|
24 |
/*
|
|
25 |
* ===========================================================================
|
|
26 |
*
|
|
27 |
* (C) Copyright IBM Corp. 2003 All Rights Reserved.
|
|
28 |
*
|
|
29 |
* ===========================================================================
|
|
30 |
*/
|
|
31 |
/*
|
1337
|
32 |
* $Id: DOMRetrievalMethod.java,v 1.2 2008/07/24 15:20:32 mullan Exp $
|
2
|
33 |
*/
|
|
34 |
package org.jcp.xml.dsig.internal.dom;
|
|
35 |
|
|
36 |
import java.io.ByteArrayInputStream;
|
|
37 |
import java.net.URI;
|
|
38 |
import java.net.URISyntaxException;
|
1337
|
39 |
import java.security.Provider;
|
2
|
40 |
import java.util.*;
|
|
41 |
import javax.xml.crypto.*;
|
|
42 |
import javax.xml.crypto.dsig.*;
|
|
43 |
import javax.xml.crypto.dom.DOMCryptoContext;
|
|
44 |
import javax.xml.crypto.dom.DOMURIReference;
|
|
45 |
import javax.xml.crypto.dsig.keyinfo.RetrievalMethod;
|
|
46 |
import javax.xml.parsers.*;
|
|
47 |
import org.w3c.dom.Attr;
|
|
48 |
import org.w3c.dom.Document;
|
|
49 |
import org.w3c.dom.Element;
|
|
50 |
import org.w3c.dom.Node;
|
|
51 |
|
|
52 |
import com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput;
|
|
53 |
|
|
54 |
/**
|
|
55 |
* DOM-based implementation of RetrievalMethod.
|
|
56 |
*
|
|
57 |
* @author Sean Mullan
|
|
58 |
* @author Joyce Leung
|
|
59 |
*/
|
|
60 |
public final class DOMRetrievalMethod extends DOMStructure
|
|
61 |
implements RetrievalMethod, DOMURIReference {
|
|
62 |
|
|
63 |
private final List transforms;
|
|
64 |
private String uri;
|
|
65 |
private String type;
|
|
66 |
private Attr here;
|
|
67 |
|
|
68 |
/**
|
|
69 |
* Creates a <code>DOMRetrievalMethod</code> containing the specified
|
|
70 |
* URIReference and List of Transforms.
|
|
71 |
*
|
|
72 |
* @param uri the URI
|
|
73 |
* @param type the type
|
|
74 |
* @param transforms a list of {@link Transform}s. The list is defensively
|
|
75 |
* copied to prevent subsequent modification. May be <code>null</code>
|
|
76 |
* or empty.
|
|
77 |
* @throws IllegalArgumentException if the format of <code>uri</code> is
|
|
78 |
* invalid, as specified by Reference's URI attribute in the W3C
|
|
79 |
* specification for XML-Signature Syntax and Processing
|
|
80 |
* @throws NullPointerException if <code>uriReference</code>
|
|
81 |
* is <code>null</code>
|
|
82 |
* @throws ClassCastException if <code>transforms</code> contains any
|
|
83 |
* entries that are not of type {@link Transform}
|
|
84 |
*/
|
|
85 |
public DOMRetrievalMethod(String uri, String type, List transforms) {
|
|
86 |
if (uri == null) {
|
|
87 |
throw new NullPointerException("uri cannot be null");
|
|
88 |
}
|
|
89 |
if (transforms == null || transforms.isEmpty()) {
|
|
90 |
this.transforms = Collections.EMPTY_LIST;
|
|
91 |
} else {
|
|
92 |
List transformsCopy = new ArrayList(transforms);
|
|
93 |
for (int i = 0, size = transformsCopy.size(); i < size; i++) {
|
|
94 |
if (!(transformsCopy.get(i) instanceof Transform)) {
|
|
95 |
throw new ClassCastException
|
|
96 |
("transforms["+i+"] is not a valid type");
|
|
97 |
}
|
|
98 |
}
|
|
99 |
this.transforms = Collections.unmodifiableList(transformsCopy);
|
|
100 |
}
|
|
101 |
this.uri = uri;
|
|
102 |
if ((uri != null) && (!uri.equals(""))) {
|
|
103 |
try {
|
|
104 |
new URI(uri);
|
|
105 |
} catch (URISyntaxException e) {
|
|
106 |
throw new IllegalArgumentException(e.getMessage());
|
|
107 |
}
|
|
108 |
}
|
|
109 |
|
|
110 |
this.type = type;
|
|
111 |
}
|
|
112 |
|
|
113 |
/**
|
|
114 |
* Creates a <code>DOMRetrievalMethod</code> from an element.
|
|
115 |
*
|
|
116 |
* @param rmElem a RetrievalMethod element
|
|
117 |
*/
|
1337
|
118 |
public DOMRetrievalMethod(Element rmElem, XMLCryptoContext context,
|
|
119 |
Provider provider) throws MarshalException {
|
2
|
120 |
// get URI and Type attributes
|
|
121 |
uri = DOMUtils.getAttributeValue(rmElem, "URI");
|
|
122 |
type = DOMUtils.getAttributeValue(rmElem, "Type");
|
|
123 |
|
|
124 |
// get here node
|
|
125 |
here = rmElem.getAttributeNodeNS(null, "URI");
|
|
126 |
|
|
127 |
// get Transforms, if specified
|
|
128 |
List transforms = new ArrayList();
|
|
129 |
Element transformsElem = DOMUtils.getFirstChildElement(rmElem);
|
|
130 |
if (transformsElem != null) {
|
|
131 |
Element transformElem =
|
|
132 |
DOMUtils.getFirstChildElement(transformsElem);
|
|
133 |
while (transformElem != null) {
|
1337
|
134 |
transforms.add
|
|
135 |
(new DOMTransform(transformElem, context, provider));
|
2
|
136 |
transformElem = DOMUtils.getNextSiblingElement(transformElem);
|
|
137 |
}
|
|
138 |
}
|
|
139 |
if (transforms.isEmpty()) {
|
|
140 |
this.transforms = Collections.EMPTY_LIST;
|
|
141 |
} else {
|
|
142 |
this.transforms = Collections.unmodifiableList(transforms);
|
|
143 |
}
|
|
144 |
}
|
|
145 |
|
|
146 |
public String getURI() {
|
|
147 |
return uri;
|
|
148 |
}
|
|
149 |
|
|
150 |
public String getType() {
|
|
151 |
return type;
|
|
152 |
}
|
|
153 |
|
|
154 |
public List getTransforms() {
|
|
155 |
return transforms;
|
|
156 |
}
|
|
157 |
|
|
158 |
public void marshal(Node parent, String dsPrefix, DOMCryptoContext context)
|
|
159 |
throws MarshalException {
|
|
160 |
Document ownerDoc = DOMUtils.getOwnerDocument(parent);
|
|
161 |
|
|
162 |
Element rmElem = DOMUtils.createElement
|
|
163 |
(ownerDoc, "RetrievalMethod", XMLSignature.XMLNS, dsPrefix);
|
|
164 |
|
|
165 |
// add URI and Type attributes
|
|
166 |
DOMUtils.setAttribute(rmElem, "URI", uri);
|
|
167 |
DOMUtils.setAttribute(rmElem, "Type", type);
|
|
168 |
|
|
169 |
// add Transforms elements
|
|
170 |
if (!transforms.isEmpty()) {
|
|
171 |
Element transformsElem = DOMUtils.createElement
|
|
172 |
(ownerDoc, "Transforms", XMLSignature.XMLNS, dsPrefix);
|
|
173 |
rmElem.appendChild(transformsElem);
|
|
174 |
for (int i = 0, size = transforms.size(); i < size; i++) {
|
|
175 |
((DOMTransform) transforms.get(i)).marshal
|
|
176 |
(transformsElem, dsPrefix, context);
|
|
177 |
}
|
|
178 |
}
|
|
179 |
|
|
180 |
parent.appendChild(rmElem);
|
|
181 |
|
|
182 |
// save here node
|
|
183 |
here = rmElem.getAttributeNodeNS(null, "URI");
|
|
184 |
}
|
|
185 |
|
|
186 |
public Node getHere() {
|
|
187 |
return here;
|
|
188 |
}
|
|
189 |
|
|
190 |
public Data dereference(XMLCryptoContext context)
|
|
191 |
throws URIReferenceException {
|
|
192 |
|
|
193 |
if (context == null) {
|
|
194 |
throw new NullPointerException("context cannot be null");
|
|
195 |
}
|
|
196 |
|
|
197 |
/*
|
|
198 |
* If URIDereferencer is specified in context; use it, otherwise use
|
|
199 |
* built-in.
|
|
200 |
*/
|
|
201 |
URIDereferencer deref = context.getURIDereferencer();
|
|
202 |
if (deref == null) {
|
|
203 |
deref = DOMURIDereferencer.INSTANCE;
|
|
204 |
}
|
|
205 |
|
|
206 |
Data data = deref.dereference(this, context);
|
|
207 |
|
|
208 |
// pass dereferenced data through Transforms
|
|
209 |
try {
|
|
210 |
for (int i = 0, size = transforms.size(); i < size; i++) {
|
|
211 |
Transform transform = (Transform) transforms.get(i);
|
|
212 |
data = ((DOMTransform) transform).transform(data, context);
|
|
213 |
}
|
|
214 |
} catch (Exception e) {
|
|
215 |
throw new URIReferenceException(e);
|
|
216 |
}
|
|
217 |
return data;
|
|
218 |
}
|
|
219 |
|
|
220 |
public XMLStructure dereferenceAsXMLStructure(XMLCryptoContext context)
|
|
221 |
throws URIReferenceException {
|
|
222 |
|
|
223 |
try {
|
|
224 |
ApacheData data = (ApacheData) dereference(context);
|
|
225 |
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
|
226 |
dbf.setNamespaceAware(true);
|
|
227 |
DocumentBuilder db = dbf.newDocumentBuilder();
|
|
228 |
Document doc = db.parse(new ByteArrayInputStream
|
|
229 |
(data.getXMLSignatureInput().getBytes()));
|
|
230 |
Element kiElem = doc.getDocumentElement();
|
|
231 |
if (kiElem.getLocalName().equals("X509Data")) {
|
|
232 |
return new DOMX509Data(kiElem);
|
|
233 |
} else {
|
|
234 |
return null; // unsupported
|
|
235 |
}
|
|
236 |
} catch (Exception e) {
|
|
237 |
throw new URIReferenceException(e);
|
|
238 |
}
|
|
239 |
}
|
|
240 |
|
|
241 |
public boolean equals(Object obj) {
|
|
242 |
if (this == obj) {
|
|
243 |
return true;
|
|
244 |
}
|
|
245 |
if (!(obj instanceof RetrievalMethod)) {
|
|
246 |
return false;
|
|
247 |
}
|
|
248 |
RetrievalMethod orm = (RetrievalMethod) obj;
|
|
249 |
|
|
250 |
boolean typesEqual = (type == null ? orm.getType() == null :
|
|
251 |
type.equals(orm.getType()));
|
|
252 |
|
|
253 |
return (uri.equals(orm.getURI()) &&
|
|
254 |
transforms.equals(orm.getTransforms()) && typesEqual);
|
|
255 |
}
|
|
256 |
}
|