HSMUtils.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.utils.certificate.hsm;

  21. import java.util.List;

  22. import org.openspcoop2.utils.UtilsException;
  23. import org.openspcoop2.utils.certificate.KeyStore;

  24. /**
  25.  * HSMUtils
  26.  *
  27.  * @author Poli Andrea (apoli@link.it)
  28.  * @author $Author$
  29.  * @version $Rev$, $Date$
  30.  */
  31. public class HSMUtils {
  32.    
  33.     private HSMUtils() {}

  34.     public static final String KEYSTORE_HSM_PREFIX = "HSM-";
  35.     public static final String KEYSTORE_HSM_STORE_PASSWORD_UNDEFINED = "-";
  36.     public static final String KEYSTORE_HSM_PRIVATE_KEY_PASSWORD_UNDEFINED = "-";
  37.    
  38.     private static boolean hsmConfigurableKeyPassword = false;
  39.     public static boolean isHsmConfigurableKeyPassword() {
  40.         return hsmConfigurableKeyPassword;
  41.     }
  42.     public static void setHsmConfigurableKeyPassword(boolean hsmConfigurableKeyPassword) {
  43.         HSMUtils.hsmConfigurableKeyPassword = hsmConfigurableKeyPassword;
  44.     }
  45.    
  46.     public static void fillTipologieKeystore(boolean trustStore, boolean secretKeyStore, List<String> l){
  47.         HSMManager hsmManager = HSMManager.getInstance();
  48.         if(hsmManager!=null) {
  49.             List<String> typeHsm = hsmManager.getKeystoreTypes();
  50.             if(typeHsm!=null && !typeHsm.isEmpty()) {
  51.                 if(secretKeyStore) {
  52.                     initSecretKeyStore(typeHsm, hsmManager, l);
  53.                 }
  54.                 else if(trustStore) {
  55.                     initTrustStore(typeHsm, hsmManager, l);
  56.                 }
  57.                 else {
  58.                     l.addAll(typeHsm);
  59.                 }
  60.             }
  61.         }
  62.     }
  63.     private static void initSecretKeyStore(List<String> typeHsm, HSMManager hsmManager, List<String> l) {
  64.         for (String type : typeHsm) {
  65.             try {
  66.                 if(hsmManager.isUsableAsSecretKeyStore(type)) {
  67.                     l.add(type);
  68.                 }
  69.             }catch(Exception e) { // ignore
  70.             }
  71.         }
  72.     }
  73.     private static void initTrustStore(List<String> typeHsm, HSMManager hsmManager, List<String> l) {
  74.         for (String type : typeHsm) {
  75.             try {
  76.                 if(hsmManager.isUsableAsTrustStore(type)) {
  77.                     l.add(type);
  78.                 }
  79.             }catch(Exception e) { // ignore
  80.             }
  81.         }
  82.     }
  83.     public static boolean existsTipologieKeystoreHSM(boolean trustStore, boolean secretKeyStore){
  84.         HSMManager hsmManager = HSMManager.getInstance();
  85.         if(hsmManager!=null) {
  86.             List<String> typeHsm = hsmManager.getKeystoreTypes();
  87.             if(existsTipologieKeystoreHSM(typeHsm, hsmManager,
  88.                     trustStore, secretKeyStore)) {
  89.                 return true;
  90.             }
  91.         }
  92.         return false;
  93.     }
  94.     private static boolean existsTipologieKeystoreHSM(List<String> typeHsm, HSMManager hsmManager,
  95.             boolean trustStore, boolean secretKeyStore) {
  96.         if(typeHsm!=null && !typeHsm.isEmpty()) {
  97.             if(secretKeyStore) {
  98.                 if(existsSecretKeyStore(typeHsm, hsmManager)) {
  99.                     return true;
  100.                 }
  101.             }
  102.             else if(trustStore) {
  103.                 if(existsTrustStore(typeHsm, hsmManager)) {
  104.                     return true;
  105.                 }
  106.             }
  107.             else {
  108.                 return true;
  109.             }
  110.         }
  111.         return false;
  112.     }
  113.     private static boolean existsSecretKeyStore(List<String> typeHsm, HSMManager hsmManager) {
  114.         for (String type : typeHsm) {
  115.             try {
  116.                 if(hsmManager.isUsableAsSecretKeyStore(type)) {
  117.                     return true;
  118.                 }
  119.             }catch(Exception e) { // ignore
  120.             }
  121.         }
  122.         return false;
  123.     }
  124.     private static boolean existsTrustStore(List<String> typeHsm, HSMManager hsmManager) {
  125.         for (String type : typeHsm) {
  126.             try {
  127.                 if(hsmManager.isUsableAsTrustStore(type)) {
  128.                     return true;
  129.                 }
  130.             }catch(Exception e) { // ignore
  131.             }
  132.         }
  133.         return false;
  134.     }
  135.    
  136.     public static boolean isKeystoreHSM(String tipo) {
  137.         if(HSMManager.getInstance()!=null) {
  138.             return HSMManager.getInstance().existsKeystoreType(tipo);
  139.         }
  140.         return false;
  141.     }
  142.    
  143.     public static KeyStore getKeystoreHSM(String tipo) throws UtilsException {
  144.         if(HSMManager.getInstance()!=null) {
  145.             return HSMManager.getInstance().getKeystore(tipo);
  146.         }
  147.         return null;
  148.     }
  149.    
  150. }