MessageSecuritySender_xml.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.xml;


  21. import org.openspcoop2.message.OpenSPCoop2Message;
  22. import org.openspcoop2.message.OpenSPCoop2RestXmlMessage;
  23. import org.openspcoop2.message.constants.MessageType;
  24. import org.openspcoop2.message.constants.ServiceBinding;
  25. import org.openspcoop2.security.SecurityException;
  26. import org.openspcoop2.security.message.AbstractRESTMessageSecuritySender;
  27. import org.openspcoop2.security.message.MessageSecurityContext;
  28. import org.openspcoop2.security.message.constants.SecurityConstants;
  29. import org.openspcoop2.security.message.utils.EncryptionBean;
  30. import org.openspcoop2.security.message.utils.KeystoreUtils;
  31. import org.openspcoop2.security.message.utils.SignatureBean;
  32. import org.openspcoop2.utils.Utilities;
  33. import org.openspcoop2.utils.certificate.KeyStore;
  34. import org.openspcoop2.utils.security.SymmetricKeyWrappedMode;
  35. import org.openspcoop2.utils.security.XmlEncrypt;
  36. import org.openspcoop2.utils.security.XmlSignature;

  37. /**
  38.  * MessageSecuritySender_xml
  39.  *
  40.  * @author Andrea Poli (apoli@link.it)
  41.  * @author $Author$
  42.  * @version $Rev$, $Date$
  43.  */

  44. public class MessageSecuritySender_xml extends AbstractRESTMessageSecuritySender{

  45.    
  46.      @Override
  47.     public void process(MessageSecurityContext messageSecurityContext,OpenSPCoop2Message messageParam, org.openspcoop2.utils.Map<Object> ctx) throws SecurityException{
  48.         try{    
  49.            
  50.             if(ServiceBinding.REST.equals(messageParam.getServiceBinding())==false){
  51.                 throw new SecurityException(XMLCostanti.XML_ENGINE_DESCRIPTION+" usable only with REST Binding");
  52.             }
  53.             if(MessageType.XML.equals(messageParam.getMessageType())==false) {
  54.                 throw new SecurityException(XMLCostanti.XML_ENGINE_DESCRIPTION+" usable only with REST Binding and a xml message, found: "+messageParam.getMessageType());
  55.             }
  56.             OpenSPCoop2RestXmlMessage restXmlMessage = messageParam.castAsRestXml();
  57.            
  58.            
  59.            
  60.             // ********** Leggo operazioni ***************
  61.             boolean encrypt = false;
  62.             boolean signature = false;

  63.             String[]actions = ((String)messageSecurityContext.getOutgoingProperties().get(SecurityConstants.ACTION)).split(" ");
  64.             for (int i = 0; i < actions.length; i++) {
  65.                 if(SecurityConstants.isActionEncryption(actions[i].trim())){
  66.                     encrypt = true;
  67.                 }
  68.                 else if(SecurityConstants.SIGNATURE_ACTION.equals(actions[i].trim())){
  69.                     signature = true;
  70.                 }
  71.                 else {
  72.                     throw new SecurityException(XMLCostanti.XML_ENGINE_DESCRIPTION+"; action '"+actions[i]+"' unsupported");
  73.                 }
  74.             }
  75.            
  76.             if(encrypt && signature) {
  77.                 throw new SecurityException(XMLCostanti.XML_ENGINE_DESCRIPTION+" usable only with one function beetwen encrypt or signature");
  78.             }
  79.             if(!encrypt && !signature) {
  80.                 throw new SecurityException(XMLCostanti.XML_ENGINE_DESCRIPTION+" require one function beetwen encrypt or signature");
  81.             }
  82.            

  83.            
  84.            
  85.             if(signature) {
  86.                
  87.                
  88.                 // **************** Leggo parametri signature store **************************

  89.                 SignatureBean bean = null;
  90.                 try {
  91.                     bean = KeystoreUtils.getSenderSignatureBean(messageSecurityContext,ctx);
  92.                 }catch(Exception e) {
  93.                     throw e;
  94.                 }
  95.                
  96.                 KeyStore signatureKS = bean.getKeystore();
  97.                 String aliasSignatureUser = bean.getUser();
  98.                 String aliasSignaturePassword = bean.getPassword();

  99.                 if(signatureKS==null) {
  100.                     throw new SecurityException(XMLCostanti.XML_ENGINE_SIGNATURE_DESCRIPTION+" require keystore");
  101.                 }
  102.                 if(aliasSignatureUser==null) {
  103.                     throw new SecurityException(XMLCostanti.XML_ENGINE_SIGNATURE_DESCRIPTION+" require alias private key");
  104.                 }
  105.                 if(aliasSignaturePassword==null) {
  106.                     throw new SecurityException(XMLCostanti.XML_ENGINE_SIGNATURE_DESCRIPTION+" require password private key");
  107.                 }
  108.                                
  109.                 String signatureAlgorithm = (String) messageSecurityContext.getOutgoingProperties().get(SecurityConstants.SIGNATURE_ALGORITHM);
  110.                 if(signatureAlgorithm==null || "".equals(signatureAlgorithm.trim())){
  111.                     throw new SecurityException(XMLCostanti.XML_ENGINE_SIGNATURE_DESCRIPTION+" require '"+SecurityConstants.SIGNATURE_ALGORITHM+"' property");
  112.                 }
  113.                
  114.                 String digestMethodAlgorithm = (String) messageSecurityContext.getOutgoingProperties().get(SecurityConstants.SIGNATURE_DIGEST_ALGORITHM);
  115.                 if(digestMethodAlgorithm==null || "".equals(digestMethodAlgorithm.trim())){
  116.                     throw new SecurityException(XMLCostanti.XML_ENGINE_SIGNATURE_DESCRIPTION+" require '"+SecurityConstants.SIGNATURE_DIGEST_ALGORITHM+"' property");
  117.                 }
  118.                
  119.                 String signatureCanonicalizationMethod = (String) messageSecurityContext.getOutgoingProperties().get(SecurityConstants.SIGNATURE_C14N_ALGORITHM);
  120.                 if(signatureCanonicalizationMethod==null || "".equals(signatureCanonicalizationMethod.trim())){
  121.                     throw new SecurityException(XMLCostanti.XML_ENGINE_SIGNATURE_DESCRIPTION+" require '"+SecurityConstants.SIGNATURE_C14N_ALGORITHM+"' property");
  122.                 }
  123.                
  124.                 XmlSignature xmlSignature = new XmlSignature(signatureKS, aliasSignatureUser, aliasSignaturePassword);
  125.                
  126.                 String signatureKeyInfo = (String) messageSecurityContext.getOutgoingProperties().get(SecurityConstants.SIGNATURE_XML_KEY_INFO);
  127.                 String signatureKeyInfoAlias = (String) messageSecurityContext.getOutgoingProperties().get(SecurityConstants.SIGNATURE_XML_KEY_INFO_ALIAS);
  128.                
  129.                
  130.                
  131.                

  132.                
  133.                
  134.                
  135.                
  136.                 // **************** Process **************************
  137.                
  138.                 if(signatureKeyInfo!=null && !"".equals(signatureKeyInfo.trim())){
  139.                    
  140.                     if(SecurityConstants.SIGNATURE_XML_KEY_INFO_X509.equals(signatureKeyInfo)){
  141.                         if(signatureKeyInfoAlias!=null && !"".equals(signatureKeyInfoAlias.trim())){
  142.                             xmlSignature.addX509KeyInfo(signatureKeyInfoAlias);
  143.                         }
  144.                         else {
  145.                             xmlSignature.addX509KeyInfo();
  146.                         }
  147.                     }
  148.                     else if(SecurityConstants.SIGNATURE_XML_KEY_INFO_RSA.equals(signatureKeyInfo)){
  149.                         if(signatureKeyInfoAlias!=null && !"".equals(signatureKeyInfoAlias.trim())){
  150.                             xmlSignature.addRSAKeyInfo(signatureKeyInfoAlias);
  151.                         }
  152.                         else {
  153.                             xmlSignature.addRSAKeyInfo();
  154.                         }
  155.                     }
  156.                     else{
  157.                         throw new Exception(SecurityConstants.SIGNATURE_XML_KEY_INFO+" not supported ["+signatureKeyInfo+"]");
  158.                     }
  159.                    
  160.                 }
  161.                
  162.                 xmlSignature.sign(restXmlMessage.getContent(), signatureAlgorithm, digestMethodAlgorithm, signatureCanonicalizationMethod);
  163.                
  164.                
  165.                
  166.             } // fine signature
  167.            
  168.            
  169.            
  170.            
  171.            
  172.             else if(encrypt){
  173.            
  174.                 // **************** Leggo parametri encryption store **************************
  175.                
  176.                 XmlEncrypt xmlEncrypt = null;
  177.                 EncryptionBean bean = null;
  178.                 try {
  179.                     bean = KeystoreUtils.getSenderEncryptionBean(messageSecurityContext, ctx);
  180.                 }catch(Exception e) {
  181.                     throw e;
  182.                 }
  183.                
  184.                 KeyStore encryptionKS = bean.getKeystore();
  185.                 KeyStore encryptionTrustStoreKS = bean.getTruststore();
  186.                 boolean encryptionSymmetric = bean.isEncryptionSimmetric();
  187.                 SymmetricKeyWrappedMode encryptionSymmetricWrappedMode = SymmetricKeyWrappedMode.SYM_ENC_KEY_WRAPPED_SYMMETRIC_KEY;
  188.                 String aliasEncryptUser = bean.getUser();
  189.                 String aliasEncryptPassword = bean.getPassword();

  190.                 if(encryptionSymmetric) {
  191.                    
  192.                     String encryptionSymmetricWrapped = (String) messageSecurityContext.getOutgoingProperties().get(SecurityConstants.ENCRYPTION_SYMMETRIC_WRAPPED);
  193.                     if(encryptionSymmetricWrapped!=null) {
  194.                         if(SecurityConstants.ENCRYPTION_SYMMETRIC_WRAPPED_TRUE.equalsIgnoreCase(encryptionSymmetricWrapped)) {
  195.                             encryptionSymmetricWrappedMode = SymmetricKeyWrappedMode.SYM_ENC_KEY_WRAPPED_SYMMETRIC_KEY;
  196.                         }
  197.                         else if(SecurityConstants.ENCRYPTION_SYMMETRIC_WRAPPED_FALSE.equalsIgnoreCase(encryptionSymmetricWrapped)) {
  198.                             encryptionSymmetricWrappedMode = SymmetricKeyWrappedMode.SYM_ENC_KEY_NO_WRAPPED;
  199.                         }
  200.                     }
  201.                    
  202.                     if(encryptionKS==null) {
  203.                         throw new SecurityException(XMLCostanti.XML_ENGINE_ENCRYPT_DESCRIPTION+" require keystore");
  204.                     }
  205.                     if(aliasEncryptUser==null) {
  206.                         throw new SecurityException(XMLCostanti.XML_ENGINE_ENCRYPT_DESCRIPTION+" require alias secret key");
  207.                     }
  208.                     if(aliasEncryptPassword==null) {
  209.                         throw new SecurityException(XMLCostanti.XML_ENGINE_ENCRYPT_DESCRIPTION+" require password secret key");
  210.                     }
  211.                 }
  212.                 else {
  213.                     if(encryptionTrustStoreKS==null) {
  214.                         throw new SecurityException(XMLCostanti.XML_ENGINE_ENCRYPT_DESCRIPTION+" require truststore");
  215.                     }
  216.                     if(aliasEncryptUser==null) {
  217.                         throw new SecurityException(XMLCostanti.XML_ENGINE_ENCRYPT_DESCRIPTION+" require alias public key");
  218.                     }
  219.                    
  220.                     encryptionSymmetricWrappedMode = SymmetricKeyWrappedMode.SYM_ENC_KEY_WRAPPED_ASYMMETRIC_KEY;
  221.                 }
  222.                
  223.                
  224.                 String wrappedKeyAlgorithm = null;
  225.                 if(encryptionSymmetric) {  
  226.                
  227.                     if(SymmetricKeyWrappedMode.SYM_ENC_KEY_WRAPPED_SYMMETRIC_KEY.equals(encryptionSymmetricWrappedMode)) {
  228.                    
  229.                         wrappedKeyAlgorithm = (String) messageSecurityContext.getOutgoingProperties().get(SecurityConstants.ENCRYPTION_SYMMETRIC_ALGORITHM);
  230.                         if(wrappedKeyAlgorithm==null || "".equals(wrappedKeyAlgorithm.trim())){
  231.                             throw new SecurityException(XMLCostanti.XML_ENGINE_ENCRYPT_DESCRIPTION+" require '"+SecurityConstants.ENCRYPTION_SYMMETRIC_ALGORITHM+"' property");
  232.                         }
  233.                        
  234.                     }
  235.                    
  236.                     xmlEncrypt = new XmlEncrypt(encryptionKS, true, encryptionSymmetricWrappedMode, aliasEncryptUser, aliasEncryptPassword);
  237.                    
  238.                 }
  239.                 else {
  240.                                            
  241.                     wrappedKeyAlgorithm = (String) messageSecurityContext.getOutgoingProperties().get(SecurityConstants.ENCRYPTION_KEY_TRANSPORT_ALGORITHM);
  242.                     if(wrappedKeyAlgorithm==null || "".equals(wrappedKeyAlgorithm.trim())){
  243.                         throw new SecurityException(XMLCostanti.XML_ENGINE_ENCRYPT_DESCRIPTION+" require '"+SecurityConstants.ENCRYPTION_KEY_TRANSPORT_ALGORITHM+"' property");
  244.                     }
  245.                    
  246.                     xmlEncrypt = new XmlEncrypt(encryptionTrustStoreKS, aliasEncryptUser);
  247.                 }
  248.                
  249.                 String encryptionKeyAlgorithm = null;
  250.                 if(SymmetricKeyWrappedMode.SYM_ENC_KEY_WRAPPED_SYMMETRIC_KEY.equals(encryptionSymmetricWrappedMode) ||
  251.                         SymmetricKeyWrappedMode.SYM_ENC_KEY_WRAPPED_ASYMMETRIC_KEY.equals(encryptionSymmetricWrappedMode)) {
  252.                     encryptionKeyAlgorithm = (String) messageSecurityContext.getOutgoingProperties().get(SecurityConstants.ENCRYPTION_KEY_ALGORITHM);
  253.                     if(encryptionKeyAlgorithm==null || "".equals(encryptionKeyAlgorithm.trim())){
  254.                         throw new SecurityException(XMLCostanti.XML_ENGINE_ENCRYPT_DESCRIPTION+" require '"+SecurityConstants.ENCRYPTION_KEY_ALGORITHM+"' property");
  255.                     }
  256.                 }
  257.                
  258.                 String encryptionAlgorithm = (String) messageSecurityContext.getOutgoingProperties().get(SecurityConstants.ENCRYPTION_ALGORITHM);
  259.                 if(encryptionAlgorithm==null || "".equals(encryptionAlgorithm.trim())){
  260.                     throw new SecurityException(XMLCostanti.XML_ENGINE_ENCRYPT_DESCRIPTION+" require '"+SecurityConstants.ENCRYPTION_ALGORITHM+"' property");
  261.                 }
  262.                
  263.                 String encryptionDigestAlgorithm = (String) messageSecurityContext.getOutgoingProperties().get(SecurityConstants.ENCRYPTION_DIGEST_ALGORITHM);
  264.                 if(encryptionDigestAlgorithm!=null && "".equals(encryptionDigestAlgorithm.trim())){
  265.                     encryptionDigestAlgorithm = null; // normalizzo
  266.                 }
  267.                
  268.                 String encryptionCanonicalizationMethodAlgorithm = (String) messageSecurityContext.getOutgoingProperties().get(SecurityConstants.ENCRYPTION_C14N_ALGORITHM);
  269.                 if(encryptionCanonicalizationMethodAlgorithm!=null && "".equals(encryptionCanonicalizationMethodAlgorithm.trim())){
  270.                     encryptionCanonicalizationMethodAlgorithm = null; // normalizzo
  271.                 }
  272.                
  273.                
  274.                
  275.                 // **************** Process **************************
  276.                
  277.                 if(encryptionSymmetric) {  
  278.                     if(SymmetricKeyWrappedMode.SYM_ENC_KEY_WRAPPED_SYMMETRIC_KEY.equals(encryptionSymmetricWrappedMode)) {
  279.                         xmlEncrypt.encrypt(restXmlMessage.getContent(), encryptionAlgorithm, encryptionCanonicalizationMethodAlgorithm, encryptionDigestAlgorithm,
  280.                                 encryptionKeyAlgorithm, wrappedKeyAlgorithm);
  281.                     }
  282.                     else {
  283.                         xmlEncrypt.encryptSymmetric(restXmlMessage.getContent(), encryptionAlgorithm, encryptionCanonicalizationMethodAlgorithm, encryptionDigestAlgorithm);
  284.                     }
  285.                 }
  286.                 else {
  287.                     xmlEncrypt.encrypt(restXmlMessage.getContent(), encryptionAlgorithm, encryptionCanonicalizationMethodAlgorithm, encryptionDigestAlgorithm,
  288.                             encryptionKeyAlgorithm, wrappedKeyAlgorithm);
  289.                 }
  290.                

  291.             } // fine encrypt


  292.         }
  293.         catch(Exception e){
  294.            
  295.             String msg = Utilities.getInnerNotEmptyMessageException(e).getMessage();
  296.            
  297.             Throwable innerExc = Utilities.getLastInnerException(e);
  298.             String innerMsg = null;
  299.             if(innerExc!=null){
  300.                 innerMsg = innerExc.getMessage();
  301.             }
  302.            
  303.             String messaggio = null;
  304.             if(msg!=null){
  305.                 messaggio = new String(msg);
  306.                 if(innerMsg!=null && !innerMsg.equals(msg)){
  307.                     messaggio = messaggio + " ; " + innerMsg;
  308.                 }
  309.             }
  310.             else{
  311.                 if(innerMsg!=null){
  312.                     messaggio = innerMsg;
  313.                 }
  314.             }
  315.            
  316.             SecurityException wssException = new SecurityException(e.getMessage(), e);
  317.             wssException.setMsgErrore(messaggio);
  318.             throw wssException;
  319.         }
  320.        
  321.     }

  322.  
  323. }