src/java.xml.crypto/share/classes/org/jcp/xml/dsig/internal/dom/DOMExcC14NMethod.java
changeset 50614 3810c9a2efa1
parent 47216 71c04702a3d5
child 53998 d870bb08194a
equal deleted inserted replaced
50613:0f93a75b9213 50614:3810c9a2efa1
    19  * KIND, either express or implied. See the License for the
    19  * KIND, either express or implied. See the License for the
    20  * specific language governing permissions and limitations
    20  * specific language governing permissions and limitations
    21  * under the License.
    21  * under the License.
    22  */
    22  */
    23 /*
    23 /*
    24  * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.
    24  * Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
    25  */
    25  */
    26 /*
    26 /*
    27  * $Id: DOMExcC14NMethod.java 1197150 2011-11-03 14:34:57Z coheigea $
    27  * $Id: DOMExcC14NMethod.java 1788465 2017-03-24 15:10:51Z coheigea $
    28  */
    28  */
    29 package org.jcp.xml.dsig.internal.dom;
    29 package org.jcp.xml.dsig.internal.dom;
    30 
    30 
    31 import javax.xml.crypto.*;
    31 import javax.xml.crypto.*;
    32 import javax.xml.crypto.dsig.*;
    32 import javax.xml.crypto.dsig.*;
    35 import javax.xml.crypto.dsig.spec.TransformParameterSpec;
    35 import javax.xml.crypto.dsig.spec.TransformParameterSpec;
    36 
    36 
    37 import java.security.InvalidAlgorithmParameterException;
    37 import java.security.InvalidAlgorithmParameterException;
    38 import java.security.spec.AlgorithmParameterSpec;
    38 import java.security.spec.AlgorithmParameterSpec;
    39 import java.util.*;
    39 import java.util.*;
       
    40 
    40 import org.w3c.dom.Element;
    41 import org.w3c.dom.Element;
    41 
       
    42 import com.sun.org.apache.xml.internal.security.c14n.Canonicalizer;
    42 import com.sun.org.apache.xml.internal.security.c14n.Canonicalizer;
    43 import com.sun.org.apache.xml.internal.security.c14n.InvalidCanonicalizerException;
    43 import com.sun.org.apache.xml.internal.security.c14n.InvalidCanonicalizerException;
    44 
    44 
    45 /**
    45 /**
    46  * DOM-based implementation of CanonicalizationMethod for Exclusive
    46  * DOM-based implementation of CanonicalizationMethod for Exclusive
    47  * Canonical XML algorithm (with or without comments).
    47  * Canonical XML algorithm (with or without comments).
    48  * Uses Apache XML-Sec Canonicalizer.
    48  * Uses Apache XML-Sec Canonicalizer.
    49  *
    49  *
    50  * @author Sean Mullan
       
    51  */
    50  */
    52 public final class DOMExcC14NMethod extends ApacheCanonicalizer {
    51 public final class DOMExcC14NMethod extends ApacheCanonicalizer {
    53 
    52 
       
    53     @Override
    54     public void init(TransformParameterSpec params)
    54     public void init(TransformParameterSpec params)
    55         throws InvalidAlgorithmParameterException
    55         throws InvalidAlgorithmParameterException
    56     {
    56     {
    57         if (params != null) {
    57         if (params != null) {
    58             if (!(params instanceof ExcC14NParameterSpec)) {
    58             if (!(params instanceof ExcC14NParameterSpec)) {
    61             }
    61             }
    62             this.params = (C14NMethodParameterSpec)params;
    62             this.params = (C14NMethodParameterSpec)params;
    63         }
    63         }
    64     }
    64     }
    65 
    65 
       
    66     @Override
    66     public void init(XMLStructure parent, XMLCryptoContext context)
    67     public void init(XMLStructure parent, XMLCryptoContext context)
    67         throws InvalidAlgorithmParameterException
    68         throws InvalidAlgorithmParameterException
    68     {
    69     {
    69         super.init(parent, context);
    70         super.init(parent, context);
    70         Element paramsElem = DOMUtils.getFirstChildElement(transformElem);
    71         Element paramsElem = DOMUtils.getFirstChildElement(transformElem);
    79     private void unmarshalParams(Element paramsElem) {
    80     private void unmarshalParams(Element paramsElem) {
    80         String prefixListAttr = paramsElem.getAttributeNS(null, "PrefixList");
    81         String prefixListAttr = paramsElem.getAttributeNS(null, "PrefixList");
    81         this.inclusiveNamespaces = prefixListAttr;
    82         this.inclusiveNamespaces = prefixListAttr;
    82         int begin = 0;
    83         int begin = 0;
    83         int end = prefixListAttr.indexOf(' ');
    84         int end = prefixListAttr.indexOf(' ');
    84         List<String> prefixList = new ArrayList<String>();
    85         List<String> prefixList = new ArrayList<>();
    85         while (end != -1) {
    86         while (end != -1) {
    86             prefixList.add(prefixListAttr.substring(begin, end));
    87             prefixList.add(prefixListAttr.substring(begin, end));
    87             begin = end + 1;
    88             begin = end + 1;
    88             end = prefixListAttr.indexOf(' ', begin);
    89             end = prefixListAttr.indexOf(' ', begin);
    89         }
    90         }
    91             prefixList.add(prefixListAttr.substring(begin));
    92             prefixList.add(prefixListAttr.substring(begin));
    92         }
    93         }
    93         this.params = new ExcC14NParameterSpec(prefixList);
    94         this.params = new ExcC14NParameterSpec(prefixList);
    94     }
    95     }
    95 
    96 
       
    97     @SuppressWarnings("unchecked")
       
    98     public List<String> getParameterSpecPrefixList(ExcC14NParameterSpec paramSpec) {
       
    99         return paramSpec.getPrefixList();
       
   100     }
       
   101 
       
   102     @Override
    96     public void marshalParams(XMLStructure parent, XMLCryptoContext context)
   103     public void marshalParams(XMLStructure parent, XMLCryptoContext context)
    97         throws MarshalException
   104         throws MarshalException
    98     {
   105     {
    99         super.marshalParams(parent, context);
   106         super.marshalParams(parent, context);
   100         AlgorithmParameterSpec spec = getParameterSpec();
   107         AlgorithmParameterSpec spec = getParameterSpec();
   101         if (spec == null) {
   108         if (spec == null) {
   102             return;
   109             return;
   103         }
   110         }
   104 
   111 
   105         String prefix = DOMUtils.getNSPrefix(context,
   112         XmlWriterToTree xwriter = new XmlWriterToTree(Marshaller.getMarshallers(), transformElem);
   106                                              CanonicalizationMethod.EXCLUSIVE);
   113 
   107         Element eElem = DOMUtils.createElement(ownerDoc,
   114         String prefix =
   108                                                "InclusiveNamespaces",
   115             DOMUtils.getNSPrefix(context, CanonicalizationMethod.EXCLUSIVE);
   109                                                CanonicalizationMethod.EXCLUSIVE,
   116         xwriter.writeStartElement(prefix, "InclusiveNamespaces", CanonicalizationMethod.EXCLUSIVE);
   110                                                prefix);
   117         xwriter.writeNamespace(prefix, CanonicalizationMethod.EXCLUSIVE);
   111         if (prefix == null || prefix.length() == 0) {
       
   112             eElem.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns",
       
   113                                  CanonicalizationMethod.EXCLUSIVE);
       
   114         } else {
       
   115             eElem.setAttributeNS("http://www.w3.org/2000/xmlns/",
       
   116                                    "xmlns:" + prefix,
       
   117                                    CanonicalizationMethod.EXCLUSIVE);
       
   118         }
       
   119 
   118 
   120         ExcC14NParameterSpec params = (ExcC14NParameterSpec)spec;
   119         ExcC14NParameterSpec params = (ExcC14NParameterSpec)spec;
   121         StringBuilder prefixListAttr = new StringBuilder("");
   120         StringBuilder prefixListAttr = new StringBuilder("");
   122         List<String> prefixList = params.getPrefixList();
   121         List<String> prefixList = getParameterSpecPrefixList(params);
   123         for (int i = 0, size = prefixList.size(); i < size; i++) {
   122         for (int i = 0, size = prefixList.size(); i < size; i++) {
   124             prefixListAttr.append(prefixList.get(i));
   123             prefixListAttr.append(prefixList.get(i));
   125             if (i < size - 1) {
   124             if (i < size - 1) {
   126                 prefixListAttr.append(" ");
   125                 prefixListAttr.append(" ");
   127             }
   126             }
   128         }
   127         }
   129         DOMUtils.setAttribute(eElem, "PrefixList", prefixListAttr.toString());
   128         xwriter.writeAttribute("", "", "PrefixList", prefixListAttr.toString());
   130         this.inclusiveNamespaces = prefixListAttr.toString();
   129         this.inclusiveNamespaces = prefixListAttr.toString();
   131         transformElem.appendChild(eElem);
   130         xwriter.writeEndElement(); // "InclusiveNamespaces"
   132     }
   131     }
   133 
   132 
   134     public String getParamsNSURI() {
   133     public String getParamsNSURI() {
   135         return CanonicalizationMethod.EXCLUSIVE;
   134         return CanonicalizationMethod.EXCLUSIVE;
   136     }
   135     }
   137 
   136 
       
   137     @Override
   138     public Data transform(Data data, XMLCryptoContext xc)
   138     public Data transform(Data data, XMLCryptoContext xc)
   139         throws TransformException
   139         throws TransformException
   140     {
   140     {
   141         // ignore comments if dereferencing same-document URI that require
   141         // ignore comments if dereferencing same-document URI that require
   142         // you to omit comments, even if the Transform says otherwise -
   142         // you to omit comments, even if the Transform says otherwise -
   145             DOMSubTreeData subTree = (DOMSubTreeData)data;
   145             DOMSubTreeData subTree = (DOMSubTreeData)data;
   146             if (subTree.excludeComments()) {
   146             if (subTree.excludeComments()) {
   147                 try {
   147                 try {
   148                     apacheCanonicalizer = Canonicalizer.getInstance
   148                     apacheCanonicalizer = Canonicalizer.getInstance
   149                         (CanonicalizationMethod.EXCLUSIVE);
   149                         (CanonicalizationMethod.EXCLUSIVE);
       
   150                     boolean secVal = Utils.secureValidation(xc);
       
   151                     apacheCanonicalizer.setSecureValidation(secVal);
   150                 } catch (InvalidCanonicalizerException ice) {
   152                 } catch (InvalidCanonicalizerException ice) {
   151                     throw new TransformException
   153                     throw new TransformException
   152                         ("Couldn't find Canonicalizer for: " +
   154                         ("Couldn't find Canonicalizer for: " +
   153                          CanonicalizationMethod.EXCLUSIVE + ": " +
   155                          CanonicalizationMethod.EXCLUSIVE + ": " +
   154                          ice.getMessage(), ice);
   156                          ice.getMessage(), ice);