AttributeAuthorityProvider.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.pdd.core.token.attribute_authority;

  21. import java.util.ArrayList;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.Properties;

  25. import org.apache.commons.lang.StringUtils;
  26. import org.openspcoop2.core.config.constants.CostantiConfigurazione;
  27. import org.openspcoop2.core.constants.CostantiConnettori;
  28. import org.openspcoop2.core.mvc.properties.Item;
  29. import org.openspcoop2.core.mvc.properties.constants.ItemType;
  30. import org.openspcoop2.core.mvc.properties.provider.ExternalResources;
  31. import org.openspcoop2.core.mvc.properties.provider.IProvider;
  32. import org.openspcoop2.core.mvc.properties.provider.InputValidationUtils;
  33. import org.openspcoop2.core.mvc.properties.provider.ProviderException;
  34. import org.openspcoop2.core.mvc.properties.provider.ProviderInfo;
  35. import org.openspcoop2.core.mvc.properties.provider.ProviderValidationException;
  36. import org.openspcoop2.core.plugins.constants.TipoPlugin;
  37. import org.openspcoop2.pdd.core.dynamic.DynamicHelperCostanti;
  38. import org.openspcoop2.pdd.core.token.Costanti;
  39. import org.openspcoop2.pdd.core.token.TokenUtilities;
  40. import org.openspcoop2.pdd.core.token.parser.Claims;
  41. import org.openspcoop2.security.message.constants.SecurityConstants;
  42. import org.openspcoop2.security.message.utils.AbstractSecurityProvider;
  43. import org.openspcoop2.utils.UtilsRuntimeException;
  44. import org.openspcoop2.utils.certificate.byok.BYOKProvider;
  45. import org.openspcoop2.utils.certificate.hsm.HSMUtils;
  46. import org.openspcoop2.utils.certificate.ocsp.OCSPProvider;
  47. import org.openspcoop2.utils.properties.PropertiesUtilities;
  48. import org.openspcoop2.utils.regexp.RegularExpressionEngine;
  49. import org.openspcoop2.utils.transport.http.SSLUtilities;

  50. /**    
  51.  * GetTokenProvider
  52.  *
  53.  * @author Poli Andrea (poli@link.it)
  54.  * @author $Author$
  55.  * @version $Rev$, $Date$
  56.  */
  57. public class AttributeAuthorityProvider implements IProvider {
  58.    
  59.     private OCSPProvider ocspProvider;
  60.     private BYOKProvider byokProvider;

  61.     public AttributeAuthorityProvider() {
  62.         this.ocspProvider = new OCSPProvider();
  63.         try {
  64.             this.byokProvider = BYOKProvider.getUnwrapInstance();
  65.         }catch(Exception e) {
  66.             throw new UtilsRuntimeException(e.getMessage(),e);
  67.         }
  68.     }
  69.    
  70.     @Override
  71.     public void validateId(String name) throws ProviderException, ProviderValidationException {
  72.         if(name==null || StringUtils.isEmpty(name)) {
  73.             throw new ProviderValidationException("Deve essere indicato un nome che identifica la policy per verso l'Attribute Authority");
  74.         }
  75.         if(name.contains(" ")) {
  76.             throw new ProviderValidationException("Il nome associato alla policy non deve contenere spazi");
  77.         }
  78.         boolean match = false;
  79.         try {
  80.             match = RegularExpressionEngine.isMatch(name,"^[_A-Za-z][\\-_A-Za-z0-9]*$");
  81.         }catch(Exception e) {
  82.             throw new ProviderException(e.getMessage(),e);
  83.         }
  84.         if (!match) {
  85.             throw new ProviderValidationException("Il nome associato alla policy può iniziare solo con un carattere [A-Za-z] o il simbolo '_' e dev'essere formato solo da caratteri, cifre, '_' , e '-'");
  86.         }
  87.     }
  88.    
  89.     @Override
  90.     public void validate(Map<String, Properties> mapProperties) throws ProviderException, ProviderValidationException {
  91.        
  92.         Properties pDefault = TokenUtilities.getDefaultProperties(mapProperties);
  93.            
  94.         boolean endpointSSL = TokenUtilities.isEnabled(pDefault, Costanti.POLICY_ENDPOINT_HTTPS_STATO);
  95.         boolean ssl = TokenUtilities.isEnabled(pDefault, org.openspcoop2.pdd.core.token.attribute_authority.Costanti.AA_AUTH_SSL_CLIENT_STATO);
  96.        
  97.         if(endpointSSL || ssl) {
  98.             validateEndpointSsl(mapProperties);
  99.         }
  100.        
  101.         String url = pDefault.getProperty(org.openspcoop2.pdd.core.token.attribute_authority.Costanti.AA_URL);
  102.         InputValidationUtils.validateTextAreaInput(url, "Endpoint - URL");
  103.         try{
  104.             org.openspcoop2.utils.regexp.RegExpUtilities.validateUrl(url, true);
  105.         }catch(Exception e){
  106.             throw new ProviderValidationException("La URL fornita non è valida: "+e.getMessage());
  107.         }  
  108.        
  109.                    
  110.         boolean basic = TokenUtilities.isEnabled(pDefault, org.openspcoop2.pdd.core.token.attribute_authority.Costanti.AA_AUTH_BASIC_STATO);
  111.         if(basic) {
  112.             validateBasicCredentials(pDefault);
  113.         }
  114.        
  115.         boolean bearer = TokenUtilities.isEnabled(pDefault, org.openspcoop2.pdd.core.token.attribute_authority.Costanti.AA_AUTH_BEARER_STATO);
  116.         if(bearer) {
  117.             String token = pDefault.getProperty(org.openspcoop2.pdd.core.token.attribute_authority.Costanti.AA_AUTH_BEARER_TOKEN);
  118.             InputValidationUtils.validateTextAreaInput(token, "Endpoint - Autenticazione Client - Token");
  119.         }
  120.        
  121.         if(ssl) {
  122.             validateSslCredentials(mapProperties);
  123.         }
  124.        
  125.         boolean proxy = TokenUtilities.isEnabled(pDefault, Costanti.POLICY_ENDPOINT_PROXY_STATO);
  126.         if(proxy) {
  127.             validateProxy(mapProperties);
  128.         }
  129.        
  130.         validateClaims(pDefault);
  131.        
  132.         String jwtKeystore = pDefault.getProperty(org.openspcoop2.pdd.core.token.attribute_authority.Costanti.AA_REQUEST_JWT_SIGN_KEYSTORE_FILE);
  133.         if(jwtKeystore!=null && StringUtils.isNotEmpty(jwtKeystore)) {
  134.             InputValidationUtils.validateTextAreaInput(jwtKeystore, "Richiesta - JWS KeyStore - File");
  135.         }
  136.        
  137.         String jwtKeystorePublicKey = pDefault.getProperty(org.openspcoop2.pdd.core.token.attribute_authority.Costanti.AA_REQUEST_JWT_SIGN_KEYSTORE_FILE_PUBLIC);
  138.         if(jwtKeystorePublicKey!=null && StringUtils.isNotEmpty(jwtKeystorePublicKey)) {
  139.             InputValidationUtils.validateTextAreaInput(jwtKeystorePublicKey, "Richiesta - JWS KeyStore - Chiave Pubblica");
  140.         }
  141.        
  142.         String requestX5U = pDefault.getProperty(org.openspcoop2.pdd.core.token.attribute_authority.Costanti.AA_REQUEST_JWT_SIGN_INCLUDE_X509_URL);
  143.         if(requestX5U!=null && StringUtils.isNotEmpty(requestX5U)) {
  144.             InputValidationUtils.validateTextAreaInput(requestX5U, "Richiesta - JWS Header - URL");
  145.         }
  146.        
  147.         String mode = pDefault.getProperty(org.openspcoop2.pdd.core.token.attribute_authority.Costanti.AA_RESPONSE_TYPE);
  148.        
  149.         if(org.openspcoop2.pdd.core.token.attribute_authority.Costanti.AA_RESPONSE_TYPE_VALUE_JWS.equals(mode) ) {
  150.             validateResponseTypeJWS(mapProperties, pDefault);
  151.         }
  152.        
  153.         if(org.openspcoop2.pdd.core.token.attribute_authority.Costanti.AA_RESPONSE_TYPE_VALUE_CUSTOM.equals(mode) ) {
  154.             validateResponseTypeCustom(pDefault);
  155.         }
  156.     }
  157.     private void validateEndpointSsl(Map<String, Properties> mapProperties) throws ProviderValidationException {
  158.         Properties p = mapProperties.get(Costanti.POLICY_ENDPOINT_SSL_CONFIG);
  159.         if(p==null || p.size()<=0) {
  160.             throw new ProviderValidationException("Nonostante sia stato indicato un endpoint 'https', non è stata fornita una configurazione dei parametri ssl da utilizzare");
  161.         }
  162.        
  163.         String trustAllCerts = p.getProperty(CostantiConnettori.CONNETTORE_HTTPS_TRUST_ALL_CERTS);
  164.         boolean trustAll = false;
  165.         if(trustAllCerts!=null && StringUtils.isNotEmpty(trustAllCerts)) {
  166.             trustAll = Boolean.valueOf(trustAllCerts);
  167.         }
  168.        
  169.         if(!trustAll) {
  170.             String location = p.getProperty(CostantiConnettori.CONNETTORE_HTTPS_TRUST_STORE_LOCATION);
  171.             InputValidationUtils.validateTextAreaInput(location, "Https - Autenticazione Server - File (TrustStore per l'autenticazione server)");
  172.            
  173.             String algo = p.getProperty(CostantiConnettori.CONNETTORE_HTTPS_TRUST_MANAGEMENT_ALGORITHM);
  174.             if(algo==null || "".equals(algo)) {
  175.                 throw new ProviderValidationException("Indicare un algoritmo per l'autenticazione server");
  176.             }
  177.             if(algo.contains(" ")) {
  178.                 throw new ProviderValidationException("Non indicare spazi nell'algoritmo per l'autenticazione server");
  179.             }
  180.            
  181.             String locationCRL = p.getProperty(CostantiConnettori.CONNETTORE_HTTPS_TRUST_STORE_CRLS);
  182.             if(locationCRL!=null && !"".equals(locationCRL)) {
  183.                 InputValidationUtils.validateTextAreaInput(locationCRL, "Https - Autenticazione Server - CRL File(s)");
  184.             }
  185.         }
  186.     }
  187.     private void validateBasicCredentials(Properties pDefault) throws ProviderValidationException {
  188.         String username = pDefault.getProperty(org.openspcoop2.pdd.core.token.attribute_authority.Costanti.AA_AUTH_BASIC_USERNAME);
  189.         if(username==null || "".equals(username)) {
  190.             throw new ProviderValidationException("Nonostante sia richiesta una autenticazione 'HttpBasic', non è stato fornito un 'Username' da utilizzare durante la connessione verso il servizio");
  191.         }
  192.         if(username.contains(" ")) {
  193.             throw new ProviderValidationException("Non indicare spazi nel 'Username'");
  194.         }
  195.         String password = pDefault.getProperty(org.openspcoop2.pdd.core.token.attribute_authority.Costanti.AA_AUTH_BASIC_PASSWORD);
  196.         if(password==null || "".equals(password)) {
  197.             throw new ProviderValidationException("Nonostante sia richiesta una autenticazione 'HttpBasic', non è stato fornita una 'Password' da utilizzare durante la connessione verso il servizio");
  198.         }
  199.         if(password.contains(" ")) {
  200.             throw new ProviderValidationException("Non indicare spazi nella 'Password'");
  201.         }
  202.     }
  203.     private void validateSslCredentials(Map<String, Properties> mapProperties) throws ProviderValidationException {
  204.         Properties p = mapProperties.get(Costanti.POLICY_ENDPOINT_SSL_CLIENT_CONFIG);
  205.         if(p==null || p.size()<=0) {
  206.             throw new ProviderValidationException("Nonostante sia richiesta una autenticazione 'Https', non sono stati forniti i parametri di connessione ssl client da utilizzare verso il servizio");
  207.         }
  208.        
  209.         String location = p.getProperty(CostantiConnettori.CONNETTORE_HTTPS_KEY_STORE_LOCATION);
  210.         if(location!=null && !"".equals(location)) {
  211.             InputValidationUtils.validateTextAreaInput(location, "Https - Autenticazione Client - File (KeyStore per l'autenticazione client)");
  212.         }
  213.        
  214.         String algo = p.getProperty(CostantiConnettori.CONNETTORE_HTTPS_KEY_MANAGEMENT_ALGORITHM);
  215.         if(algo==null || "".equals(algo)) {
  216.             throw new ProviderValidationException("Indicare un algoritmo per l'autenticazione client");
  217.         }
  218.         if(algo.contains(" ")) {
  219.             throw new ProviderValidationException("Non indicare spazi nell'algoritmo per l'autenticazione client");
  220.         }
  221.     }
  222.     private void validateProxy(Map<String, Properties> mapProperties) throws ProviderValidationException {
  223.         Properties p = mapProperties.get(Costanti.POLICY_ENDPOINT_CONFIG);
  224.         if(p==null || p.size()<=0) {
  225.             throw new ProviderValidationException("Nonostante sia richiesta un proxy, non sono stati forniti i parametri di connessione");
  226.         }
  227.        
  228.         String hostname = p.getProperty(CostantiConnettori.CONNETTORE_HTTP_PROXY_HOSTNAME);
  229.         if(hostname==null || "".equals(hostname)) {
  230.             throw new ProviderValidationException("Indicare un hostname per il Proxy");
  231.         }
  232.         if(hostname.contains(" ")) {
  233.             throw new ProviderValidationException("Non indicare spazi nell'hostname del Proxy");
  234.         }
  235.        
  236.         String username = p.getProperty(CostantiConnettori.CONNETTORE_HTTP_PROXY_USERNAME);
  237.         if(username!=null && !"".equals(username) &&
  238.             username.contains(" ")) {
  239.             throw new ProviderValidationException("Non indicare spazi nell'username del Proxy");
  240.         }
  241.        
  242.         String password = p.getProperty(CostantiConnettori.CONNETTORE_HTTP_PROXY_PASSWORD);
  243.         if(password!=null && !"".equals(password) &&
  244.             password.contains(" ")) {
  245.             throw new ProviderValidationException("Non indicare spazi nella password del Proxy");
  246.         }
  247.     }
  248.     private void validateClaims(Properties pDefault) throws ProviderValidationException {
  249.         String claims = pDefault.getProperty(org.openspcoop2.pdd.core.token.attribute_authority.Costanti.AA_REQUEST_JWT_CLAIMS);
  250.         if(claims!=null && !"".equals(claims)) {
  251.             Properties convertTextToProperties = PropertiesUtilities.convertTextToProperties(claims);
  252.             List<String> deny = new ArrayList<>();
  253.             deny.add(Claims.JSON_WEB_TOKEN_RFC_7519_ISSUER);
  254.             deny.add(Claims.JSON_WEB_TOKEN_RFC_7519_SUBJECT);
  255.             deny.add(Claims.JSON_WEB_TOKEN_RFC_7519_AUDIENCE);
  256.             deny.add(Claims.JSON_WEB_TOKEN_RFC_7519_ISSUED_AT);
  257.             deny.add(Claims.JSON_WEB_TOKEN_RFC_7519_NOT_TO_BE_USED_BEFORE);
  258.             deny.add(Claims.JSON_WEB_TOKEN_RFC_7519_EXPIRED);
  259.             deny.add(Claims.JSON_WEB_TOKEN_RFC_7519_JWT_ID);
  260.             TokenUtilities.checkClaims("claim", convertTextToProperties, "Claims", deny, false);
  261.         }
  262.     }
  263.     private void validateResponseTypeJWS(Map<String, Properties> mapProperties, Properties pDefault) throws ProviderValidationException {
  264.         Properties p = mapProperties.get(org.openspcoop2.pdd.core.token.attribute_authority.Costanti.POLICY_VALIDAZIONE_JWS_VERIFICA_PROP_REF_ID);
  265.         if(p==null || p.size()<=0) {
  266.             throw new ProviderValidationException("La validazione di una risposta JWS richiede una configurazione del TrustStore; configurazione non riscontrata");
  267.         }
  268.        
  269.         if(!p.containsKey(SecurityConstants.JOSE_KEYSTORE) && !p.containsKey(SecurityConstants.JOSE_KEYSTORE_JWKSET)) {
  270.             // altrimenti è stato fatto inject del keystore
  271.             String file = p.getProperty(SecurityConstants.JOSE_KEYSTORE_FILE);
  272.             InputValidationUtils.validateTextAreaInput(file, "Risposta - TrustStore - File");
  273.         }
  274.        
  275.         String crl = pDefault.getProperty(SecurityConstants.SIGNATURE_CRL);
  276.         if(crl!=null && StringUtils.isNotEmpty(crl)) {
  277.             InputValidationUtils.validateTextAreaInput(crl, "Risposta - TrustStore - CRL File(s)");
  278.         }
  279.     }
  280.     private void validateResponseTypeCustom(Properties pDefault) throws ProviderValidationException {
  281.         String pluginType = pDefault.getProperty(org.openspcoop2.pdd.core.token.attribute_authority.Costanti.AA_RESPONSE_PARSER_PLUGIN_TYPE);
  282.         if(pluginType!=null && StringUtils.isNotEmpty(pluginType) && CostantiConfigurazione.POLICY_ID_NON_DEFINITA.equals(pluginType)) {
  283.             String className = pDefault.getProperty(org.openspcoop2.pdd.core.token.attribute_authority.Costanti.AA_RESPONSE_PARSER_CLASS_NAME);
  284.             if(CostantiConfigurazione.POLICY_ID_NON_DEFINITA.equals(className)) {
  285.                 throw new ProviderValidationException("Deve essere selezionato un plugin per la risposta");
  286.             }
  287.             else {
  288.                 if(className==null || "".equals(className)) {
  289.                     throw new ProviderValidationException("Non è stata fornita la classe del parser dei claims della risposta");
  290.                 }
  291.                 if(className.contains(" ")) {
  292.                     throw new ProviderValidationException("Non indicare spazi nella classe del parser dei claims della risposta");
  293.                 }
  294.             }
  295.         }
  296.     }

  297.     @Override
  298.     public List<String> getValues(String id) throws ProviderException {
  299.         return this.getValues(id, null);
  300.     }
  301.     @Override
  302.     public List<String> getValues(String id, ExternalResources externalResources) throws ProviderException{
  303.         List<String> values = null;
  304.         if(org.openspcoop2.pdd.core.token.attribute_authority.Costanti.ID_AA_SIGNATURE_ALGORITHM.equals(id)) {
  305.             return getValuesSignatureAlgorithm();
  306.         }
  307.         else if(Costanti.ID_TIPOLOGIA_HTTPS.equals(id)) {
  308.             List<String> tipologie = null;
  309.             try{
  310.                 tipologie = SSLUtilities.getSSLSupportedProtocols();
  311.             }catch(Exception e){
  312.                 tipologie = SSLUtilities.getAllSslProtocol();
  313.             }
  314.             return tipologie;
  315.         }
  316.         else if(Costanti.ID_AA_JWS_TRUSTSTORE_TYPE.equals(id) ||
  317.                 Costanti.ID_AA_JWS_KEYSTORE_TYPE.equals(id) ||
  318.                 Costanti.ID_HTTPS_TRUSTSTORE_TYPE.equals(id) ||
  319.                 Costanti.ID_HTTPS_KEYSTORE_TYPE.equals(id)) {
  320.             return getStoreType(id,true);
  321.         }
  322.         else if(Costanti.ID_AA_JWS_TRUSTSTORE_TYPE_SELECT_CERTIFICATE.equals(id)) {
  323.             return Costanti.getIdValidazioneJwtTruststoreTypeSelectCertificateValues();
  324.         }
  325.         else if(Costanti.ID_AA_JWS_TRUSTSTORE_TYPE_SELECT_JWK_PUBLIC_KEY.equals(id)) {
  326.             return Costanti.getIdValidazioneJwtTruststoreTypeSelectJwkPublicKeyValues();
  327.         }
  328.         else if(Costanti.ID_AA_JWS_TRUSTSTORE_OCSP_POLICY.equals(id) ||
  329.                 Costanti.ID_HTTPS_TRUSTSTORE_OCSP_POLICY.equals(id)) {
  330.             return this.ocspProvider.getValues();
  331.         }
  332.         else if(Costanti.ID_AA_JWS_KEYSTORE_BYOK_POLICY.equals(id) ||
  333.                 Costanti.ID_HTTPS_KEYSTORE_BYOK_POLICY.equals(id)) {
  334.             return this.byokProvider.getValues();
  335.         }
  336.         else if(org.openspcoop2.pdd.core.token.attribute_authority.Costanti.ID_AA_PARSER_TOKEN_CUSTOM_PLUGIN_CHOICE.equals(id)) {
  337.             return TokenUtilities.getTokenPluginValues(externalResources, TipoPlugin.ATTRIBUTE_AUTHORITY);
  338.         }
  339.         return values;
  340.     }
  341.     private List<String> getValuesSignatureAlgorithm() {
  342.         List<String> l = new ArrayList<>();
  343.         org.apache.cxf.rs.security.jose.jwa.SignatureAlgorithm [] tmp = org.apache.cxf.rs.security.jose.jwa.SignatureAlgorithm.values();
  344.         for (int i = 0; i < tmp.length; i++) {
  345.             if(org.apache.cxf.rs.security.jose.jwa.SignatureAlgorithm.NONE.equals(tmp[i])) {
  346.                 continue;
  347.             }
  348.             if(!tmp[i].name().toLowerCase().startsWith("hs")) {
  349.                 l.add(tmp[i].name());
  350.             }
  351.         }
  352.         return l;
  353.     }

  354.     @Override
  355.     public List<String> getLabels(String id) throws ProviderException {
  356.         return this.getLabels(id, null);
  357.     }
  358.     @Override
  359.     public List<String> getLabels(String id, ExternalResources externalResources) throws ProviderException{
  360.         if(org.openspcoop2.pdd.core.token.attribute_authority.Costanti.ID_AA_SIGNATURE_ALGORITHM.equals(id)) {
  361.             return getLabelsSignatureAlgorithm(id);
  362.         }
  363.         else if(Costanti.ID_AA_JWS_TRUSTSTORE_TYPE.equals(id) ||
  364.                 Costanti.ID_AA_JWS_KEYSTORE_TYPE.equals(id) ||
  365.                 Costanti.ID_HTTPS_TRUSTSTORE_TYPE.equals(id) ||
  366.                 Costanti.ID_HTTPS_KEYSTORE_TYPE.equals(id)) {
  367.             return getStoreType(id,false);
  368.         }
  369.         else if(Costanti.ID_AA_JWS_TRUSTSTORE_TYPE_SELECT_CERTIFICATE.equals(id)) {
  370.             return Costanti.getIdValidazioneJwtTruststoreTypeSelectCertificateLabels();
  371.         }
  372.         else if(Costanti.ID_AA_JWS_TRUSTSTORE_TYPE_SELECT_JWK_PUBLIC_KEY.equals(id)) {
  373.             return Costanti.getIdValidazioneJwtTruststoreTypeSelectJwkPublicKeyLabels();
  374.         }
  375.         else if(Costanti.ID_AA_JWS_TRUSTSTORE_OCSP_POLICY.equals(id) ||
  376.                 Costanti.ID_HTTPS_TRUSTSTORE_OCSP_POLICY.equals(id)) {
  377.             return this.ocspProvider.getLabels();
  378.         }
  379.         else if(Costanti.ID_AA_JWS_KEYSTORE_BYOK_POLICY.equals(id) ||
  380.                 Costanti.ID_HTTPS_KEYSTORE_BYOK_POLICY.equals(id)) {
  381.             return this.byokProvider.getLabels();
  382.         }
  383.         else if(org.openspcoop2.pdd.core.token.attribute_authority.Costanti.ID_AA_PARSER_TOKEN_CUSTOM_PLUGIN_CHOICE.equals(id)) {
  384.             return TokenUtilities.getTokenPluginLabels(externalResources, TipoPlugin.ATTRIBUTE_AUTHORITY);
  385.         }
  386.         return this.getValues(id); // torno uguale ai valori negli altri casi
  387.     }
  388.     private List<String> getLabelsSignatureAlgorithm(String id) throws ProviderException{
  389.         List<String> l = this.getValues(id);
  390.         List<String> labels = new ArrayList<>();
  391.         for (String value : l) {
  392.             if(value.contains("_")) {
  393.                 String t = "" + value;
  394.                 while(t.contains("_")) {
  395.                     t = t.replace("_", "-");
  396.                 }
  397.                 labels.add(t);
  398.             }
  399.             else {
  400.                 labels.add(value);
  401.             }
  402.         }
  403.         return labels;
  404.     }

  405.     private static boolean secret = false;
  406.     public static boolean isSecret() {
  407.         return secret;
  408.     }
  409.     public static void setSecret(boolean secret) {
  410.         AttributeAuthorityProvider.secret = secret;
  411.     }

  412.     private List<String> getStoreType(String id,boolean value){
  413.         boolean trustStore = true;
  414.         List<String> l = new ArrayList<>();
  415.         l.add(value ? SecurityConstants.KEYSTORE_TYPE_JKS_VALUE : SecurityConstants.KEYSTORE_TYPE_JKS_LABEL);
  416.         l.add(value ? SecurityConstants.KEYSTORE_TYPE_PKCS12_VALUE : SecurityConstants.KEYSTORE_TYPE_PKCS12_LABEL);
  417.         if(Costanti.ID_AA_JWS_TRUSTSTORE_TYPE.equals(id) ||
  418.                 Costanti.ID_AA_JWS_KEYSTORE_TYPE.equals(id)) {
  419.             l.add(value ? SecurityConstants.KEYSTORE_TYPE_JWK_VALUE: SecurityConstants.KEYSTORE_TYPE_JWK_LABEL);
  420.         }
  421.         if(Costanti.ID_AA_JWS_KEYSTORE_TYPE.equals(id)) {
  422.             l.add(value ? SecurityConstants.KEYSTORE_TYPE_KEY_PAIR_VALUE: SecurityConstants.KEYSTORE_TYPE_KEY_PAIR_LABEL);
  423.         }
  424.         if(Costanti.ID_AA_JWS_TRUSTSTORE_TYPE.equals(id)) {
  425.             l.add(value ? SecurityConstants.KEYSTORE_TYPE_PUBLIC_KEY_VALUE: SecurityConstants.KEYSTORE_TYPE_PUBLIC_KEY_LABEL);
  426.         }
  427. /**     if(Costanti.ID_AA_JWS_TRUSTSTORE_TYPE.equals(id)) {
  428. //          l.add(value ? SecurityConstants.KEYSTORE_TYPE_JCEKS_VALUE : SecurityConstants.KEYSTORE_TYPE_JCEKS_LABEL);
  429. //          secret = true;
  430. //      } */
  431.        
  432.         if(Costanti.ID_AA_JWS_KEYSTORE_TYPE.equals(id) ||
  433.                 Costanti.ID_HTTPS_KEYSTORE_TYPE.equals(id)) {
  434.             trustStore = false;
  435.         }
  436.         HSMUtils.fillTipologieKeystore(trustStore, false, l);
  437.        
  438.         if(secret) {
  439.             // aggiunto info mancanti come secret
  440.             addStoreTypeSecret(l);
  441.         }
  442.         return l;
  443.     }
  444.     private void addStoreTypeSecret(List<String> l) {
  445.         List<String> lSecret = new ArrayList<>();
  446.         HSMUtils.fillTipologieKeystore(false, true, lSecret);
  447.         if(!lSecret.isEmpty()) {
  448.             for (String type : lSecret) {
  449.                 if(!l.contains(type)) {
  450.                     l.add(type);
  451.                 }
  452.             }
  453.         }
  454.     }
  455.    
  456.     @Override
  457.     public String getNote(String id, String actualValue) throws ProviderException{
  458.         if(Costanti.ID_AA_JWS_TRUSTSTORE_TYPE_SELECT_CERTIFICATE.equals(id)) {
  459.             if(Costanti.ID_VALIDAZIONE_JWT_TRUSTSTORE_TYPE_SELECT_CERTIFICATE_VALUE_ALIAS.equals(actualValue)) {
  460.                 return Costanti.ID_VALIDAZIONE_JWT_TRUSTSTORE_TYPE_SELECT_CERTIFICATE_NOTE_ALIAS;
  461.             }
  462.             else if(Costanti.ID_VALIDAZIONE_JWT_TRUSTSTORE_TYPE_SELECT_CERTIFICATE_VALUE_X5C.equals(actualValue)) {
  463.                 return Costanti.ID_VALIDAZIONE_JWT_TRUSTSTORE_TYPE_SELECT_CERTIFICATE_NOTE_X5C;
  464.             }
  465.             else if(Costanti.ID_VALIDAZIONE_JWT_TRUSTSTORE_TYPE_SELECT_CERTIFICATE_VALUE_X5T256.equals(actualValue)) {
  466.                 return Costanti.ID_VALIDAZIONE_JWT_TRUSTSTORE_TYPE_SELECT_CERTIFICATE_NOTE_X5T256;
  467.             }
  468.             else if(Costanti.ID_VALIDAZIONE_JWT_TRUSTSTORE_TYPE_SELECT_CERTIFICATE_VALUE_X5C_X5T256.equals(actualValue)) {
  469.                 return Costanti.ID_VALIDAZIONE_JWT_TRUSTSTORE_TYPE_SELECT_CERTIFICATE_NOTE_X5C_X5T256;
  470.             }
  471.             else if(Costanti.ID_VALIDAZIONE_JWT_TRUSTSTORE_TYPE_SELECT_CERTIFICATE_VALUE_KID.equals(actualValue)) {
  472.                 return Costanti.ID_VALIDAZIONE_JWT_TRUSTSTORE_TYPE_SELECT_CERTIFICATE_NOTE_KID;
  473.             }
  474.             else if(Costanti.ID_VALIDAZIONE_JWT_TRUSTSTORE_TYPE_SELECT_CERTIFICATE_VALUE_X5U.equals(actualValue)) {
  475.                 return Costanti.ID_VALIDAZIONE_JWT_TRUSTSTORE_TYPE_SELECT_CERTIFICATE_NOTE_X5U;
  476.             }
  477.         }
  478.         else if(Costanti.ID_AA_JWS_TRUSTSTORE_TYPE_SELECT_JWK_PUBLIC_KEY.equals(id)) {
  479.             if(Costanti.ID_VALIDAZIONE_JWT_TRUSTSTORE_TYPE_SELECT_JWK_PUBLIC_KEY_VALUE_ALIAS.equals(actualValue)) {
  480.                 return Costanti.ID_VALIDAZIONE_JWT_TRUSTSTORE_TYPE_SELECT_JWK_PUBLIC_KEY_NOTE_ALIAS;
  481.             }
  482.             else if(Costanti.ID_VALIDAZIONE_JWT_TRUSTSTORE_TYPE_SELECT_JWK_PUBLIC_KEY_VALUE_KID.equals(actualValue)) {
  483.                 return Costanti.ID_VALIDAZIONE_JWT_TRUSTSTORE_TYPE_SELECT_JWK_PUBLIC_KEY_NOTE_KID;
  484.             }
  485.         }
  486.         return null;
  487.     }
  488.    
  489.     @Override
  490.     public String getDefault(String id) throws ProviderException {
  491.         return getDefault(id, null);
  492.     }
  493.     @Override
  494.     public String getDefault(String id, ExternalResources externalResources) throws ProviderException {
  495.         if(org.openspcoop2.pdd.core.token.attribute_authority.Costanti.ID_AA_SIGNATURE_ALGORITHM.equals(id)) {
  496.             return org.apache.cxf.rs.security.jose.jwa.SignatureAlgorithm.RS256.name();
  497.         }
  498.         else if(org.openspcoop2.pdd.core.token.attribute_authority.Costanti.ID_AA_TOKEN_JWT_EXPIRED_TTL_SECONDS.equals(id)) {
  499.             return Costanti.POLICY_RETRIEVE_TOKEN_JWT_EXPIRED_TTL_SECONDS_DEFAULT_VALUE;
  500.         }
  501.         else if(Costanti.ID_TIPOLOGIA_HTTPS.equals(id)) {
  502.             return SSLUtilities.getSafeDefaultProtocol();
  503.         }
  504.         return null;
  505.     }
  506.    
  507.     @Override
  508.     public ProviderInfo getProviderInfo(String id) throws ProviderException{
  509.         if(org.openspcoop2.pdd.core.token.attribute_authority.Costanti.ID_AA_ENDPOINT_URL.equals(id) ||
  510.                 org.openspcoop2.pdd.core.token.attribute_authority.Costanti.ID_AA_AUTENTICAZIONE_ENDPOINT_BASIC_USERNAME.equals(id) ||
  511.                 org.openspcoop2.pdd.core.token.attribute_authority.Costanti.ID_AA_AUTENTICAZIONE_ENDPOINT_BASIC_PASSWORD.equals(id) ||
  512.                 org.openspcoop2.pdd.core.token.attribute_authority.Costanti.ID_AA_AUTENTICAZIONE_ENDPOINT_BEARER_TOKEN.equals(id) ||
  513.                 org.openspcoop2.pdd.core.token.attribute_authority.Costanti.ID_AA_RICHIESTA_JWS_PAYLOAD_ISSUER.equals(id) ||
  514.                 org.openspcoop2.pdd.core.token.attribute_authority.Costanti.ID_AA_RICHIESTA_JWS_PAYLOAD_SUBJECT.equals(id) ||
  515.                 org.openspcoop2.pdd.core.token.attribute_authority.Costanti.ID_AA_RICHIESTA_JWS_PAYLOAD_AUDIENCE.equals(id) ||
  516.                 org.openspcoop2.pdd.core.token.attribute_authority.Costanti.ID_AA_RISPOSTA_JWS_PAYLOAD_AUDIENCE.equals(id)
  517.                 ) {
  518.             ProviderInfo pInfo = new ProviderInfo();
  519.             pInfo.setHeaderBody(DynamicHelperCostanti.LABEL_CONFIGURAZIONE_INFO_TRASPORTO);
  520.             pInfo.setListBody(DynamicHelperCostanti.LABEL_CONFIGURAZIONE_ATTRIBUTE_AUTHORITY_INFO_VALORI);
  521.             return pInfo;
  522.         }
  523.         else if(org.openspcoop2.pdd.core.token.attribute_authority.Costanti.ID_AA_RICHIESTA_JWS_PAYLOAD_TEMPLATE.equals(id) ||
  524.                 org.openspcoop2.pdd.core.token.attribute_authority.Costanti.ID_AA_RICHIESTA_PAYLOAD_TEMPLATE.equals(id)
  525.                 ) {
  526.             ProviderInfo pInfo = new ProviderInfo();
  527.             pInfo.setHeaderBody(DynamicHelperCostanti.LABEL_CONFIGURAZIONE_INFO_TRASPORTO);
  528.             pInfo.setListBody(DynamicHelperCostanti.LABEL_CONFIGURAZIONE_ATTRIBUTE_AUTHORITY_INFO_VALORI_CON_REQUIRED_ATTRIBUTES);
  529.             return pInfo;
  530.         }
  531.         else if(org.openspcoop2.pdd.core.token.attribute_authority.Costanti.ID_AA_RICHIESTA_JWS_PAYLOAD_CLAIMS.equals(id)
  532.             ) {
  533.             ProviderInfo pInfo = new ProviderInfo();
  534.             pInfo.setHeaderBody(DynamicHelperCostanti.LABEL_CONFIGURAZIONE_CLAIMS);
  535.             pInfo.setListBody(DynamicHelperCostanti.LABEL_CONFIGURAZIONE_ATTRIBUTE_AUTHORITY_INFO_VALORI_CON_REQUIRED_ATTRIBUTES);
  536.             return pInfo;
  537.         }
  538.         else if(org.openspcoop2.pdd.core.token.attribute_authority.Costanti.ID_AA_RICHIESTA_JWS_PAYLOAD_TEMPLATE_FREEMARKER.equals(id) ||
  539.                 org.openspcoop2.pdd.core.token.attribute_authority.Costanti.ID_AA_RICHIESTA_PAYLOAD_TEMPLATE_FREEMARKER.equals(id)
  540.                 ) {
  541.             ProviderInfo pInfo = new ProviderInfo();
  542.             pInfo.setHeaderBody(DynamicHelperCostanti.LABEL_CONFIGURAZIONE_INFO_OBJECT_TEMPLATE_FREEMARKER);
  543.             pInfo.setListBody(DynamicHelperCostanti.LABEL_CONFIGURAZIONE_ATTRIBUTE_AUTHORITY_INFO_OBJECT_VALORI);
  544.             return pInfo;
  545.         }
  546.         else if(org.openspcoop2.pdd.core.token.attribute_authority.Costanti.ID_AA_RICHIESTA_JWS_PAYLOAD_TEMPLATE_VELOCITY.equals(id) ||
  547.                 org.openspcoop2.pdd.core.token.attribute_authority.Costanti.ID_AA_RICHIESTA_PAYLOAD_TEMPLATE_VELOCITY.equals(id)
  548.                 ) {
  549.             ProviderInfo pInfo = new ProviderInfo();
  550.             pInfo.setHeaderBody(DynamicHelperCostanti.LABEL_CONFIGURAZIONE_INFO_OBJECT_TEMPLATE_VELOCITY);
  551.             pInfo.setListBody(DynamicHelperCostanti.LABEL_CONFIGURAZIONE_ATTRIBUTE_AUTHORITY_INFO_OBJECT_VALORI);
  552.             return pInfo;
  553.         }
  554.         else if(org.openspcoop2.pdd.core.token.attribute_authority.Costanti.ID_AA_PARSER_TOKEN_CUSTOM_PLUGIN_CLASSNAME.equals(id)) {
  555.             ProviderInfo pInfo = new ProviderInfo();
  556.             pInfo.setHeaderBody(DynamicHelperCostanti.PLUGIN_CLASSNAME_INFO_SINGOLA);
  557.             pInfo.setListBody(new ArrayList<>());
  558.             pInfo.getListBody().add(IRetrieveAttributeAuthorityResponseParser.class.getName());
  559.             return pInfo;
  560.         }
  561.        
  562.        
  563.         return null;
  564.     }
  565.    
  566.     @Override
  567.     public String dynamicUpdate(List<?> items, Map<String, String> mapNameValue, Item item, String actualValue) {
  568.         return dynamicUpdate(items, mapNameValue, item, actualValue, null);
  569.     }
  570.     @Override
  571.     public String dynamicUpdate(List<?> items, Map<String, String> mapNameValue, Item item, String actualValue, ExternalResources externalResources) {
  572.    
  573.         if(Costanti.ID_AA_JWS_TRUSTSTORE_FILE.equals(item.getName()) ||
  574.                 Costanti.ID_AA_JWS_KEYSTORE_FILE.equals(item.getName()) ||
  575.                 Costanti.ID_HTTPS_TRUSTSTORE_FILE.equals(item.getName()) ||
  576.                 Costanti.ID_HTTPS_KEYSTORE_FILE.equals(item.getName())) {
  577.             return dynamicUpdateStoreFile(items, mapNameValue, item, actualValue);
  578.         }
  579.         else if(Costanti.ID_AA_JWS_TRUSTSTORE_PASSWORD.equals(item.getName()) ||
  580.                 Costanti.ID_AA_JWS_KEYSTORE_PASSWORD.equals(item.getName()) ||
  581.                 Costanti.ID_HTTPS_TRUSTSTORE_PASSWORD.equals(item.getName()) ||
  582.                 Costanti.ID_HTTPS_KEYSTORE_PASSWORD.equals(item.getName())) {
  583.             return dynamicUpdateStorePassword(items, mapNameValue, item, actualValue);  
  584.         }
  585.         else if(Costanti.ID_AA_JWS_KEYSTORE_PASSWORD_PRIVATE_KEY.equals(item.getName()) ||
  586.                 Costanti.ID_HTTPS_KEYSTORE_PASSWORD_PRIVATE_KEY.equals(item.getName()) ) {
  587.             return dynamicUpdateStoreKeyPassword(items, mapNameValue, item, actualValue);
  588.         }
  589.         else if(Costanti.ID_AA_JWS_TRUSTSTORE_OCSP_POLICY.equals(item.getName()) ||
  590.                 Costanti.ID_HTTPS_TRUSTSTORE_OCSP_POLICY.equals(item.getName())) {
  591.             if(!this.ocspProvider.isOcspEnabled()) {
  592.                 item.setValue("");
  593.                 item.setType(ItemType.HIDDEN);
  594.             }
  595.         }
  596.         else if(Costanti.ID_AA_JWS_KEYSTORE_BYOK_POLICY.equals(item.getName()) ||
  597.                 Costanti.ID_HTTPS_KEYSTORE_BYOK_POLICY.equals(item.getName())) {
  598.             return dynamicUpdateByok(items, mapNameValue, item, actualValue);
  599.         }
  600.         else if(org.openspcoop2.pdd.core.token.attribute_authority.Costanti.ID_AA_PARSER_TOKEN_CUSTOM_PLUGIN_CHOICE.equals(item.getName())) {
  601.             return TokenUtilities.dynamicUpdateTokenPluginChoice(externalResources, TipoPlugin.ATTRIBUTE_AUTHORITY, item, actualValue);
  602.         }
  603.         else if(org.openspcoop2.pdd.core.token.attribute_authority.Costanti.ID_AA_PARSER_TOKEN_CUSTOM_PLUGIN_CLASSNAME.equals(item.getName())) {
  604.             return TokenUtilities.dynamicUpdateTokenPluginClassName(externalResources, TipoPlugin.ATTRIBUTE_AUTHORITY,
  605.                     items, mapNameValue, item,
  606.                     org.openspcoop2.pdd.core.token.attribute_authority.Costanti.ID_AA_PARSER_TOKEN_CUSTOM_PLUGIN_CHOICE, actualValue);          
  607.         }
  608.        
  609.         return actualValue;
  610.     }
  611.     public String dynamicUpdateStoreFile(List<?> items, Map<String, String> mapNameValue, Item item, String actualValue) {
  612.         String type = Costanti.ID_AA_JWS_TRUSTSTORE_TYPE;
  613.         if(Costanti.ID_AA_JWS_KEYSTORE_FILE.equals(item.getName())) {
  614.             type = Costanti.ID_AA_JWS_KEYSTORE_TYPE;
  615.         }
  616.         else if(Costanti.ID_HTTPS_TRUSTSTORE_FILE.equals(item.getName())) {
  617.             type = Costanti.ID_HTTPS_TRUSTSTORE_TYPE;
  618.         }
  619.         else if(Costanti.ID_HTTPS_KEYSTORE_FILE.equals(item.getName())) {
  620.             type = Costanti.ID_HTTPS_KEYSTORE_TYPE;
  621.         }
  622.        
  623.         return AbstractSecurityProvider.processStoreFile(type, items, mapNameValue, item, actualValue);
  624.     }
  625.     public String dynamicUpdateStorePassword(List<?> items, Map<String, String> mapNameValue, Item item, String actualValue) {
  626.         String type = Costanti.ID_AA_JWS_TRUSTSTORE_TYPE;
  627.         boolean keystore = true;
  628.         if(Costanti.ID_AA_JWS_KEYSTORE_PASSWORD.equals(item.getName())) {
  629.             type = Costanti.ID_AA_JWS_KEYSTORE_TYPE;
  630.         }
  631.         else if(Costanti.ID_HTTPS_TRUSTSTORE_PASSWORD.equals(item.getName())) {
  632.             type = Costanti.ID_HTTPS_TRUSTSTORE_TYPE;
  633.             keystore = false;
  634.         }
  635.         else if(Costanti.ID_HTTPS_KEYSTORE_PASSWORD.equals(item.getName())) {
  636.             type = Costanti.ID_HTTPS_KEYSTORE_TYPE;
  637.         }
  638.        
  639.         return AbstractSecurityProvider.processStorePassword(keystore, type, items, mapNameValue, item, actualValue);
  640.     }
  641.     private String dynamicUpdateStoreKeyPassword(List<?> items, Map<String, String> mapNameValue, Item item, String actualValue) {
  642.         String type = Costanti.ID_AA_JWS_KEYSTORE_TYPE;
  643.         if(Costanti.ID_HTTPS_KEYSTORE_PASSWORD_PRIVATE_KEY.equals(item.getName())) {
  644.             type = Costanti.ID_HTTPS_KEYSTORE_TYPE;
  645.         }
  646.         return AbstractSecurityProvider.processStoreKeyPassword(type, items, mapNameValue, item, actualValue);
  647.     }
  648.     private String dynamicUpdateByok(List<?> items, Map<String, String> mapNameValue, Item item, String actualValue) {      
  649.         if(!this.byokProvider.isByokEnabled()) {
  650.             item.setValue("");
  651.             item.setType(ItemType.HIDDEN);
  652.             return actualValue;
  653.         }
  654.         else {
  655.             return dynamicUpdateByokPolicy(items, mapNameValue, item, actualValue);
  656.         }
  657.     }
  658.     private String dynamicUpdateByokPolicy(List<?> items, Map<String, String> mapNameValue, Item item, String actualValue) {
  659.         String type = null;
  660.         if(Costanti.ID_AA_JWS_KEYSTORE_BYOK_POLICY.equals(item.getName())) {
  661.             type = Costanti.ID_AA_JWS_KEYSTORE_TYPE;
  662.         }
  663.         else if(Costanti.ID_HTTPS_KEYSTORE_BYOK_POLICY.equals(item.getName())) {
  664.             type = Costanti.ID_HTTPS_KEYSTORE_TYPE;
  665.         }
  666.        
  667.         return AbstractSecurityProvider.processStoreByokPolicy(type, items, mapNameValue, item, actualValue);
  668.     }

  669. }