ModIUtils.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.protocol.utils;

  21. import java.lang.reflect.Method;
  22. import java.util.ArrayList;
  23. import java.util.HashMap;
  24. import java.util.List;
  25. import java.util.Map;

  26. import org.apache.commons.lang.StringUtils;
  27. import org.openspcoop2.core.config.constants.StatoFunzionalita;
  28. import org.openspcoop2.core.constants.CostantiDB;
  29. import org.openspcoop2.core.constants.CostantiLabel;
  30. import org.openspcoop2.core.id.IDAccordo;
  31. import org.openspcoop2.core.id.IDServizioApplicativo;
  32. import org.openspcoop2.core.id.IDSoggetto;
  33. import org.openspcoop2.core.registry.AccordoServizioParteComune;
  34. import org.openspcoop2.core.registry.AccordoServizioParteSpecifica;
  35. import org.openspcoop2.core.registry.Fruitore;
  36. import org.openspcoop2.core.registry.Proprieta;
  37. import org.openspcoop2.core.registry.ProtocolProperty;
  38. import org.openspcoop2.core.registry.Soggetto;
  39. import org.openspcoop2.core.registry.constants.ServiceBinding;
  40. import org.openspcoop2.core.registry.driver.IDAccordoFactory;
  41. import org.openspcoop2.core.registry.utils.RegistroServiziUtils;
  42. import org.openspcoop2.message.OpenSPCoop2Message;
  43. import org.openspcoop2.protocol.sdk.Busta;
  44. import org.openspcoop2.protocol.sdk.ProtocolException;
  45. import org.openspcoop2.utils.MapKey;
  46. import org.openspcoop2.utils.certificate.KeystoreParams;
  47. import org.openspcoop2.utils.certificate.KeystoreType;
  48. import org.openspcoop2.utils.certificate.hsm.HSMUtils;
  49. import org.openspcoop2.utils.certificate.remote.RemoteKeyType;
  50. import org.openspcoop2.utils.certificate.remote.RemoteStoreConfig;
  51. import org.openspcoop2.utils.digest.DigestEncoding;
  52. import org.openspcoop2.utils.io.Base64Utilities;
  53. import org.openspcoop2.utils.json.JsonPathExpressionEngine;
  54. import org.slf4j.Logger;

  55. /**
  56.  * ModIUtils
  57.  *
  58.  * @author Poli Andrea (apoli@link.it)
  59.  * @author $Author$
  60.  * @version $Rev$, $Date$
  61.  */
  62. public class ModIUtils {
  63.    
  64.     private ModIUtils() {}

  65.     // COSTANTI boolean
  66.     public static final String INTEGRITY = "integrity";
  67.     public static final String DIGEST_RICHIESTA = "request-digest";
  68.     public static final String CORNICE_SICUREZZA = "user-info";
  69.     public static final String CORNICE_SICUREZZA_PATTERN = "user-info-pattern";
  70.     public static final String CORNICE_SICUREZZA_SCHEMA = "user-info-schema";
  71.     public static final String HEADER_DUPLICATI = "contemporary-headers";
  72.     public static boolean isBooleanIndicator(String key) {
  73.         return ModIUtils.INTEGRITY.equals(key) ||
  74.                 ModIUtils.DIGEST_RICHIESTA.equals(key) ||
  75.                 ModIUtils.CORNICE_SICUREZZA.equals(key) ||
  76.                 ModIUtils.HEADER_DUPLICATI.equals(key);
  77.     }
  78.    
  79.     // COSTANTI string
  80.     private static final String API_PREFIX = "api-";
  81.     public static final String API_SICUREZZA_CANALE_PATTERN = API_PREFIX+"channel-security-pattern";
  82.     public static final String API_SICUREZZA_MESSAGGIO_PREFIX = API_PREFIX+"message-security";
  83.     public static final String API_SICUREZZA_MESSAGGIO_PATTERN = API_SICUREZZA_MESSAGGIO_PREFIX+"-pattern";
  84.     public static final String API_SICUREZZA_MESSAGGIO_SORGENTE_TOKEN_ID_AUTH = API_SICUREZZA_MESSAGGIO_PREFIX+"-sorgente-token-id-auth";
  85.     public static final String API_SICUREZZA_MESSAGGIO_HTTP_HEADER = API_SICUREZZA_MESSAGGIO_PREFIX+"-http-header";
  86.     public static final String API_SICUREZZA_MESSAGGIO_APPLICABILITA = API_SICUREZZA_MESSAGGIO_PREFIX+"-applicability";
  87.     public static final String API_SICUREZZA_MESSAGGIO_REQUEST_DIGEST = API_SICUREZZA_MESSAGGIO_PREFIX+"-request-digest";
  88.     public static final String API_SICUREZZA_MESSAGGIO_USER_INFO = API_SICUREZZA_MESSAGGIO_PREFIX+"-user-info";
  89.    
  90.     private static final String REQUEST_PREFIX = "request-";
  91.     private static final String RESPONSE_PREFIX = "response-";
  92.    
  93.     public static final String API_IMPL_SICUREZZA_MESSAGGIO_SIGNATURE_ALGORITHM = "signature-algorithm";
  94.     public static final String API_IMPL_SICUREZZA_MESSAGGIO_CANONICALIZATION_ALGORITHM = "canonicalization-algorithm";
  95.     public static final String API_IMPL_SICUREZZA_MESSAGGIO_DIGEST_ENCODING = "digest-encoding";
  96.     public static final String API_IMPL_SICUREZZA_MESSAGGIO_HTTP_HEADER_FIRMATI = "signed-http-headers";
  97.     public static final String API_IMPL_SICUREZZA_MESSAGGIO_SOAP_HEADER_FIRMATI = "signed-soap-headers";
  98.     public static final String API_IMPL_SICUREZZA_MESSAGGIO_RIFERIMENTO_X509 = "x509-reference";
  99.     public static final String API_IMPL_SICUREZZA_MESSAGGIO_CATENA_CERTIFICATI_X509 = "x509-certificate-chain";
  100.     public static final String API_IMPL_SICUREZZA_MESSAGGIO_INCLUDE_SIGNATURE_TOKEN = "include-signature-token";
  101.     public static final String API_IMPL_SICUREZZA_MESSAGGIO_TTL = "ttl";
  102.     public static final String API_IMPL_SICUREZZA_MESSAGGIO_AUDIENCE = "audience";
  103.     public static final String API_IMPL_SICUREZZA_MESSAGGIO_VERIFICA_AUDIENCE = "audience-verify";
  104.     public static final String API_IMPL_SICUREZZA_MESSAGGIO_INTEGRITY_AUDIENCE = "integrity-audience";
  105.     public static final String API_IMPL_SICUREZZA_MESSAGGIO_FRUIZIONE_KEYSTORE_MODE = "fruizione-keystore-mode";
  106.     public static final String API_IMPL_SICUREZZA_MESSAGGIO_X5U_CERTIFICATE_URL = "x5u-certificate-url";
  107.     public static final String API_IMPL_SICUREZZA_MESSAGGIO_CORNICE_SICUREZZA_PREFIX = "audit-info-";
  108.     public static final String API_IMPL_SICUREZZA_MESSAGGIO_AUDIT_AUDIENCE = "audit-audience";
  109.    
  110.     public static final String API_IMPL_SICUREZZA_MESSAGGIO_STORE_TYPE = "type";
  111.     public static final String API_IMPL_SICUREZZA_MESSAGGIO_STORE_PATH = "path";
  112.     public static final String API_IMPL_SICUREZZA_MESSAGGIO_STORE_CRLS = "crls";
  113.     public static final String API_IMPL_SICUREZZA_MESSAGGIO_STORE_OCSP_POLICY = "ocsp";
  114.     public static final String API_IMPL_SICUREZZA_MESSAGGIO_KEY_ALIAS = "key-alias";
  115.    
  116.     public static final String API_IMPL_SICUREZZA_OAUTH_IDENTIFICATIVO = "oauth-id";
  117.     public static final String API_IMPL_SICUREZZA_OAUTH_KID = "oauth-kid";
  118.    
  119.     public static final String API_IMPL_INFO_ESERVICE_ID = "info-eservice-id";
  120.     public static final String API_IMPL_INFO_DESCRIPTOR_ID = "info-descriptor-id";
  121.     public static final String API_IMPL_INFO_SIGNAL_HUB = "info-signal-hub";
  122.    
  123.        
  124.    
  125.     // COSTANTI SERVIZIO
  126.     public static final String HSM = "hsm";
  127.    
  128.     public static Map<String, String> configToMap(AccordoServizioParteComune aspc, AccordoServizioParteSpecifica asps,
  129.             String urlInvocazione,
  130.             Fruitore fruitore, String urlConnettoreFruitoreModI
  131.             ) throws ProtocolException{
  132.        
  133.         boolean gestioneErogatori = (fruitore==null);
  134.         boolean gestioneFruitori = !gestioneErogatori;
  135.        
  136.         Map<String, String> map = new HashMap<>();
  137.                
  138.         List<ProtocolProperty> protocolPropertyList = gestioneErogatori ? asps.getProtocolPropertyList() : fruitore.getProtocolPropertyList();
  139.        
  140.         boolean rest = ServiceBinding.REST.equals(aspc.getServiceBinding());
  141.         boolean digest = isProfiloSicurezzaMessaggioConIntegritaX509(aspc, asps.getPortType()) || isProfiloSicurezzaMessaggioConIntegritaKid(aspc, asps.getPortType());
  142.         boolean digestRichiesta = isProfiloSicurezzaMessaggioRequestDigest(aspc, asps.getPortType());
  143.         boolean corniceSicurezza = isProfiloSicurezzaMessaggioCorniceSicurezza(aspc, asps.getPortType());
  144.         String patternDatiCorniceSicurezza = null;
  145.         String schemaDatiCorniceSicurezza = null;
  146.         if(corniceSicurezza) {
  147.             patternDatiCorniceSicurezza = getProfiloSicurezzaMessaggioCorniceSicurezzaPattern(aspc, asps.getPortType());
  148.             if(patternDatiCorniceSicurezza==null) {
  149.                 // backward compatibility
  150.                 patternDatiCorniceSicurezza = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CORNICE_SICUREZZA_PATTERN_OLD;
  151.             }
  152.             if(!CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CORNICE_SICUREZZA_PATTERN_OLD.equals(patternDatiCorniceSicurezza)) {
  153.                 schemaDatiCorniceSicurezza = getProfiloSicurezzaMessaggioCorniceSicurezzaSchema(aspc, asps.getPortType());
  154.             }
  155.         }
  156.         boolean headerDuplicati = false;
  157.         if(rest) {
  158.             headerDuplicati = isProfiloSicurezzaMessaggioConHeaderDuplicati(aspc, asps.getPortType());
  159.         }
  160.         map.put(INTEGRITY, digest+"");
  161.         map.put(DIGEST_RICHIESTA, digestRichiesta+"");
  162.         map.put(CORNICE_SICUREZZA, corniceSicurezza+"");
  163.         if(corniceSicurezza) {
  164.             map.put(CORNICE_SICUREZZA_PATTERN, patternDatiCorniceSicurezza);
  165.             if(schemaDatiCorniceSicurezza!=null) {
  166.                 map.put(CORNICE_SICUREZZA_SCHEMA, schemaDatiCorniceSicurezza);
  167.             }
  168.         }
  169.         map.put(HEADER_DUPLICATI, headerDuplicati+"");
  170.        
  171.         // sicurezza canale
  172.         String v = getStringValue(aspc.getProtocolPropertyList(), CostantiDB.MODIPA_PROFILO_SICUREZZA_CANALE);
  173.         String canale = null;
  174.         if(CostantiDB.MODIPA_PROFILO_SICUREZZA_CANALE_VALUE_IDAC01.equals(v)) {
  175.             canale = CostantiLabel.MODIPA_PROFILO_SICUREZZA_CANALE_LABEL_IDAC01;
  176.         }
  177.         else if(CostantiDB.MODIPA_PROFILO_SICUREZZA_CANALE_VALUE_IDAC02.equals(v)) {
  178.             canale = CostantiLabel.MODIPA_PROFILO_SICUREZZA_CANALE_LABEL_IDAC02;
  179.         }
  180.         if(canale!=null) {
  181.             map.put(API_SICUREZZA_CANALE_PATTERN, canale);
  182.         }
  183.        
  184.         // sicurezza messaggio
  185.         boolean sicurezzaMessaggio = false;
  186.         List<String> tmp = RegistroServiziUtils.fillPropertyProtocollo(CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO, aspc, asps.getPortType(), false);
  187.         if(tmp!=null && !tmp.isEmpty()) {
  188.             StringBuilder sbMes = new StringBuilder();
  189.             for (String pattern : tmp) {
  190.                 if(CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_VALUE_IDAM01.equals(pattern)) {
  191.                     if(sbMes.length()>0) {
  192.                         sbMes.append(", ");
  193.                     }
  194.                     sbMes.append(rest ? CostantiLabel.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_LABEL_IDAM01_REST : CostantiLabel.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_LABEL_IDAM01_SOAP );
  195.                 }
  196.                 else if(CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_VALUE_IDAM02.equals(pattern)) {
  197.                     if(sbMes.length()>0) {
  198.                         sbMes.append(", ");
  199.                     }
  200.                     sbMes.append(rest ? CostantiLabel.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_LABEL_IDAM02_REST : CostantiLabel.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_LABEL_IDAM02_SOAP );
  201.                 }
  202.                 else if(CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_VALUE_IDAM0301.equals(pattern)) {
  203.                     if(sbMes.length()>0) {
  204.                         sbMes.append(", ");
  205.                     }
  206.                     sbMes.append(rest ? CostantiLabel.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_LABEL_IDAM0301_REST : CostantiLabel.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_LABEL_IDAM0301_SOAP );
  207.                 }
  208.                 else if(CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_VALUE_IDAM0302.equals(pattern)) {
  209.                     if(sbMes.length()>0) {
  210.                         sbMes.append(", ");
  211.                     }
  212.                     sbMes.append(rest ? CostantiLabel.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_LABEL_IDAM0302_REST : CostantiLabel.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_LABEL_IDAM0302_SOAP );
  213.                 }
  214.                 else if(CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_VALUE_IDAM0401.equals(pattern) && rest) {
  215.                     if(sbMes.length()>0) {
  216.                         sbMes.append(", ");
  217.                     }
  218.                     sbMes.append(CostantiLabel.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_LABEL_IDAM0401_REST);
  219.                 }
  220.                 else if(CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_VALUE_IDAM0402.equals(pattern) && rest) {
  221.                     if(sbMes.length()>0) {
  222.                         sbMes.append(", ");
  223.                     }
  224.                     sbMes.append(CostantiLabel.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_LABEL_IDAM0402_REST);
  225.                 }
  226.             }
  227.             if(sbMes.length()>0) {
  228.                 sicurezzaMessaggio = true;
  229.                 map.put(API_SICUREZZA_MESSAGGIO_PATTERN, sbMes.toString());
  230.             }
  231.         }
  232.        
  233.         boolean sicurezzaMessaggioPdnd = false;
  234.         if(sicurezzaMessaggio) {
  235.            
  236.             // Sorgente Token ID AUTH
  237.             tmp = RegistroServiziUtils.fillPropertyProtocollo(CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_SORGENTE_TOKEN_IDAUTH, aspc, asps.getPortType(), false);
  238.             if(tmp!=null && !tmp.isEmpty()) {
  239.                 StringBuilder sbMes = new StringBuilder();
  240.                 for (String mode : tmp) {
  241.                     if(CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_SORGENTE_TOKEN_IDAUTH_VALUE_LOCALE.equals(mode)) {
  242.                         if(sbMes.length()>0) {
  243.                             sbMes.append(", ");
  244.                         }
  245.                         sbMes.append(CostantiLabel.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_SORGENTE_TOKEN_IDAUTH_LOCALE);
  246.                     }
  247.                     else if(CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_SORGENTE_TOKEN_IDAUTH_VALUE_PDND.equals(mode)) {
  248.                         if(sbMes.length()>0) {
  249.                             sbMes.append(", ");
  250.                         }
  251.                         sbMes.append(CostantiLabel.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_SORGENTE_TOKEN_IDAUTH_PDND);
  252.                         sicurezzaMessaggioPdnd = true;
  253.                     }
  254.                     else if(CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_SORGENTE_TOKEN_IDAUTH_VALUE_OAUTH.equals(mode)) {
  255.                         if(sbMes.length()>0) {
  256.                             sbMes.append(", ");
  257.                         }
  258.                         sbMes.append(CostantiLabel.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_SORGENTE_TOKEN_IDAUTH_OAUTH);
  259.                     }
  260.                 }
  261.                 if(sbMes.length()>0) {
  262.                     map.put(API_SICUREZZA_MESSAGGIO_SORGENTE_TOKEN_ID_AUTH, sbMes.toString());
  263.                 }
  264.             }
  265.            
  266.             if(rest) {
  267.                 // Header (duplicati)
  268.                 tmp = RegistroServiziUtils.fillPropertyProtocollo(CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_HEADER, aspc, asps.getPortType(), false);
  269.                 if(tmp!=null && !tmp.isEmpty()) {
  270.                     StringBuilder sbMes = new StringBuilder();
  271.                     String headerModI = getHeaderModI();
  272.                     for (String mode : tmp) {
  273.                         if(CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_HEADER_VALUE_MODIPA.equals(mode)) {
  274.                             if(sbMes.length()>0) {
  275.                                 sbMes.append(", ");
  276.                             }
  277.                             sbMes.append(CostantiLabel.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_HEADER_LABEL_MODIPA.
  278.                                     replace(CostantiLabel.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_HEADER_LABEL_MODIPA, headerModI));
  279.                         }
  280.                         else if(CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_HEADER_VALUE_AUTHORIZATION.equals(mode)) {
  281.                             if(sbMes.length()>0) {
  282.                                 sbMes.append(", ");
  283.                             }
  284.                             sbMes.append(CostantiLabel.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_HEADER_LABEL_AUTHORIZATION);
  285.                         }
  286.                         else if(CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_HEADER_VALUE_AUTHORIZATION_MODIPA.equals(mode)) {
  287.                             if(sbMes.length()>0) {
  288.                                 sbMes.append(", ");
  289.                             }
  290.                             sbMes.append(CostantiLabel.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_HEADER_LABEL_AUTHORIZATION_MODIPA.
  291.                                     replace(CostantiLabel.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_HEADER_LABEL_MODIPA, headerModI));
  292.                         }
  293.                         else if(CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_HEADER_VALUE_AUTHORIZATION_MODIPA_AUTH_IN_RESPONSE.equals(mode)) {
  294.                             if(sbMes.length()>0) {
  295.                                 sbMes.append(", ");
  296.                             }
  297.                             sbMes.append(CostantiLabel.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_HEADER_LABEL_AUTHORIZATION_MODIPA_AUTH_IN_RESPONSE.
  298.                                     replace(CostantiLabel.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_HEADER_LABEL_MODIPA, headerModI));
  299.                         }
  300.                         else if(CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_HEADER_VALUE_CUSTOM.equals(mode)) {
  301.                             if(sbMes.length()>0) {
  302.                                 sbMes.append(", ");
  303.                             }
  304.                             sbMes.append(CostantiLabel.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_HEADER_LABEL_CUSTOM);
  305.                         }
  306.                         else if(CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_HEADER_VALUE_AUTHORIZATION_CUSTOM.equals(mode)) {
  307.                             if(sbMes.length()>0) {
  308.                                 sbMes.append(", ");
  309.                             }
  310.                             sbMes.append(CostantiLabel.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_HEADER_LABEL_AUTHORIZATION_CUSTOM);
  311.                         }
  312.                         else if(CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_HEADER_VALUE_AUTHORIZATION_CUSTOM_AUTH_IN_RESPONSE.equals(mode)) {
  313.                             if(sbMes.length()>0) {
  314.                                 sbMes.append(", ");
  315.                             }
  316.                             sbMes.append(CostantiLabel.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_HEADER_LABEL_AUTHORIZATION_CUSTOM_AUTH_IN_RESPONSE);
  317.                         }
  318.                     }
  319.                     if(sbMes.length()>0) {
  320.                         map.put(API_SICUREZZA_MESSAGGIO_HTTP_HEADER, sbMes.toString());
  321.                     }
  322.                 }
  323.             }
  324.            
  325.             // Applicabilita
  326.             tmp = RegistroServiziUtils.fillPropertyProtocollo(CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_MODE, aspc, asps.getPortType(), false);
  327.             if(tmp!=null && !tmp.isEmpty()) {
  328.                 StringBuilder sbMes = new StringBuilder();
  329.                 for (String mode : tmp) {
  330.                     if(CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_MODE_VALUE_ENTRAMBI.equals(mode)) {
  331.                         if(sbMes.length()>0) {
  332.                             sbMes.append(", ");
  333.                         }
  334.                         sbMes.append(CostantiLabel.MODIPA_API_CONFIGURAZIONE_SICUREZZA_MESSAGGIO_MODE_LABEL_ENTRAMBI);
  335.                     }
  336.                     else if(CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_MODE_VALUE_RICHIESTA.equals(mode)) {
  337.                         if(sbMes.length()>0) {
  338.                             sbMes.append(", ");
  339.                         }
  340.                         sbMes.append(CostantiLabel.MODIPA_API_CONFIGURAZIONE_SICUREZZA_MESSAGGIO_MODE_LABEL_RICHIESTA);
  341.                     }
  342.                     else if(CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_MODE_VALUE_RISPOSTA.equals(mode)) {
  343.                         if(sbMes.length()>0) {
  344.                             sbMes.append(", ");
  345.                         }
  346.                         sbMes.append(CostantiLabel.MODIPA_API_CONFIGURAZIONE_SICUREZZA_MESSAGGIO_MODE_LABEL_RISPOSTA);
  347.                     }
  348.                     else if(CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_MODE_VALUE_ENTRAMBI_CON_ATTACHMENTS.equals(mode)) {
  349.                         if(sbMes.length()>0) {
  350.                             sbMes.append(", ");
  351.                         }
  352.                         sbMes.append(CostantiLabel.MODIPA_API_CONFIGURAZIONE_SICUREZZA_MESSAGGIO_MODE_LABEL_ENTRAMBI_CON_ATTACHMENTS);
  353.                     }
  354.                     else if(CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_MODE_VALUE_RICHIESTA_CON_ATTACHMENTS.equals(mode)) {
  355.                         if(sbMes.length()>0) {
  356.                             sbMes.append(", ");
  357.                         }
  358.                         sbMes.append(CostantiLabel.MODIPA_API_CONFIGURAZIONE_SICUREZZA_MESSAGGIO_MODE_LABEL_RICHIESTA_CON_ATTACHMENTS);
  359.                     }
  360.                     else if(CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_MODE_VALUE_RISPOSTA_CON_ATTACHMENTS.equals(mode)) {
  361.                         if(sbMes.length()>0) {
  362.                             sbMes.append(", ");
  363.                         }
  364.                         sbMes.append(CostantiLabel.MODIPA_API_CONFIGURAZIONE_SICUREZZA_MESSAGGIO_MODE_LABEL_RISPOSTA_CON_ATTACHMENTS);
  365.                     }
  366.                     else if(CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_MODE_VALUE_PERSONALIZZATO.equals(mode)) {
  367.                         if(sbMes.length()>0) {
  368.                             sbMes.append(", ");
  369.                         }
  370.                         sbMes.append(CostantiLabel.MODIPA_API_CONFIGURAZIONE_SICUREZZA_MESSAGGIO_MODE_LABEL_PERSONALIZZATO);
  371.                     }
  372.                 }
  373.                 if(sbMes.length()>0) {
  374.                     map.put(API_SICUREZZA_MESSAGGIO_APPLICABILITA, sbMes.toString());
  375.                 }
  376.             }
  377.            
  378.             // Request Digest
  379.             map.put(API_SICUREZZA_MESSAGGIO_REQUEST_DIGEST,
  380.                     digestRichiesta ? StatoFunzionalita.ABILITATO.getValue() : StatoFunzionalita.DISABILITATO.getValue());
  381.            
  382.             // Cornice Sicurezza
  383.             if(corniceSicurezza) {
  384.                 if(CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CORNICE_SICUREZZA_PATTERN_OLD.equals(patternDatiCorniceSicurezza)) {
  385.                     map.put(API_SICUREZZA_MESSAGGIO_USER_INFO,
  386.                             CostantiLabel.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CORNICE_SICUREZZA_PATTERN_OLD);
  387.                 }
  388.                 else if(CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CORNICE_SICUREZZA_PATTERN_AUDIT_REST_01.equals(patternDatiCorniceSicurezza)) {
  389.                     map.put(API_SICUREZZA_MESSAGGIO_USER_INFO,
  390.                             CostantiLabel.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CORNICE_SICUREZZA_PATTERN_AUDIT_REST_01+" (schema:"+schemaDatiCorniceSicurezza+")");
  391.                 }
  392.                 else if(CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CORNICE_SICUREZZA_PATTERN_AUDIT_REST_02.equals(patternDatiCorniceSicurezza)) {
  393.                     map.put(API_SICUREZZA_MESSAGGIO_USER_INFO,
  394.                             CostantiLabel.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CORNICE_SICUREZZA_PATTERN_AUDIT_REST_02+" (schema:"+schemaDatiCorniceSicurezza+")");
  395.                 }
  396.             }
  397.             else {
  398.                 map.put(API_SICUREZZA_MESSAGGIO_USER_INFO,
  399.                     StatoFunzionalita.DISABILITATO.getValue());
  400.             }
  401.                            
  402.             boolean request = true;
  403.             addProfiloModISicurezza(map,
  404.                     protocolPropertyList,
  405.                     rest, gestioneFruitori, request,
  406.                     digest,
  407.                     patternDatiCorniceSicurezza, schemaDatiCorniceSicurezza,
  408.                     headerDuplicati,
  409.                     urlInvocazione, urlConnettoreFruitoreModI);
  410.             addProfiloModISicurezza(map,
  411.                     protocolPropertyList,
  412.                     rest, gestioneFruitori, !request,
  413.                     digest,
  414.                     patternDatiCorniceSicurezza, schemaDatiCorniceSicurezza,
  415.                     headerDuplicati,
  416.                     urlInvocazione, urlConnettoreFruitoreModI);
  417.         }
  418.         else {
  419.            
  420.             // Sicurezza OAuth
  421.            
  422.             if( gestioneFruitori ) {
  423.                
  424.                 v = getStringValue(protocolPropertyList, CostantiDB.MODIPA_PROFILO_SICUREZZA_OAUTH_IDENTIFICATIVO);
  425.                 if(v!=null && StringUtils.isNotEmpty(v)) {
  426.                     map.put(API_IMPL_SICUREZZA_OAUTH_IDENTIFICATIVO, v);
  427.                 }
  428.                 v = getStringValue(protocolPropertyList, CostantiDB.MODIPA_PROFILO_SICUREZZA_OAUTH_KID);
  429.                 if(v!=null && StringUtils.isNotEmpty(v)) {
  430.                     map.put(API_IMPL_SICUREZZA_OAUTH_KID, v);
  431.                 }
  432.                
  433.                 addStore(map, protocolPropertyList, "", false, false);
  434.             }
  435.            
  436.         }
  437.        
  438.         if(sicurezzaMessaggioPdnd && gestioneErogatori) {
  439.            
  440.             v = getStringValue(protocolPropertyList, CostantiDB.MODIPA_API_IMPL_INFO_ESERVICE_ID);
  441.             if(v!=null && StringUtils.isNotEmpty(v)) {
  442.                 map.put(API_IMPL_INFO_ESERVICE_ID, v);
  443.             }
  444.            
  445.             v = getStringValue(protocolPropertyList, CostantiDB.MODIPA_API_IMPL_INFO_DESCRIPTOR_ID);
  446.             if(v!=null && StringUtils.isNotEmpty(v)) {
  447.                 map.put(API_IMPL_INFO_DESCRIPTOR_ID, v);
  448.             }
  449.            
  450.            
  451.             v = getStringValue(protocolPropertyList, CostantiDB.MODIPA_API_IMPL_INFO_SIGNAL_HUB_ID);
  452.             if(v!=null && StringUtils.isNotEmpty(v)) {
  453.                 map.put(API_IMPL_INFO_SIGNAL_HUB, "true".equals(v) ? StatoFunzionalita.ABILITATO.getValue() : StatoFunzionalita.DISABILITATO.getValue());
  454.             }
  455.             else if(ModIUtils.isSignalHubEnabled()) {
  456.                 map.put(API_IMPL_INFO_SIGNAL_HUB, StatoFunzionalita.DISABILITATO.getValue());
  457.             }
  458.         }
  459.        
  460.         return map;
  461.        
  462.     }
  463.    
  464.     public static String getPrefixKey(boolean fruizione, boolean request) {
  465.         String prefixKey = null;
  466.         if(fruizione) {
  467.             // nop
  468.         }
  469.         /**if(request) {
  470.             prefixKey = fruizione ? "out-request-" : "in-request-";
  471.         }
  472.         else {
  473.             prefixKey = fruizione ? "in-response-" : "out-response-";
  474.         }*/
  475.         prefixKey = request ? REQUEST_PREFIX : RESPONSE_PREFIX;
  476.         return prefixKey;
  477.     }
  478.    
  479.     private static void addProfiloModISicurezza(Map<String, String> map,
  480.             List<ProtocolProperty> protocolPropertyList,
  481.             boolean rest, boolean fruizione, boolean request,
  482.             boolean digest,
  483.             String patternDatiCorniceSicurezza, String schemaDatiCorniceSicurezza,
  484.             boolean headerDuplicati,
  485.             String urlInvocazione, String urlConnettoreFruitoreModI) {
  486.        
  487.         String prefixKey = getPrefixKey(fruizione, request);
  488.        
  489.         // Firma
  490.         boolean x5u = false;
  491.         if(rest) {
  492.            
  493.             // Algoritmo
  494.             String idProfiloSicurezzaMessaggioAlgItem = null;
  495.             if(fruizione && request) {
  496.                 idProfiloSicurezzaMessaggioAlgItem = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_REST_RICHIESTA_ALG;
  497.             }
  498.             else if(!fruizione && !request) {
  499.                 idProfiloSicurezzaMessaggioAlgItem = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_REST_RISPOSTA_ALG;
  500.             }
  501.             if(idProfiloSicurezzaMessaggioAlgItem!=null) {
  502.                 String v = getStringValue(protocolPropertyList, idProfiloSicurezzaMessaggioAlgItem);
  503.                 map.put(prefixKey+API_IMPL_SICUREZZA_MESSAGGIO_SIGNATURE_ALGORITHM, v);
  504.             }
  505.            
  506.             // Codifica Digest
  507.             String idProfiloSicurezzaMessaggioDigestEncodingItem = null;
  508.             if(fruizione && request) {
  509.                 idProfiloSicurezzaMessaggioDigestEncodingItem = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_REST_RICHIESTA_DIGEST_ENCODING;
  510.             }
  511.             else if(!fruizione && !request) {
  512.                 idProfiloSicurezzaMessaggioDigestEncodingItem = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_REST_RISPOSTA_DIGEST_ENCODING;
  513.             }
  514.             if(idProfiloSicurezzaMessaggioDigestEncodingItem!=null) {
  515.                 String v = getStringValue(protocolPropertyList, idProfiloSicurezzaMessaggioDigestEncodingItem);
  516.                 if(v!=null) {
  517.                     try {
  518.                         DigestEncoding de = DigestEncoding.valueOf(v);
  519.                         if(DigestEncoding.BASE64.equals(de)) {
  520.                             map.put(prefixKey+API_IMPL_SICUREZZA_MESSAGGIO_DIGEST_ENCODING, CostantiLabel.MODIPA_API_IMPL_PROFILO_SICUREZZA_MESSAGGIO_REST_DIGEST_ENCODING_LABEL_BASE64);
  521.                         }else{ //HEX:
  522.                             map.put(prefixKey+API_IMPL_SICUREZZA_MESSAGGIO_DIGEST_ENCODING, CostantiLabel.MODIPA_API_IMPL_PROFILO_SICUREZZA_MESSAGGIO_REST_DIGEST_ENCODING_LABEL_HEX);
  523.                         }
  524.                     }catch(Exception t) {
  525.                         map.put(prefixKey+API_IMPL_SICUREZZA_MESSAGGIO_DIGEST_ENCODING, v);
  526.                     }
  527.                 }
  528.                 else {
  529.                     map.put(prefixKey+API_IMPL_SICUREZZA_MESSAGGIO_DIGEST_ENCODING, v);
  530.                 }
  531.             }
  532.            
  533.             if(digest &&
  534.                 // header firmati
  535.                 ( (request && fruizione) || (!request && !fruizione) )
  536.                 ){
  537.                 map.put(prefixKey+API_IMPL_SICUREZZA_MESSAGGIO_HTTP_HEADER_FIRMATI,
  538.                         getStringValue(protocolPropertyList, CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_HTTP_HEADERS_REST));
  539.             }
  540.            
  541.             // Posizione Certificato
  542.             String rifX509Id = request ? CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_REST_RICHIESTA_RIFERIMENTO_X509 :
  543.                 CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_REST_RISPOSTA_RIFERIMENTO_X509;
  544.             String v = getStringValue(protocolPropertyList, rifX509Id);
  545.             map.put(prefixKey+API_IMPL_SICUREZZA_MESSAGGIO_RIFERIMENTO_X509, v);
  546.             x5u = (v!=null) && v.contains("x5u");
  547.            
  548.             // Certificate Chain
  549.             String rifX509Xc5ChainId = null;
  550.             if(fruizione && request) {
  551.                 rifX509Xc5ChainId = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_REST_RICHIESTA_RIFERIMENTO_X509_X5C_USE_CERTIFICATE_CHAIN;
  552.             }
  553.             else if(!fruizione && !request) {
  554.                 rifX509Xc5ChainId = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_REST_RISPOSTA_RIFERIMENTO_X509_X5C_USE_CERTIFICATE_CHAIN;
  555.             }
  556.             if(rifX509Xc5ChainId!=null) {
  557.                 map.put(prefixKey+API_IMPL_SICUREZZA_MESSAGGIO_CATENA_CERTIFICATI_X509,
  558.                         getBooleanValueAsStato(protocolPropertyList, rifX509Xc5ChainId));
  559.             }
  560.         }
  561.         else {
  562.            
  563.             // Algoritmo
  564.             String idProfiloSicurezzaMessaggioAlgItem = null;
  565.             if(fruizione && request) {
  566.                 idProfiloSicurezzaMessaggioAlgItem = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_SOAP_RICHIESTA_ALG;
  567.             }
  568.             else if(!fruizione && !request) {
  569.                 idProfiloSicurezzaMessaggioAlgItem = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_SOAP_RISPOSTA_ALG;
  570.             }
  571.             if(idProfiloSicurezzaMessaggioAlgItem!=null) {
  572.                 String algoLabel = getStringValue(protocolPropertyList, idProfiloSicurezzaMessaggioAlgItem);
  573.                 if(algoLabel.contains("#") && !algoLabel.endsWith("#")) {
  574.                     algoLabel = algoLabel.substring(algoLabel.indexOf("#")+1, algoLabel.length()).toUpperCase();
  575.                 }
  576.                 map.put(prefixKey+API_IMPL_SICUREZZA_MESSAGGIO_SIGNATURE_ALGORITHM, algoLabel);
  577.             }
  578.            
  579.             // Algoritmo C14N
  580.             String idProfiloSicurezzaMessaggioAlgC14NItem = null;
  581.             if(fruizione && request) {
  582.                 idProfiloSicurezzaMessaggioAlgC14NItem = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_SOAP_RICHIESTA_CANONICALIZATION_ALG;
  583.             }
  584.             else if(!fruizione && !request) {
  585.                 idProfiloSicurezzaMessaggioAlgC14NItem = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_SOAP_RISPOSTA_CANONICALIZATION_ALG;
  586.             }
  587.             if(idProfiloSicurezzaMessaggioAlgC14NItem!=null) {
  588.                 String algoLabel = getStringValue(protocolPropertyList, idProfiloSicurezzaMessaggioAlgC14NItem);
  589.                 if(CostantiDB.INCLUSIVE_C14N_10_OMITS_COMMENTS_URI.equals(algoLabel)) {
  590.                     algoLabel = CostantiLabel.MODIPA_API_IMPL_PROFILO_SICUREZZA_MESSAGGIO_SOAP_CANONICALIZATION_ALG_LABEL_INCLUSIVE_C14N_10;
  591.                 }
  592.                 else if(CostantiDB.INCLUSIVE_C14N_11_OMITS_COMMENTS_URI.equals(algoLabel)) {
  593.                     algoLabel = CostantiLabel.MODIPA_API_IMPL_PROFILO_SICUREZZA_MESSAGGIO_SOAP_CANONICALIZATION_ALG_LABEL_INCLUSIVE_C14N_11;
  594.                 }
  595.                 else if(CostantiDB.EXCLUSIVE_C14N_10_OMITS_COMMENTS_URI.equals(algoLabel)) {
  596.                     algoLabel = CostantiLabel.MODIPA_API_IMPL_PROFILO_SICUREZZA_MESSAGGIO_SOAP_CANONICALIZATION_ALG_LABEL_EXCLUSIVE_C14N_10;
  597.                 }
  598.                 map.put(prefixKey+API_IMPL_SICUREZZA_MESSAGGIO_CANONICALIZATION_ALGORITHM,algoLabel);
  599.             }
  600.            
  601.             if(digest &&
  602.                 // header firmati
  603.                 ( (request && fruizione) || (!request && !fruizione) )
  604.             ){
  605.                 String v = getStringValue(protocolPropertyList, CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_SOAP_HEADERS_SOAP);
  606.                 if(v!=null) {
  607.                     int index = 0;
  608.                     while(v.contains("\n") && index<1000) {
  609.                         v = v.replace("\n", ", ");
  610.                         index++;
  611.                     }
  612.                     map.put(prefixKey+API_IMPL_SICUREZZA_MESSAGGIO_SOAP_HEADER_FIRMATI, v);
  613.                 }
  614.             }
  615.            
  616.             // Posizione Certificato
  617.             String rifX509Id = null;
  618.             if(fruizione && request) {
  619.                 rifX509Id = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_SOAP_RICHIESTA_RIFERIMENTO_X509;
  620.             }
  621.             else if(!fruizione && !request) {
  622.                 rifX509Id = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_SOAP_RISPOSTA_RIFERIMENTO_X509;
  623.             }
  624.             boolean useCertificateChain = false;
  625.             boolean includeSignatureToken = false;
  626.             if(rifX509Id!=null) {
  627.                 String refLabel = getStringValue(protocolPropertyList, rifX509Id);
  628.                 if(CostantiDB.KEY_IDENTIFIER_BST_DIRECT_REFERENCE.equals(refLabel)){
  629.                     refLabel = CostantiLabel.MODIPA_API_IMPL_PROFILO_SICUREZZA_MESSAGGIO_SOAP_RIFERIMENTO_X509_LABEL_BINARY_SECURITY_TOKEN;
  630.                     useCertificateChain = true;
  631.                 }
  632.                 else if(CostantiDB.KEY_IDENTIFIER_ISSUER_SERIAL.equals(refLabel)){
  633.                     refLabel = CostantiLabel.MODIPA_API_IMPL_PROFILO_SICUREZZA_MESSAGGIO_SOAP_RIFERIMENTO_X509_LABEL_SECURITY_TOKEN_REFERENCE;
  634.                     includeSignatureToken = true;
  635.                 }
  636.                 else if(CostantiDB.KEY_IDENTIFIER_X509.equals(refLabel)){
  637.                     refLabel = CostantiLabel.MODIPA_API_IMPL_PROFILO_SICUREZZA_MESSAGGIO_SOAP_RIFERIMENTO_X509_LABEL_KEY_IDENTIFIER_X509;
  638.                 }
  639.                 else if(CostantiDB.KEY_IDENTIFIER_THUMBPRINT.equals(refLabel)){
  640.                     refLabel = CostantiLabel.MODIPA_API_IMPL_PROFILO_SICUREZZA_MESSAGGIO_SOAP_RIFERIMENTO_X509_LABEL_KEY_IDENTIFIER_THUMBPRINT;
  641.                     includeSignatureToken = true;
  642.                 }
  643.                 else if(CostantiDB.KEY_IDENTIFIER_SKI.equals(refLabel)){
  644.                     refLabel = CostantiLabel.MODIPA_API_IMPL_PROFILO_SICUREZZA_MESSAGGIO_SOAP_RIFERIMENTO_X509_LABEL_KEY_IDENTIFIER_SKI;
  645.                     includeSignatureToken = true;
  646.                 }
  647.                 map.put(prefixKey+API_IMPL_SICUREZZA_MESSAGGIO_RIFERIMENTO_X509, refLabel);
  648.             }
  649.            
  650.             if(useCertificateChain) {
  651.                 // Certificate Chain
  652.                 String certId = null;
  653.                 if(fruizione && request) {
  654.                     certId = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_SOAP_RICHIESTA_RIFERIMENTO_X509_BINARY_SECURITY_TOKEN_USE_CERTIFICATE_CHAIN;
  655.                 }
  656.                 else if(!fruizione && !request) {
  657.                     certId = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_SOAP_RISPOSTA_RIFERIMENTO_X509_BINARY_SECURITY_TOKEN_USE_CERTIFICATE_CHAIN;
  658.                 }
  659.                 if(certId!=null) {
  660.                     map.put(prefixKey+API_IMPL_SICUREZZA_MESSAGGIO_CATENA_CERTIFICATI_X509,
  661.                             getBooleanValueAsStato(protocolPropertyList, certId));
  662.                 }
  663.             }
  664.            
  665.             if(includeSignatureToken) {
  666.                 // Signature Token
  667.                 String certId = null;
  668.                 if(fruizione && request) {
  669.                     certId = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_SOAP_RICHIESTA_RIFERIMENTO_X509_BINARY_SECURITY_TOKEN_INCLUDE_SIGNATURE_TOKEN;
  670.                 }
  671.                 else if(!fruizione && !request) {
  672.                     certId = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_SOAP_RISPOSTA_RIFERIMENTO_X509_BINARY_SECURITY_TOKEN_INCLUDE_SIGNATURE_TOKEN;
  673.                 }
  674.                 if(certId!=null) {
  675.                     map.put(prefixKey+API_IMPL_SICUREZZA_MESSAGGIO_INCLUDE_SIGNATURE_TOKEN,
  676.                             getBooleanValueAsStato(protocolPropertyList, certId));
  677.                 }
  678.             }
  679.            
  680.         }
  681.        
  682.         // Ttl
  683.         String idProfiloSicurezzaMessaggioIatTtlItem = null;
  684.         String idProfiloSicurezzaMessaggioIatTtlSecondsItem = null;
  685.         if(fruizione && !request) {
  686.             idProfiloSicurezzaMessaggioIatTtlItem = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_RISPOSTA_IAT;
  687.             idProfiloSicurezzaMessaggioIatTtlSecondsItem = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_RISPOSTA_IAT_SECONDS;
  688.         }
  689.         else if(!fruizione && request) {
  690.             idProfiloSicurezzaMessaggioIatTtlItem = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_RICHIESTA_IAT;
  691.             idProfiloSicurezzaMessaggioIatTtlSecondsItem = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_RICHIESTA_IAT_SECONDS;
  692.         }
  693.         if(idProfiloSicurezzaMessaggioIatTtlItem!=null && idProfiloSicurezzaMessaggioIatTtlSecondsItem!=null) {
  694.             String v = getStringValue(protocolPropertyList, idProfiloSicurezzaMessaggioIatTtlItem);
  695.             if(v==null || StringUtils.isEmpty(v) || CostantiDB.MODIPA_PROFILO_DEFAULT.equals(v)) {
  696.                 map.put(prefixKey+API_IMPL_SICUREZZA_MESSAGGIO_TTL, CostantiLabel.MODIPA_LABEL_DEFAULT);
  697.             }
  698.             else {
  699.                 v = getStringValue(protocolPropertyList, idProfiloSicurezzaMessaggioIatTtlSecondsItem);
  700.                 map.put(prefixKey+API_IMPL_SICUREZZA_MESSAGGIO_TTL, v);
  701.             }
  702.         }
  703.        
  704.         // Expiration Time
  705.         String idProfiloSicurezzaMessaggioExpItem = null;
  706.         if(fruizione && request) {
  707.             idProfiloSicurezzaMessaggioExpItem = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_RICHIESTA_EXPIRED;
  708.         }
  709.         else if(!fruizione && !request) {
  710.             idProfiloSicurezzaMessaggioExpItem = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_RISPOSTA_EXPIRED;
  711.         }
  712.         if(idProfiloSicurezzaMessaggioExpItem!=null) {
  713.             String v = getStringValue(protocolPropertyList, idProfiloSicurezzaMessaggioExpItem);
  714.             if(v!=null && StringUtils.isNotEmpty(v)) {
  715.                 map.put(prefixKey+API_IMPL_SICUREZZA_MESSAGGIO_TTL, v);
  716.             }
  717.         }
  718.        
  719.         // Audit
  720.         boolean audit = false;
  721.         if(request) {
  722.             String v = getStringValue(protocolPropertyList, CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_RICHIESTA_AUDIENCE);
  723.             String aud = null;
  724.             if(v!=null) {
  725.                 aud = v;
  726.             }else {
  727.                 aud = fruizione ? urlConnettoreFruitoreModI : urlInvocazione;
  728.             }
  729.             map.put(prefixKey+API_IMPL_SICUREZZA_MESSAGGIO_AUDIENCE,
  730.                     aud);
  731.             audit = true;
  732.         }
  733.         else {
  734.             if(fruizione) {
  735.                 String stato = getBooleanValueAsStato(protocolPropertyList, CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_RISPOSTA_AUDIENCE);
  736.                 if(stato!=null && StringUtils.isNotEmpty(stato)) {
  737.                     map.put(prefixKey+API_IMPL_SICUREZZA_MESSAGGIO_VERIFICA_AUDIENCE, stato);
  738.                     if(StatoFunzionalita.ABILITATO.getValue().equals(stato)) {
  739.                        
  740.                         audit = true;
  741.                        
  742.                         String v = getStringValue(protocolPropertyList, CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_RISPOSTA_AUDIENCE_VALORE);
  743.                         if(v!=null && StringUtils.isNotEmpty(v)) {
  744.                             map.put(prefixKey+API_IMPL_SICUREZZA_MESSAGGIO_AUDIENCE, v);
  745.                         }
  746.                     }
  747.                 }
  748.             }
  749.         }
  750.        
  751.         // X5U URL
  752.         if(x5u) {
  753.             String id = null;
  754.             if(request && fruizione) {
  755.                 id = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_REST_RICHIESTA_X509_VALUE_X5URL;
  756.             }
  757.             if(!request && !fruizione) {
  758.                 id = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_REST_RISPOSTA_X509_VALUE_X5URL;
  759.             }
  760.             if(id!=null) {
  761.                 String v = getStringValue(protocolPropertyList, id);
  762.                 if(v!=null && StringUtils.isNotEmpty(v)) {
  763.                     map.put(prefixKey+API_IMPL_SICUREZZA_MESSAGGIO_X5U_CERTIFICATE_URL, v);
  764.                 }
  765.             }
  766.         }
  767.        
  768.         // CorniceSicurezza
  769.         if(patternDatiCorniceSicurezza!=null) {
  770.             if(CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CORNICE_SICUREZZA_PATTERN_OLD.equals(patternDatiCorniceSicurezza)) {
  771.                
  772.                 String claimCodiceEnte = CostantiLabel.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CORNICE_SICUREZZA_CODICE_ENTE;
  773.                 String idCodiceEnteMode = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CORNICE_SICUREZZA_CODICE_ENTE_MODE;
  774.                 String vCodiceEnteMode = getStringValue(protocolPropertyList, idCodiceEnteMode);
  775.                 if(vCodiceEnteMode!=null && CostantiDB.MODIPA_PROFILO_RIDEFINISCI.equals(vCodiceEnteMode)) {
  776.                     String idValue = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CORNICE_SICUREZZA_CODICE_ENTE;
  777.                     String value = getStringValue(protocolPropertyList, idValue);
  778.                     if(value!=null) {
  779.                         map.put(prefixKey+ API_IMPL_SICUREZZA_MESSAGGIO_CORNICE_SICUREZZA_PREFIX+claimCodiceEnte, value);
  780.                     }
  781.                 }
  782.                 else {
  783.                     map.put(prefixKey+ API_IMPL_SICUREZZA_MESSAGGIO_CORNICE_SICUREZZA_PREFIX+claimCodiceEnte, CostantiDB.MODIPA_PROFILO_DEFAULT);
  784.                 }
  785.                
  786.                 String claimUser = CostantiLabel.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CORNICE_SICUREZZA_USER;
  787.                 String idUserMode = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CORNICE_SICUREZZA_USER_MODE;
  788.                 String vUserMode = getStringValue(protocolPropertyList, idUserMode);
  789.                 if(vUserMode!=null && CostantiDB.MODIPA_PROFILO_RIDEFINISCI.equals(vUserMode)) {
  790.                     String idValue = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CORNICE_SICUREZZA_USER;
  791.                     String value = getStringValue(protocolPropertyList, idValue);
  792.                     if(value!=null) {
  793.                         map.put(prefixKey+ API_IMPL_SICUREZZA_MESSAGGIO_CORNICE_SICUREZZA_PREFIX+claimUser, value);
  794.                     }
  795.                 }
  796.                 else {
  797.                     map.put(prefixKey+ API_IMPL_SICUREZZA_MESSAGGIO_CORNICE_SICUREZZA_PREFIX+claimUser, CostantiDB.MODIPA_PROFILO_DEFAULT);
  798.                 }
  799.                
  800.                 String claimUserIP = CostantiLabel.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CORNICE_SICUREZZA_IP_USER;
  801.                 String idUserIPMode = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CORNICE_SICUREZZA_IP_USER_MODE;
  802.                 String vUserIPMode = getStringValue(protocolPropertyList, idUserIPMode);
  803.                 if(vUserIPMode!=null && CostantiDB.MODIPA_PROFILO_RIDEFINISCI.equals(vUserIPMode)) {
  804.                     String idValue = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CORNICE_SICUREZZA_IP_USER;
  805.                     String value = getStringValue(protocolPropertyList, idValue);
  806.                     if(value!=null) {
  807.                         map.put(prefixKey+ API_IMPL_SICUREZZA_MESSAGGIO_CORNICE_SICUREZZA_PREFIX+claimUserIP, value);
  808.                     }
  809.                 }
  810.                 else {
  811.                     map.put(prefixKey+ API_IMPL_SICUREZZA_MESSAGGIO_CORNICE_SICUREZZA_PREFIX+claimUserIP, CostantiDB.MODIPA_PROFILO_DEFAULT);
  812.                 }
  813.                
  814.             }
  815.             else if(schemaDatiCorniceSicurezza!=null){
  816.                
  817.                 String idAuditDifferent = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_RICHIESTA_AUDIT_AUDIENCE ;
  818.                 String v = getStringValue(protocolPropertyList, idAuditDifferent);
  819.                 if(CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_RICHIESTA_AUDIT_AUDIENCE_VALUE_DIFFERENT.equals(v)) {
  820.                     String idAudit = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_RICHIESTA_AUDIENCE_CUSTOM_AUDIT;                
  821.                     v = getStringValue(protocolPropertyList, idAudit);
  822.                     if(v!=null && StringUtils.isNotEmpty(v)) {
  823.                         map.put(prefixKey+ API_IMPL_SICUREZZA_MESSAGGIO_AUDIT_AUDIENCE, v);
  824.                     }
  825.                 }  
  826.                
  827.                 List<String> l = new ArrayList<>();
  828.                 for (ProtocolProperty pp : protocolPropertyList) {
  829.                     if(pp.getName().startsWith(CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CORNICE_SICUREZZA_SCHEMA_MODE_PREFIX)) {
  830.                         String claim = pp.getName().substring(CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CORNICE_SICUREZZA_SCHEMA_MODE_PREFIX.length());
  831.                         l.add(claim);
  832.                     }
  833.                 }
  834.                 if(l!=null && !l.isEmpty()) {
  835.                     for (String c : l) {
  836.                         String idMode = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CORNICE_SICUREZZA_SCHEMA_MODE_PREFIX+c;
  837.                         String vMode = getStringValue(protocolPropertyList, idMode);
  838.                         if(vMode!=null && CostantiDB.MODIPA_PROFILO_RIDEFINISCI.equals(vMode)) {
  839.                             String idValue = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CORNICE_SICUREZZA_SCHEMA_PREFIX+c;
  840.                             String value = getStringValue(protocolPropertyList, idValue);
  841.                             if(value!=null) {
  842.                                 map.put(prefixKey+ API_IMPL_SICUREZZA_MESSAGGIO_CORNICE_SICUREZZA_PREFIX+c, value);
  843.                             }
  844.                         }
  845.                         else {
  846.                             map.put(prefixKey+ API_IMPL_SICUREZZA_MESSAGGIO_CORNICE_SICUREZZA_PREFIX+c, CostantiDB.MODIPA_PROFILO_DEFAULT);
  847.                         }
  848.                     }
  849.                 }
  850.             }
  851.         }
  852.        
  853.         // Header Duplicati
  854.         if(rest && headerDuplicati &&
  855.                 (
  856.                         (!request && fruizione)
  857.                         ||
  858.                         (request && !fruizione)
  859.                 )
  860.                 &&
  861.                 audit) {
  862.             String idAuditDifferent = request ? CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_RICHIESTA_REST_DOPPI_HEADER_AUDIENCE  :
  863.                 CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_RISPOSTA_REST_DOPPI_HEADER_AUDIENCE;
  864.             String v = getStringValue(protocolPropertyList, idAuditDifferent);
  865.             if(CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_REST_DOPPI_HEADER_AUDIENCE_VALUE_DIFFERENT.equals(v)) {
  866.                 String idAudit = request ? CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_RICHIESTA_REST_DOPPI_HEADER_AUDIENCE_INTEGRITY :
  867.                     CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_RISPOSTA_REST_DOPPI_HEADER_AUDIENCE_INTEGRITY;                    
  868.                 v = getStringValue(protocolPropertyList, idAudit);
  869.                 if(v!=null && StringUtils.isNotEmpty(v)) {
  870.                     map.put(prefixKey+ API_IMPL_SICUREZZA_MESSAGGIO_INTEGRITY_AUDIENCE, v);
  871.                 }
  872.             }  
  873.         }
  874.        
  875.        
  876.         // Sicurezza OAuth
  877.        
  878.         if( fruizione && request ) {
  879.             String v = getStringValue(protocolPropertyList, CostantiDB.MODIPA_PROFILO_SICUREZZA_OAUTH_IDENTIFICATIVO);
  880.             if(v!=null && StringUtils.isNotEmpty(v)) {
  881.                 map.put(prefixKey+ API_IMPL_SICUREZZA_OAUTH_IDENTIFICATIVO, v);
  882.             }
  883.             v = getStringValue(protocolPropertyList, CostantiDB.MODIPA_PROFILO_SICUREZZA_OAUTH_KID);
  884.             if(v!=null && StringUtils.isNotEmpty(v)) {
  885.                 map.put(prefixKey+ API_IMPL_SICUREZZA_OAUTH_KID, v);
  886.             }
  887.         }
  888.        
  889.         // TrustStore
  890.         if( (fruizione && !request) || (!fruizione && request) ) {
  891.            
  892.             // truststore per i certificati
  893.             addStore(map, protocolPropertyList, prefixKey, false, true);
  894.            
  895.             if(rest && x5u) {
  896.                 // ssl per le url (x5u)
  897.                 addStore(map, protocolPropertyList, prefixKey, true, false);
  898.             }
  899.            
  900.         }
  901.        
  902.         // KeyStore
  903.         if(!fruizione && !request) {
  904.             addStore(map, protocolPropertyList, prefixKey, false, false);
  905.         }
  906.        
  907.         if(fruizione && request) {
  908.            
  909.             boolean keystoreDefinitoInFruizione = false;
  910.             String idFruizioneKeystoreFruizioneMode = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_FRUIZIONE_KEYSTORE_MODE;
  911.             String v = getStringValue(protocolPropertyList, idFruizioneKeystoreFruizioneMode);
  912.             if(CostantiDB.MODIPA_KEYSTORE_FRUIZIONE.equals(v)) {
  913.                 keystoreDefinitoInFruizione = true;
  914.                 map.put(prefixKey+ API_IMPL_SICUREZZA_MESSAGGIO_FRUIZIONE_KEYSTORE_MODE,
  915.                         CostantiLabel.MODIPA_API_IMPL_PROFILO_SICUREZZA_MESSAGGIO_FRUIZIONE_KEYSTORE_MODE_LABEL_FRUIZIONE);
  916.             }
  917.             else if(CostantiDB.MODIPA_KEYSTORE_FRUIZIONE_TOKEN_POLICY.equals(v)) {
  918.                 map.put(prefixKey+ API_IMPL_SICUREZZA_MESSAGGIO_FRUIZIONE_KEYSTORE_MODE,
  919.                         CostantiLabel.MODIPA_API_IMPL_PROFILO_SICUREZZA_MESSAGGIO_FRUIZIONE_KEYSTORE_MODE_LABEL_TOKEN_POLICY);
  920.             }
  921.             else {
  922.                 // verifico di non essere nel caso di API con pattern ID_AUTH_REST_01 via PDND dove la modalità di scelta non viene indicata.
  923.                 if(v==null || StringUtils.isEmpty(v)) {
  924.                     String idFruizioneKeystoreMode = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CERTIFICATI_KEYSTORE_MODE;
  925.                     String vMode = getStringValue(protocolPropertyList, idFruizioneKeystoreMode);
  926.                     if(CostantiDB.MODIPA_PROFILO_RIDEFINISCI.equals(vMode)) {
  927.                         keystoreDefinitoInFruizione = true;
  928.                     }
  929.                 }
  930.                 if(keystoreDefinitoInFruizione) {
  931.                     map.put(prefixKey+ API_IMPL_SICUREZZA_MESSAGGIO_FRUIZIONE_KEYSTORE_MODE,
  932.                             CostantiLabel.MODIPA_API_IMPL_PROFILO_SICUREZZA_MESSAGGIO_FRUIZIONE_KEYSTORE_MODE_LABEL_FRUIZIONE);
  933.                 }
  934.                 else {
  935.                     map.put(prefixKey+ API_IMPL_SICUREZZA_MESSAGGIO_FRUIZIONE_KEYSTORE_MODE,
  936.                             CostantiLabel.MODIPA_API_IMPL_PROFILO_SICUREZZA_MESSAGGIO_FRUIZIONE_KEYSTORE_MODE_LABEL_APPLICATIVO);
  937.                 }
  938.             }
  939.            
  940.             if(keystoreDefinitoInFruizione) {
  941.                 addStore(map, protocolPropertyList, prefixKey, false, false);
  942.             }
  943.            
  944.         }
  945.     }
  946.    
  947.     public static String getPrefixKeyStore(boolean prefix, String prefixKey, boolean ssl, boolean truststore) {
  948.         String s = null;
  949.         if(ssl) {
  950.             s = prefixKey+"truststore-ssl";
  951.         }
  952.         else if(truststore) {
  953.             s = prefixKey+"truststore";
  954.         }
  955.         else {
  956.             s = prefixKey+"keystore";
  957.         }
  958.         return prefix ? s+"-" : s;
  959.     }
  960.    
  961.     private static void addStore(Map<String, String> map, List<ProtocolProperty> protocolPropertyList,
  962.             String prefixKeyParam, boolean ssl, boolean truststore) {
  963.        
  964.         String id = null;
  965.         if(ssl) {
  966.             id = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_SSL_TRUSTSTORE_MODE;
  967.         }
  968.         else if(truststore) {
  969.             id = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CERTIFICATI_TRUSTSTORE_MODE;
  970.         }
  971.         else {
  972.             id = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CERTIFICATI_KEYSTORE_MODE;
  973.         }
  974.        
  975.         String v = getStringValue(protocolPropertyList, id);
  976.         if(v==null  || StringUtils.isEmpty(v) || CostantiDB.MODIPA_PROFILO_DEFAULT.equals(v)) {
  977.             // aggiungo informazione solo se presente
  978.             String store = getPrefixKeyStore(false, prefixKeyParam, ssl, truststore);
  979.             if(CostantiDB.MODIPA_PROFILO_DEFAULT.equals(v)) {
  980.                 map.put(store,CostantiLabel.MODIPA_API_IMPL_PROFILO_SICUREZZA_MESSAGGIO_STORE_MODE_LABEL_DEFAULT);
  981.             }
  982.         }
  983.         else {
  984.            
  985.             String prefixKey = getPrefixKeyStore(true, prefixKeyParam, ssl, truststore);
  986.                        
  987.             String type = null;
  988.             String path = null;
  989.             String crl = null;
  990.             String ocsp = null;
  991.             String aliasKey = null;
  992.             boolean keystoreModePath = false;
  993.             boolean keystoreModeArchive = false;
  994.             boolean keystoreModeHsm = false;
  995.             if(ssl) {
  996.                 type = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_SSL_TRUSTSTORE_TYPE;
  997.                 path = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_SSL_TRUSTSTORE_PATH;
  998.                 crl = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_SSL_TRUSTSTORE_CRLS;
  999.                 ocsp = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_SSL_TRUSTSTORE_OCSP_POLICY;
  1000.             }
  1001.             else if(truststore) {
  1002.                 type = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CERTIFICATI_TRUSTSTORE_TYPE;
  1003.                 path = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CERTIFICATI_TRUSTSTORE_PATH;
  1004.                 crl = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CERTIFICATI_TRUSTSTORE_CRLS;
  1005.                 ocsp = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CERTIFICATI_TRUSTSTORE_OCSP_POLICY;
  1006.             }
  1007.             else {
  1008.                 type = CostantiDB.MODIPA_KEYSTORE_TYPE;
  1009.                 aliasKey = CostantiDB.MODIPA_KEY_ALIAS;
  1010.                 String mode = getStringValue(protocolPropertyList, CostantiDB.MODIPA_KEYSTORE_MODE);
  1011.                 if(CostantiDB.MODIPA_KEYSTORE_MODE_VALUE_ARCHIVE.equals(mode)) {
  1012.                     keystoreModeArchive = true;
  1013.                 }
  1014.                 else if(CostantiDB.MODIPA_KEYSTORE_MODE_VALUE_PATH.equals(mode)) {
  1015.                     keystoreModePath = true;
  1016.                     path = CostantiDB.MODIPA_KEYSTORE_PATH;
  1017.                 }
  1018.                 else if(CostantiDB.MODIPA_KEYSTORE_MODE_VALUE_HSM.equals(mode)) {
  1019.                     keystoreModeHsm = true;
  1020.                 }
  1021.             }
  1022.            
  1023.             String vType = null;
  1024.             if(type!=null) {
  1025.                 vType = getStringValue(protocolPropertyList, type);
  1026.                 map.put(prefixKey+ API_IMPL_SICUREZZA_MESSAGGIO_STORE_TYPE,vType);
  1027.             }
  1028.            
  1029.             if(keystoreModePath) {
  1030.                 // nop
  1031.             }
  1032.            
  1033.             boolean hsm = false;
  1034.             if(ssl || truststore) {
  1035.                 if(vType!=null) {
  1036.                     hsm = HSMUtils.isKeystoreHSM(vType);
  1037.                 }
  1038.             }
  1039.             else {
  1040.                 hsm = keystoreModeHsm;
  1041.             }
  1042.             map.put(prefixKey+HSM,hsm+"");
  1043.            
  1044.             if(hsm) {
  1045.                 map.put(prefixKey+ API_IMPL_SICUREZZA_MESSAGGIO_STORE_PATH,CostantiLabel.STORE_HSM);
  1046.             }
  1047.             else if(path!=null) {
  1048.                 String vPath = getStringValue(protocolPropertyList, path);
  1049.                 map.put(prefixKey+ API_IMPL_SICUREZZA_MESSAGGIO_STORE_PATH,vPath);
  1050.             }
  1051.             else if(keystoreModeArchive) {
  1052.                 map.put(prefixKey+ API_IMPL_SICUREZZA_MESSAGGIO_STORE_PATH,CostantiLabel.STORE_CARICATO_BASEDATI);
  1053.             }
  1054.            
  1055.             if(crl!=null) {
  1056.                 String vCrl = getStringValue(protocolPropertyList, crl);
  1057.                 map.put(prefixKey+ API_IMPL_SICUREZZA_MESSAGGIO_STORE_CRLS,vCrl);
  1058.             }
  1059.            
  1060.             if(ocsp!=null) {
  1061.                 String vOcsp = getStringValue(protocolPropertyList, ocsp);
  1062.                 map.put(prefixKey+ API_IMPL_SICUREZZA_MESSAGGIO_STORE_OCSP_POLICY,vOcsp);
  1063.             }
  1064.            
  1065.             if(aliasKey!=null) {
  1066.                 String vAliasKey = getStringValue(protocolPropertyList, aliasKey);
  1067.                 map.put(prefixKey+ API_IMPL_SICUREZZA_MESSAGGIO_KEY_ALIAS,vAliasKey);
  1068.             }
  1069.                        
  1070.         }
  1071.        
  1072.     }
  1073.        
  1074.     private static boolean isProfiloSicurezzaMessaggioConIntegritaX509(AccordoServizioParteComune api, String portType) {
  1075.         List<String> tmp = RegistroServiziUtils.fillPropertyProtocollo(CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO, api, portType, false);
  1076.         if(tmp!=null && !tmp.isEmpty()) {
  1077.             for (String profiloSicurezzaMessaggio : tmp) {
  1078.                 if(CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_VALUE_IDAM0301.equals(profiloSicurezzaMessaggio) ||
  1079.                         CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_VALUE_IDAM0302.equals(profiloSicurezzaMessaggio)) {
  1080.                     return true;
  1081.                 }      
  1082.             }
  1083.         }
  1084.         return false;
  1085.     }
  1086.     private static boolean isProfiloSicurezzaMessaggioConIntegritaKid(AccordoServizioParteComune api, String portType) {
  1087.         List<String> tmp = RegistroServiziUtils.fillPropertyProtocollo(CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO, api, portType, false);
  1088.         if(tmp!=null && !tmp.isEmpty()) {
  1089.             for (String profiloSicurezzaMessaggio : tmp) {
  1090.                 if(CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_VALUE_IDAM0401.equals(profiloSicurezzaMessaggio) ||
  1091.                         CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_VALUE_IDAM0402.equals(profiloSicurezzaMessaggio)) {
  1092.                     return true;
  1093.                 }      
  1094.             }
  1095.         }
  1096.         return false;
  1097.     }
  1098.     private static boolean isProfiloSicurezzaMessaggioCorniceSicurezza(AccordoServizioParteComune api, String portType) {
  1099.         List<String> tmp = RegistroServiziUtils.fillPropertyProtocollo(CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CORNICE_SICUREZZA, api, portType, true);
  1100.         if(tmp!=null && !tmp.isEmpty()) {
  1101.             for (String v : tmp) {
  1102.                 if(v!=null && "true".equals(v)) {
  1103.                     return true;
  1104.                 }
  1105.             }
  1106.         }
  1107.         return false;
  1108.     }
  1109.     private static String getProfiloSicurezzaMessaggioCorniceSicurezzaPattern(AccordoServizioParteComune api, String portType) {
  1110.         List<String> tmp = RegistroServiziUtils.fillPropertyProtocollo(CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CORNICE_SICUREZZA_PATTERN, api, portType, false);
  1111.         if(tmp!=null && !tmp.isEmpty()) {
  1112.             return tmp.get(0);
  1113.         }
  1114.         return null;
  1115.     }
  1116.     private static String getProfiloSicurezzaMessaggioCorniceSicurezzaSchema(AccordoServizioParteComune api, String portType) {
  1117.         List<String> tmp = RegistroServiziUtils.fillPropertyProtocollo(CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CORNICE_SICUREZZA_SCHEMA, api, portType, false);
  1118.         if(tmp!=null && !tmp.isEmpty()) {
  1119.             return tmp.get(0);
  1120.         }
  1121.         return null;
  1122.     }
  1123.     private static boolean isProfiloSicurezzaMessaggioRequestDigest(AccordoServizioParteComune api, String portType) {
  1124.         List<String> tmp = RegistroServiziUtils.fillPropertyProtocollo(CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_RISPOSTA_REQUEST_DIGEST, api, portType, true);
  1125.         if(tmp!=null && !tmp.isEmpty()) {
  1126.             for (String v : tmp) {
  1127.                 if(v!=null && "true".equals(v)) {
  1128.                     return true;
  1129.                 }
  1130.             }
  1131.         }
  1132.         return false;
  1133.     }
  1134.     private static boolean isProfiloSicurezzaMessaggioConHeaderDuplicati(AccordoServizioParteComune api, String portType) {
  1135.         List<String> tmp = RegistroServiziUtils.fillPropertyProtocollo(CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_HEADER, api, portType, false);
  1136.         if(tmp!=null && !tmp.isEmpty()) {
  1137.             for (String profiloSicurezzaMessaggio : tmp) {
  1138.                 if(CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_HEADER_VALUE_AUTHORIZATION_MODIPA.equals(profiloSicurezzaMessaggio) ||
  1139.                         CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_HEADER_VALUE_AUTHORIZATION_MODIPA_AUTH_IN_RESPONSE.equals(profiloSicurezzaMessaggio) ||
  1140.                         CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_HEADER_VALUE_AUTHORIZATION_CUSTOM.equals(profiloSicurezzaMessaggio) ||
  1141.                         CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_HEADER_VALUE_AUTHORIZATION_CUSTOM_AUTH_IN_RESPONSE.equals(profiloSicurezzaMessaggio)) {
  1142.                     return true;
  1143.                 }      
  1144.             }
  1145.         }
  1146.         return false;
  1147.     }
  1148.    
  1149.     // NOTA: tutti i metodi seguenti devono essere riportati in org.openspcoop2.protocol.engine.utils.ModITestUtils
  1150.     public static final String CLASS_MODIPA_PROPERTIES = "org.openspcoop2.protocol.modipa.config.ModIProperties";
  1151.     private static final String CLASS_MODIPA_PROPERTIES_GET_INSTANCE_METHOD = "getInstance";
  1152.    
  1153.     public static Object getModiProperties() throws ProtocolException {
  1154.         try {
  1155.             Class<?> modiPropertiesClass = Class.forName(CLASS_MODIPA_PROPERTIES);
  1156.             Method mGetInstance = modiPropertiesClass.getMethod(CLASS_MODIPA_PROPERTIES_GET_INSTANCE_METHOD);
  1157.             return mGetInstance.invoke(null);
  1158.         }catch(Exception e) {
  1159.             throw new ProtocolException(e.getMessage(),e);
  1160.         }
  1161.     }
  1162.    
  1163.     public static String getHeaderModI() throws ProtocolException {
  1164.         try {
  1165.             Object instance = getModiProperties();
  1166.             Method mGetRestSecurityTokenHeaderModI = instance.getClass().getMethod("getRestSecurityTokenHeaderModI");
  1167.             return (String) mGetRestSecurityTokenHeaderModI.invoke(instance);
  1168.         }catch(Exception e) {
  1169.             throw new ProtocolException(e.getMessage(),e);
  1170.         }
  1171.     }
  1172.    
  1173.     public static boolean isTokenOAuthUseJtiIntegrityAsMessageId() throws ProtocolException {
  1174.         try {
  1175.             Object instance = getModiProperties();
  1176.             Method mGetIsTokenOAuthUseJtiIntegrityAsMessageId = instance.getClass().getMethod("isTokenOAuthUseJtiIntegrityAsMessageId");
  1177.             return (Boolean) mGetIsTokenOAuthUseJtiIntegrityAsMessageId.invoke(instance);
  1178.         }catch(Exception e) {
  1179.             throw new ProtocolException(e.getMessage(),e);
  1180.         }
  1181.     }
  1182.    
  1183.     public static IDAccordo buildSignalHubPushIdAPI(IDSoggetto idSoggettoDefault) throws ProtocolException {
  1184.         try {
  1185.             Object instance = getModiProperties();
  1186.             Method mGetSignalHubApiName = instance.getClass().getMethod("getSignalHubApiName");
  1187.             Method mGetSignalHubApiVersion = instance.getClass().getMethod("getSignalHubApiVersion");
  1188.             return IDAccordoFactory.getInstance().getIDAccordoFromValues((String) mGetSignalHubApiName.invoke(instance), idSoggettoDefault, (Integer) mGetSignalHubApiVersion.invoke(instance));
  1189.         }catch(Exception e) {
  1190.             throw new ProtocolException(e.getMessage(),e);
  1191.         }
  1192.     }
  1193.    
  1194.     public static boolean isSignalHubEnabled() throws ProtocolException {
  1195.         try {
  1196.             Object instance = getModiProperties();
  1197.             Method mIsSignalHubEnabled = instance.getClass().getMethod("isSignalHubEnabled");
  1198.             return (Boolean) mIsSignalHubEnabled.invoke(instance);
  1199.         }catch(Exception e) {
  1200.             throw new ProtocolException(e.getMessage(),e);
  1201.         }
  1202.     }
  1203.    
  1204.     public static boolean isTracingPDNDEnabled() throws ProtocolException {
  1205.         try {
  1206.             Object instance = getModiProperties();
  1207.             Method mIsTracingPDNDEnabled = instance.getClass().getMethod("isTracingPDNDEnabled");
  1208.             return (Boolean) mIsTracingPDNDEnabled.invoke(instance);
  1209.         }catch(Exception e) {
  1210.             throw new ProtocolException(e.getMessage(),e);
  1211.         }
  1212.     }
  1213.    
  1214.     @SuppressWarnings("unchecked")
  1215.     public static List<RemoteStoreConfig> getRemoteStoreConfig() throws ProtocolException {
  1216.         try {
  1217.             Object instance = getModiProperties();
  1218.             Method mGetRemoteStoreConfig = instance.getClass().getMethod("getRemoteStoreConfig");
  1219.             return (List<RemoteStoreConfig>) mGetRemoteStoreConfig.invoke(instance);
  1220.         }catch(Exception e) {
  1221.             throw new ProtocolException(e.getMessage(),e);
  1222.         }
  1223.     }
  1224.    
  1225.     public static RemoteKeyType getRemoteKeyType(String name) throws ProtocolException {
  1226.         try {
  1227.             Object instance = getModiProperties();
  1228.             Method mGetRemoteKey = instance.getClass().getMethod("getRemoteKeyType",String.class);
  1229.             return (RemoteKeyType) mGetRemoteKey.invoke(instance,name);
  1230.         }catch(Exception e) {
  1231.             throw new ProtocolException(e.getMessage(),e);
  1232.         }
  1233.     }
  1234.    
  1235.     public static KeystoreParams getSicurezzaMessaggioCertificatiTrustStore() throws ProtocolException {
  1236.         try {
  1237.             Object instance = getModiProperties();
  1238.             Method mGet = instance.getClass().getMethod("getSicurezzaMessaggioCertificatiTrustStore");
  1239.             return (KeystoreParams) mGet.invoke(instance);
  1240.         }catch(Exception e) {
  1241.             throw new ProtocolException(e.getMessage(),e);
  1242.         }
  1243.     }
  1244.    
  1245.     public static KeystoreParams getSicurezzaMessaggioSslTrustStore() throws ProtocolException {
  1246.         try {
  1247.             Object instance = getModiProperties();
  1248.             Method mGet = instance.getClass().getMethod("getSicurezzaMessaggioSslTrustStore");
  1249.             return (KeystoreParams) mGet.invoke(instance);
  1250.         }catch(Exception e) {
  1251.             throw new ProtocolException(e.getMessage(),e);
  1252.         }
  1253.     }
  1254.    
  1255.     public static KeystoreParams getSicurezzaMessaggioCertificatiKeyStore() throws ProtocolException {
  1256.         try {
  1257.             Object instance = getModiProperties();
  1258.             Method mGet = instance.getClass().getMethod("getSicurezzaMessaggioCertificatiKeyStore");
  1259.             return (KeystoreParams) mGet.invoke(instance);
  1260.         }catch(Exception e) {
  1261.             throw new ProtocolException(e.getMessage(),e);
  1262.         }
  1263.     }
  1264.    
  1265.     public static KeystoreParams getApplicativoKeystoreParams(List<org.openspcoop2.core.config.ProtocolProperty> protocolPropertyList) {
  1266.        
  1267.         if(protocolPropertyList==null || protocolPropertyList.isEmpty()) {
  1268.             return null;
  1269.         }
  1270.        
  1271.         KeystoreParams keystoreParams = null;
  1272.        
  1273.         String sicurezza = getStringValueConfig(protocolPropertyList,CostantiDB.MODIPA_SICUREZZA_MESSAGGIO);
  1274.         if("true".equals(sicurezza)) {
  1275.            
  1276.             boolean keystoreModePath = false;
  1277.             boolean keystoreModeArchive = false;
  1278.             boolean keystoreModeHsm = false;
  1279.             String mode = getStringValueConfig(protocolPropertyList, CostantiDB.MODIPA_KEYSTORE_MODE);
  1280.             String path =null;
  1281.             String type = CostantiDB.MODIPA_KEYSTORE_TYPE;
  1282.             if(CostantiDB.MODIPA_KEYSTORE_MODE_VALUE_ARCHIVE.equals(mode)) {
  1283.                 keystoreModeArchive = true;
  1284.             }
  1285.             else if(CostantiDB.MODIPA_KEYSTORE_MODE_VALUE_PATH.equals(mode)) {
  1286.                 keystoreModePath = true;
  1287.                 path = CostantiDB.MODIPA_KEYSTORE_PATH;
  1288.             }
  1289.             else if(CostantiDB.MODIPA_KEYSTORE_MODE_VALUE_HSM.equals(mode)) {
  1290.                 keystoreModeHsm = true;
  1291.             }
  1292.            
  1293.             String vType = null;
  1294.             if(type!=null) {
  1295.                 vType = getStringValueConfig(protocolPropertyList, type);
  1296.             }
  1297.            
  1298.             if(keystoreModePath) {
  1299.                 // nop
  1300.             }
  1301.            
  1302.             String vPath = null;
  1303.             byte[] vStore = null;
  1304.             if(keystoreModeHsm) {
  1305.                 vPath = CostantiLabel.STORE_HSM;
  1306.             }
  1307.             else if(path!=null) {
  1308.                 vPath = getStringValueConfig(protocolPropertyList, path);
  1309.             }
  1310.             else if(keystoreModeArchive) {
  1311.                 vPath = CostantiLabel.STORE_CARICATO_BASEDATI;
  1312.                 vStore = getBinaryValueConfig(protocolPropertyList, CostantiDB.MODIPA_KEYSTORE_ARCHIVE);
  1313.             }
  1314.            
  1315.             String pw = CostantiDB.MODIPA_KEYSTORE_PASSWORD;
  1316.             String vPassword = getStringValueConfig(protocolPropertyList, pw);
  1317.                        
  1318.             String aliasKey = CostantiDB.MODIPA_KEY_ALIAS;
  1319.             String vAliasKey = getStringValueConfig(protocolPropertyList, aliasKey);
  1320.            
  1321.             String passwordKey = CostantiDB.MODIPA_KEY_PASSWORD;
  1322.             String vPassordKey = getStringValueConfig(protocolPropertyList, passwordKey);
  1323.            
  1324.             String vPathPublicKey = null;
  1325.             if(KeystoreType.KEY_PAIR.getNome().equals(vType)) {
  1326.                 vPathPublicKey = getStringValueConfig(protocolPropertyList, CostantiDB.MODIPA_KEYSTORE_PATH_PUBLIC_KEY);
  1327.             }
  1328.            
  1329.             String vKeyPairAlgorithm = null;
  1330.             if(KeystoreType.KEY_PAIR.getNome().equals(vType) || KeystoreType.PUBLIC_KEY.getNome().equals(vType)) {
  1331.                 vKeyPairAlgorithm = getStringValueConfig(protocolPropertyList, CostantiDB.MODIPA_KEYSTORE_KEY_ALGORITHM);
  1332.             }
  1333.            
  1334.             String byok = CostantiDB.MODIPA_KEYSTORE_BYOK_POLICY;
  1335.             String vByok = null;
  1336.             if(!keystoreModeArchive && !keystoreModeHsm) {
  1337.                 vByok = getStringValueConfig(protocolPropertyList, byok);
  1338.             }
  1339.            
  1340.             keystoreParams = new KeystoreParams();
  1341.             keystoreParams.setType(vType);
  1342.             keystoreParams.setPath(vPath);
  1343.             keystoreParams.setStore(vStore);
  1344.             keystoreParams.setPassword(vPassword);
  1345.             keystoreParams.setKeyAlias(vAliasKey);
  1346.             keystoreParams.setKeyPassword(vPassordKey);
  1347.             keystoreParams.setKeyPairPublicKeyPath(vPathPublicKey);
  1348.             keystoreParams.setKeyPairAlgorithm(vKeyPairAlgorithm);
  1349.             keystoreParams.setByokPolicy(vByok);
  1350.            
  1351.         }
  1352.    
  1353.         return keystoreParams;
  1354.     }
  1355.    
  1356.     public static byte[] getApplicativoKeystoreCertificate(List<org.openspcoop2.core.config.ProtocolProperty> protocolPropertyList) {
  1357.        
  1358.         byte[] b = null;
  1359.        
  1360.         if(protocolPropertyList==null || protocolPropertyList.isEmpty()) {
  1361.             return b;
  1362.         }
  1363.        
  1364.         String sicurezza = getStringValueConfig(protocolPropertyList,CostantiDB.MODIPA_SICUREZZA_MESSAGGIO);
  1365.         if("true".equals(sicurezza)) {
  1366.            
  1367.             return getBinaryValueConfig(protocolPropertyList, CostantiDB.MODIPA_KEYSTORE_CERTIFICATE);
  1368.            
  1369.         }
  1370.    
  1371.         return b;
  1372.     }
  1373.    
  1374.     public static boolean existsStoreConfig(List<org.openspcoop2.core.registry.ProtocolProperty> protocolPropertyList, boolean includeRemoteStore,
  1375.             boolean checkModeFruizioneKeystoreId) throws ProtocolException {
  1376.         KeystoreParams keystoreParams = null;
  1377.         try {
  1378.             keystoreParams = ModIUtils.getKeyStoreParams(protocolPropertyList,
  1379.                     checkModeFruizioneKeystoreId);
  1380.         }catch(Exception e) {
  1381.             throw new ProtocolException(e.getMessage(),e);
  1382.         }
  1383.         KeystoreParams truststoreParams = null;
  1384.         try {
  1385.             truststoreParams = ModIUtils.getTrustStoreParams(protocolPropertyList);
  1386.         }catch(Exception e) {
  1387.             throw new ProtocolException(e.getMessage(),e);
  1388.         }
  1389.         KeystoreParams truststoreSslParams = null;
  1390.         try {
  1391.             truststoreSslParams = ModIUtils.getTrustStoreSSLParams(protocolPropertyList);
  1392.         }catch(Exception e) {
  1393.             throw new ProtocolException(e.getMessage(),e);
  1394.         }
  1395.        
  1396.         boolean existsStore = keystoreParams!=null || truststoreParams!=null || truststoreSslParams!=null;
  1397.         if(!includeRemoteStore) {
  1398.             return existsStore;
  1399.         }
  1400.        
  1401.         List<RemoteStoreConfig> trustStoreRemoteConfig = null;
  1402.         try {
  1403.             trustStoreRemoteConfig = ModIUtils.getRemoteStoreConfig();
  1404.         }catch(Exception e) {
  1405.             throw new ProtocolException(e.getMessage(),e);
  1406.         }
  1407.        
  1408.         return existsStore || (trustStoreRemoteConfig!=null && !trustStoreRemoteConfig.isEmpty());
  1409.     }
  1410.    
  1411.     public static KeystoreParams getKeyStoreParams(List<org.openspcoop2.core.registry.ProtocolProperty> protocolPropertyList,
  1412.             boolean checkModeFruizioneKeystoreId) throws ProtocolException {
  1413.         return getKeystoreParamsEngine(protocolPropertyList, false, false,
  1414.                 checkModeFruizioneKeystoreId);
  1415.     }
  1416.     public static KeystoreParams getTrustStoreParams(List<org.openspcoop2.core.registry.ProtocolProperty> protocolPropertyList) throws ProtocolException {
  1417.         return getKeystoreParamsEngine(protocolPropertyList, false, true,
  1418.                 false);
  1419.     }
  1420.     public static KeystoreParams getTrustStoreSSLParams(List<org.openspcoop2.core.registry.ProtocolProperty> protocolPropertyList) throws ProtocolException {
  1421.         return getKeystoreParamsEngine(protocolPropertyList, true, false,
  1422.                 false);
  1423.     }
  1424.     private static KeystoreParams getKeystoreParamsEngine(List<org.openspcoop2.core.registry.ProtocolProperty> protocolPropertyList,
  1425.             boolean ssl, boolean truststore,
  1426.             boolean checkModeFruizioneKeystoreId) throws ProtocolException {
  1427.                
  1428.         if(protocolPropertyList==null || protocolPropertyList.isEmpty()) {
  1429.             return null;
  1430.         }
  1431.        
  1432.         if(checkModeFruizioneKeystoreId) {
  1433.             String v = getStringValue(protocolPropertyList, CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_FRUIZIONE_KEYSTORE_MODE);
  1434.             if(
  1435.                     v!=null && !StringUtils.isEmpty(v) && // aggiungo questo controllo per evitare che nel caso di API con solo pattern ID_AUTH_REST_01 via PDND dove non viene visualizzato 'MODIPA_PROFILO_SICUREZZA_MESSAGGIO_FRUIZIONE_KEYSTORE_MODE' si esca e non si controlli i certificati
  1436.                     !CostantiDB.MODIPA_KEYSTORE_FRUIZIONE.equals(v)) {
  1437.                 return null;
  1438.             }
  1439.         }
  1440.        
  1441.         KeystoreParams keystoreParams = null;
  1442.        
  1443.         String id = null;
  1444.         if(ssl) {
  1445.             id = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_SSL_TRUSTSTORE_MODE;
  1446.         }
  1447.         else if(truststore) {
  1448.             id = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CERTIFICATI_TRUSTSTORE_MODE;
  1449.         }
  1450.         else {
  1451.             id = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CERTIFICATI_KEYSTORE_MODE;
  1452.         }
  1453.        
  1454.         String v = getStringValue(protocolPropertyList, id);
  1455.         if(v!=null && !StringUtils.isEmpty(v) && !CostantiDB.MODIPA_PROFILO_UNDEFINED.equals(v)) {
  1456.             if(CostantiDB.MODIPA_PROFILO_DEFAULT.equals(v)) {
  1457.                 if(ssl) {
  1458.                     keystoreParams = ModIUtils.getSicurezzaMessaggioSslTrustStore();
  1459.                 }
  1460.                 else if(truststore) {
  1461.                     keystoreParams = ModIUtils.getSicurezzaMessaggioCertificatiTrustStore();
  1462.                 }
  1463.                 else {
  1464.                     keystoreParams = ModIUtils.getSicurezzaMessaggioCertificatiKeyStore();
  1465.                 }
  1466.             }
  1467.             else if(CostantiDB.MODIPA_PROFILO_RIDEFINISCI.equals(v)) {
  1468.                
  1469.                 String type = null;
  1470.                 String path = null;
  1471.                 String archive = null;
  1472.                 String pw = null;
  1473.                 String crl = null;
  1474.                 String ocsp = null;
  1475.                 String aliasKey = null;
  1476.                 String pwKey = null;
  1477.                 String byok = null;
  1478.                 boolean keystoreModePath = false;
  1479.                 boolean keystoreModeArchive = false;
  1480.                 boolean keystoreModeHsm = false;
  1481.                 if(ssl) {
  1482.                     type = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_SSL_TRUSTSTORE_TYPE;
  1483.                     path = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_SSL_TRUSTSTORE_PATH;
  1484.                     pw = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_SSL_TRUSTSTORE_PASSWORD;
  1485.                     crl = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_SSL_TRUSTSTORE_CRLS;
  1486.                     ocsp = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_SSL_TRUSTSTORE_OCSP_POLICY;
  1487.                 }
  1488.                 else if(truststore) {
  1489.                     type = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CERTIFICATI_TRUSTSTORE_TYPE;
  1490.                     path = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CERTIFICATI_TRUSTSTORE_PATH;
  1491.                     pw = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CERTIFICATI_TRUSTSTORE_PASSWORD;
  1492.                     crl = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CERTIFICATI_TRUSTSTORE_CRLS;
  1493.                     ocsp = CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CERTIFICATI_TRUSTSTORE_OCSP_POLICY;
  1494.                 }
  1495.                 else {
  1496.                     type = CostantiDB.MODIPA_KEYSTORE_TYPE;
  1497.                     pw = CostantiDB.MODIPA_KEYSTORE_PASSWORD;
  1498.                     aliasKey = CostantiDB.MODIPA_KEY_ALIAS;
  1499.                     pwKey = CostantiDB.MODIPA_KEY_PASSWORD;
  1500.                     String mode = getStringValue(protocolPropertyList, CostantiDB.MODIPA_KEYSTORE_MODE);
  1501.                     if(CostantiDB.MODIPA_KEYSTORE_MODE_VALUE_ARCHIVE.equals(mode)) {
  1502.                         keystoreModeArchive = true;
  1503.                         archive = CostantiDB.MODIPA_KEYSTORE_ARCHIVE;
  1504.                     }
  1505.                     else if(CostantiDB.MODIPA_KEYSTORE_MODE_VALUE_PATH.equals(mode)) {
  1506.                         keystoreModePath = true;
  1507.                         path = CostantiDB.MODIPA_KEYSTORE_PATH;
  1508.                         byok = CostantiDB.MODIPA_KEYSTORE_BYOK_POLICY;
  1509.                     }
  1510.                     else if(CostantiDB.MODIPA_KEYSTORE_MODE_VALUE_HSM.equals(mode)) {
  1511.                         keystoreModeHsm = true;
  1512.                     }
  1513.                 }
  1514.                
  1515.                 String vType = null;
  1516.                 if(type!=null) {
  1517.                     vType = getStringValue(protocolPropertyList, type);
  1518.                 }
  1519.                
  1520.                 if(keystoreModePath) {
  1521.                     // nop
  1522.                 }
  1523.                
  1524.                 String vPassword = null;
  1525.                 if(pw!=null) {
  1526.                     vPassword = getStringValue(protocolPropertyList, pw);
  1527.                 }
  1528.                
  1529.                 boolean hsm = false;
  1530.                 if(ssl || truststore) {
  1531.                     if(vType!=null) {
  1532.                         hsm = HSMUtils.isKeystoreHSM(vType);
  1533.                     }
  1534.                 }
  1535.                 else {
  1536.                     hsm = keystoreModeHsm;
  1537.                 }
  1538.                
  1539.                 String vPath = null;
  1540.                 byte[] vStore = null;
  1541.                 if(hsm) {
  1542.                     vPath = CostantiLabel.STORE_HSM;
  1543.                 }
  1544.                 else if(path!=null) {
  1545.                     vPath = getStringValue(protocolPropertyList, path);
  1546.                 }
  1547.                 else if(keystoreModeArchive) {
  1548.                     vPath = CostantiLabel.STORE_CARICATO_BASEDATI;
  1549.                     vStore = getBinaryValue(protocolPropertyList, archive);
  1550.                 }
  1551.                
  1552.                 String vCrl = null;
  1553.                 if(crl!=null) {
  1554.                     vCrl = getStringValue(protocolPropertyList, crl);
  1555.                 }
  1556.                
  1557.                 String vOcsp = null;
  1558.                 if(ocsp!=null) {
  1559.                     vOcsp = getStringValue(protocolPropertyList, ocsp);
  1560.                 }
  1561.                
  1562.                 String vAliasKey = null;
  1563.                 if(aliasKey!=null) {
  1564.                     vAliasKey = getStringValue(protocolPropertyList, aliasKey);
  1565.                 }
  1566.                
  1567.                 String vPasswordKey = null;
  1568.                 if(pwKey!=null) {
  1569.                     vPasswordKey = getStringValue(protocolPropertyList, pwKey);
  1570.                 }
  1571.                
  1572.                 String vPathPublicKey = null;
  1573.                 if(KeystoreType.KEY_PAIR.getNome().equals(vType)) {
  1574.                     vPathPublicKey = getStringValue(protocolPropertyList, CostantiDB.MODIPA_KEYSTORE_PATH_PUBLIC_KEY);
  1575.                 }
  1576.                
  1577.                 String vKeyPairAlgorithm = null;
  1578.                 if(KeystoreType.KEY_PAIR.getNome().equals(vType) || KeystoreType.PUBLIC_KEY.getNome().equals(vType)) {
  1579.                     vKeyPairAlgorithm = getStringValue(protocolPropertyList, CostantiDB.MODIPA_KEYSTORE_KEY_ALGORITHM);
  1580.                 }
  1581.                
  1582.                 String vByok = null;
  1583.                 if(byok!=null) {
  1584.                     vByok = getStringValue(protocolPropertyList, byok);
  1585.                 }
  1586.                
  1587.                 keystoreParams = new KeystoreParams();
  1588.                 keystoreParams.setType(vType);
  1589.                 keystoreParams.setPath(vPath);
  1590.                 keystoreParams.setStore(vStore);
  1591.                 keystoreParams.setPassword(vPassword);
  1592.                 keystoreParams.setCrls(vCrl);
  1593.                 keystoreParams.setOcspPolicy(vOcsp);
  1594.                 keystoreParams.setKeyAlias(vAliasKey);
  1595.                 keystoreParams.setKeyPassword(vPasswordKey);
  1596.                 keystoreParams.setKeyPairPublicKeyPath(vPathPublicKey);
  1597.                 keystoreParams.setKeyPairAlgorithm(vKeyPairAlgorithm);
  1598.                 keystoreParams.setByokPolicy(vByok);
  1599.             }
  1600.         }
  1601.    
  1602.         return keystoreParams;
  1603.     }

  1604.    
  1605.     private static String getStringValue(List<ProtocolProperty> protocolPropertyList, String id) {
  1606.         for (ProtocolProperty protocolProperty : protocolPropertyList) {
  1607.             if(protocolProperty.getName().equals(id)) {
  1608.                 if(StringUtils.isNotEmpty(protocolProperty.getValue())) {
  1609.                     return protocolProperty.getValue();
  1610.                 }
  1611.                 else if(protocolProperty.getNumberValue()!=null) {
  1612.                     return protocolProperty.getNumberValue().toString();
  1613.                 }
  1614.                 else if(protocolProperty.getBooleanValue()!=null) {
  1615.                     return protocolProperty.getBooleanValue().toString();
  1616.                 }
  1617.                 else if(protocolProperty.getByteFile()!=null) {
  1618.                     return "Archivio binario";
  1619.                 }
  1620.                 else {
  1621.                     return null;
  1622.                 }
  1623.             }
  1624.         }
  1625.         return null;
  1626.     }
  1627.     private static String getBooleanValueAsStato(List<ProtocolProperty> protocolPropertyList, String id) {
  1628.         for (ProtocolProperty protocolProperty : protocolPropertyList) {
  1629.             if(protocolProperty.getName().equals(id)) {
  1630.                 return protocolProperty.getBooleanValue()!=null && protocolProperty.getBooleanValue() ?
  1631.                         StatoFunzionalita.ABILITATO.getValue() : StatoFunzionalita.DISABILITATO.getValue();
  1632.             }
  1633.         }
  1634.         return null;
  1635.     }
  1636.     static byte[] getBinaryValue(List<ProtocolProperty> protocolPropertyList, String id) {
  1637.         byte[] b = null;
  1638.         for (ProtocolProperty protocolProperty : protocolPropertyList) {
  1639.             if(protocolProperty.getName().equals(id)) {
  1640.                 return protocolProperty.getByteFile();
  1641.             }
  1642.         }
  1643.         return b;
  1644.     }
  1645.    
  1646.     private static String getStringValueConfig(List<org.openspcoop2.core.config.ProtocolProperty> protocolPropertyList, String id) {
  1647.         for (org.openspcoop2.core.config.ProtocolProperty protocolProperty : protocolPropertyList) {
  1648.             if(protocolProperty.getName().equals(id)) {
  1649.                 if(StringUtils.isNotEmpty(protocolProperty.getValue())) {
  1650.                     return protocolProperty.getValue();
  1651.                 }
  1652.                 else if(protocolProperty.getNumberValue()!=null) {
  1653.                     return protocolProperty.getNumberValue().toString();
  1654.                 }
  1655.                 else if(protocolProperty.getBooleanValue()!=null) {
  1656.                     return protocolProperty.getBooleanValue().toString();
  1657.                 }
  1658.                 else if(protocolProperty.getByteFile()!=null) {
  1659.                     return "Archivio binario";
  1660.                 }
  1661.                 else {
  1662.                     return null;
  1663.                 }
  1664.             }
  1665.         }
  1666.         return null;
  1667.     }
  1668.     static String getBooleanValueAsStatoConfig(List<org.openspcoop2.core.config.ProtocolProperty> protocolPropertyList, String id) {
  1669.         for (org.openspcoop2.core.config.ProtocolProperty protocolProperty : protocolPropertyList) {
  1670.             if(protocolProperty.getName().equals(id)) {
  1671.                 return protocolProperty.getBooleanValue()!=null && protocolProperty.getBooleanValue() ?
  1672.                         StatoFunzionalita.ABILITATO.getValue() : StatoFunzionalita.DISABILITATO.getValue();
  1673.             }
  1674.         }
  1675.         return null;
  1676.     }
  1677.     private static byte[] getBinaryValueConfig(List<org.openspcoop2.core.config.ProtocolProperty> protocolPropertyList, String id) {
  1678.         byte[] b = null;
  1679.         for (org.openspcoop2.core.config.ProtocolProperty protocolProperty : protocolPropertyList) {
  1680.             if(protocolProperty.getName().equals(id)) {
  1681.                 return protocolProperty.getByteFile();
  1682.             }
  1683.         }
  1684.         return b;
  1685.     }
  1686.    
  1687.     public static final String PROPRIETA_INTERMEDIARIO = "intermediario";
  1688.     public static boolean isSoggettoCanaleIntermediario(Soggetto soggettoCanale, Logger log) {
  1689.         try {
  1690.             if(soggettoCanale.sizeProprietaList()>0) {
  1691.                 for (Proprieta proprieta :soggettoCanale.getProprieta()) {
  1692.                     if(PROPRIETA_INTERMEDIARIO.equals(proprieta.getNome())) {
  1693.                         return "true".equalsIgnoreCase(proprieta.getValore());
  1694.                     }
  1695.                 }
  1696.             }
  1697.         }catch(Exception e) {
  1698.             log.error("[isSoggettoCanaleIntermediario] Process failed: "+e.getMessage(),e);
  1699.         }
  1700.         return false;
  1701.     }
  1702.    
  1703.     public static String getMessaggioErroreDominioCanaleDifferenteDominioApplicativo(IDServizioApplicativo idServizioApplicativoMessaggio, IDSoggetto idSoggettoMittenteCanale) {
  1704.         return "Token di sicurezza firmato da un applicativo '"+idServizioApplicativoMessaggio.getNome()+
  1705.                 "' risiedente nel dominio del soggetto '"+idServizioApplicativoMessaggio.getIdSoggettoProprietario().toString()+"'; il dominio differisce dal soggetto identificato sul canale di trasporto ("+idSoggettoMittenteCanale+
  1706.                 ")";
  1707.     }
  1708.    
  1709.     public static String convertProfiloSicurezzaToSDKValue(String securityMessageProfile, boolean rest) {
  1710.         String profilo = securityMessageProfile.toUpperCase();
  1711.         if(rest) {
  1712.             profilo = profilo.replace("M", "R");
  1713.         }
  1714.         else {
  1715.             profilo = profilo.replace("M", "S");
  1716.         }
  1717.         return profilo;
  1718.     }
  1719.     public static String convertProfiloSicurezzaToConfigurationValue(String securityMessageProfileSDKValue) {
  1720.         String securityMessageProfile = securityMessageProfileSDKValue.toLowerCase();
  1721.         securityMessageProfile = securityMessageProfile.replace("r", "m");
  1722.         securityMessageProfile = securityMessageProfile.replace("s", "m");
  1723.         return securityMessageProfile;
  1724.     }
  1725.    
  1726.     public static String convertProfiloSicurezzaSorgenteTokenToSDKValue(String sorgenteToken) {
  1727.         if(CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_SORGENTE_TOKEN_IDAUTH_VALUE_LOCALE.equals(sorgenteToken)) {
  1728.             return CostantiLabel.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_SORGENTE_TOKEN_IDAUTH_LOCALE;
  1729.         }
  1730.         else if(CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_SORGENTE_TOKEN_IDAUTH_VALUE_PDND.equals(sorgenteToken)) {
  1731.             return CostantiLabel.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_SORGENTE_TOKEN_IDAUTH_PDND;
  1732.         }
  1733.         else if(CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_SORGENTE_TOKEN_IDAUTH_VALUE_OAUTH.equals(sorgenteToken)) {
  1734.             return CostantiLabel.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_SORGENTE_TOKEN_IDAUTH_OAUTH;
  1735.         }
  1736.         else {
  1737.             // backward compatibility
  1738.             return CostantiLabel.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_SORGENTE_TOKEN_IDAUTH_LOCALE;
  1739.         }
  1740.     }
  1741.     public static String convertProfiloSicurezzaSorgenteTokenToConfigurationValue(String sorgenteTokenSDKValue) throws ProtocolException {
  1742.         if(CostantiLabel.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_SORGENTE_TOKEN_IDAUTH_LOCALE.equals(sorgenteTokenSDKValue)) {
  1743.             return CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_SORGENTE_TOKEN_IDAUTH_VALUE_LOCALE;
  1744.         }
  1745.         else if(CostantiLabel.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_SORGENTE_TOKEN_IDAUTH_PDND.equals(sorgenteTokenSDKValue)) {
  1746.             return CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_SORGENTE_TOKEN_IDAUTH_VALUE_PDND;
  1747.         }
  1748.         else if(CostantiLabel.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_SORGENTE_TOKEN_IDAUTH_OAUTH.equals(sorgenteTokenSDKValue)) {
  1749.             return CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_SORGENTE_TOKEN_IDAUTH_VALUE_OAUTH;
  1750.         }
  1751.         throw newProtocolExceptionUnknown("Source label",sorgenteTokenSDKValue);
  1752.     }
  1753.    
  1754.    
  1755.     public static String convertProfiloAuditToSDKValue(String patternCorniceSicurezza) throws ProtocolException {
  1756.         if(CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CORNICE_SICUREZZA_PATTERN_OLD.equals(patternCorniceSicurezza)) {
  1757.             return CostantiLabel.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CORNICE_SICUREZZA_PATTERN_OLD;
  1758.         }
  1759.         else if(CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CORNICE_SICUREZZA_PATTERN_AUDIT_REST_01.equals(patternCorniceSicurezza)) {
  1760.             return CostantiLabel.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CORNICE_SICUREZZA_PATTERN_AUDIT_REST_01;
  1761.         }
  1762.         else if(CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CORNICE_SICUREZZA_PATTERN_AUDIT_REST_02.equals(patternCorniceSicurezza)) {
  1763.             return CostantiLabel.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CORNICE_SICUREZZA_PATTERN_AUDIT_REST_02;
  1764.         }
  1765.         throw newProtocolExceptionUnknown("Pattern value",patternCorniceSicurezza);
  1766.     }
  1767.     public static String convertProfiloAuditToConfigurationValue(String patternCorniceSicurezzaSDKValue) throws ProtocolException {
  1768.         if(CostantiLabel.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CORNICE_SICUREZZA_PATTERN_OLD.equals(patternCorniceSicurezzaSDKValue)) {
  1769.             return CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CORNICE_SICUREZZA_PATTERN_OLD;
  1770.         }
  1771.         else if(CostantiLabel.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CORNICE_SICUREZZA_PATTERN_AUDIT_REST_01.equals(patternCorniceSicurezzaSDKValue)) {
  1772.             return CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CORNICE_SICUREZZA_PATTERN_AUDIT_REST_01;
  1773.         }
  1774.         else if(CostantiLabel.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CORNICE_SICUREZZA_PATTERN_AUDIT_REST_02.equals(patternCorniceSicurezzaSDKValue)) {
  1775.             return CostantiDB.MODIPA_PROFILO_SICUREZZA_MESSAGGIO_CORNICE_SICUREZZA_PATTERN_AUDIT_REST_02;
  1776.         }
  1777.         throw newProtocolExceptionUnknown("Pattern label",patternCorniceSicurezzaSDKValue);
  1778.     }
  1779.    
  1780.     public static ProtocolException newProtocolExceptionUnknown(String prefix, String value) {
  1781.         return new ProtocolException(prefix+" '"+value+"' unknown");
  1782.     }
  1783.    
  1784.    
  1785.     public static final MapKey<String> MODIPA_OPENSPCOOP2_MSG_CONTEXT_USE_JTI_AUTHORIZATION = org.openspcoop2.utils.Map.newMapKey("MODIPA_USE_JTI_AUTHORIZATION");
  1786.     public static boolean useJtiAuthorizationObject(OpenSPCoop2Message msg) {
  1787.         Object useJtiAuthorizationObject = msg.getContextProperty(MODIPA_OPENSPCOOP2_MSG_CONTEXT_USE_JTI_AUTHORIZATION);
  1788.         boolean useJtiAuthorization = false;
  1789.         if(useJtiAuthorizationObject instanceof Boolean) {
  1790.             useJtiAuthorization = (Boolean) useJtiAuthorizationObject;
  1791.         }
  1792.         return useJtiAuthorization;
  1793.     }
  1794.     public static void replaceBustaIdWithJtiTokenId(ModIValidazioneSemanticaProfiloSicurezza modIValidazioneSemanticaProfiloSicurezza, String jtiClaimReceived) {
  1795.         if(jtiClaimReceived!=null && !StringUtils.isEmpty(jtiClaimReceived)) {
  1796.             Busta busta = modIValidazioneSemanticaProfiloSicurezza.getBusta();
  1797.             String idIntegrity = busta.getID();
  1798.             busta.removeProperty(CostantiDB.MODIPA_BUSTA_EXT_PROFILO_SICUREZZA_MESSAGGIO_ID);
  1799.             if(modIValidazioneSemanticaProfiloSicurezza.isSicurezzaMessaggio()) {
  1800.                 busta.addProperty(CostantiDB.MODIPA_BUSTA_EXT_PROFILO_SICUREZZA_MESSAGGIO_ID, idIntegrity);
  1801.             }
  1802.             busta.setID(jtiClaimReceived);
  1803.         }
  1804.     }
  1805.    
  1806.     public static String readJti(String token, Logger log) {
  1807.         try {
  1808.             if(token!=null && token.contains(".")) {
  1809.                 String [] tmp = token.split("\\.");
  1810.                 if(tmp!=null && tmp.length>=3) {
  1811.                     String jsonBase64 = tmp[1];
  1812.                     String json = new String(Base64Utilities.decode(jsonBase64.getBytes()));
  1813.                     return JsonPathExpressionEngine.extractAndConvertResultAsString(json, "$.jti", log);
  1814.                 }
  1815.             }
  1816.         }catch(Exception e) {
  1817.             // ignore
  1818.         }
  1819.         return null;
  1820.     }

  1821. }