|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
package com.sun.org.apache.xml.internal.security.utils; |
|
|
|
import java.math.BigInteger; |
|
import java.util.concurrent.ConcurrentHashMap; |
|
import java.util.Map; |
|
|
|
import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException; |
|
import org.w3c.dom.Attr; |
|
import org.w3c.dom.Document; |
|
import org.w3c.dom.Element; |
|
import org.w3c.dom.Node; |
|
import org.w3c.dom.NodeList; |
|
import org.w3c.dom.Text; |
|
|
|
|
|
|
|
|
|
*/ |
|
public abstract class ElementProxy { |
|
|
|
protected static final com.sun.org.slf4j.internal.Logger LOG = |
|
com.sun.org.slf4j.internal.LoggerFactory.getLogger(ElementProxy.class); |
|
|
|
|
|
|
|
*/ |
|
private Element wrappedElement; |
|
|
|
|
|
protected String baseURI; |
|
|
|
|
|
private Document wrappedDoc; |
|
|
|
|
|
private static Map<String, String> prefixMappings = new ConcurrentHashMap<String, String>(); |
|
|
|
|
|
|
|
|
|
*/ |
|
public ElementProxy() { |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public ElementProxy(Document doc) { |
|
if (doc == null) { |
|
throw new RuntimeException("Document is null"); |
|
} |
|
|
|
this.wrappedDoc = doc; |
|
this.wrappedElement = createElementForFamilyLocal(this.getBaseNamespace(), this.getBaseLocalName()); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public ElementProxy(Element element, String baseURI) throws XMLSecurityException { |
|
if (element == null) { |
|
throw new XMLSecurityException("ElementProxy.nullElement"); |
|
} |
|
|
|
LOG.debug("setElement(\"{}\", \"{}\")", element.getTagName(), baseURI); |
|
|
|
setElement(element); |
|
this.baseURI = baseURI; |
|
|
|
this.guaranteeThatElementInCorrectSpace(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public abstract String getBaseNamespace(); |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public abstract String getBaseLocalName(); |
|
|
|
|
|
protected Element createElementForFamilyLocal( |
|
String namespace, String localName |
|
) { |
|
Document doc = getDocument(); |
|
Element result = null; |
|
if (namespace == null) { |
|
result = doc.createElementNS(null, localName); |
|
} else { |
|
String baseName = this.getBaseNamespace(); |
|
String prefix = ElementProxy.getDefaultPrefix(baseName); |
|
if (prefix == null || prefix.length() == 0) { |
|
result = doc.createElementNS(namespace, localName); |
|
result.setAttributeNS(Constants.NamespaceSpecNS, "xmlns", namespace); |
|
} else { |
|
result = doc.createElementNS(namespace, prefix + ":" + localName); |
|
result.setAttributeNS(Constants.NamespaceSpecNS, "xmlns:" + prefix, namespace); |
|
} |
|
} |
|
return result; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public static Element createElementForFamily(Document doc, String namespace, String localName) { |
|
Element result = null; |
|
String prefix = ElementProxy.getDefaultPrefix(namespace); |
|
|
|
if (namespace == null) { |
|
result = doc.createElementNS(null, localName); |
|
} else { |
|
if (prefix == null || prefix.length() == 0) { |
|
result = doc.createElementNS(namespace, localName); |
|
result.setAttributeNS(Constants.NamespaceSpecNS, "xmlns", namespace); |
|
} else { |
|
result = doc.createElementNS(namespace, prefix + ":" + localName); |
|
result.setAttributeNS(Constants.NamespaceSpecNS, "xmlns:" + prefix, namespace); |
|
} |
|
} |
|
|
|
return result; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void setElement(Element element, String baseURI) throws XMLSecurityException { |
|
if (element == null) { |
|
throw new XMLSecurityException("ElementProxy.nullElement"); |
|
} |
|
|
|
LOG.debug("setElement({}, \"{}\")", element.getTagName(), baseURI); |
|
|
|
setElement(element); |
|
this.baseURI = baseURI; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public final Element getElement() { |
|
return this.wrappedElement; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public final NodeList getElementPlusReturns() { |
|
|
|
HelperNodeList nl = new HelperNodeList(); |
|
|
|
nl.appendChild(createText("\n")); |
|
nl.appendChild(getElement()); |
|
nl.appendChild(createText("\n")); |
|
|
|
return nl; |
|
} |
|
|
|
protected Text createText(String text) { |
|
return this.wrappedDoc.createTextNode(text); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public Document getDocument() { |
|
if (wrappedDoc == null) { |
|
wrappedDoc = XMLUtils.getOwnerDocument(wrappedElement); |
|
} |
|
return wrappedDoc; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public String getBaseURI() { |
|
return this.baseURI; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
void guaranteeThatElementInCorrectSpace() throws XMLSecurityException { |
|
|
|
String expectedLocalName = this.getBaseLocalName(); |
|
String expectedNamespaceUri = this.getBaseNamespace(); |
|
|
|
String actualLocalName = getElement().getLocalName(); |
|
String actualNamespaceUri = getElement().getNamespaceURI(); |
|
|
|
if(!expectedNamespaceUri.equals(actualNamespaceUri) |
|
&& !expectedLocalName.equals(actualLocalName)) { |
|
Object exArgs[] = { actualNamespaceUri + ":" + actualLocalName, |
|
expectedNamespaceUri + ":" + expectedLocalName}; |
|
throw new XMLSecurityException("xml.WrongElement", exArgs); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void addBigIntegerElement(BigInteger bi, String localname) { |
|
if (bi != null) { |
|
Element e = XMLUtils.createElementInSignatureSpace(getDocument(), localname); |
|
|
|
byte[] bytes = XMLUtils.getBytes(bi, bi.bitLength()); |
|
String encodedInt = XMLUtils.encodeToString(bytes); |
|
|
|
Document doc = e.getOwnerDocument(); |
|
Text text = doc.createTextNode(encodedInt); |
|
|
|
e.appendChild(text); |
|
|
|
appendSelf(e); |
|
addReturnToSelf(); |
|
} |
|
} |
|
|
|
protected void addReturnToSelf() { |
|
XMLUtils.addReturnToElement(getElement()); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void addBase64Element(byte[] bytes, String localname) { |
|
if (bytes != null) { |
|
Element el = XMLUtils.createElementInSignatureSpace(getDocument(), localname); |
|
Text text = getDocument().createTextNode(XMLUtils.encodeToString(bytes)); |
|
|
|
el.appendChild(text); |
|
|
|
appendSelf(el); |
|
if (!XMLUtils.ignoreLineBreaks()) { |
|
appendSelf(createText("\n")); |
|
} |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void addTextElement(String text, String localname) { |
|
Element e = XMLUtils.createElementInSignatureSpace(getDocument(), localname); |
|
Text t = createText(text); |
|
|
|
appendOther(e, t); |
|
appendSelf(e); |
|
addReturnToSelf(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void addBase64Text(byte[] bytes) { |
|
if (bytes != null) { |
|
Text t = XMLUtils.ignoreLineBreaks() |
|
? createText(XMLUtils.encodeToString(bytes)) |
|
: createText("\n" + XMLUtils.encodeToString(bytes) + "\n"); |
|
appendSelf(t); |
|
} |
|
} |
|
|
|
protected void appendSelf(ElementProxy toAppend) { |
|
getElement().appendChild(toAppend.getElement()); |
|
} |
|
|
|
protected void appendSelf(Node toAppend) { |
|
getElement().appendChild(toAppend); |
|
} |
|
|
|
protected void appendOther(Element parent, Node toAppend) { |
|
parent.appendChild(toAppend); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void addText(String text) { |
|
if (text != null) { |
|
Text t = createText(text); |
|
|
|
appendSelf(t); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public BigInteger getBigIntegerFromChildElement( |
|
String localname, String namespace |
|
) { |
|
Node n = XMLUtils.selectNode(getFirstChild(), namespace, localname, 0); |
|
if (n != null) { |
|
return new BigInteger(1, XMLUtils.decode(XMLUtils.getFullTextChildrenFromNode(n))); |
|
} |
|
return null; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public String getTextFromChildElement(String localname, String namespace) { |
|
return XMLUtils.selectNode( |
|
getFirstChild(), |
|
namespace, |
|
localname, |
|
0).getTextContent(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public byte[] getBytesFromTextChild() throws XMLSecurityException { |
|
return XMLUtils.decode(getTextFromTextChild()); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public String getTextFromTextChild() { |
|
return XMLUtils.getFullTextChildrenFromNode(getElement()); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public int length(String namespace, String localname) { |
|
int number = 0; |
|
Node sibling = getFirstChild(); |
|
while (sibling != null) { |
|
if (localname.equals(sibling.getLocalName()) |
|
&& namespace.equals(sibling.getNamespaceURI())) { |
|
number++; |
|
} |
|
sibling = sibling.getNextSibling(); |
|
} |
|
return number; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void setXPathNamespaceContext(String prefix, String uri) |
|
throws XMLSecurityException { |
|
String ns; |
|
|
|
if (prefix == null || prefix.length() == 0) { |
|
throw new XMLSecurityException("defaultNamespaceCannotBeSetHere"); |
|
} else if ("xmlns".equals(prefix)) { |
|
throw new XMLSecurityException("defaultNamespaceCannotBeSetHere"); |
|
} else if (prefix.startsWith("xmlns:")) { |
|
ns = prefix; |
|
} else { |
|
ns = "xmlns:" + prefix; |
|
} |
|
|
|
Attr a = getElement().getAttributeNodeNS(Constants.NamespaceSpecNS, ns); |
|
|
|
if (a != null) { |
|
if (!a.getNodeValue().equals(uri)) { |
|
Object exArgs[] = { ns, getElement().getAttributeNS(null, ns) }; |
|
|
|
throw new XMLSecurityException("namespacePrefixAlreadyUsedByOtherURI", exArgs); |
|
} |
|
return; |
|
} |
|
|
|
getElement().setAttributeNS(Constants.NamespaceSpecNS, ns, uri); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public static void setDefaultPrefix(String namespace, String prefix) |
|
throws XMLSecurityException { |
|
JavaUtils.checkRegisterPermission(); |
|
setNamespacePrefix(namespace, prefix); |
|
} |
|
|
|
private static void setNamespacePrefix(String namespace, String prefix) |
|
throws XMLSecurityException { |
|
if (prefixMappings.containsValue(prefix)) { |
|
String storedPrefix = prefixMappings.get(namespace); |
|
if (!storedPrefix.equals(prefix)) { |
|
Object exArgs[] = { prefix, namespace, storedPrefix }; |
|
|
|
throw new XMLSecurityException("prefix.AlreadyAssigned", exArgs); |
|
} |
|
} |
|
|
|
if (Constants.SignatureSpecNS.equals(namespace)) { |
|
XMLUtils.setDsPrefix(prefix); |
|
} else if (Constants.SignatureSpec11NS.equals(namespace)) { |
|
XMLUtils.setDs11Prefix(prefix); |
|
} else if (EncryptionConstants.EncryptionSpecNS.equals(namespace)) { |
|
XMLUtils.setXencPrefix(prefix); |
|
} |
|
prefixMappings.put(namespace, prefix); |
|
} |
|
|
|
|
|
|
|
*/ |
|
public static void registerDefaultPrefixes() throws XMLSecurityException { |
|
setNamespacePrefix("http://www.w3.org/2000/09/xmldsig#", "ds"); |
|
setNamespacePrefix("http://www.w3.org/2001/04/xmlenc#", "xenc"); |
|
setNamespacePrefix("http://www.w3.org/2009/xmlenc11#", "xenc11"); |
|
setNamespacePrefix("http://www.xmlsecurity.org/experimental#", "experimental"); |
|
setNamespacePrefix("http://www.w3.org/2002/04/xmldsig-filter2", "dsig-xpath-old"); |
|
setNamespacePrefix("http://www.w3.org/2002/06/xmldsig-filter2", "dsig-xpath"); |
|
setNamespacePrefix("http://www.w3.org/2001/10/xml-exc-c14n#", "ec"); |
|
setNamespacePrefix( |
|
"http://www.nue.et-inf.uni-siegen.de/~geuer-pollmann/#xpathFilter", "xx" |
|
); |
|
setNamespacePrefix("http://www.w3.org/2009/xmldsig11#", "dsig11"); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public static String getDefaultPrefix(String namespace) { |
|
return prefixMappings.get(namespace); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
protected void setElement(Element elem) { |
|
wrappedElement = elem; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
protected void setDocument(Document doc) { |
|
wrappedDoc = doc; |
|
} |
|
|
|
protected String getLocalAttribute(String attrName) { |
|
return getElement().getAttributeNS(null, attrName); |
|
} |
|
|
|
protected void setLocalAttribute(String attrName, String value) { |
|
getElement().setAttributeNS(null, attrName, value); |
|
} |
|
|
|
protected void setLocalIdAttribute(String attrName, String value) { |
|
|
|
if (value != null) { |
|
Attr attr = getDocument().createAttributeNS(null, attrName); |
|
attr.setValue(value); |
|
getElement().setAttributeNodeNS(attr); |
|
getElement().setIdAttributeNode(attr, true); |
|
} |
|
else { |
|
getElement().removeAttributeNS(null, attrName); |
|
} |
|
} |
|
|
|
protected Node getFirstChild() { |
|
return getElement().getFirstChild(); |
|
} |
|
|
|
} |