VaultEncDecUtilities.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.config.vault.cli;

  21. import java.io.File;
  22. import java.util.Arrays;
  23. import java.util.HashMap;
  24. import java.util.Map;

  25. import org.apache.commons.lang.StringUtils;
  26. import org.openspcoop2.core.byok.BYOKWrappedValue;
  27. import org.openspcoop2.core.commons.CoreException;
  28. import org.openspcoop2.pdd.core.byok.DriverBYOK;
  29. import org.openspcoop2.utils.UtilsException;
  30. import org.openspcoop2.utils.certificate.byok.BYOKInstance;
  31. import org.openspcoop2.utils.certificate.byok.BYOKManager;
  32. import org.openspcoop2.utils.certificate.byok.BYOKRequestParams;
  33. import org.openspcoop2.utils.resources.FileSystemUtilities;

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

  42.     private VaultEncDecConfig encDecConfig = null;
  43.    
  44.     public VaultEncDecUtilities(VaultEncDecConfig c) {
  45.         this.encDecConfig = c;
  46.     }
  47.    
  48.     public void process() throws CoreException {

  49.         try {
  50.            
  51.             byte[]input = null;
  52.             VaultTools.logCoreDebug("Lettura input ...");
  53.             if(this.encDecConfig.isInSystemMode()) {
  54.                 input = this.encDecConfig.getInText().getBytes();
  55.             }
  56.             else {
  57.                 input = FileSystemUtilities.readBytesFromFile(this.encDecConfig.getInFilePath());
  58.             }
  59.             VaultTools.logCoreDebug("Lettura input completata");
  60.            
  61.             byte [] output = null;
  62.            
  63.             if(this.encDecConfig.isSecurityMode()) {
  64.                 output = processBySecurity(input);
  65.             }
  66.             else {
  67.                 output = processByKms(input);
  68.             }
  69.            
  70.             VaultTools.logCoreDebug("Serializzazione output ...");
  71.             if(this.encDecConfig.isOutSystemMode()) {
  72.                 VaultTools.logOutput(new String(output));
  73.             }
  74.             else if(this.encDecConfig.isOutFileMode()) {
  75.                 File f = new File(this.encDecConfig.getOutFilePath());
  76.                 FileSystemUtilities.writeFile(f, output);
  77.                 String op = this.encDecConfig.isEncode() ? "Encrypted" : "Decrypted";
  78.                 VaultTools.logOutput(op+" content in '"+f.getAbsolutePath()+"'");
  79.             }
  80.             else {
  81.                 throw new CoreException("Unsupported mode");
  82.             }
  83.             VaultTools.logCoreDebug("Serializzazione output completata");
  84.         }
  85.         catch(Exception t) {
  86.             VaultTools.logCoreError(t.getMessage(),t);
  87.             throw new CoreException(t.getMessage(),t);
  88.         }

  89.     }
  90.     public byte[] processBySecurity(byte[] input) throws UtilsException {
  91.         String policy = this.encDecConfig.getId();
  92.         if(policy==null || StringUtils.isEmpty(policy)) {
  93.             policy = BYOKManager.getSecurityRemoteEngineGovWayPolicy();
  94.         }
  95.         if(policy==null || StringUtils.isEmpty(policy)) {
  96.             policy = BYOKManager.getSecurityEngineGovWayPolicy();
  97.         }
  98.        
  99.         VaultTools.logCoreDebug("Cifratura tramite security policy '"+policy+"' ...");
  100.        
  101.         DriverBYOK driver = new DriverBYOK(VaultTools.getLogCore(), policy, policy);
  102.        
  103.         byte [] output = null;
  104.         if(this.encDecConfig.isEncode()) {
  105.             BYOKWrappedValue v = driver.wrap(input);
  106.             output = v.getWrappedValue().getBytes();
  107.         }
  108.         else {
  109.             output = driver.unwrap(input);
  110.         }
  111.        
  112.         if(Arrays.equals(input, output)) {
  113.             throw new UtilsException("Unwrap failed");
  114.         }
  115.        
  116.         VaultTools.logCoreDebug("Cifratura tramite security policy '"+policy+"' completata");
  117.        
  118.         return output;
  119.     }
  120.     public byte[] processByKms(byte[] input) throws UtilsException {
  121.         String kmsId = this.encDecConfig.getId();
  122.         VaultTools.logCoreDebug("Cifratura tramite kms '"+kmsId+"' ...");
  123.        
  124.         Map<String, Object> dynamicMap = DriverBYOK.buildDynamicMap(VaultTools.getLogCore());
  125.         Map<String, String> inputMap = new HashMap<>();
  126.         BYOKRequestParams requestParams = BYOKRequestParams.getBYOKRequestParamsByKmsId(kmsId, inputMap, dynamicMap);
  127.        
  128.         BYOKInstance instance = BYOKInstance.newInstance(VaultTools.getLogCore(), requestParams, input);
  129.        
  130.         byte[] output = DriverBYOK.processInstance(instance, true);
  131.        
  132.         VaultTools.logCoreDebug("Cifratura tramite kms '"+kmsId+"' completata");
  133.        
  134.         return output;
  135.     }
  136. }