XMLSecAttachmentTextXMLContentTransform.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */
  20. package org.openspcoop2.security.message.soapbox.signature;

  21. import java.io.ByteArrayOutputStream;
  22. import java.io.OutputStream;

  23. import org.apache.xml.security.c14n.CanonicalizationException;
  24. import org.apache.xml.security.c14n.implementations.Canonicalizer20010315ExclOmitComments;
  25. import org.apache.xml.security.signature.XMLSignatureInput;
  26. import org.apache.xml.security.transforms.implementations.TransformC14NExclusive;
  27. import org.apache.xml.security.transforms.params.InclusiveNamespaces;
  28. import org.apache.xml.security.utils.XMLUtils;
  29. import org.w3c.dom.Element;
  30. import org.w3c.dom.Node;

  31. /**
  32.  * XMLSecAttachmentTextXMLContentTransform
  33.  *
  34.  * @author Andrea Poli (apoli@link.it)
  35.  * @author Tommaso Burlon (tommaso.burlon@link.it)
  36.  * @author $Author$
  37.  * @version $Rev$, $Date$
  38.  */
  39. public class XMLSecAttachmentTextXMLContentTransform extends TransformC14NExclusive {

  40.     @Override
  41.     protected XMLSignatureInput enginePerformTransform(XMLSignatureInput xmlsignatureinput,
  42.             OutputStream outputstream, Element transformElement, String baseURI, boolean secureValidation) throws CanonicalizationException {
  43.        
  44.         try{        
  45.             XMLSignatureInput xmlsignatureinput1 = null;
  46.             String s = null;
  47.             if(length("http://www.w3.org/2001/10/xml-exc-c14n#", "InclusiveNamespaces", transformElement) == 1)
  48.             {
  49.                 Element element = XMLUtils.selectNode(transformElement.getFirstChild(),
  50.                         "http://www.w3.org/2001/10/xml-exc-c14n#", "InclusiveNamespaces", 0);
  51.                 s = (new InclusiveNamespaces(element, baseURI)).getInclusiveNamespaces();
  52.             }
  53.             Canonicalizer20010315ExclOmitComments canonicalizer20010315exclomitcomments =
  54.                     new Canonicalizer20010315ExclOmitComments();
  55.             xmlsignatureinput.setNeedsToBeExpanded(true);
  56.             ByteArrayOutputStream bout = new ByteArrayOutputStream();
  57.             canonicalizer20010315exclomitcomments.engineCanonicalize(xmlsignatureinput, s, bout, secureValidation);
  58.             bout.flush();
  59.             bout.close();
  60.             byte[] abyte0 = bout.toByteArray();
  61.             xmlsignatureinput1 = new XMLSignatureInput(abyte0);
  62.             if(outputstream != null) {
  63.                 outputstream.write(abyte0);
  64.                 xmlsignatureinput1.setOutputStream(outputstream);
  65.             }
  66.             return xmlsignatureinput1;
  67.         }catch(Exception e){
  68.             throw new CanonicalizationException(e);
  69.         }
  70.     }
  71.    
  72.     private int length(String namespace, String localname, Element transformElement) {
  73.         int number = 0;
  74.         Node sibling = transformElement.getFirstChild();
  75.         while (sibling != null) {        
  76.             if (localname.equals(sibling.getLocalName())
  77.                 && namespace.equals(sibling.getNamespaceURI())) {
  78.                 number++;
  79.             }
  80.             sibling = sibling.getNextSibling();
  81.         }
  82.         return number;
  83.     }
  84. }