001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the License). You may not use this file except in
005: * compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://glassfish.dev.java.net/public/CDDLv1.0.html.
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * Header Notice in each file and include the License file
014: * at https://glassfish.dev.java.net/public/CDDLv1.0.html.
015: * If applicable, add the following below the CDDL Header,
016: * with the fields enclosed by brackets [] replaced by
017: * you own identifying information:
018: * "Portions Copyrighted [year] [name of copyright owner]"
019: *
020: * Copyright 2006 Sun Microsystems Inc. All Rights Reserved
021: */
022:
023: package com.sun.xml.wss.impl;
024:
025: import com.sun.xml.wss.ProcessingContext;
026: import com.sun.xml.wss.XWSSecurityException;
027: import com.sun.xml.wss.core.SecurityHeader;
028: import com.sun.xml.wss.impl.policy.mls.SignaturePolicy;
029: import com.sun.xml.wss.impl.policy.mls.Target;
030: import com.sun.xml.wss.impl.policy.mls.WSSPolicy;
031: import com.sun.xml.wss.impl.policy.verifier.TargetResolver;
032: import com.sun.xml.wss.logging.LogDomainConstants;
033: import java.util.List;
034: import java.util.logging.Level;
035: import java.util.logging.Logger;
036: import javax.xml.xpath.XPath;
037: import javax.xml.xpath.XPathConstants;
038: import javax.xml.xpath.XPathExpression;
039: import javax.xml.xpath.XPathExpressionException;
040: import javax.xml.xpath.XPathFactory;
041: import org.w3c.dom.Document;
042: import org.w3c.dom.Element;
043: import org.w3c.dom.NodeList;
044:
045: /**
046: *
047: * @author Ashutosh.Shahi@sun.com
048: */
049: public class TargetResolverImpl implements TargetResolver {
050: private ProcessingContext ctx = null;
051: private FilterProcessingContext fpContext = null;
052: private static Logger log = Logger.getLogger(
053: LogDomainConstants.WSS_API_DOMAIN,
054: LogDomainConstants.WSS_API_DOMAIN_BUNDLE);
055:
056: /** Creates a new instance of TargetResolverImpl */
057: public TargetResolverImpl(ProcessingContext ctx) {
058: this .ctx = ctx;
059: }
060:
061: public void resolveAndVerifyTargets(List<Target> actualTargets,
062: List<Target> inferredTargets, WSSPolicy actualPolicy)
063: throws XWSSecurityException {
064:
065: String policyType = PolicyTypeUtil
066: .signaturePolicy(actualPolicy) ? "Signature"
067: : "Encryption";
068: boolean isEndorsing = false;
069:
070: if (PolicyTypeUtil.signaturePolicy(actualPolicy)) {
071: SignaturePolicy.FeatureBinding fp = (SignaturePolicy.FeatureBinding) actualPolicy
072: .getFeatureBinding();
073: if (fp.isEndorsingSignature()) {
074: isEndorsing = true;
075: }
076: }
077:
078: fpContext = new FilterProcessingContext(ctx);
079: SecurityHeader header = fpContext.getSecurableSoapMessage()
080: .findSecurityHeader();
081: Document doc = header.getOwnerDocument();
082:
083: for (Target actualTarget : actualTargets) {
084: boolean found = false;
085: String targetInPolicy = getTargetValue(doc, actualTarget);
086: for (Target inferredTarget : inferredTargets) {
087: String targetInMessage = getTargetValue(doc,
088: inferredTarget);
089: if (targetInPolicy != null
090: && targetInPolicy.equals(targetInMessage)) {
091: found = true;
092: break;
093: }
094: }
095: if (!found && targetInPolicy != null) {
096: //check if message has the target
097: //check if the message has the element
098: NodeList nl = doc.getElementsByTagName(targetInPolicy);
099: if (nl != null && nl.getLength() > 0) {
100: log.log(Level.SEVERE,
101: "WSS0206.policy.violation.exception");
102: log.log(Level.SEVERE, "Missing target : "
103: + targetInPolicy + " for " + policyType);
104: if (isEndorsing) {
105: throw new XWSSecurityException(
106: "Policy verification error:"
107: + "Missing target "
108: + targetInPolicy
109: + " for Endorsing "
110: + policyType);
111: } else {
112: throw new XWSSecurityException(
113: "Policy verification error:"
114: + "Missing target "
115: + targetInPolicy + " for "
116: + policyType);
117: }
118:
119: }
120: }
121: }
122: }
123:
124: private String getTargetValue(final Document doc,
125: final Target actualTarget) {
126: String targetInPolicy = null;
127: if (actualTarget.getType() == Target.TARGET_TYPE_VALUE_QNAME) {
128: targetInPolicy = actualTarget.getQName().getLocalPart();
129: } else if (actualTarget.getType() == Target.TARGET_TYPE_VALUE_URI) {
130: String val = actualTarget.getValue();
131: String id = null;
132: if (val.charAt(0) == '#')
133: id = val.substring(1, val.length());
134: else
135: id = val;
136: Element signedElement = doc.getElementById(id);
137: if (signedElement != null) {
138: targetInPolicy = signedElement.getLocalName();
139: }
140: }
141: return targetInPolicy;
142: }
143:
144: public boolean isTargetPresent(List<Target> actualTargets)
145: throws XWSSecurityException {
146: FilterProcessingContext fpContext = new FilterProcessingContext(
147: ctx);
148: SecurityHeader header = fpContext.getSecurableSoapMessage()
149: .findSecurityHeader();
150: Document doc = header.getOwnerDocument();
151: for (Target actualTarget : actualTargets) {
152: if (actualTarget.getType() == Target.TARGET_TYPE_VALUE_XPATH) {
153: String val = actualTarget.getValue();
154: try {
155: XPathFactory xpathFactory = XPathFactory
156: .newInstance();
157: XPath xpath = xpathFactory.newXPath();
158: xpath.setNamespaceContext(fpContext
159: .getSecurableSoapMessage()
160: .getNamespaceContext());
161: XPathExpression xpathExpr = xpath.compile(val);
162: NodeList nodes = (NodeList) xpathExpr.evaluate(
163: (Object) fpContext
164: .getSecurableSoapMessage()
165: .getSOAPPart(),
166: XPathConstants.NODESET);
167: if (nodes != null && nodes.getLength() > 0) {
168: return true;
169: }
170: } catch (XPathExpressionException xpe) {
171: throw new XWSSecurityException(xpe);
172: }
173: } else {
174: String targetInPolicy = getTargetValue(doc,
175: actualTarget);
176: NodeList nl = doc.getElementsByTagName(targetInPolicy);
177: if (nl != null && nl.getLength() > 0) {
178: return true;
179: }
180: }
181: }
182: return false;
183: }
184:
185: }
|