MessageSecuritySender_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 java.util.ArrayList;
  22. import java.util.HashMap;
  23. import java.util.Iterator;
  24. import java.util.List;
  25. import java.util.Map;

  26. import javax.xml.soap.AttachmentPart;
  27. import javax.xml.soap.SOAPPart;
  28. import javax.xml.transform.dom.DOMSource;

  29. import org.adroitlogic.soapbox.MessageSecurityContext;
  30. import org.adroitlogic.soapbox.SecurityRequest;
  31. import org.adroitlogic.ultraesb.core.MessageImpl;
  32. import org.openspcoop2.message.OpenSPCoop2Message;
  33. import org.openspcoop2.message.OpenSPCoop2SoapMessage;
  34. import org.openspcoop2.message.constants.ServiceBinding;
  35. import org.openspcoop2.message.xml.MessageXMLUtils;
  36. import org.openspcoop2.security.SecurityException;
  37. import org.openspcoop2.security.message.IMessageSecuritySender;
  38. import org.openspcoop2.security.message.constants.SecurityConstants;
  39. import org.openspcoop2.security.message.utils.AttachmentProcessingPart;
  40. import org.openspcoop2.security.message.utils.ElementProcessingPart;
  41. import org.openspcoop2.security.message.utils.EncryptionBean;
  42. import org.openspcoop2.security.message.utils.KeystoreUtils;
  43. import org.openspcoop2.security.message.utils.ProcessingPart;
  44. import org.openspcoop2.security.message.utils.ProcessingPartUtils;
  45. import org.openspcoop2.security.message.utils.SignatureBean;
  46. import org.openspcoop2.utils.Utilities;
  47. import org.openspcoop2.utils.certificate.KeyStore;
  48. import org.openspcoop2.utils.resources.ClassLoaderUtilities;
  49. import org.openspcoop2.utils.xml.AbstractXMLUtils;
  50. import org.w3c.dom.Document;

  51. /**
  52.  * WSSContext_soapbox
  53.  *
  54.  * @author Andrea Poli (apoli@link.it)
  55.  * @author Giovanni Bussu (bussu@link.it)
  56.  * @author $Author$
  57.  * @version $Rev$, $Date$
  58.  */
  59. public class MessageSecuritySender_soapbox implements IMessageSecuritySender{

  60.    
  61.     @Override
  62.     public void process(org.openspcoop2.security.message.MessageSecurityContext messageSecurityContext,OpenSPCoop2Message messageParam, org.openspcoop2.utils.Map<Object> ctx) throws SecurityException{
  63.         try{    

  64.             if(ServiceBinding.SOAP.equals(messageParam.getServiceBinding())==false){
  65.                 throw new SecurityException("SoapBox Engine usable only with SOAP Binding");
  66.             }
  67.             OpenSPCoop2SoapMessage message = messageParam.castAsSoap();
  68.             AbstractXMLUtils xmlUtils = MessageXMLUtils.getInstance(message.getFactory());
  69.            



  70.             // ********** Leggo operazioni ***************
  71.             boolean encrypt = false;
  72.             boolean signature = false;
  73.             boolean timestamp = false;

  74.             String[]actions = ((String)messageSecurityContext.getOutgoingProperties().get(SecurityConstants.ACTION)).split(" ");
  75.             for (int i = 0; i < actions.length; i++) {
  76.                 if(SecurityConstants.isActionEncryption(actions[i].trim())){
  77.                     encrypt = true;
  78.                 }
  79.                 else if(SecurityConstants.SIGNATURE_ACTION.equals(actions[i].trim())){
  80.                     signature = true;
  81.                 }
  82.                 else if(SecurityConstants.TIMESTAMP_ACTION.equals(actions[i].trim())){
  83.                     timestamp = true;
  84.                 }
  85.             }
  86.            
  87.             //rilasciato vincolo di abilitazione signature o encryption per abilitare il timestamp
  88. //          if(!signature && !encrypt && timestamp){
  89. //              throw new WSSException("La funzionalita' "+WSSConstants.TIMESTAMP_ACTION+" richiede per essere abilitata almeno una delle seguenti altre funzionalita': "+
  90. //                      WSSConstants.ENCRYPT_ACTION+","+WSSConstants.SIGNATURE_ACTION);
  91. //          }
  92.            




  93.             // ********** Inizializzo Header WSS ***************

  94.             SOAPPart sp = message.getSOAPPart();
  95.             //Document d = sp;
  96.             Document d = sp.getDocumentElement().getOwnerDocument();
  97.             MessageSecurityContext msgSecCtx = new MessageSecurityContext(d, new MessageImpl(true, null, "http"));
  98.             Object mustUnderstandObject = messageSecurityContext.getOutgoingProperties().get(SecurityConstants.MUST_UNDERSTAND);
  99.             boolean mustUnderstand = false;
  100.             if(mustUnderstandObject!=null){
  101.                 mustUnderstand = SecurityConstants.TRUE.equals(mustUnderstandObject);
  102.             }
  103.             WSSUtils.initWSSecurityHeader(message,messageSecurityContext.getActor(),mustUnderstand);


  104.            
  105.            
  106.            
  107.            
  108.            
  109.             // **************** Inizializzo process per timestamp **************************
  110.            
  111.             TimestampMessageProcessor timestampProc = null;
  112.             if(timestamp){
  113.                 timestampProc = new TimestampMessageProcessor();
  114.             }
  115.            
  116.            
  117.            
  118.            
  119.            

  120.             // **************** Inizializzo process per cifrare **************************

  121.             EncryptPartialMessageProcessor encMsgProc = null;
  122.             if(encrypt){
  123.                 encMsgProc = new EncryptPartialMessageProcessor();
  124.                 encMsgProc.setMessage(message);
  125.                 encMsgProc.setActor(messageSecurityContext.getActor());
  126.                 encMsgProc.setMustUnderstand(mustUnderstand);

  127.                 // encryptionParts
  128.                 Object encryptionParts =  messageSecurityContext.getOutgoingProperties().get(SecurityConstants.ENCRYPTION_PARTS);
  129.                 List<ProcessingPart<?,?>> lstEncryptionParts = null;

  130.                 if(encryptionParts!=null){
  131.                     lstEncryptionParts = ProcessingPartUtils.getEncryptionInstance().getProcessingParts((String)encryptionParts);
  132.                 }

  133.                 boolean allPresente = false;
  134.                 boolean allComplete = false;
  135.                 List<AttachmentProcessingPart> lstAttachments = new ArrayList<AttachmentProcessingPart>();
  136.                
  137.                 if(lstEncryptionParts!=null) {
  138.                     for(ProcessingPart<?,?> part: lstEncryptionParts) {
  139.    
  140.                         if(part instanceof AttachmentProcessingPart) {
  141.                             AttachmentProcessingPart attProcessingPart = (AttachmentProcessingPart) part;
  142.                             if(attProcessingPart.isAllAttachments()) {
  143.                                 allPresente = true;
  144.                                 allComplete = !attProcessingPart.isContent();
  145.                             } else {
  146.                                 lstAttachments.add(attProcessingPart);
  147.                             }
  148.                            
  149.    
  150.                         } else if(part instanceof ElementProcessingPart) {  
  151.                             ElementProcessingPart elProcessingPart = (ElementProcessingPart) part;
  152.                             encMsgProc.addElementToEncrypt(elProcessingPart.getPart(),elProcessingPart.isContent());
  153.                         }
  154.                     }
  155.                 }
  156.                
  157.                 if(allPresente || lstAttachments.size()>0){
  158.                    
  159.                     Iterator<?> it = message.getAttachments();
  160.                    
  161.                     if(it.hasNext()==false){
  162.                         throw new Exception("Property "+SecurityConstants.ENCRYPTION_PARTS+ " contiene la richiesta di cifrare attachments, ma il messaggio non ne contiene");
  163.                     }
  164.                
  165.                     int indexAllegatoEncrypt = 1;
  166.                     while (it.hasNext()) {
  167.                         AttachmentPart ap = (AttachmentPart) it.next();
  168.                         if(allPresente){
  169.                             encMsgProc.addAttachmentToEncrypt(ap, !allComplete);
  170.                         }else{
  171.                             boolean found = false;
  172.                             for (int i = 0; i < lstAttachments.size() && !found; i++) {
  173.                                 if(lstAttachments.get(i).getPart().equals(indexAllegatoEncrypt)) {
  174.                                     encMsgProc.addAttachmentToEncrypt(ap, !lstAttachments.get(i).isContent());
  175.                                     found = true;
  176.                                 }
  177.                             }
  178.                             indexAllegatoEncrypt++;
  179.                         }
  180.                     }
  181.                    
  182.                 }

  183.             }
  184.        
  185.            





  186.             // **************** Inizializzo process per firmare **************************
  187.            
  188.             SignPartialMessageProcessor signMsgProc = null;
  189.             if(signature){
  190.                 signMsgProc = (SignPartialMessageProcessor) ClassLoaderUtilities.newInstance(message.getSignPartialMessageProcessorClass());
  191.                 signMsgProc.setMessage(message);
  192.                 signMsgProc.setActor(messageSecurityContext.getActor());
  193.                 signMsgProc.setMustUnderstand(mustUnderstand);

  194.                 // encryptionParts
  195.                 Object signatureParts =  messageSecurityContext.getOutgoingProperties().get(SecurityConstants.SIGNATURE_PARTS);
  196.                 List<ProcessingPart<?,?>> lstSignatureParts = null;

  197.                 if(signatureParts!=null){
  198.                     lstSignatureParts = ProcessingPartUtils.getSignatureInstance().getProcessingParts((String)signatureParts);
  199.                 }

  200.                 boolean allPresente = false;
  201.                 boolean allComplete = false;
  202.                 List<AttachmentProcessingPart> lstAttachments = new ArrayList<AttachmentProcessingPart>();
  203.                
  204.                 if(lstSignatureParts!=null) {
  205.                     for(ProcessingPart<?,?> part: lstSignatureParts) {
  206.    
  207.                         if(part instanceof AttachmentProcessingPart) {
  208.                             AttachmentProcessingPart attProcessingPart = (AttachmentProcessingPart) part;
  209.                             if(attProcessingPart.isAllAttachments()) {
  210.                                 allPresente = true;
  211.                                 allComplete = !attProcessingPart.isContent();
  212.                             } else {
  213.                                 lstAttachments.add(attProcessingPart);
  214.                             }
  215.                            
  216.    
  217.                         } else if(part instanceof ElementProcessingPart) {  
  218.                             ElementProcessingPart elProcessingPart = (ElementProcessingPart) part;
  219.                             signMsgProc.addElementToSign(elProcessingPart.getPart(),elProcessingPart.isContent());
  220.                         }
  221.                     }
  222.                 }
  223.                
  224.                 if(allPresente || lstAttachments.size()>0){
  225.                    
  226.                     Iterator<?> it = message.getAttachments();
  227.                    
  228.                     if(it.hasNext()==false){
  229.                         throw new Exception("Property "+SecurityConstants.SIGNATURE_PARTS+ " contiene la richiesta di cifrare attachments, ma il messaggio non ne contiene");
  230.                     }
  231.                
  232.                     int indexAllegatoEncrypt = 1;
  233.                     while (it.hasNext()) {
  234.                         AttachmentPart ap = (AttachmentPart) it.next();
  235.                         if(allPresente){
  236.                             signMsgProc.addAttachmentsToSign(ap, !allComplete);
  237.                         }else{
  238.                             boolean found = false;
  239.                             for (int i = 0; i < lstAttachments.size() && !found; i++) {
  240.                                 if(lstAttachments.get(i).getPart().equals(indexAllegatoEncrypt)) {
  241.                                     signMsgProc.addAttachmentsToSign(ap, !lstAttachments.get(i).isContent());
  242.                                     found = true;
  243.                                 }
  244.                             }
  245.                             indexAllegatoEncrypt++;
  246.                         }
  247.                     }
  248.                    
  249.                 }

  250.             }
  251.        
  252.            




  253.             // **************** Leggo parametri encryption store **************************
  254.             KeyStore encryptionKS = null;
  255.             KeyStore encryptionTrustStoreKS = null;
  256.             boolean encryptionSymmetric = false;
  257.             String aliasEncryptUser = null;
  258.             String aliasEncryptPassword = null;
  259.             if(encrypt){
  260.                
  261.                 EncryptionBean bean = KeystoreUtils.getSenderEncryptionBean(messageSecurityContext, ctx);
  262.                
  263.                 encryptionKS = bean.getKeystore();
  264.                 encryptionTrustStoreKS = bean.getTruststore();
  265.                 encryptionSymmetric = bean.isEncryptionSimmetric();
  266.                 aliasEncryptUser = bean.getUser();
  267.                 aliasEncryptPassword = bean.getPassword();

  268.             }







  269.             // **************** Leggo parametri signature store **************************
  270.             KeyStore signatureKS = null;
  271.             KeyStore signatureTrustStoreKS = null;
  272.             String aliasSignatureUser = null;
  273.             String aliasSignaturePassword = null;
  274.             if(signature){
  275.            
  276.                 SignatureBean bean = KeystoreUtils.getSenderSignatureBean(messageSecurityContext, ctx);
  277.                
  278.                 signatureKS = bean.getKeystore();
  279.                 signatureTrustStoreKS = bean.getTruststore();
  280.                 aliasSignatureUser = bean.getUser();
  281.                 aliasSignaturePassword = bean.getPassword();

  282.             }
  283.              

  284.            
  285.            
  286.            
  287.            
  288.            
  289.            
  290.             // **************** Inizializzo Context for timestamp **************************
  291.            
  292.             if(timestamp){
  293.                 Object ttlObject = messageSecurityContext.getOutgoingProperties().get(SecurityConstants.TIMESTAMP_TTL);
  294.                 if(ttlObject==null){
  295.                     ttlObject = SecurityConstants.TIMESTAMP_SOAPBOX_TTL_DEFAULT;
  296.                 }
  297.                 String ttl = (String) ttlObject;
  298.                 long ttlLong = -1;
  299.                 try{
  300.                     ttlLong = Long.parseLong(ttl);
  301.                 }catch(Exception e){
  302.                     throw new Exception("Indicazione "+SecurityConstants.TIMESTAMP_TTL+" non corretta: "+e.getMessage());
  303.                 }
  304.                 msgSecCtx.getTimestampRequest().setTimeForExpiryMillis(ttlLong*1000l); // secondi
  305.                
  306.                 //precision in milliseconds in Timestamp handling
  307.                 Object precisionObject = messageSecurityContext.getOutgoingProperties().get(SecurityConstants.TIMESTAMP_PRECISION);
  308.                 if(precisionObject==null){
  309.                     precisionObject = SecurityConstants.TRUE;
  310.                 }
  311.                 String precision = (String) precisionObject;
  312.                 boolean precisionBoolean;
  313.                 try{
  314.                     precisionBoolean = Boolean.parseBoolean(precision);
  315.                 }catch(Exception e){
  316.                     throw new Exception("Indicazione "+SecurityConstants.TIMESTAMP_PRECISION+" non corretta: "+e.getMessage());
  317.                 }

  318.                 msgSecCtx.setProperty(SecurityConstants.TIMESTAMP_PRECISION, precisionBoolean);

  319.             }
  320.            
  321.            
  322.            
  323.            



  324.             // **************** Inizializzo Secure Context for encryption **************************
  325.            
  326.             org.openspcoop2.security.message.soapbox.SoapBoxSecurityConfig securityConfig_encryption = null;
  327.             if(encrypt){
  328.                 updateSecurityContextForEncryption(msgSecCtx, messageSecurityContext.getOutgoingProperties(), aliasEncryptUser);
  329.                
  330.                 Map<String, String> passwordMap_encryption = new HashMap<>();
  331.                 passwordMap_encryption.put(aliasEncryptUser, aliasEncryptPassword);

  332.                 if(encryptionTrustStoreKS==null){
  333.                     encryptionTrustStoreKS = encryptionKS;
  334.                 }
  335.                 securityConfig_encryption = new org.openspcoop2.security.message.soapbox.SoapBoxSecurityConfig(encryptionKS, encryptionTrustStoreKS, passwordMap_encryption,ctx);
  336.                 securityConfig_encryption.setSymmetricSharedKey(encryptionSymmetric);
  337.             }










  338.             // **************** Inizializzo Secure Context for signature **************************
  339.             org.openspcoop2.security.message.soapbox.SoapBoxSecurityConfig securityConfig_signature = null;
  340.             if(signature){
  341.                
  342.                 updateSecurityContextForSignature(msgSecCtx, messageSecurityContext.getOutgoingProperties(), aliasSignatureUser);
  343.                                
  344.                 Map<String, String> passwordMap_signature = new HashMap<>();
  345.                 passwordMap_signature.put(aliasSignatureUser, aliasSignaturePassword);

  346.                 if(signatureTrustStoreKS==null){
  347.                     signatureTrustStoreKS = signatureKS;
  348.                 }
  349.                 securityConfig_signature = new org.openspcoop2.security.message.soapbox.SoapBoxSecurityConfig(signatureKS, signatureTrustStoreKS, passwordMap_signature,ctx);
  350.             }              
  351.            








  352.             // **************** Process **************************

  353.             // Devo rileggerle per eseguire nell'ordine
  354.             actions = ((String)messageSecurityContext.getOutgoingProperties().get(SecurityConstants.ACTION)).split(" ");
  355.             boolean actionSignatureOrEncryptDo = false;
  356.             for (int i = 0; i < actions.length; i++) {
  357.                 if(SecurityConstants.isActionEncryption(actions[i].trim())){
  358.                     if(actionSignatureOrEncryptDo){
  359.                         // Refresh serve anche per la encrypt ??
  360.                         //byte[]xmlCifrato=this.xmlUtils.toByteArray(msgSecCtx.getDocument().getDocumentElement());
  361.                         //message.getSOAPPart().setContent(new DOMSource(this.xmlUtils.newElement(xmlCifrato)));
  362.                         //signMsgProc.setMessage(message);
  363.                         //Document dUpdated = message.getSOAPPart().getDocumentElement().getOwnerDocument();
  364.                         //msgSecCtx = new MessageSecurityContext(dUpdated, new MessageImpl(true, null, "http"));
  365.                         //updateSecurityContextForEncryption(msgSecCtx, messageSecurityContext.getOutgoingProperties(), aliasEncryptUser);
  366.                     }
  367.                     encMsgProc.process(securityConfig_encryption, msgSecCtx);
  368.                     actionSignatureOrEncryptDo = true;
  369.                 }
  370.                 else if(SecurityConstants.SIGNATURE_ACTION.equals(actions[i].trim())){
  371.                     if(signMsgProc!=null) {
  372.                         if(actionSignatureOrEncryptDo){
  373.                             byte[]xmlCifrato=xmlUtils.toByteArray(msgSecCtx.getDocument().getDocumentElement());
  374.                             message.getSOAPPart().setContent(new DOMSource(xmlUtils.newElement(xmlCifrato)));
  375.                             signMsgProc.setMessage(message);
  376.                             Document dUpdated = message.getSOAPPart().getDocumentElement().getOwnerDocument();
  377.                             msgSecCtx = new MessageSecurityContext(dUpdated, new MessageImpl(true, null, "http"));
  378.                             updateSecurityContextForSignature(msgSecCtx, messageSecurityContext.getOutgoingProperties(), aliasSignatureUser);
  379.                         }
  380.                         signMsgProc.process(securityConfig_signature, msgSecCtx);
  381.                         actionSignatureOrEncryptDo = true;
  382.                     }
  383.                 }
  384.                 else if(SecurityConstants.TIMESTAMP_ACTION.equals(actions[i].trim())){
  385.                     if(timestampProc!=null) {
  386.                         if(securityConfig_signature!=null){
  387.                             timestampProc.process(securityConfig_signature,msgSecCtx);
  388.                         }
  389.                         else if(securityConfig_encryption!=null){
  390.                             timestampProc.process(securityConfig_encryption,msgSecCtx);
  391.                         } else {
  392.                             timestampProc.process(null,msgSecCtx);
  393.                         }
  394.                     }
  395.                 }
  396.             }




  397.         }
  398.         catch(Exception e){
  399.            
  400.             String msg = Utilities.getInnerNotEmptyMessageException(e).getMessage();
  401.            
  402.             Throwable innerExc = Utilities.getLastInnerException(e);
  403.             String innerMsg = null;
  404.             if(innerExc!=null){
  405.                 innerMsg = innerExc.getMessage();
  406.             }
  407.            
  408.             String messaggio = null;
  409.             if(msg!=null){
  410.                 messaggio = new String(msg);
  411.                 if(innerMsg!=null && !innerMsg.equals(msg)){
  412.                     messaggio = messaggio + " ; " + innerMsg;
  413.                 }
  414.             }
  415.             else{
  416.                 if(innerMsg!=null){
  417.                     messaggio = innerMsg;
  418.                 }
  419.             }
  420.            
  421.             // L'if scopre l'eventuale motivo preciso riguardo al fallimento della cifratura/firma.
  422.             if(Utilities.existsInnerException(e, org.adroitlogic.soapbox.ElementNotFoundException.class)){
  423.                 Throwable t = Utilities.getLastInnerException(e);
  424.                 if(t instanceof org.adroitlogic.soapbox.ElementNotFoundException){
  425.                     String notFoundMessage = t.getMessage();
  426.                     messaggio = t.getMessage().substring(0, notFoundMessage.indexOf("not found as a child of"));
  427.                     messaggio = messaggio + "not found as a child of SoapEnvelope";
  428.                 }
  429.             }
  430.            
  431.             SecurityException wssException = new SecurityException(e.getMessage(), e);
  432.             wssException.setMsgErrore(messaggio);
  433.             throw wssException;

  434.         }

  435.     }


  436.    
  437.     private void updateSecurityContextForSignature(MessageSecurityContext msgSecCtx, Map<String, Object> outProps, String aliasSignatureUser) throws Exception {
  438.        
  439.         Object c14nAlgoURI = outProps.get(SecurityConstants.SIGNATURE_C14N_ALGORITHM);
  440.         Object digestAlgoURI = outProps.get(SecurityConstants.SIGNATURE_DIGEST_ALGORITHM);
  441.         Object signatureAlgoURI = outProps.get(SecurityConstants.SIGNATURE_ALGORITHM);
  442.         Object wsiBPCompliant = outProps.get(SecurityConstants.IS_BSP_COMPLIANT);
  443.         Object signatureKeyIdentifier = outProps.get(SecurityConstants.SIGNATURE_KEY_IDENTIFIER);

  444.         if(c14nAlgoURI!=null){
  445.             msgSecCtx.getSignatureRequest().setC14nAlgoURI((String)c14nAlgoURI);
  446.         }
  447.         if(digestAlgoURI!=null){
  448.             msgSecCtx.getSignatureRequest().setDigestAlgoURI((String)digestAlgoURI);
  449.         }
  450.         if(wsiBPCompliant!=null){
  451.             try{
  452.                 msgSecCtx.getSignatureRequest().setWsiBPCompliant(Boolean.parseBoolean((String)wsiBPCompliant));
  453.             }catch(Exception e){
  454.                 throw new Exception(SecurityConstants.IS_BSP_COMPLIANT+" con valore non valido (atteso true/false): ["+wsiBPCompliant+"]");
  455.             }
  456.         }else{
  457.             msgSecCtx.getSignatureRequest().setWsiBPCompliant(true);
  458.         }
  459.         if(signatureAlgoURI!=null){
  460.             msgSecCtx.getSignatureRequest().setSignatureAlgoURI((String)signatureAlgoURI);
  461.         }else{
  462.             throw new Exception(SecurityConstants.SIGNATURE_ALGORITHM+" non fornito");
  463.         }
  464.         if(signatureKeyIdentifier!=null){
  465.             if(SecurityConstants.KEY_IDENTIFIER_BST_DIRECT_REFERENCE.equals(signatureKeyIdentifier)){
  466.                 msgSecCtx.getSignatureRequest().setKeyIdentifierType(SecurityRequest.KeyIdentifierType.BST_DIRECT_REFERENCE);      
  467.             }
  468.             else if(SecurityConstants.KEY_IDENTIFIER_ISSUER_SERIAL.equals(signatureKeyIdentifier)){
  469.                 msgSecCtx.getSignatureRequest().setKeyIdentifierType(SecurityRequest.KeyIdentifierType.ISSUER_SERIAL);      
  470.             }
  471.             else if(SecurityConstants.KEY_IDENTIFIER_SKI.equals(signatureKeyIdentifier)){
  472.                 msgSecCtx.getSignatureRequest().setKeyIdentifierType(SecurityRequest.KeyIdentifierType.SKI_KEY_IDENTIFIER);    
  473.             }
  474.             else if(SecurityConstants.KEY_IDENTIFIER_THUMBPRINT.equals(signatureKeyIdentifier)){
  475.                 msgSecCtx.getSignatureRequest().setKeyIdentifierType(SecurityRequest.KeyIdentifierType.THUMBPRINT_IDENTIFIER);      
  476.             }
  477.             else if(SecurityConstants.KEY_IDENTIFIER_X509.equals(signatureKeyIdentifier)){
  478.                 msgSecCtx.getSignatureRequest().setKeyIdentifierType(SecurityRequest.KeyIdentifierType.X509_KEY_IDENTIFIER);        
  479.             }
  480.             else{
  481.                 throw new Exception(SecurityConstants.SIGNATURE_KEY_IDENTIFIER+" not supported ["+signatureKeyIdentifier+"]");
  482.             }
  483.         }else{
  484.             // default: BST
  485.             msgSecCtx.getSignatureRequest().setKeyIdentifierType(SecurityRequest.KeyIdentifierType.BST_DIRECT_REFERENCE);  
  486.         }
  487.         msgSecCtx.getSignatureRequest().setCertAlias(aliasSignatureUser);
  488.     }
  489.    
  490.     private void updateSecurityContextForEncryption(MessageSecurityContext msgSecCtx, Map<String, Object> outProps, String aliasEncryptUser) throws Exception {
  491.        
  492.         Object encryptionKeySizeObject = outProps.get(SecurityConstants.ENCRYPTION_KEY_SIZE);
  493.         int encryptionKeySize = -1;
  494.         if(encryptionKeySizeObject!=null){
  495.             encryptionKeySize = Integer.parseInt((String)encryptionKeySizeObject);
  496.         }

  497.         Object encryptionSymmetricAlgoritm = outProps.get(SecurityConstants.ENCRYPTION_SYMMETRIC_ALGORITHM);
  498.         Object encryptionKeyTransport = outProps.get(SecurityConstants.ENCRYPTION_KEY_TRANSPORT_ALGORITHM);
  499.        
  500.         if(encryptionKeySize>0){
  501.             msgSecCtx.getEncryptionRequest().setKeySize(encryptionKeySize);
  502.         }
  503.         if(encryptionSymmetricAlgoritm!=null){
  504.             msgSecCtx.getEncryptionRequest().setSymmetricKeyAlgoURI((String)encryptionSymmetricAlgoritm);
  505.         }
  506.         if(encryptionKeyTransport!=null){
  507.             msgSecCtx.getEncryptionRequest().setEncryptionAlgoURI((String)encryptionKeyTransport);
  508.         }
  509.         Object encryptionKeyIdentifier = outProps.get(SecurityConstants.ENCRYPTION_KEY_IDENTIFIER);
  510.         if(encryptionKeyIdentifier!=null){
  511.             if(SecurityConstants.KEY_IDENTIFIER_BST_DIRECT_REFERENCE.equals(encryptionKeyIdentifier)){
  512.                 msgSecCtx.getEncryptionRequest().setKeyIdentifierType(SecurityRequest.KeyIdentifierType.BST_DIRECT_REFERENCE);      
  513.             }
  514.             else if(SecurityConstants.KEY_IDENTIFIER_ISSUER_SERIAL.equals(encryptionKeyIdentifier)){
  515.                 msgSecCtx.getEncryptionRequest().setKeyIdentifierType(SecurityRequest.KeyIdentifierType.ISSUER_SERIAL);    
  516.             }
  517.             else if(SecurityConstants.KEY_IDENTIFIER_SKI.equals(encryptionKeyIdentifier)){
  518.                 msgSecCtx.getEncryptionRequest().setKeyIdentifierType(SecurityRequest.KeyIdentifierType.SKI_KEY_IDENTIFIER);        
  519.             }
  520.             else if(SecurityConstants.KEY_IDENTIFIER_THUMBPRINT.equals(encryptionKeyIdentifier)){
  521.                 msgSecCtx.getEncryptionRequest().setKeyIdentifierType(SecurityRequest.KeyIdentifierType.THUMBPRINT_IDENTIFIER);    
  522.             }
  523.             else if(SecurityConstants.KEY_IDENTIFIER_X509.equals(encryptionKeyIdentifier)){
  524.                 msgSecCtx.getEncryptionRequest().setKeyIdentifierType(SecurityRequest.KeyIdentifierType.X509_KEY_IDENTIFIER);      
  525.             }
  526.             else{
  527.                 throw new Exception(SecurityConstants.ENCRYPTION_KEY_IDENTIFIER+" not supported ["+encryptionKeyIdentifier+"]");
  528.             }
  529.         }else{
  530.             // default: BST
  531.             msgSecCtx.getEncryptionRequest().setKeyIdentifierType(SecurityRequest.KeyIdentifierType.BST_DIRECT_REFERENCE);  
  532.         }
  533.        
  534.         msgSecCtx.getEncryptionRequest().setCertAlias(aliasEncryptUser);
  535.     }
  536. }