StoreUtils.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.keystore;

  21. import java.io.File;
  22. import java.io.FileInputStream;
  23. import java.io.InputStream;
  24. import java.util.Properties;

  25. import org.openspcoop2.core.byok.BYOKUtilities;
  26. import org.openspcoop2.security.SecurityException;
  27. import org.openspcoop2.utils.LoggerWrapperFactory;
  28. import org.openspcoop2.utils.Utilities;
  29. import org.openspcoop2.utils.certificate.byok.BYOKEncryptionMode;
  30. import org.openspcoop2.utils.certificate.byok.BYOKInstance;
  31. import org.openspcoop2.utils.certificate.byok.BYOKRequestParams;
  32. import org.slf4j.Logger;

  33. /**
  34.  * StoreUtils
  35.  *
  36.  * @author Andrea Poli (apoli@link.it)
  37.  * @author $Author$
  38.  * @version $Rev$, $Date$
  39.  */
  40. public class StoreUtils {

  41.     private StoreUtils() {}
  42.    
  43.     public static byte[] readContent(String pName, String path) throws SecurityException {
  44.        
  45.         if(path==null){
  46.             throw new SecurityException("Property "+pName+" non indicata");
  47.         }
  48.        
  49.         File fStore = new File(path);
  50.         boolean fExists = fStore.exists();
  51.         byte[] array = null;
  52.         try {
  53.             try(InputStream isStore = fExists ? new FileInputStream(fStore) : StoreUtils.class.getResourceAsStream(path)){
  54.                 if(isStore!=null){
  55.                     array = Utilities.getAsByteArray(isStore);
  56.                 }
  57.             }
  58.            
  59.             if(array==null && !fExists) {
  60.                 try(InputStream isStore = StoreUtils.class.getResourceAsStream("/"+path)){
  61.                     if(isStore!=null){
  62.                         array = Utilities.getAsByteArray(isStore);
  63.                     }
  64.                 }
  65.             }
  66.         }catch(Exception e){
  67.             throw new SecurityException(e.getMessage(),e);
  68.         }
  69.        
  70.         if(array==null){
  71.             throw new SecurityException("Store ["+path+"] not found");
  72.         }
  73.        
  74.         return array;
  75.        
  76.     }
  77.    
  78.     public static Properties readProperties(String pName, String path) throws SecurityException {
  79.        
  80.         if(path==null){
  81.             throw new SecurityException("Property "+pName+" non indicata");
  82.         }
  83.        
  84.         File fStore = new File(path);
  85.         boolean fExists = fStore.exists();
  86.         Properties propStore = null;
  87.         try {
  88.             try(InputStream isStore = fExists ? new FileInputStream(fStore) : StoreUtils.class.getResourceAsStream(path)){
  89.                 if(isStore!=null){
  90.                     propStore = new Properties();
  91.                     propStore.load(isStore);
  92.                 }
  93.             }
  94.            
  95.             if(propStore==null && !fStore.exists()) {
  96.                 try(InputStream isStore = StoreUtils.class.getResourceAsStream("/"+path)){
  97.                     if(isStore!=null){
  98.                         propStore = new Properties();
  99.                         propStore.load(isStore);
  100.                     }
  101.                 }
  102.             }
  103.         }catch(Exception e){
  104.             throw new SecurityException(e.getMessage(),e);
  105.         }
  106.        
  107.         if(propStore==null){
  108.             throw new SecurityException("Store ["+path+"] not found");
  109.         }
  110.        
  111.         return propStore;
  112.        
  113.     }
  114.    
  115.     public static byte[] unwrapBYOK(byte[] archive, BYOKRequestParams requestParams) throws SecurityException {
  116.         if(requestParams!=null) {
  117.            
  118.             if(archive!=null && BYOKUtilities.isWrappedValue(archive)) {
  119.                 archive = BYOKUtilities.deletePrefixWrappedValue(archive);
  120.             }
  121.            
  122.             Logger log = LoggerWrapperFactory.getLogger(StoreUtils.class);
  123.             BYOKInstance instance = null;
  124.             try {
  125.                 if(requestParams.getConfig()!=null && BYOKEncryptionMode.LOCAL.equals(requestParams.getConfig().getEncryptionMode())) {
  126.                     instance = BYOKInstance.newLocalInstance(log, requestParams, archive);
  127.                 }
  128.                 else {
  129.                     instance = BYOKInstance.newRemoteInstance(log, requestParams, archive);
  130.                 }
  131.             }catch(Exception e){
  132.                 throw new SecurityException(e.getMessage(),e);
  133.             }
  134.             BYOKStore store = new BYOKStore(instance.getKeyCache(), instance);
  135.             return store.getStoreBytes();
  136.         }
  137.         return archive;
  138.     }
  139. }