XMLSecAttachmentContentTransform.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.IOException;
  22. import java.io.OutputStream;

  23. import javax.xml.parsers.ParserConfigurationException;

  24. import org.apache.xml.security.c14n.CanonicalizationException;
  25. import org.apache.xml.security.c14n.InvalidCanonicalizerException;
  26. import org.apache.xml.security.signature.XMLSignatureInput;
  27. import org.apache.xml.security.transforms.TransformSpi;
  28. import org.apache.xml.security.transforms.TransformationException;
  29. import org.bouncycastle.util.encoders.Base64;
  30. import org.openspcoop2.message.xml.MessageXMLUtils;
  31. import org.openspcoop2.security.message.constants.WSSAttachmentsConstants;
  32. import org.openspcoop2.utils.xml.AbstractXMLUtils;
  33. import org.openspcoop2.utils.xml.XMLException;
  34. import org.w3c.dom.Element;
  35. import org.xml.sax.SAXException;

  36. import com.sun.xml.wss.impl.c14n.Canonicalizer;
  37. import com.sun.xml.wss.impl.c14n.CanonicalizerFactory;

  38. /**
  39.  * XMLSecAttachmentContentTransform
  40.  *
  41.  * @author Andrea Poli (apoli@link.it)
  42.  * @author $Author$
  43.  * @version $Rev$, $Date$
  44.  */
  45. public class XMLSecAttachmentContentTransform extends TransformSpi {

  46.     private AbstractXMLUtils xmlUtils = null;
  47.    
  48.     public XMLSecAttachmentContentTransform(){
  49.         this.xmlUtils = MessageXMLUtils.DEFAULT;
  50.     }
  51.    
  52.     @Override
  53.     protected String engineGetURI() {
  54.         return WSSAttachmentsConstants.ATTACHMENT_CONTENT_SIGNATURE_TRANSFORM_URI;
  55.     }
  56.    
  57.    
  58.     // con jar: metro2.2-webservices_xwss_com_sun_org_jdk1.5.jar

  59.     @Override
  60.     protected XMLSignatureInput enginePerformTransform(org.apache.xml.security.signature.XMLSignatureInput xmlSignatureInput,
  61.             OutputStream out,
  62.             Element transformElement, String baseURI, boolean secureValidation) throws IOException, CanonicalizationException, InvalidCanonicalizerException,
  63.             TransformationException, ParserConfigurationException, SAXException {

  64.         //System.out.println("XMLSignatureInput METRO 1");
  65.         if("text/xml".equals(xmlSignatureInput.getMIMEType())){
  66.            
  67.             //System.out.println("XMLContent METRO 1");
  68.             XMLSecAttachmentTextXMLContentTransform t =
  69.                     new XMLSecAttachmentTextXMLContentTransform();
  70.             try {
  71.                 return t.enginePerformTransform(getTextXMLSignatureInput(xmlSignatureInput), out, transformElement, baseURI, secureValidation);
  72.             } catch (XMLException e) {
  73.                 throw new SAXException(e.getMessage(),e);
  74.             }
  75.         }
  76.         else{
  77.            
  78.             //System.out.println("Base64Content METRO 1");
  79.             XMLSecAttachmentBase64ContentTransform t =
  80.                     new XMLSecAttachmentBase64ContentTransform();
  81.             try {
  82.                 return t.enginePerformTransform(getBase64SignatureInput(xmlSignatureInput), out, transformElement, baseURI, secureValidation);
  83.             } catch (Exception e) {
  84.                 throw new SAXException(e.getMessage(),e);
  85.             }
  86.         }
  87.     }

  88.     private XMLSignatureInput getTextXMLSignatureInput(XMLSignatureInput input) throws CanonicalizationException, IOException, SAXException, ParserConfigurationException, XMLException{
  89.         Element signElement = this.xmlUtils.newElement(input.getBytes());
  90.         XMLSignatureInput result = new XMLSignatureInput(signElement);
  91.         result.setMIMEType(input.getMIMEType());
  92.         return result;
  93.     }
  94.    
  95.     private XMLSignatureInput getBase64SignatureInput(XMLSignatureInput input) throws Exception{
  96.         Canonicalizer canonicalizer = CanonicalizerFactory.getCanonicalizer(input.getMIMEType());
  97.         byte[] canonicalize = canonicalizer.canonicalize(input.getBytes());
  98.         XMLSignatureInput result = new XMLSignatureInput(Base64.encode(canonicalize));
  99.         result.setMIMEType(input.getMIMEType());
  100.         return result;
  101.     }

  102. }