src/share/classes/com/sun/org/apache/xml/internal/security/transforms/implementations/TransformEnvelopedSignature.java

Print this page


   1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Copyright  1999-2004 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 package com.sun.org.apache.xml.internal.security.transforms.implementations;
  22 


  23 import com.sun.org.apache.xml.internal.security.signature.NodeFilter;
  24 import com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput;
  25 import com.sun.org.apache.xml.internal.security.transforms.Transform;
  26 import com.sun.org.apache.xml.internal.security.transforms.TransformSpi;
  27 import com.sun.org.apache.xml.internal.security.transforms.TransformationException;
  28 import com.sun.org.apache.xml.internal.security.transforms.Transforms;
  29 import com.sun.org.apache.xml.internal.security.utils.Constants;
  30 import com.sun.org.apache.xml.internal.security.utils.XMLUtils;
  31 import org.w3c.dom.Element;
  32 import org.w3c.dom.Node;
  33 
  34 /**
  35  * Implements the <CODE>http://www.w3.org/2000/09/xmldsig#enveloped-signature</CODE>
  36  * transform.
  37  *
  38  * @author Christian Geuer-Pollmann
  39  */
  40 public class TransformEnvelopedSignature extends TransformSpi {
  41 
  42    /** Field implementedTransformURI */
  43    public static final String implementedTransformURI =
  44       Transforms.TRANSFORM_ENVELOPED_SIGNATURE;
  45 
  46    /**
  47     * Method engineGetURI
  48     *
  49     * @inheritDoc
  50     */
  51    protected String engineGetURI() {
  52       return implementedTransformURI;
  53    }
  54 
  55    /**
  56     * @inheritDoc
  57     */
  58    protected XMLSignatureInput enginePerformTransform(XMLSignatureInput input, Transform _transformObject)
  59            throws TransformationException {
  60 
  61 
  62 
  63          /**
  64           * If the actual input is an octet stream, then the application MUST
  65           * convert the octet stream to an XPath node-set suitable for use by
  66           * Canonical XML with Comments. (A subsequent application of the
  67           * REQUIRED Canonical XML algorithm would strip away these comments.)
  68           *
  69           * ...
  70           *
  71           * The evaluation of this expression includes all of the document's nodes
  72           * (including comments) in the node-set representing the octet stream.
  73           */
  74 
  75          Node signatureElement = _transformObject.getElement();
  76 
  77 
  78          signatureElement = searchSignatureElement(signatureElement);
  79                 input.setExcludeNode(signatureElement);
  80                 input.addNodeFilter(new EnvelopedNodeFilter(signatureElement));
  81                 return input;
  82 
  83          //
  84 
  85 
  86    }
  87 
  88    /**
  89     * @param signatureElement
  90     * @return the node that is the signature
  91     * @throws TransformationException
  92     */
  93     private static Node searchSignatureElement(Node signatureElement) throws TransformationException {
  94             boolean found=false;

  95 
  96             while (true) {
  97                 if ((signatureElement == null)
  98                     || (signatureElement.getNodeType() == Node.DOCUMENT_NODE)) {
  99                         break;
 100                 }
 101                 Element el=(Element)signatureElement;
 102                 if (el.getNamespaceURI().equals(Constants.SignatureSpecNS)
 103                     &&
 104                        el.getLocalName().equals(Constants._TAG_SIGNATURE)) {
 105                         found = true;
 106                         break;
 107                 }
 108 
 109                 signatureElement = signatureElement.getParentNode();
 110             }
 111 
 112             if (!found) {
 113               throw new TransformationException(
 114                "envelopedSignatureTransformNotInSignatureElement");
 115             }
 116             return signatureElement;
 117     }

 118     static class EnvelopedNodeFilter implements NodeFilter {

 119         Node exclude;

 120         EnvelopedNodeFilter(Node n) {
 121             exclude=n;
 122         }

 123     public int isNodeIncludeDO(Node n, int level) {
 124         if ((n==exclude))
 125                         return -1;

 126         return 1;
 127     }

 128         /**
 129          * @see com.sun.org.apache.xml.internal.security.signature.NodeFilter#isNodeInclude(org.w3c.dom.Node)
 130          */
 131         public int isNodeInclude(Node n) {
 132                 if ((n==exclude) || XMLUtils.isDescendantOrSelf(exclude,n))
 133                         return -1;

 134                 return 1;
 135             //return !XMLUtils.isDescendantOrSelf(exclude,n);
 136         }
 137     }
 138 }
   1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /**
   6  * Licensed to the Apache Software Foundation (ASF) under one
   7  * or more contributor license agreements. See the NOTICE file
   8  * distributed with this work for additional information
   9  * regarding copyright ownership. The ASF licenses this file
  10  * to you under the Apache License, Version 2.0 (the
  11  * "License"); you may not use this file except in compliance
  12  * with the License. You may obtain a copy of the License at
  13  *
  14  * http://www.apache.org/licenses/LICENSE-2.0
  15  *
  16  * Unless required by applicable law or agreed to in writing,
  17  * software distributed under the License is distributed on an
  18  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  19  * KIND, either express or implied. See the License for the
  20  * specific language governing permissions and limitations
  21  * under the License.
  22  */
  23 package com.sun.org.apache.xml.internal.security.transforms.implementations;
  24 
  25 import java.io.OutputStream;
  26 
  27 import com.sun.org.apache.xml.internal.security.signature.NodeFilter;
  28 import com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput;
  29 import com.sun.org.apache.xml.internal.security.transforms.Transform;
  30 import com.sun.org.apache.xml.internal.security.transforms.TransformSpi;
  31 import com.sun.org.apache.xml.internal.security.transforms.TransformationException;
  32 import com.sun.org.apache.xml.internal.security.transforms.Transforms;
  33 import com.sun.org.apache.xml.internal.security.utils.Constants;
  34 import com.sun.org.apache.xml.internal.security.utils.XMLUtils;
  35 import org.w3c.dom.Element;
  36 import org.w3c.dom.Node;
  37 
  38 /**
  39  * Implements the <CODE>http://www.w3.org/2000/09/xmldsig#enveloped-signature</CODE>
  40  * transform.
  41  *
  42  * @author Christian Geuer-Pollmann
  43  */
  44 public class TransformEnvelopedSignature extends TransformSpi {
  45 
  46     /** Field implementedTransformURI */
  47     public static final String implementedTransformURI =
  48         Transforms.TRANSFORM_ENVELOPED_SIGNATURE;
  49 
  50     /**
  51      * Method engineGetURI
  52      *
  53      * @inheritDoc
  54      */
  55     protected String engineGetURI() {
  56         return implementedTransformURI;
  57     }
  58 
  59     /**
  60      * @inheritDoc
  61      */
  62     protected XMLSignatureInput enginePerformTransform(
  63         XMLSignatureInput input, OutputStream os, Transform transformObject
  64     ) throws TransformationException {


  65         /**
  66          * If the actual input is an octet stream, then the application MUST
  67          * convert the octet stream to an XPath node-set suitable for use by
  68          * Canonical XML with Comments. (A subsequent application of the
  69          * REQUIRED Canonical XML algorithm would strip away these comments.)
  70          *
  71          * ...
  72          *
  73          * The evaluation of this expression includes all of the document's nodes
  74          * (including comments) in the node-set representing the octet stream.
  75          */
  76 
  77         Node signatureElement = transformObject.getElement();

  78 
  79         signatureElement = searchSignatureElement(signatureElement);        
  80         input.setExcludeNode(signatureElement);   
  81         input.addNodeFilter(new EnvelopedNodeFilter(signatureElement));
  82         return input;




  83     }
  84 
  85     /**
  86      * @param signatureElement    
  87      * @return the node that is the signature
  88      * @throws TransformationException
  89      */
  90     private static Node searchSignatureElement(Node signatureElement) 
  91         throws TransformationException {
  92         boolean found = false;
  93 
  94         while (true) {
  95             if (signatureElement == null
  96                 || signatureElement.getNodeType() == Node.DOCUMENT_NODE) {
  97                 break;
  98             }
  99             Element el = (Element) signatureElement;
 100             if (el.getNamespaceURI().equals(Constants.SignatureSpecNS)
 101                 && el.getLocalName().equals(Constants._TAG_SIGNATURE)) {

 102                 found = true;
 103                 break;
 104             }
 105 
 106             signatureElement = signatureElement.getParentNode();
 107         }
 108 
 109         if (!found) {
 110             throw new TransformationException(
 111                 "transform.envelopedSignatureTransformNotInSignatureElement");
 112         }
 113         return signatureElement;
 114     }
 115 
 116     static class EnvelopedNodeFilter implements NodeFilter {
 117         
 118         Node exclude;
 119         
 120         EnvelopedNodeFilter(Node n) {
 121             exclude = n;
 122         }
 123         
 124         public int isNodeIncludeDO(Node n, int level) {
 125             if (n == exclude) {
 126                 return -1;
 127             }
 128             return 1;
 129         }
 130         
 131         /**
 132          * @see com.sun.org.apache.xml.internal.security.signature.NodeFilter#isNodeInclude(org.w3c.dom.Node)
 133          */
 134         public int isNodeInclude(Node n) {
 135             if (n == exclude || XMLUtils.isDescendantOrSelf(exclude, n)) { 
 136                 return -1;
 137             }
 138             return 1;
 139             //return !XMLUtils.isDescendantOrSelf(exclude,n);
 140         }
 141     }
 142 }