0N/A/*
0N/A * reserved comment block
0N/A * DO NOT REMOVE OR ALTER!
0N/A */
0N/A/*
0N/A * Copyright 1999-2004 The Apache Software Foundation.
0N/A *
0N/A * Licensed under the Apache License, Version 2.0 (the "License");
0N/A * you may not use this file except in compliance with the License.
0N/A * You may obtain a copy of the License at
0N/A *
0N/A * http://www.apache.org/licenses/LICENSE-2.0
0N/A *
0N/A * Unless required by applicable law or agreed to in writing, software
0N/A * distributed under the License is distributed on an "AS IS" BASIS,
0N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0N/A * See the License for the specific language governing permissions and
0N/A * limitations under the License.
0N/A *
0N/A */
0N/Apackage com.sun.org.apache.xml.internal.security.utils.resolver.implementations;
0N/A
0N/A
0N/A
0N/Aimport com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput;
6159N/Aimport com.sun.org.apache.xml.internal.security.utils.XMLUtils;
0N/Aimport com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverException;
0N/Aimport com.sun.org.apache.xml.internal.security.utils.resolver.ResourceResolverSpi;
0N/Aimport org.w3c.dom.Attr;
0N/Aimport org.w3c.dom.Document;
6159N/Aimport org.w3c.dom.Element;
0N/Aimport org.w3c.dom.Node;
0N/A
0N/A
0N/A/**
0N/A * This resolver is used for resolving same-document URIs like URI="" of URI="#id".
0N/A *
661N/A * @author $Author: mullan $
0N/A * @see <A HREF="http://www.w3.org/TR/xmldsig-core/#sec-ReferenceProcessingModel">The Reference processing model in the XML Signature spec</A>
0N/A * @see <A HREF="http://www.w3.org/TR/xmldsig-core/#sec-Same-Document">Same-Document URI-References in the XML Signature spec</A>
0N/A * @see <A HREF="http://www.ietf.org/rfc/rfc2396.txt">Section 4.2 of RFC 2396</A>
0N/A */
0N/Apublic class ResolverFragment extends ResourceResolverSpi {
0N/A
0N/A /** {@link java.util.logging} logging facility */
0N/A static java.util.logging.Logger log =
0N/A java.util.logging.Logger.getLogger(
0N/A ResolverFragment.class.getName());
661N/A public boolean engineIsThreadSafe() {
661N/A return true;
661N/A }
0N/A /**
0N/A * Method engineResolve
0N/A *
0N/A * @inheritDoc
0N/A * @param uri
6159N/A * @param baseURI
0N/A */
6159N/A public XMLSignatureInput engineResolve(Attr uri, String baseURI)
0N/A throws ResourceResolverException
0N/A {
6159N/A String uriNodeValue = uri.getNodeValue();
6159N/A Document doc = uri.getOwnerElement().getOwnerDocument();
0N/A
6159N/A Node selectedElem = null;
6159N/A if (uriNodeValue.equals("")) {
0N/A
6159N/A /*
6159N/A * Identifies the node-set (minus any comment nodes) of the XML
6159N/A * resource containing the signature
6159N/A */
0N/A
6159N/A log.log(java.util.logging.Level.FINE, "ResolverFragment with empty URI (means complete document)");
6159N/A selectedElem = doc;
6159N/A } else {
0N/A
6159N/A /*
6159N/A * URI="#chapter1"
6159N/A * Identifies a node-set containing the element with ID attribute
6159N/A * value 'chapter1' of the XML resource containing the signature.
6159N/A * XML Signature (and its applications) modify this node-set to
6159N/A * include the element plus all descendents including namespaces and
6159N/A * attributes -- but not comments.
6159N/A */
6159N/A String id = uriNodeValue.substring(1);
0N/A
6159N/A selectedElem = doc.getElementById(id);
6159N/A if (selectedElem == null) {
0N/A Object exArgs[] = { id };
6159N/A throw new ResourceResolverException(
6159N/A "signature.Verification.MissingID", exArgs, uri, baseURI);
6159N/A }
6159N/A if (secureValidation) {
6159N/A Element start = uri.getOwnerDocument().getDocumentElement();
6159N/A if (!XMLUtils.protectAgainstWrappingAttack(start, id)) {
6159N/A Object exArgs[] = { id };
6159N/A throw new ResourceResolverException(
6159N/A "signature.Verification.MultipleIDs", exArgs,
6159N/A uri, baseURI);
6159N/A }
6159N/A }
6159N/A if (log.isLoggable(java.util.logging.Level.FINE))
661N/A log.log(java.util.logging.Level.FINE, "Try to catch an Element with ID " + id + " and Element was " + selectedElem);
6159N/A }
0N/A
6159N/A XMLSignatureInput result = new XMLSignatureInput(selectedElem);
6159N/A result.setExcludeComments(true);
0N/A
6159N/A result.setMIMEType("text/xml");
6159N/A if (baseURI != null && baseURI.length() > 0) {
6159N/A result.setSourceURI(baseURI.concat(uri.getNodeValue()));
6159N/A } else {
6159N/A result.setSourceURI(uri.getNodeValue());
6159N/A }
6159N/A return result;
6159N/A }
0N/A
0N/A /**
0N/A * Method engineCanResolve
0N/A * @inheritDoc
0N/A * @param uri
0N/A * @param BaseURI
0N/A *
0N/A */
0N/A public boolean engineCanResolve(Attr uri, String BaseURI) {
0N/A
0N/A if (uri == null) {
661N/A log.log(java.util.logging.Level.FINE, "Quick fail for null uri");
0N/A return false;
0N/A }
0N/A
0N/A String uriNodeValue = uri.getNodeValue();
0N/A
661N/A if (uriNodeValue.equals("") ||
661N/A (
661N/A (uriNodeValue.charAt(0)=='#')
661N/A && !((uriNodeValue.charAt(1)=='x') && uriNodeValue.startsWith("#xpointer("))
661N/A )
661N/A ){
661N/A if (log.isLoggable(java.util.logging.Level.FINE))
661N/A log.log(java.util.logging.Level.FINE, "State I can resolve reference: \"" + uriNodeValue + "\"");
0N/A return true;
0N/A }
661N/A if (log.isLoggable(java.util.logging.Level.FINE))
661N/A log.log(java.util.logging.Level.FINE, "Do not seem to be able to resolve reference: \"" + uriNodeValue + "\"");
0N/A return false;
0N/A }
0N/A
0N/A}