AbstractSecurityProvider.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */


  20. package org.openspcoop2.security.message.utils;

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

  24. import org.openspcoop2.core.commons.DBUtils;
  25. import org.openspcoop2.core.mvc.properties.Item;
  26. import org.openspcoop2.core.mvc.properties.constants.ItemType;
  27. import org.openspcoop2.core.mvc.properties.provider.IProvider;
  28. import org.openspcoop2.core.mvc.properties.provider.ProviderException;
  29. import org.openspcoop2.core.mvc.properties.provider.ProviderValidationException;
  30. import org.openspcoop2.security.message.constants.SecurityConstants;
  31. import org.openspcoop2.utils.UtilsRuntimeException;
  32. import org.openspcoop2.utils.certificate.KeystoreType;
  33. import org.openspcoop2.utils.certificate.byok.BYOKProvider;
  34. import org.openspcoop2.utils.certificate.hsm.HSMUtils;
  35. import org.openspcoop2.utils.certificate.ocsp.OCSPProvider;

  36. /**    
  37.  * AbstractSecurityProvider
  38.  *
  39.  * @author Poli Andrea (poli@link.it)
  40.  * @author $Author$
  41.  * @version $Rev$, $Date$
  42.  */
  43. public abstract class AbstractSecurityProvider implements IProvider {

  44.     private boolean asTruststore = false; // indicazione se questo provider serve per un keystore o truststore
  45.    
  46.     public boolean isAsTruststore() {
  47.         return this.asTruststore;
  48.     }
  49.     public void useAsKeystore() {
  50.         this.asTruststore = false;
  51.     }
  52.     public void useAsTruststore() {
  53.         this.asTruststore = true;
  54.     }
  55.    
  56.     private OCSPProvider ocspProvider;
  57.     private BYOKProvider byokProvider;

  58.     protected AbstractSecurityProvider() {
  59.         this.ocspProvider = new OCSPProvider();
  60.         try {
  61.             this.byokProvider = BYOKProvider.getUnwrapInstance();
  62.         }catch(Exception e) {
  63.             throw new UtilsRuntimeException(e.getMessage(),e);
  64.         }
  65.     }
  66.    
  67.    
  68.     @Override
  69.     public void validate(Map<String, Properties> mapProperties) throws ProviderException, ProviderValidationException {

  70.     }

  71.     @Override
  72.     public List<String> getValues(String id) throws ProviderException {
  73.         List<String> l = null;
  74.         if(SecurityConstants.KEYSTORE_TYPE.equals(id)) {
  75.             l = SecurityConstants.getTipologieKeystoreValues(this.asTruststore);
  76.         }
  77.         else if(SecurityConstants.SECRETKEYSTORE_TYPE.equals(id)) {
  78.             l = SecurityConstants.getTipologieSecretKeystoreValues();
  79.         }
  80.         else if(SecurityConstants.TRUSTSTORE_TYPE.equals(id)) {
  81.             l = SecurityConstants.getTipologieKeystoreValues(true);
  82.         }
  83.         else if(SecurityConstants.TRUSTSTORE_OCSP_POLICY.equals(id) ||
  84.                 SecurityConstants.KEYSTORE_OCSP_POLICY.equals(id)) {
  85.             l = this.ocspProvider.getValues();
  86.         }
  87.         else if(SecurityConstants.KEYSTORE_BYOK_POLICY.equals(id) ||
  88.                 SecurityConstants.SECRETKEYSTORE_BYOK_POLICY.equals(id)) {
  89.             l = this.byokProvider.getValues();
  90.         }
  91.         return l;
  92.     }

  93.     @Override
  94.     public List<String> getLabels(String id) throws ProviderException {
  95.         List<String> l = null;
  96.         if(SecurityConstants.KEYSTORE_TYPE.equals(id)) {
  97.             l = SecurityConstants.getTipologieKeystoreLabels(this.asTruststore);
  98.         }
  99.         else if(SecurityConstants.SECRETKEYSTORE_TYPE.equals(id)) {
  100.             l = SecurityConstants.getTipologieSecretKeystoreLabels();
  101.         }
  102.         else if(SecurityConstants.TRUSTSTORE_TYPE.equals(id)) {
  103.             l = SecurityConstants.getTipologieKeystoreLabels(true);
  104.         }
  105.         else if(SecurityConstants.TRUSTSTORE_OCSP_POLICY.equals(id) ||
  106.                 SecurityConstants.KEYSTORE_OCSP_POLICY.equals(id)) {
  107.             l = this.ocspProvider.getLabels();
  108.         }
  109.         else if(SecurityConstants.KEYSTORE_BYOK_POLICY.equals(id) ||
  110.                 SecurityConstants.SECRETKEYSTORE_BYOK_POLICY.equals(id)) {
  111.             l = this.byokProvider.getLabels();
  112.         }
  113.         else {
  114.             l = this.getValues(id);
  115.         }
  116.         return l;
  117.     }
  118.    
  119.     @Override
  120.     public String getDefault(String id) throws ProviderException {

  121.         return null;
  122.     }

  123.     @Override
  124.     public String dynamicUpdate(List<?> items, Map<String, String> mapNameValue, Item item, String actualValue) {
  125.         if(SecurityConstants.KEYSTORE_FILE.equals(item.getName()) ||
  126.                 SecurityConstants.SECRETKEYSTORE_FILE.equals(item.getName()) ||
  127.                 SecurityConstants.TRUSTSTORE_FILE.equals(item.getName())) {
  128.             return dynamicUpdateStoreFile(items, mapNameValue, item, actualValue);
  129.         }
  130.         else if(SecurityConstants.KEYSTORE_PASSWORD.equals(item.getName()) ||
  131.                 SecurityConstants.SECRETKEYSTORE_PASSWORD.equals(item.getName()) ||
  132.                 SecurityConstants.TRUSTSTORE_PASSWORD.equals(item.getName())) {
  133.             return dynamicUpdateStorePassword(items, mapNameValue, item, actualValue);          
  134.         }
  135.         else if(SecurityConstants.KEYSTORE_PRIVATE_KEY_PASSWORD.equals(item.getName()) ||
  136.                 SecurityConstants.SECRETKEYSTORE_PRIVATE_KEY_PASSWORD.equals(item.getName())) {
  137.             return dynamicUpdateStoreKeyPassword(items, mapNameValue, item, actualValue);
  138.         }
  139.         else if(SecurityConstants.TRUSTSTORE_OCSP_POLICY.equals(item.getName()) ||
  140.                 SecurityConstants.KEYSTORE_OCSP_POLICY.equals(item.getName())) {
  141.             if(!this.ocspProvider.isOcspEnabled()) {
  142.                 item.setValue("");
  143.                 item.setType(ItemType.HIDDEN);
  144.             }
  145.         }
  146.         else if(SecurityConstants.KEYSTORE_BYOK_POLICY.equals(item.getName()) ||
  147.                 SecurityConstants.SECRETKEYSTORE_BYOK_POLICY.equals(item.getName())) {
  148.             return dynamicUpdateByok(items, mapNameValue, item, actualValue);
  149.         }
  150.        
  151.         return actualValue;
  152.     }
  153.     private String dynamicUpdateStoreFile(List<?> items, Map<String, String> mapNameValue, Item item, String actualValue) {
  154.         String type = SecurityConstants.KEYSTORE_TYPE;
  155.         if(SecurityConstants.SECRETKEYSTORE_FILE.equals(item.getName())) {
  156.             type = SecurityConstants.SECRETKEYSTORE_TYPE;
  157.         }
  158.         else if(SecurityConstants.TRUSTSTORE_FILE.equals(item.getName())) {
  159.             type = SecurityConstants.TRUSTSTORE_TYPE;
  160.         }
  161.        
  162.         return processStoreFile(type, items, mapNameValue, item, actualValue);
  163.     }
  164.     private String dynamicUpdateStorePassword(List<?> items, Map<String, String> mapNameValue, Item item, String actualValue) {
  165.         String type = SecurityConstants.KEYSTORE_TYPE;
  166.         boolean keystore = true;
  167.         if(SecurityConstants.SECRETKEYSTORE_PASSWORD.equals(item.getName())) {
  168.             type = SecurityConstants.SECRETKEYSTORE_TYPE;
  169.         }
  170.         else if(SecurityConstants.TRUSTSTORE_PASSWORD.equals(item.getName())) {
  171.             type = SecurityConstants.TRUSTSTORE_TYPE;
  172.             keystore = false;
  173.         }
  174.        
  175.         return processStorePassword(keystore, type, items, mapNameValue, item, actualValue);
  176.     }
  177.     private String dynamicUpdateStoreKeyPassword(List<?> items, Map<String, String> mapNameValue, Item item, String actualValue) {
  178.         if(!HSMUtils.isHsmConfigurableKeyPassword()) {
  179.            
  180.             String type = SecurityConstants.KEYSTORE_TYPE;
  181.             if(SecurityConstants.SECRETKEYSTORE_PRIVATE_KEY_PASSWORD.equals(item.getName())) {
  182.                 type = SecurityConstants.SECRETKEYSTORE_TYPE;
  183.             }
  184.            
  185.             return processStoreKeyPassword(type, items, mapNameValue, item, actualValue);
  186.         }
  187.         return actualValue;
  188.     }
  189.     private String dynamicUpdateByok(List<?> items, Map<String, String> mapNameValue, Item item, String actualValue) {      
  190.         if(!this.byokProvider.isByokEnabled()) {
  191.             item.setValue("");
  192.             item.setType(ItemType.HIDDEN);
  193.             return actualValue;
  194.         }
  195.         else {
  196.             return dynamicUpdateByokPolicy(items, mapNameValue, item, actualValue);
  197.         }
  198.     }
  199.     private String dynamicUpdateByokPolicy(List<?> items, Map<String, String> mapNameValue, Item item, String actualValue) {
  200.         String type = null;
  201.         if(SecurityConstants.SECRETKEYSTORE_BYOK_POLICY.equals(item.getName())) {
  202.             type = SecurityConstants.SECRETKEYSTORE_TYPE;
  203.         }
  204.         else if(SecurityConstants.KEYSTORE_BYOK_POLICY.equals(item.getName())) {
  205.             type = SecurityConstants.KEYSTORE_TYPE;
  206.         }
  207.        
  208.         return AbstractSecurityProvider.processStoreByokPolicy(type, items, mapNameValue, item, actualValue);
  209.     }
  210.    
  211.     public static String readValue(String identificativo, List<?> items, Map<String, String> mapNameValue) {
  212.         String value = null;
  213.         if(items!=null && !items.isEmpty()) {
  214.             for (Object itemCheck : items) {
  215.                 /**System.out.println("CHECK ["+itemCheck.getClass().getName()+"]");*/
  216.                 if(itemCheck instanceof Item) {
  217.                     Item listItem = (Item) itemCheck;
  218.                    
  219.                     if(identificativo.equals(listItem.getName())) {
  220.                         value = mapNameValue.get(identificativo);
  221.                         break;
  222.                     }
  223.                 }
  224.             }
  225.         }
  226.         return value;
  227.     }
  228.    
  229.     public static String processStoreFile(String type, List<?> items, Map<String, String> mapNameValue, Item item, String actualValue) {
  230.         if(items!=null && !items.isEmpty()) {
  231.             for (Object itemCheck : items) {
  232.                 /**System.out.println("CHECK ["+itemCheck.getClass().getName()+"]");*/
  233.                 if(itemCheck instanceof Item) {
  234.                     Item listItem = (Item) itemCheck;
  235.                    
  236.                     boolean find = false;
  237.                     String value = null;
  238.                     if(type.equals(listItem.getName())) {
  239.                         find = true;
  240.                         value = mapNameValue.get(type);
  241.                     }
  242.                                            
  243.                     if(find) {
  244.                         /**System.out.println("TROVATO TYPE ["+mapNameValue.get(SecurityConstants.KEYSTORE_TYPE)+"]");*/
  245.                         return processStoreFile(value, item, actualValue);
  246.                     }
  247.                 }
  248.             }
  249.         }
  250.         return actualValue;
  251.     }
  252.     private static String processStoreFile(String value, Item item, String actualValue) {
  253.         if(value!=null && HSMUtils.isKeystoreHSM(value)) {
  254.             /**System.out.println("SET HIDDEN ["+HSMUtils.KEYSTORE_HSM_PREFIX+value+"]");*/
  255.             item.setValue(HSMUtils.KEYSTORE_HSM_PREFIX+value);
  256.             item.setType(ItemType.HIDDEN);
  257.             return item.getValue();
  258.         }
  259.         else {
  260.             item.setValue(actualValue);
  261.             item.setType(ItemType.TEXTAREA);
  262.             return item.getValue();
  263.         }
  264.     }
  265.    
  266.     public static String processStorePassword(boolean keystore, String type, List<?> items, Map<String, String> mapNameValue, Item item, String actualValue) {
  267.         if(items!=null && !items.isEmpty()) {
  268.             for (Object itemCheck : items) {
  269.                 /**System.out.println("CHECK ["+itemCheck.getClass().getName()+"]");*/
  270.                 if(itemCheck instanceof Item) {
  271.                     Item listItem = (Item) itemCheck;
  272.                     boolean find = false;
  273.                     String value = null;
  274.                     if(type.equals(listItem.getName())) {
  275.                         find = true;
  276.                         value = mapNameValue.get(type);
  277.                     }
  278.                                            
  279.                     if(find) {
  280.                         /**System.out.println("TROVATO TYPE ["+mapNameValue.get(SecurityConstants.KEYSTORE_TYPE)+"]");*/
  281.                         processStorePasswordRequired(keystore, value, item);
  282.                         return processStorePassword(value, item, actualValue);
  283.                     }
  284.                 }
  285.             }
  286.         }
  287.         return actualValue;
  288.     }
  289.     private static String processStorePassword(String value, Item item, String actualValue) {
  290.         if(value!=null && HSMUtils.isKeystoreHSM(value)) {
  291.             /**System.out.println("SET HIDDEN ["+HSMUtils.KEYSTORE_HSM_PREFIX+value+"]");*/
  292.             item.setValue(HSMUtils.KEYSTORE_HSM_STORE_PASSWORD_UNDEFINED);
  293.             item.setType(ItemType.HIDDEN);
  294.             return item.getValue();
  295.         }
  296.         else {
  297.             item.setValue(actualValue);
  298.             item.setType(ItemType.LOCK);
  299.             return item.getValue();
  300.         }
  301.     }
  302.     private static void processStorePasswordRequired(boolean keystore, String value, Item item) {
  303.         if(KeystoreType.JKS.isType(value)) {
  304.             item.setRequired(keystore ? DBUtils.isKeystoreJksPasswordRequired() : DBUtils.isTruststoreJksPasswordRequired());
  305.         }
  306.         else if(KeystoreType.PKCS12.isType(value)) {
  307.             item.setRequired(keystore ? DBUtils.isKeystorePkcs12PasswordRequired() : DBUtils.isTruststorePkcs12PasswordRequired());
  308.         }
  309.     }
  310.    
  311.     public static String processStoreKeyPassword(String type, List<?> items, Map<String, String> mapNameValue, Item item, String actualValue) {
  312.         if(items!=null && !items.isEmpty()) {
  313.             for (Object itemCheck : items) {
  314.                 /**System.out.println("CHECK ["+itemCheck.getClass().getName()+"]");*/
  315.                 if(itemCheck instanceof Item) {
  316.                     Item listItem = (Item) itemCheck;
  317.                     boolean find = false;
  318.                     String value = null;
  319.                     if(type.equals(listItem.getName())) {
  320.                         find = true;
  321.                         value = mapNameValue.get(type);
  322.                     }
  323.                                            
  324.                     if(find) {
  325.                         /**System.out.println("TROVATO TYPE ["+mapNameValue.get(SecurityConstants.KEYSTORE_TYPE)+"]");*/
  326.                         processStoreKeyPasswordRequired(value, item);
  327.                         return processStoreKeyPassword(value, item, actualValue);
  328.                     }
  329.                 }
  330.             }
  331.         }
  332.         return actualValue;
  333.     }
  334.     private static String processStoreKeyPassword(String value, Item item, String actualValue) {
  335.         if(value!=null && HSMUtils.isKeystoreHSM(value)) {
  336.             /**System.out.println("SET HIDDEN ["+HSMUtils.KEYSTORE_HSM_PREFIX+value+"]");*/
  337.             item.setValue(HSMUtils.KEYSTORE_HSM_PRIVATE_KEY_PASSWORD_UNDEFINED);
  338.             item.setType(ItemType.HIDDEN);
  339.             return item.getValue();
  340.         }
  341.         else {
  342.             item.setValue(actualValue);
  343.             item.setType(ItemType.LOCK);
  344.             return item.getValue();
  345.         }
  346.     }
  347.     private static void processStoreKeyPasswordRequired(String value, Item item) {
  348.         if(KeystoreType.JKS.isType(value)) {
  349.             item.setRequired(DBUtils.isKeystoreJksKeyPasswordRequired());
  350.         }
  351.         else if(KeystoreType.PKCS12.isType(value)) {
  352.             item.setRequired(DBUtils.isKeystorePkcs12PasswordRequired());
  353.         }
  354.     }
  355.    
  356.     public static String processStoreByokPolicy(String type, List<?> items, Map<String, String> mapNameValue, Item item, String actualValue) {
  357.         if(items!=null && !items.isEmpty()) {
  358.             for (Object itemCheck : items) {
  359.                 /**System.out.println("CHECK ["+itemCheck.getClass().getName()+"]");*/
  360.                 if(itemCheck instanceof Item) {
  361.                     Item listItem = (Item) itemCheck;
  362.                    
  363.                     boolean find = false;
  364.                     String value = null;
  365.                     if(type.equals(listItem.getName())) {
  366.                         find = true;
  367.                         value = mapNameValue.get(type);
  368.                     }
  369.                                            
  370.                     if(find) {
  371.                         /**System.out.println("TROVATO TYPE ["+mapNameValue.get(SecurityConstants.KEYSTORE_TYPE)+"]");*/
  372.                         return processStoreByokPolicy(value, item, actualValue);
  373.                     }
  374.                 }
  375.             }
  376.         }
  377.         return actualValue;
  378.     }
  379.     private static String processStoreByokPolicy(String value, Item item, String actualValue) {
  380.         if(value!=null && HSMUtils.isKeystoreHSM(value)) {
  381.             /**System.out.println("SET HIDDEN ["+HSMUtils.KEYSTORE_HSM_PREFIX+value+"]");*/
  382.             item.setValue(BYOKProvider.BYOK_POLICY_UNDEFINED);
  383.             item.setType(ItemType.HIDDEN);
  384.             return item.getValue();
  385.         }
  386.         else {
  387.             item.setValue(actualValue);
  388.             item.setType(ItemType.SELECT);
  389.             return item.getValue();
  390.         }
  391.     }
  392. }