MessageSecurityContext_soapbox.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;

  21. import org.adroitlogic.soapbox.CryptoSupport;
  22. import org.slf4j.Logger;
  23. import org.apache.xml.security.exceptions.AlgorithmAlreadyRegisteredException;
  24. import org.apache.xml.security.transforms.Transform;
  25. import org.openspcoop2.security.SecurityException;
  26. import org.openspcoop2.security.message.IMessageSecurityContext;
  27. import org.openspcoop2.security.message.MessageSecurityContext;
  28. import org.openspcoop2.security.message.constants.WSSAttachmentsConstants;
  29. import org.openspcoop2.security.message.soapbox.signature.XMLSecAttachmentContentTransform;

  30. /**
  31.  * WSSContext_soapbox
  32.  *
  33.  * @author Andrea Poli (apoli@link.it)
  34.  * @author Giovanni Bussu (bussu@link.it)
  35.  * @author $Author$
  36.  * @version $Rev$, $Date$
  37.  */
  38. public class MessageSecurityContext_soapbox implements IMessageSecurityContext{

  39.     private static boolean initialized = false;
  40.     private static synchronized void init_engine(MessageSecurityContext wssContext)throws SecurityException {
  41.         if(MessageSecurityContext_soapbox.initialized==false){
  42.            
  43.             try{        
  44.                 MessageSecurityContext_soapbox.initWsuIdAllocator(wssContext.getPrefixWsuId(),wssContext.getLog());
  45.             }catch(Throwable e){
  46.                 wssContext.getLog().error("Inizializzazione wsu id allocator non riuscita: "+e.getMessage(),e);
  47.                 throw new SecurityException(e.getMessage(),e);
  48.             }
  49.            
  50.             try{        
  51.                 CryptoSupport.initializeInstance(100, 100, 100);
  52.             }catch(Throwable e){
  53.                 wssContext.getLog().error("Inizializzazione CryptoSupport non riuscita: "+e.getMessage(),e);
  54.                 throw new SecurityException(e.getMessage(),e);
  55.             }
  56.                
  57. //          try{
  58. //              org.apache.xml.security.Init.init();
  59. //          }catch(Throwable e){
  60. //              wssContext.getLog().error("Inizializzazione org.apache.xml.security non riuscita: "+e.getMessage(),e);
  61. //              throw new SecurityException(e.getMessage(),e);
  62. //          }
  63.            
  64.             // Li registro entrambi in modo da poter switchare tra le implementazioni
  65.             try{
  66.                 try{
  67.                     wssContext.getLog().info("Transformer registrato: ["+XMLSecAttachmentContentTransform.class.getName()+"]");
  68.                     Transform.register(WSSAttachmentsConstants.ATTACHMENT_CONTENT_SIGNATURE_TRANSFORM_URI, XMLSecAttachmentContentTransform.class.getName());
  69.                 }catch(AlgorithmAlreadyRegisteredException e){
  70.                     // Succede in caso di hot redeploy
  71.                     wssContext.getLog().debug("Registrazione org.apache.xml.security.transforms.Transform non riuscita, classe gia registrata: "+e.getMessage());
  72.                 }
  73.                 catch(Throwable e){
  74.                     wssContext.getLog().error("Registrazione org.apache.xml.security.transforms.Transform non riuscita: "+e.getMessage(),e);
  75.                     throw new SecurityException(e.getMessage(),e);
  76.                 }
  77.             }
  78.             catch(SecurityException e){
  79.                 throw e;
  80.             }
  81.             catch(Throwable e){
  82.                 wssContext.getLog().error("Inizializzazione Trasform Security non riuscita: "+e.getMessage(),e);
  83.                 throw new SecurityException(e.getMessage(),e);
  84.             }
  85.            
  86.            
  87.             MessageSecurityContext_soapbox.initialized = true;
  88.         }
  89.     }
  90.    
  91.     @Override
  92.     public void init(MessageSecurityContext wssContext) throws SecurityException {

  93.         if(MessageSecurityContext_soapbox.initialized==false){
  94.             MessageSecurityContext_soapbox.init_engine(wssContext);
  95.         }
  96.        
  97.     }


  98.     /** WSS Id Allocator */
  99.     private static org.apache.wss4j.dom.WsuIdAllocator wsuIdAllocator = null;
  100.     private static String prefixWsuId = null;
  101.     public static org.apache.wss4j.dom.WsuIdAllocator getWsuIdAllocator() {
  102.         return MessageSecurityContext_soapbox.wsuIdAllocator;
  103.     }
  104.     private static synchronized void initWsuIdAllocator(String prefixWsuIdParam,Logger log) throws Exception{
  105.         if(MessageSecurityContext_soapbox.wsuIdAllocator==null){
  106.             MessageSecurityContext_soapbox.prefixWsuId=prefixWsuIdParam;
  107.             if(prefixWsuIdParam==null || "".equals(prefixWsuIdParam)){
  108.                 MessageSecurityContext_soapbox.wsuIdAllocator = new org.openspcoop2.security.message.WsuIdAllocator("openspcoop2_soapbox_");// Default di wss4j
  109.             }
  110.             else{
  111.                 MessageSecurityContext_soapbox.wsuIdAllocator = new org.openspcoop2.security.message.WsuIdAllocator(prefixWsuIdParam);
  112.             }
  113.             log.info("WsuIdAllocator="+MessageSecurityContext_soapbox.wsuIdAllocator.getClass().getName());
  114.         }
  115.         else{
  116.             if(MessageSecurityContext_soapbox.prefixWsuId==null){
  117.                 throw new Exception("WsuIdAllocator istanziato con la classe ["+MessageSecurityContext_soapbox.wsuIdAllocator.getClass().getName()+"] e variabile prefixWsuId non istanziata??");
  118.             }
  119.             else if(!MessageSecurityContext_soapbox.prefixWsuId.equals(prefixWsuIdParam)){
  120.                 throw new Exception("WsuIdAllocator gia' istanziato con la classe ["+MessageSecurityContext_soapbox.wsuIdAllocator.getClass().getName()
  121.                         +"] e variabile prefixWsuId uguale al valore ["+MessageSecurityContext_soapbox.prefixWsuId+"]. Nuovo valore ["+prefixWsuIdParam+"] non impostabile.");
  122.             }
  123.         }
  124.     }

  125. }