WSSUtilities.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.engine;

  21. import java.util.List;
  22. import java.util.Map;

  23. import org.openspcoop2.message.OpenSPCoop2SoapMessage;
  24. import org.openspcoop2.message.constants.Costanti;
  25. import org.openspcoop2.message.soap.reference.Reference;
  26. import org.openspcoop2.protocol.sdk.constants.CodiceErroreCooperazione;
  27. import org.openspcoop2.security.SecurityException;
  28. import org.openspcoop2.security.message.MessageSecurityContext;
  29. import org.openspcoop2.security.message.constants.SecurityConstants;
  30. import org.openspcoop2.utils.Utilities;

  31. /**
  32.  * WSSUtilities
  33.  *
  34.  * @author Andrea Poli (apoli@link.it)
  35.  * @author $Author$
  36.  * @version $Rev$, $Date$
  37.  */
  38. public class WSSUtilities {

  39.     public static List<Reference> getDirtyElements(MessageSecurityContext messageSecurityContext,OpenSPCoop2SoapMessage message) throws SecurityException{
  40.        
  41.         try{
  42.            
  43.             boolean mustUnderstandValue = false;
  44.             Object mustUnderstand = messageSecurityContext.getIncomingProperties().get(SecurityConstants.MUST_UNDERSTAND);
  45.             if(mustUnderstand!=null){
  46.                 if(mustUnderstand instanceof String) {
  47.                     mustUnderstandValue = Boolean.parseBoolean((String)mustUnderstand);
  48.                 }
  49.                 else if(mustUnderstand instanceof Boolean) {
  50.                     mustUnderstandValue = (Boolean) mustUnderstand;
  51.                 }
  52.                 else {
  53.                     throw new SecurityException("Unexected type '"+mustUnderstand.getClass().getName()+"' for property '"+SecurityConstants.MUST_UNDERSTAND+"'");
  54.                 }
  55.             }
  56.             String actor = messageSecurityContext.getActor();
  57.             if("".equals(messageSecurityContext.getActor()))
  58.                 actor = null;
  59.            
  60.             List<Reference> references = message.getWSSDirtyElements(actor, mustUnderstandValue);
  61.             messageSecurityContext.setReferences(references);
  62.             return references;
  63.            
  64.         }catch(Exception e){
  65.             SecurityException sec = new SecurityException(e.getMessage(),e);
  66.             if(Utilities.existsInnerMessageException(e, Costanti.FIND_ERROR_SIGNATURE_REFERENCES, true)){
  67.                 sec.setCodiceErrore(CodiceErroreCooperazione.SICUREZZA_FIRMA_NON_VALIDA);
  68.             }
  69.             else if(Utilities.existsInnerMessageException(e, Costanti.FIND_ERROR_ENCRYPTED_REFERENCES, true)){
  70.                 sec.setCodiceErrore(CodiceErroreCooperazione.SICUREZZA_CIFRATURA_NON_VALIDA);
  71.             }
  72.             throw sec;
  73.         }
  74.     }
  75.    
  76.     public static void cleanDirtyElements(MessageSecurityContext messageSecurityContext,OpenSPCoop2SoapMessage message, List<Reference> elementsToClean,
  77.             boolean detachHeaderWSSecurity, boolean removeAllIdRef) throws SecurityException{
  78.         try{
  79.            
  80.             boolean mustUnderstandValue = false;
  81.             Object mustUnderstand = messageSecurityContext.getIncomingProperties().get(SecurityConstants.MUST_UNDERSTAND);
  82.             if(mustUnderstand!=null){
  83.                 if(mustUnderstand instanceof String) {
  84.                     mustUnderstandValue = Boolean.parseBoolean((String)mustUnderstand);
  85.                 }
  86.                 else if(mustUnderstand instanceof Boolean) {
  87.                     mustUnderstandValue = (Boolean) mustUnderstand;
  88.                 }
  89.                 else {
  90.                     throw new SecurityException("Unexected type '"+mustUnderstand.getClass().getName()+"' for property '"+SecurityConstants.MUST_UNDERSTAND+"'");
  91.                 }
  92.             }
  93.             String actor = messageSecurityContext.getActor();
  94.             if("".equals(messageSecurityContext.getActor()))
  95.                 actor = null;
  96.            
  97.             message.cleanWSSDirtyElements(actor, mustUnderstandValue, elementsToClean, detachHeaderWSSecurity, removeAllIdRef);
  98.            
  99.         }catch(Exception e){
  100.             throw new SecurityException(e.getMessage(),e);
  101.         }
  102.     }
  103.    
  104.     public static boolean isNormalizeToSaajImpl(MessageSecurityContext wssContext){
  105.        
  106.         if(wssContext==null) {
  107.             return false;
  108.         }
  109.        
  110.         Map<String,Object> wssProperties = null;
  111.         if(wssContext.isFunctionAsClient())
  112.             wssProperties = wssContext.getOutgoingProperties();
  113.         else
  114.             wssProperties = wssContext.getIncomingProperties();
  115.         if(wssProperties!=null && wssProperties.containsKey(SecurityConstants.NORMALIZE_TO_SAAJ_IMPL)){
  116.             return Boolean.parseBoolean((String)wssProperties.get(SecurityConstants.NORMALIZE_TO_SAAJ_IMPL));
  117.         }
  118.        
  119.         // default viene applicata la normalizzazione solo se siamo client e effettuamo operazioni di signature.
  120.         if(wssProperties!=null && wssProperties.containsKey(SecurityConstants.ACTION)){
  121.             String action = (String) wssProperties.get(SecurityConstants.ACTION);

  122.             // A volte i problemi di signature verification avvengono anche in ricezione. Per questo motivo si è deciso di normalizzare sia lato client che lato server
  123.             if ( action.contains(SecurityConstants.SIGNATURE_ACTION) ||
  124.                     action.contains(SecurityConstants.ACTION_SAML_TOKEN_UNSIGNED) || // la decisione di firmare o meno saml è nel file di proprietà
  125.                     action.contains(SecurityConstants.ACTION_SAML_TOKEN_SIGNED) ||
  126.                     action.contains(SecurityConstants.ACTION_USERNAME_TOKEN_SIGNATURE) ||
  127.                     action.contains(SecurityConstants.ACTION_SIGNATURE_DERIVED) ||
  128.                     action.contains(SecurityConstants.ACTION_SIGNATURE_WITH_KERBEROS_TOKEN)){
  129.                 return true;
  130.             }
  131.            
  132.             if(wssContext.isFunctionAsClient() == false){
  133.                
  134.                 if ( (SecurityConstants.containsActionEncryption(action) || SecurityConstants.containsActionDecryption(action))
  135.                         &&
  136.                         wssProperties.containsKey(SecurityConstants.ENCRYPTION_PARTS)
  137.                     ){
  138.                    
  139.                     String attach = "{"+SecurityConstants.NAMESPACE_ATTACH+"}";
  140.                     String attachAll = attach+"{"+SecurityConstants.ATTACHMENT_INDEX_ALL+"}";
  141.                     String encryptParts = (String) wssProperties.get(SecurityConstants.ENCRYPTION_PARTS);
  142.                    
  143.                     if ( encryptParts!=null &&
  144.                             encryptParts.contains(attach) &&
  145.                             (!encryptParts.contains(attachAll)) ){
  146.                         return true;
  147.                     }
  148.                 }
  149.             }
  150.            
  151.             return false;
  152.         }
  153.         else{
  154.             return false;
  155.         }
  156.     }
  157. }