2
|
1 |
/*
|
1337
|
2 |
* reserved comment block
|
|
3 |
* DO NOT REMOVE OR ALTER!
|
2
|
4 |
*/
|
|
5 |
/*
|
1337
|
6 |
* Copyright 2005 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 |
/*
|
5506
|
22 |
* Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved.
|
1337
|
23 |
*/
|
|
24 |
/*
|
|
25 |
* $Id: DOMManifest.java,v 1.2 2008/07/24 15:20:32 mullan Exp $
|
2
|
26 |
*/
|
|
27 |
package org.jcp.xml.dsig.internal.dom;
|
|
28 |
|
|
29 |
import javax.xml.crypto.*;
|
|
30 |
import javax.xml.crypto.dom.DOMCryptoContext;
|
|
31 |
import javax.xml.crypto.dsig.*;
|
|
32 |
|
1337
|
33 |
import java.security.Provider;
|
2
|
34 |
import java.util.*;
|
18240
|
35 |
import org.w3c.dom.Attr;
|
2
|
36 |
import org.w3c.dom.Document;
|
|
37 |
import org.w3c.dom.Element;
|
|
38 |
import org.w3c.dom.Node;
|
|
39 |
|
|
40 |
/**
|
|
41 |
* DOM-based implementation of Manifest.
|
|
42 |
*
|
|
43 |
* @author Sean Mullan
|
|
44 |
*/
|
|
45 |
public final class DOMManifest extends DOMStructure implements Manifest {
|
|
46 |
|
|
47 |
private final List references;
|
|
48 |
private final String id;
|
|
49 |
|
|
50 |
/**
|
|
51 |
* Creates a <code>DOMManifest</code> containing the specified
|
|
52 |
* list of {@link Reference}s and optional id.
|
|
53 |
*
|
|
54 |
* @param references a list of one or more <code>Reference</code>s. The list
|
|
55 |
* is defensively copied to protect against subsequent modification.
|
|
56 |
* @param id the id (may be <code>null</code>
|
|
57 |
* @throws NullPointerException if <code>references</code> is
|
|
58 |
* <code>null</code>
|
|
59 |
* @throws IllegalArgumentException if <code>references</code> is empty
|
|
60 |
* @throws ClassCastException if <code>references</code> contains any
|
|
61 |
* entries that are not of type {@link Reference}
|
|
62 |
*/
|
|
63 |
public DOMManifest(List references, String id) {
|
|
64 |
if (references == null) {
|
|
65 |
throw new NullPointerException("references cannot be null");
|
|
66 |
}
|
|
67 |
List refCopy = new ArrayList(references);
|
|
68 |
if (refCopy.isEmpty()) {
|
|
69 |
throw new IllegalArgumentException("list of references must " +
|
|
70 |
"contain at least one entry");
|
|
71 |
}
|
|
72 |
for (int i = 0, size = refCopy.size(); i < size; i++) {
|
|
73 |
if (!(refCopy.get(i) instanceof Reference)) {
|
|
74 |
throw new ClassCastException
|
|
75 |
("references["+i+"] is not a valid type");
|
|
76 |
}
|
|
77 |
}
|
|
78 |
this.references = Collections.unmodifiableList(refCopy);
|
|
79 |
this.id = id;
|
|
80 |
}
|
|
81 |
|
|
82 |
/**
|
|
83 |
* Creates a <code>DOMManifest</code> from an element.
|
|
84 |
*
|
|
85 |
* @param manElem a Manifest element
|
|
86 |
*/
|
1337
|
87 |
public DOMManifest(Element manElem, XMLCryptoContext context,
|
|
88 |
Provider provider) throws MarshalException {
|
18240
|
89 |
Attr attr = manElem.getAttributeNodeNS(null, "Id");
|
|
90 |
if (attr != null) {
|
|
91 |
this.id = attr.getValue();
|
|
92 |
manElem.setIdAttributeNode(attr, true);
|
|
93 |
} else {
|
|
94 |
this.id = null;
|
|
95 |
}
|
|
96 |
|
|
97 |
boolean secVal = Utils.secureValidation(context);
|
2
|
98 |
Element refElem = DOMUtils.getFirstChildElement(manElem);
|
|
99 |
List refs = new ArrayList();
|
18240
|
100 |
int refCount = 0;
|
2
|
101 |
while (refElem != null) {
|
1337
|
102 |
refs.add(new DOMReference(refElem, context, provider));
|
2
|
103 |
refElem = DOMUtils.getNextSiblingElement(refElem);
|
18240
|
104 |
|
|
105 |
refCount++;
|
|
106 |
if (secVal && (refCount > DOMSignedInfo.MAXIMUM_REFERENCE_COUNT)) {
|
|
107 |
String error = "A maxiumum of " +
|
|
108 |
DOMSignedInfo.MAXIMUM_REFERENCE_COUNT +
|
|
109 |
" references per Manifest are allowed with" +
|
|
110 |
" secure validation";
|
|
111 |
throw new MarshalException(error);
|
|
112 |
}
|
2
|
113 |
}
|
|
114 |
this.references = Collections.unmodifiableList(refs);
|
|
115 |
}
|
|
116 |
|
|
117 |
public String getId() {
|
|
118 |
return id;
|
|
119 |
}
|
|
120 |
|
|
121 |
public List getReferences() {
|
|
122 |
return references;
|
|
123 |
}
|
|
124 |
|
|
125 |
public void marshal(Node parent, String dsPrefix, DOMCryptoContext context)
|
|
126 |
throws MarshalException {
|
|
127 |
Document ownerDoc = DOMUtils.getOwnerDocument(parent);
|
|
128 |
|
|
129 |
Element manElem = DOMUtils.createElement
|
|
130 |
(ownerDoc, "Manifest", XMLSignature.XMLNS, dsPrefix);
|
|
131 |
|
|
132 |
DOMUtils.setAttributeID(manElem, "Id", id);
|
|
133 |
|
|
134 |
// add references
|
|
135 |
for (int i = 0, size = references.size(); i < size; i++) {
|
|
136 |
DOMReference ref = (DOMReference) references.get(i);
|
|
137 |
ref.marshal(manElem, dsPrefix, context);
|
|
138 |
}
|
|
139 |
parent.appendChild(manElem);
|
|
140 |
}
|
|
141 |
|
|
142 |
public boolean equals(Object o) {
|
|
143 |
if (this == o) {
|
|
144 |
return true;
|
|
145 |
}
|
|
146 |
|
|
147 |
if (!(o instanceof Manifest)) {
|
|
148 |
return false;
|
|
149 |
}
|
|
150 |
Manifest oman = (Manifest) o;
|
|
151 |
|
|
152 |
boolean idsEqual = (id == null ? oman.getId() == null :
|
|
153 |
id.equals(oman.getId()));
|
|
154 |
|
|
155 |
return (idsEqual && references.equals(oman.getReferences()));
|
|
156 |
}
|
|
157 |
}
|