| 
 | 
 | 
 | 
 | 
 | 
 | 
 */  | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 */  | 
 | 
package com.sun.org.apache.xml.internal.security.transforms;  | 
 | 
 | 
 | 
import java.io.IOException;  | 
 | 
import java.io.OutputStream;  | 
 | 
 | 
 | 
import com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException;  | 
 | 
import com.sun.org.apache.xml.internal.security.c14n.Canonicalizer;  | 
 | 
import com.sun.org.apache.xml.internal.security.c14n.InvalidCanonicalizerException;  | 
 | 
import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException;  | 
 | 
import com.sun.org.apache.xml.internal.security.signature.XMLSignatureException;  | 
 | 
import com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput;  | 
 | 
import com.sun.org.apache.xml.internal.security.utils.Constants;  | 
 | 
import com.sun.org.apache.xml.internal.security.utils.SignatureElementProxy;  | 
 | 
import com.sun.org.apache.xml.internal.security.utils.XMLUtils;  | 
 | 
import org.w3c.dom.DOMException;  | 
 | 
import org.w3c.dom.Document;  | 
 | 
import org.w3c.dom.Element;  | 
 | 
import org.w3c.dom.NodeList;  | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 */  | 
 | 
public class Transforms extends SignatureElementProxy { | 
 | 
 | 
 | 
      | 
 | 
    public static final String TRANSFORM_C14N_OMIT_COMMENTS  | 
 | 
        = Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS;  | 
 | 
 | 
 | 
      | 
 | 
    public static final String TRANSFORM_C14N_WITH_COMMENTS  | 
 | 
        = Canonicalizer.ALGO_ID_C14N_WITH_COMMENTS;  | 
 | 
 | 
 | 
      | 
 | 
    public static final String TRANSFORM_C14N11_OMIT_COMMENTS  | 
 | 
        = Canonicalizer.ALGO_ID_C14N11_OMIT_COMMENTS;  | 
 | 
 | 
 | 
      | 
 | 
    public static final String TRANSFORM_C14N11_WITH_COMMENTS  | 
 | 
        = Canonicalizer.ALGO_ID_C14N11_WITH_COMMENTS;  | 
 | 
 | 
 | 
      | 
 | 
    public static final String TRANSFORM_C14N_EXCL_OMIT_COMMENTS  | 
 | 
        = Canonicalizer.ALGO_ID_C14N_EXCL_OMIT_COMMENTS;  | 
 | 
 | 
 | 
      | 
 | 
    public static final String TRANSFORM_C14N_EXCL_WITH_COMMENTS  | 
 | 
        = Canonicalizer.ALGO_ID_C14N_EXCL_WITH_COMMENTS;  | 
 | 
 | 
 | 
      | 
 | 
    public static final String TRANSFORM_XSLT  | 
 | 
        = "http://www.w3.org/TR/1999/REC-xslt-19991116";  | 
 | 
 | 
 | 
      | 
 | 
    public static final String TRANSFORM_BASE64_DECODE  | 
 | 
        = Constants.SignatureSpecNS + "base64";  | 
 | 
 | 
 | 
      | 
 | 
    public static final String TRANSFORM_XPATH  | 
 | 
        = "http://www.w3.org/TR/1999/REC-xpath-19991116";  | 
 | 
 | 
 | 
      | 
 | 
    public static final String TRANSFORM_ENVELOPED_SIGNATURE  | 
 | 
        = Constants.SignatureSpecNS + "enveloped-signature";  | 
 | 
 | 
 | 
      | 
 | 
    public static final String TRANSFORM_XPOINTER  | 
 | 
        = "http://www.w3.org/TR/2001/WD-xptr-20010108";  | 
 | 
 | 
 | 
      | 
 | 
    public static final String TRANSFORM_XPATH2FILTER  | 
 | 
        = "http://www.w3.org/2002/06/xmldsig-filter2";  | 
 | 
 | 
 | 
    private static final com.sun.org.slf4j.internal.Logger LOG =  | 
 | 
        com.sun.org.slf4j.internal.LoggerFactory.getLogger(Transforms.class);  | 
 | 
 | 
 | 
    private Element[] transforms;  | 
 | 
 | 
 | 
    protected Transforms() { } | 
 | 
 | 
 | 
    private boolean secureValidation;  | 
 | 
 | 
 | 
      | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
     */  | 
 | 
    public Transforms(Document doc) { | 
 | 
        super(doc);  | 
 | 
        addReturnToSelf();  | 
 | 
    }  | 
 | 
 | 
 | 
      | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
     */  | 
 | 
    public Transforms(Element element, String baseURI)  | 
 | 
        throws DOMException, XMLSignatureException, InvalidTransformException,  | 
 | 
            TransformationException, XMLSecurityException { | 
 | 
        super(element, baseURI);  | 
 | 
 | 
 | 
        int numberOfTransformElems = this.getLength();  | 
 | 
 | 
 | 
        if (numberOfTransformElems == 0) { | 
 | 
              | 
 | 
            Object exArgs[] = { Constants._TAG_TRANSFORM, Constants._TAG_TRANSFORMS }; | 
 | 
 | 
 | 
            throw new TransformationException("xml.WrongContent", exArgs); | 
 | 
        }  | 
 | 
    }  | 
 | 
 | 
 | 
      | 
 | 
 | 
 | 
     */  | 
 | 
    public void setSecureValidation(boolean secureValidation) { | 
 | 
        this.secureValidation = secureValidation;  | 
 | 
    }  | 
 | 
 | 
 | 
      | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
     */  | 
 | 
    public void addTransform(String transformURI) throws TransformationException { | 
 | 
        try { | 
 | 
            LOG.debug("Transforms.addTransform({})", transformURI); | 
 | 
 | 
 | 
            Transform transform = new Transform(getDocument(), transformURI);  | 
 | 
 | 
 | 
            this.addTransform(transform);  | 
 | 
        } catch (InvalidTransformException ex) { | 
 | 
            throw new TransformationException(ex);  | 
 | 
        }  | 
 | 
    }  | 
 | 
 | 
 | 
      | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
     */  | 
 | 
    public void addTransform(String transformURI, Element contextElement)  | 
 | 
       throws TransformationException { | 
 | 
        try { | 
 | 
            LOG.debug("Transforms.addTransform({})", transformURI); | 
 | 
 | 
 | 
            Transform transform = new Transform(getDocument(), transformURI, contextElement);  | 
 | 
 | 
 | 
            this.addTransform(transform);  | 
 | 
        } catch (InvalidTransformException ex) { | 
 | 
            throw new TransformationException(ex);  | 
 | 
        }  | 
 | 
    }  | 
 | 
 | 
 | 
      | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
     */  | 
 | 
    public void addTransform(String transformURI, NodeList contextNodes)  | 
 | 
       throws TransformationException { | 
 | 
 | 
 | 
        try { | 
 | 
            Transform transform = new Transform(getDocument(), transformURI, contextNodes);  | 
 | 
            this.addTransform(transform);  | 
 | 
        } catch (InvalidTransformException ex) { | 
 | 
            throw new TransformationException(ex);  | 
 | 
        }  | 
 | 
    }  | 
 | 
 | 
 | 
      | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
     */  | 
 | 
    private void addTransform(Transform transform) { | 
 | 
        LOG.debug("Transforms.addTransform({})", transform.getURI()); | 
 | 
 | 
 | 
        Element transformElement = transform.getElement();  | 
 | 
 | 
 | 
        appendSelf(transformElement);  | 
 | 
        addReturnToSelf();  | 
 | 
    }  | 
 | 
 | 
 | 
      | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
     */  | 
 | 
    public XMLSignatureInput performTransforms(  | 
 | 
        XMLSignatureInput xmlSignatureInput  | 
 | 
    ) throws TransformationException { | 
 | 
        return performTransforms(xmlSignatureInput, null);  | 
 | 
    }  | 
 | 
 | 
 | 
      | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
     */  | 
 | 
    public XMLSignatureInput performTransforms(  | 
 | 
        XMLSignatureInput xmlSignatureInput, OutputStream os  | 
 | 
    ) throws TransformationException { | 
 | 
        try { | 
 | 
            int last = this.getLength() - 1;  | 
 | 
            for (int i = 0; i < last; i++) { | 
 | 
                Transform t = this.item(i);  | 
 | 
                LOG.debug("Perform the ({})th {} transform", i, t.getURI()); | 
 | 
                checkSecureValidation(t);  | 
 | 
                xmlSignatureInput = t.performTransform(xmlSignatureInput);  | 
 | 
            }  | 
 | 
            if (last >= 0) { | 
 | 
                Transform t = this.item(last);  | 
 | 
                LOG.debug("Perform the ({})th {} transform", last, t.getURI()); | 
 | 
                checkSecureValidation(t);  | 
 | 
                xmlSignatureInput = t.performTransform(xmlSignatureInput, os);  | 
 | 
            }  | 
 | 
 | 
 | 
            return xmlSignatureInput;  | 
 | 
        } catch (IOException ex) { | 
 | 
            throw new TransformationException(ex);  | 
 | 
        } catch (CanonicalizationException ex) { | 
 | 
            throw new TransformationException(ex);  | 
 | 
        } catch (InvalidCanonicalizerException ex) { | 
 | 
            throw new TransformationException(ex);  | 
 | 
        }  | 
 | 
    }  | 
 | 
 | 
 | 
    private void checkSecureValidation(Transform transform) throws TransformationException { | 
 | 
        String uri = transform.getURI();  | 
 | 
        if (secureValidation && Transforms.TRANSFORM_XSLT.equals(uri)) { | 
 | 
            Object exArgs[] = { uri }; | 
 | 
 | 
 | 
            throw new TransformationException(  | 
 | 
                "signature.Transform.ForbiddenTransform", exArgs  | 
 | 
            );  | 
 | 
        }  | 
 | 
        transform.setSecureValidation(secureValidation);  | 
 | 
    }  | 
 | 
 | 
 | 
      | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
     */  | 
 | 
    public int getLength() { | 
 | 
        initTransforms();  | 
 | 
        return transforms.length;  | 
 | 
    }  | 
 | 
 | 
 | 
      | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
 | 
     */  | 
 | 
    public Transform item(int i) throws TransformationException { | 
 | 
        try { | 
 | 
            initTransforms();  | 
 | 
            return new Transform(transforms[i], this.baseURI);  | 
 | 
        } catch (XMLSecurityException ex) { | 
 | 
            throw new TransformationException(ex);  | 
 | 
        }  | 
 | 
    }  | 
 | 
 | 
 | 
    private void initTransforms() { | 
 | 
        if (transforms == null) { | 
 | 
            transforms = XMLUtils.selectDsNodes(getFirstChild(), "Transform");  | 
 | 
        }  | 
 | 
    }  | 
 | 
 | 
 | 
      | 
 | 
    public String getBaseLocalName() { | 
 | 
        return Constants._TAG_TRANSFORMS;  | 
 | 
    }  | 
 | 
}  |