MessageUtilities.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.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.namespace.QName;
  27. import javax.xml.soap.AttachmentPart;

  28. import org.openspcoop2.message.OpenSPCoop2SoapMessage;
  29. import org.openspcoop2.message.soap.SoapUtils;
  30. import org.openspcoop2.message.soap.reference.AttachmentReference;
  31. import org.openspcoop2.message.soap.reference.ElementReference;
  32. import org.openspcoop2.message.soap.reference.Reference;
  33. import org.openspcoop2.security.SecurityException;
  34. import org.openspcoop2.security.message.MessageSecurityContext;
  35. import org.openspcoop2.security.message.SubErrorCodeSecurity;
  36. import org.openspcoop2.security.message.constants.SecurityConstants;
  37. import org.w3c.dom.Node;
  38. import org.w3c.dom.NodeList;

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

  47.    
  48.     public static void checkEncryptionPartElements(Map<QName, QName> notResolved, OpenSPCoop2SoapMessage message, List<SubErrorCodeSecurity> listaErroriInElementi) throws SecurityException {
  49.        
  50.         try{
  51.            
  52.             if(notResolved != null && !notResolved.isEmpty()) {
  53.                 for (QName expected : notResolved.keySet()) {
  54.                     QName actualQName = notResolved.get(expected);
  55.                     boolean found = false;
  56.                     NodeList it = message.getSOAPPart().getElementsByTagNameNS(actualQName.getNamespaceURI(), actualQName.getLocalPart());
  57.                     for (int j = 0; j < it.getLength() && !found; j++) {
  58.                         Node elementFather = (Node) it.item(j);
  59.                         //SOAPElement elementFather = (SOAPElement) it.item(j);
  60.                         //List<SOAPElement> encryptedElements = SoapUtils.getNotEmptyChildSOAPElement(elementFather);
  61.                         List<Node> encryptedElements = SoapUtils.getNotEmptyChildNodes(message.getFactory(), elementFather, false);
  62.                         for (int i = 0; i < encryptedElements.size() && !found; i++) {
  63.                             //SOAPElement actual = encryptedElements.get(i);
  64.                             Node actual = encryptedElements.get(i);
  65.                             String actualNamespaceURI = actual.getNamespaceURI();
  66.                             String actualLocalName = actual.getLocalName();
  67.                             String expectedNamespaceURI = expected.getNamespaceURI();
  68.                             String expectedLocalName = expected.getLocalPart();
  69.                             if(((actualNamespaceURI == null && expectedNamespaceURI == null) || (actualNamespaceURI!=null && actualNamespaceURI.equals(expectedNamespaceURI)))
  70.                                     && actualLocalName.equals(expectedLocalName)) {
  71.                                     //trovato l'elemento che ci interessa
  72.                                     found = true;
  73.                             }
  74.                         }
  75.                     }
  76.                     if(!found) {
  77.                         SubErrorCodeSecurity subCodice = new SubErrorCodeSecurity();
  78.                         subCodice.setEncrypt(true);
  79.                         subCodice.setMsgErrore("Expected encryption part("+ expected +") not found");
  80.                         subCodice.setTipo(SecurityConstants.ENCRYPTION_PART_CONTENT);
  81.                         subCodice.setNamespace(expected.getNamespaceURI());
  82.                         subCodice.setName(expected.getLocalPart());
  83.                         listaErroriInElementi.add(subCodice);
  84.                     }
  85.                 }
  86.             }
  87.            
  88.         }catch(Exception e){
  89.             throw new SecurityException(e.getMessage(),e);
  90.         }
  91.     }

  92.     public static Map<QName, QName> checkEncryptSignatureParts(MessageSecurityContext messageSecurityContext,List<Reference> elementsToClean, OpenSPCoop2SoapMessage message,
  93.             List<SubErrorCodeSecurity> codiciErrore, QName qnameSecurity) throws SecurityException {
  94.        
  95.         Map<String, Object> properties = messageSecurityContext.getIncomingProperties();
  96.         boolean addAttachmentIdBrackets = properties.containsKey(SecurityConstants.ADD_ATTACHMENT_ID_BRACKETS) ?
  97.                 properties.get(SecurityConstants.ADD_ATTACHMENT_ID_BRACKETS).equals(SecurityConstants.ADD_ATTACHMENT_ID_BRACKETS_TRUE) :
  98.                     SecurityConstants.ADD_ATTACHMENT_ID_BRACKETS_DEFAULT;
  99.         try{
  100.             int numAttachmentsInMsg = message.countAttachments();
  101.             List<String> cidAttachments = new ArrayList<>();
  102.             if(numAttachmentsInMsg>0){
  103.                 Iterator<?> itAttach = message.getAttachments();
  104.                 while (itAttach.hasNext()) {
  105.                     AttachmentPart ap = (AttachmentPart) itAttach.next();
  106.                     String cid = ap.getContentId();
  107.                     if (!addAttachmentIdBrackets) {
  108.                         cid = cid.replaceAll("(^<)|(>$)", "");
  109.                     }
  110.                     cidAttachments.add(cid);
  111.                 }
  112.             }
  113.        
  114.             // *** ENCRYPT VERIFY ***
  115.             Map<QName, QName> notResolvedMap = new HashMap<QName, QName>();
  116.             Object encryptionPartsVerify = messageSecurityContext.getIncomingProperties().get(SecurityConstants.ENCRYPTION_PARTS_VERIFY);
  117.             Object encryptionParts = messageSecurityContext.getIncomingProperties().get(SecurityConstants.ENCRYPTION_PARTS);
  118.             if(encryptionPartsVerify!=null && "true".equalsIgnoreCase(((String)encryptionPartsVerify).trim()) &&
  119.                     encryptionParts==null){
  120.                 // Se forzo la proprietà verify allora devo indicare le encryption parts
  121.                 throw new SecurityException(SecurityConstants.ENCRYPTION_PARTS+" non indicate");
  122.             }
  123.             if( (
  124.                     encryptionPartsVerify==null ||
  125.                     "true".equalsIgnoreCase(((String)encryptionPartsVerify).trim())
  126.                 ) &&
  127.                     (encryptionParts!=null) ){
  128.                
  129.                 Map<String, Boolean> encryptionPartsMap = new HashMap<String, Boolean>();
  130.                 Map<Reference, Boolean> encryptionReferenceMap = new HashMap<Reference, Boolean>();
  131.                 boolean isAllAttachmentEncrypted = false;
  132.                 int numAttachmentsEncrypted = 0;
  133.                 if(encryptionParts!=null){
  134.                     String[]split = ((String)encryptionParts).split(";");
  135.                     int numElementsEncrypted = 0;
  136.                     for (int i = 0; i < split.length; i++) {
  137.                         boolean checked = false;
  138.                         String[]split2 = split[i].trim().split("}");
  139.                         String tipo = split2[0].trim().substring(1); // Element o Content
  140.                         if("".equals(tipo)){
  141.                             // caso speciale wss4j {}cid:Attachments
  142.                             tipo = SecurityConstants.PART_CONTENT;
  143.                         }
  144.                         String namespace = null;
  145.                         String nome = null;
  146.                         boolean attach = false;
  147.                         if(split2.length==3){
  148.                             namespace = split2[1].trim().substring(1);
  149.                             nome = split2[2].trim();
  150.                             attach = SecurityConstants.ENCRYPTION_NAMESPACE_ATTACH.equals(namespace);
  151.                         }
  152.                         else{
  153.                             // caso speciale wss4j {}cid:Attachments ?
  154.                             if(SecurityConstants.CID_ATTACH_WSS4J.equalsIgnoreCase(split2[1].trim())){
  155.                                 namespace = SecurityConstants.ENCRYPTION_NAMESPACE_ATTACH;
  156.                                 nome = SecurityConstants.ATTACHMENT_INDEX_ALL;
  157.                                 attach = true;
  158.                             }
  159.                             else{
  160.                                 throw new Exception("Part ["+split[i]+"] with wrong format");
  161.                             }
  162.                         }
  163.                         if(SecurityConstants.ENCRYPTION_PART_ELEMENT.equals(tipo)) {
  164.                             // incremento solamente se non è un attachments
  165.                             if(!attach){
  166.                                 numElementsEncrypted++;
  167.                             }
  168.                         }
  169.                         if(nome.startsWith("{"))
  170.                             nome = nome.substring(1);
  171.                         //System.out.println("CIFRO ["+tipo+"] ["+namespace+"] ["+nome+"]");    
  172.                        
  173.                         List<String> cidAttachmentsEncrypt = new ArrayList<>();
  174.                         if(cidAttachments!=null && cidAttachments.size()>0){
  175.                             cidAttachmentsEncrypt.addAll(cidAttachments);
  176.                         }
  177.                        
  178.                         for (Reference reference : elementsToClean) {
  179.                        
  180.                             if(reference instanceof AttachmentReference) {
  181.                                 if(AttachmentReference.TYPE_ENCRYPT_ATTACHMENT==reference.getType()){
  182.                                     if(SecurityConstants.ENCRYPTION_NAMESPACE_ATTACH.equals(namespace)){
  183.                                         numAttachmentsEncrypted++;
  184.                                         if(nome.equals(SecurityConstants.ATTACHMENT_INDEX_ALL)) {
  185.                                             checked = true;
  186.                                             isAllAttachmentEncrypted = true;
  187.                                             encryptionReferenceMap.put(reference, true);
  188.                                             encryptionPartsMap.put(split[i], checked);
  189.                                             // break;
  190.                                             // Vanno iterate tutte, dovendole contare
  191.                                         }else{
  192.                                             int position = -1;
  193.                                             try{
  194.                                                 position = Integer.parseInt(nome);
  195.                                             }catch(Exception e){
  196.                                                 // position non presente
  197.                                             }
  198.                                             if(position>0){
  199.                                                 int refPosition = -1;
  200.                                                 for (int j = 0; j < cidAttachments.size(); j++) {
  201.                                                     if(cidAttachments.get(j).equals(reference.getReference())){
  202.                                                         refPosition = j+1;
  203.                                                         break;
  204.                                                     }
  205.                                                 }
  206.                                                 //if(refPosition == position){ // Alcune implementazioni durante la spedizione modificano l'ordine degli attachments.
  207.                                                 // Non è possibile effettuare tale controllo sulla ricezione. Può essere usato solo per specificare quale attach firmare/cifrare in spedizione
  208.                                                 // verifico solamente che il cid sia presente
  209.                                                 if(refPosition>0){
  210.                                                     cidAttachmentsEncrypt.remove((refPosition-1));
  211.                                                     checked = true;
  212.                                                     encryptionReferenceMap.put(reference, true);
  213.                                                     encryptionPartsMap.put(split[i], checked);
  214.                                                     break;
  215.                                                 }
  216.                                             }
  217.                                             else{
  218.                                                 if(nome.equals(reference.getReference())) {
  219.                                                     checked = true;
  220.                                                     encryptionReferenceMap.put(reference, true);
  221.                                                     encryptionPartsMap.put(split[i], checked);
  222.                                                     break;
  223.                                                 }
  224.                                             }
  225.                                         }
  226.                                     }
  227.                                 }
  228.                             } else {
  229.                                 ElementReference elementReference = ((ElementReference)reference);
  230.                                 String localName = elementReference.getElement().getLocalName();
  231.                                 String namespaceURI = elementReference.getElement().getNamespaceURI();
  232.                                 if(ElementReference.TYPE_ENCRYPT_CONTENT==reference.getType()){
  233.                                     // Check encrypt content
  234.                                     if(nome.equals(localName) && namespace.equals(namespaceURI)) {
  235.                                         checked = SecurityConstants.ENCRYPTION_PART_CONTENT.equals(tipo);
  236.                                         encryptionReferenceMap.put(reference, checked);
  237.                                         encryptionPartsMap.put(split[i], checked);
  238.                                         if(checked){
  239.                                             break;
  240.                                         }
  241.                                     }
  242.                                 } else if(ElementReference.TYPE_ENCRYPT_ELEMENT==reference.getType() && SecurityConstants.ENCRYPTION_PART_ELEMENT.equals(tipo)) {
  243.                                         // Segnamo l'elemento come checked, e lo aggiungiamo alla lista di elementi da verificare
  244.                                         // dopo la decifratura, visto che allo stato attuale e' imposibile verificarlo
  245.                                         checked = true;
  246.                                         encryptionPartsMap.put(split[i], checked);
  247.                                         QName actual = new QName(namespaceURI, localName);
  248.                                         QName expected = new QName(namespace, nome);
  249.                                         boolean localPartSec = localName.equals(qnameSecurity.getLocalPart());
  250.                                         boolean namespaceURISec = namespaceURI.equals(qnameSecurity.getNamespaceURI());
  251.                                         if(!(localPartSec && namespaceURISec)) {
  252.                                             if(!notResolvedMap.containsKey(expected)) {
  253.                                                 notResolvedMap.put(expected, actual);
  254.                                             }
  255.                                         }
  256.                                 }
  257.                             }
  258.                         }
  259.                     }
  260.                    
  261.                     int numElementsExpectedToBeEncrypted = 0;
  262.                     for(Reference reference : elementsToClean) {
  263.                         if(reference.getType()==ElementReference.TYPE_ENCRYPT_ELEMENT) {
  264.                             numElementsExpectedToBeEncrypted++;
  265.                         }
  266.                     }
  267.                    
  268.                     if(numElementsExpectedToBeEncrypted != numElementsEncrypted) {
  269.                         SubErrorCodeSecurity subCodice = new SubErrorCodeSecurity();
  270.                         subCodice.setEncrypt(true);
  271.                         subCodice.setMsgErrore("Expected encryption {Element} " + numElementsExpectedToBeEncrypted + ", found " + notResolvedMap.size());
  272.                         codiciErrore.add(subCodice);
  273.                     }
  274.                 }
  275.                 if(encryptionParts!=null){
  276.                     String[]split = ((String)encryptionParts).split(";");
  277.                     for (int i = 0; i < split.length; i++) {
  278.                         String[]split2 = split[i].trim().split("}");
  279.                         String tipo = split2[0].trim().substring(1); // Element o Content
  280.                         if("".equals(tipo)){
  281.                             // caso speciale wss4j {}cid:Attachments
  282.                             tipo = SecurityConstants.PART_CONTENT;
  283.                         }
  284.                         String namespace = null;
  285.                         String nome = null;
  286.                         if(split2.length==3){
  287.                             namespace = split2[1].trim().substring(1);
  288.                             nome = split2[2].trim();
  289.                         }
  290.                         else{
  291.                             // caso speciale wss4j {}cid:Attachments ?
  292.                             if(SecurityConstants.CID_ATTACH_WSS4J.equalsIgnoreCase(split2[1].trim())){
  293.                                 namespace = SecurityConstants.ENCRYPTION_NAMESPACE_ATTACH;
  294.                                 nome = SecurityConstants.ATTACHMENT_INDEX_ALL;
  295.                             }
  296.                             else{
  297.                                 throw new Exception("Part ["+split[i]+"] with wrong format");
  298.                             }
  299.                         }
  300.                         if(nome.startsWith("{"))
  301.                             nome = nome.substring(1);
  302.                         if(SecurityConstants.ENCRYPTION_PART_CONTENT.equals(tipo)){
  303.                             if(!encryptionPartsMap.containsKey(split[i]) || !encryptionPartsMap.get(split[i])) {
  304.                                 SubErrorCodeSecurity subCodice = new SubErrorCodeSecurity();
  305.                                 subCodice.setEncrypt(true);
  306.                                 subCodice.setMsgErrore("Expected encryption part("+ split[i] +") not found");
  307.                                 subCodice.setTipo(tipo);
  308.                                 subCodice.setNamespace(namespace);
  309.                                 subCodice.setName(nome);
  310.                                 codiciErrore.add(subCodice);
  311.     //                          throw new Exception("Expected encryption part("+ split[i] +") not found");
  312.                             }
  313.                         }
  314.                     }
  315.                 }
  316.            
  317.                 if(isAllAttachmentEncrypted && numAttachmentsEncrypted != numAttachmentsInMsg) {
  318.                     SubErrorCodeSecurity subCodice = new SubErrorCodeSecurity();
  319.                     subCodice.setEncrypt(true);
  320.                     subCodice.setMsgErrore("All attachments in message (found:"+numAttachmentsInMsg+") must be encrypted, but only "+numAttachmentsEncrypted+" appear to be encrypted");
  321.                     subCodice.setTipo(SecurityConstants.ENCRYPTION_PART_CONTENT);
  322.                     subCodice.setNamespace(SecurityConstants.ENCRYPTION_NAMESPACE_ATTACH);
  323.                     subCodice.setName(SecurityConstants.ATTACHMENT_INDEX_ALL);
  324.                     codiciErrore.add(subCodice);
  325.                     //throw new Exception("All attachments in message (found:"+numAttachmentsInMsg+") must be encrypted, but only "+numAttachmentsEncrypted+" appear to be encrypted");
  326.                 }
  327.                
  328.                 for (Reference reference: elementsToClean) {
  329.                     if(reference.getType()==ElementReference.TYPE_ENCRYPT_CONTENT || reference.getType()==AttachmentReference.TYPE_ENCRYPT_ATTACHMENT) {
  330.                         if(!encryptionReferenceMap.containsKey(reference) || !encryptionReferenceMap.get(reference)) {
  331.                             SubErrorCodeSecurity subCodice = new SubErrorCodeSecurity();
  332.                             subCodice.setEncrypt(true);
  333.                             subCodice.setMsgErrore("Found encryption part("+ reference+") not expected");
  334.                             if(reference.getType()==ElementReference.TYPE_ENCRYPT_CONTENT){
  335.                                 ElementReference elReference = (ElementReference) reference;
  336.                                 subCodice.setTipo(SecurityConstants.ENCRYPTION_PART_CONTENT);
  337.                                 subCodice.setNamespace(elReference.getElement().getNamespaceURI());
  338.                                 subCodice.setName(elReference.getElement().getLocalName());
  339.                             }
  340.                             else{
  341.                                 subCodice.setTipo(SecurityConstants.ENCRYPTION_PART_CONTENT);
  342.                                 subCodice.setNamespace(SecurityConstants.ENCRYPTION_NAMESPACE_ATTACH);
  343.                                 subCodice.setName(reference.getReference());
  344.                             }
  345.                             codiciErrore.add(subCodice);
  346.                             //throw new Exception("Found encryption part("+ reference+") not expected");
  347.                         }
  348.                     }
  349.                 }
  350.                
  351.             }
  352.                    
  353.             // *** SIGNATURE VERIFY ***
  354.             Object signaturePartsVerify = messageSecurityContext.getIncomingProperties().get(SecurityConstants.SIGNATURE_PARTS_VERIFY);
  355.             Object signatureParts = messageSecurityContext.getIncomingProperties().get(SecurityConstants.SIGNATURE_PARTS);
  356.             if(signaturePartsVerify!=null && "true".equalsIgnoreCase(((String)signaturePartsVerify).trim()) &&
  357.                     signatureParts==null){
  358.                 // Se forzo la proprietà verify allora devo indicare le signature parts
  359.                 throw new SecurityException(SecurityConstants.SIGNATURE_PARTS+" non indicate");
  360.             }
  361.             if( (
  362.                     signaturePartsVerify==null ||
  363.                     "true".equalsIgnoreCase(((String)signaturePartsVerify).trim())
  364.                 ) &&
  365.                     (signatureParts!=null) ){
  366.                    
  367.                 boolean isAllAttachmentSigned = false;
  368.                 int numAttachmentsSigned = 0;
  369.                 Map<String, Boolean> signaturePartsMap = new HashMap<String, Boolean>();
  370.                 Map<Reference, Boolean> referenceMap = new HashMap<Reference, Boolean>();
  371.                 if(signatureParts!=null){
  372.                     String[]split = ((String)signatureParts).split(";");
  373.                     for (int i = 0; i < split.length; i++) {
  374.                         boolean checked = false;
  375.                         String[]split2 = split[i].trim().split("}");
  376.                         String tipo = split2[0].trim().substring(1); // Element o Content
  377.                         if("".equals(tipo)){
  378.                             // caso speciale wss4j {}cid:Attachments
  379.                             tipo = SecurityConstants.PART_CONTENT;
  380.                         }
  381.                         String namespace = null;
  382.                         String nome = null;
  383.                         if(split2.length==3){
  384.                             namespace = split2[1].trim().substring(1);
  385.                             nome = split2[2].trim();
  386.                         }
  387.                         else{
  388.                             // caso speciale wss4j {}cid:Attachments ?
  389.                             if(SecurityConstants.CID_ATTACH_WSS4J.equalsIgnoreCase(split2[1].trim())){
  390.                                 namespace = SecurityConstants.SIGNATURE_NAMESPACE_ATTACH;
  391.                                 nome = SecurityConstants.ATTACHMENT_INDEX_ALL;
  392.                             }
  393.                             else{
  394.                                 throw new Exception("Part ["+split[i]+"] with wrong format");
  395.                             }
  396.                         }
  397.                         if(nome.startsWith("{"))
  398.                             nome = nome.substring(1);
  399.                         //System.out.println("CIFRO ["+tipo+"] ["+namespace+"] ["+nome+"]");    
  400.                        
  401.                         List<String> cidAttachmentsSignature = new ArrayList<>();
  402.                         if(cidAttachments!=null && cidAttachments.size()>0){
  403.                             cidAttachmentsSignature.addAll(cidAttachments);
  404.                         }
  405.                        
  406.                         for (Reference reference : elementsToClean) {
  407.                        
  408.                             if(reference instanceof AttachmentReference) {
  409.                                 if(AttachmentReference.TYPE_SIGN_ATTACHMENT==reference.getType()){
  410.                                     if(SecurityConstants.SIGNATURE_NAMESPACE_ATTACH.equals(namespace)){
  411.                                         numAttachmentsSigned++;
  412.                                         if(nome.equals(SecurityConstants.ATTACHMENT_INDEX_ALL)) {
  413.                                             checked = true;
  414.                                             isAllAttachmentSigned = true;
  415.                                             referenceMap.put(reference, true);
  416.                                             // break;
  417.                                             // Vanno iterate tutte, dovendole contare
  418.                                         }
  419.                                         else{
  420.                                             int position = -1;
  421.                                             try{
  422.                                                 position = Integer.parseInt(nome);
  423.                                             }catch(Exception e){
  424.                                                 // position non presente
  425.                                             }
  426.                                             if(position>0){
  427.                                                 int refPosition = -1;
  428.                                                 for (int j = 0; j < cidAttachments.size(); j++) {
  429.                                                     if(cidAttachments.get(j).equals(reference.getReference())){
  430.                                                         refPosition = j+1;
  431.                                                         break;
  432.                                                     }
  433.                                                 }
  434.                                                 //if(refPosition == position){ // Alcune implementazioni durante la spedizione modificano l'ordine degli attachments.
  435.                                                 // Non è possibile effettuare tale controllo sulla ricezione. Può essere usato solo per specificare quale attach firmare/cifrare in spedizione
  436.                                                 // verifico solamente che il cid sia presente
  437.                                                 if(refPosition>0){
  438.                                                     cidAttachmentsSignature.remove((refPosition-1));
  439.                                                     checked = true;
  440.                                                     referenceMap.put(reference, true);
  441.                                                     break;
  442.                                                 }
  443.                                             }
  444.                                             else{
  445.                                                 if(nome.equals(reference.getReference())) {
  446.                                                     checked = true;
  447.                                                     referenceMap.put(reference, true);
  448.                                                     break;
  449.                                                 }
  450.                                             }
  451.                                         }
  452.                                     }
  453.                                 }
  454.                             } else {
  455.                                 ElementReference elementReference = ((ElementReference)reference);
  456.                                 String localName = elementReference.getElement().getLocalName();
  457.                                 String namespaceURI = elementReference.getElement().getNamespaceURI();
  458.                                 if(ElementReference.TYPE_SIGNATURE==reference.getType() && nome.equals(localName) && namespace.equals(namespaceURI)) {
  459.                                     checked = SecurityConstants.SIGNATURE_PART_COMPLETE.equals(tipo) ||
  460.                                             SecurityConstants.SIGNATURE_PART_CONTENT.equals(tipo) || SecurityConstants.SIGNATURE_PART_ELEMENT.equals(tipo);
  461.                                     referenceMap.put(reference, checked);
  462.                                     if(checked){
  463.                                         break;
  464.                                     }
  465.                                 }
  466.                             }
  467.                         }
  468.                         signaturePartsMap.put(split[i], checked);
  469.                     }
  470.                 }
  471.                
  472.                 for (String part : signaturePartsMap.keySet()) {
  473.                     if(!signaturePartsMap.get(part)) {
  474.                         String[]split2 = part.trim().split("}");
  475.                         String tipo = split2[0].trim().substring(1); // Element o Content
  476.                         if("".equals(tipo)){
  477.                             // caso speciale wss4j {}cid:Attachments
  478.                             tipo = SecurityConstants.PART_CONTENT;
  479.                         }
  480.                         String namespace = null;
  481.                         String nome = null;
  482.                         if(split2.length==3){
  483.                             namespace = split2[1].trim().substring(1);
  484.                             nome = split2[2].trim();
  485.                         }
  486.                         else{
  487.                             // caso speciale wss4j {}cid:Attachments ?
  488.                             if(SecurityConstants.CID_ATTACH_WSS4J.equalsIgnoreCase(split2[1].trim())){
  489.                                 namespace = SecurityConstants.SIGNATURE_NAMESPACE_ATTACH;
  490.                                 nome = SecurityConstants.ATTACHMENT_INDEX_ALL;
  491.                             }
  492.                             else{
  493.                                 throw new Exception("Part ["+part+"] with wrong format");
  494.                             }
  495.                         }
  496.                         if(nome.startsWith("{"))
  497.                             nome = nome.substring(1);
  498.                         SubErrorCodeSecurity subCodice = new SubErrorCodeSecurity();
  499.                         subCodice.setEncrypt(false);
  500.                         subCodice.setMsgErrore("Expected signature part("+ part +") not found");
  501.                         subCodice.setTipo(tipo);
  502.                         subCodice.setNamespace(namespace);
  503.                         subCodice.setName(nome);
  504.                         codiciErrore.add(subCodice);
  505.                         //throw new Exception("Expected signature part("+ part +") not found");
  506.                     }
  507.                 }
  508.                
  509.        
  510.                 if(isAllAttachmentSigned && numAttachmentsSigned != numAttachmentsInMsg) {
  511.                     SubErrorCodeSecurity subCodice = new SubErrorCodeSecurity();
  512.                     subCodice.setEncrypt(false);
  513.                     subCodice.setMsgErrore("All attachments in message (found:"+numAttachmentsInMsg+") must be signed, but only "+numAttachmentsSigned+" appear to be signed");
  514.                     subCodice.setTipo(SecurityConstants.SIGNATURE_PART_CONTENT);
  515.                     subCodice.setNamespace(SecurityConstants.SIGNATURE_NAMESPACE_ATTACH);
  516.                     subCodice.setName(SecurityConstants.ATTACHMENT_INDEX_ALL);
  517.                     codiciErrore.add(subCodice);
  518.                     //throw new Exception("All attachments in message (found:"+numAttachmentsInMsg+") must be signed, but "+numAttachmentsSigned+" appear to be signed");
  519.                 }
  520.                
  521.                 for (Reference reference: elementsToClean) {
  522.                     if(reference.getType()==ElementReference.TYPE_SIGNATURE || reference.getType()==AttachmentReference.TYPE_SIGN_ATTACHMENT) {
  523.                         if(!referenceMap.containsKey(reference) || !referenceMap.get(reference)) {
  524.                             SubErrorCodeSecurity subCodice = new SubErrorCodeSecurity();
  525.                             subCodice.setEncrypt(false);
  526.                             subCodice.setMsgErrore("Found signature part("+ reference+") not expected");
  527.                             if(reference.getType()==ElementReference.TYPE_SIGNATURE){
  528.                                 ElementReference elReference = (ElementReference) reference;
  529.                                 subCodice.setTipo("Signature"); // Non esiste Content/Element
  530.                                 subCodice.setNamespace(elReference.getElement().getNamespaceURI());
  531.                                 subCodice.setName(elReference.getElement().getLocalName());
  532.                             }
  533.                             else{
  534.                                 subCodice.setTipo(SecurityConstants.SIGNATURE_PART_CONTENT);
  535.                                 subCodice.setNamespace(SecurityConstants.ENCRYPTION_NAMESPACE_ATTACH);
  536.                                 subCodice.setName(reference.getReference());
  537.                             }
  538.                             codiciErrore.add(subCodice);                        
  539.                             //throw new Exception("Found signature part("+ reference+") not expected");
  540.                         }
  541.                     }
  542.                 }
  543.             }
  544.            
  545.             return notResolvedMap;
  546.            
  547.         }catch(Exception e){
  548.             throw new SecurityException(e.getMessage(),e);
  549.         }
  550.     }
  551.    
  552. }