BYOKUtilities.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.core.byok;

  21. import org.openspcoop2.core.constants.CostantiDB;

  22. /**
  23.  * BYOKUtilities
  24.  *
  25.  * @author Andrea Poli (poli@link.it)
  26.  * @author $Author$
  27.  * @version $Rev$, $Date$
  28.  */
  29. public class BYOKUtilities {
  30.    
  31.     private BYOKUtilities() {}

  32.     public static boolean isWrappedValue(byte[] s) {
  33.         if(s==null) {
  34.             return false;
  35.         }
  36.         return isWrappedValue(new String(s));
  37.     }
  38.     public static boolean isWrappedValue(String s) {
  39.         if(s!=null && s.startsWith(CostantiDB.ENC_PREFIX) && s.contains(".")) {
  40.             int indexOf = s.indexOf(".");
  41.             if(indexOf>0 && indexOf<s.length()) {
  42.                 String header = s.substring(0, indexOf);
  43.                 return header.endsWith(CostantiDB.ENC_PREFIX);
  44.             }
  45.         }
  46.         return false;
  47.     }
  48.    
  49.     public static String newPrefixWrappedValue(String policy) {
  50.         return CostantiDB.ENC_PREFIX + policy + CostantiDB.ENC_PREFIX + ".";
  51.     }
  52.    
  53.     public static byte[] deletePrefixWrappedValue(byte[] s) {
  54.         if(s!=null && BYOKUtilities.isWrappedValue(s)) {
  55.             byte[] newS = BYOKUtilities.deletePrefixWrappedValueEngine(s);
  56.             // controllo 2 volte perchè l'operazione encrypt tramite 'remote' aggiunge 2 prefissi
  57.             // non serve, eventualmente il servizio jmx supporta questo caso
  58.             /**if(BYOKUtilities.isWrappedValue(newS)) {
  59.                 newS = BYOKUtilities.deletePrefixWrappedValueEngine(newS);
  60.             }*/
  61.             if(newS.length>0) {
  62.                 return newS;
  63.             }
  64.         }
  65.         return s;
  66.     }
  67.     private static byte[] deletePrefixWrappedValueEngine(byte[] s) {
  68.         String prefix = BYOKUtilities.extractPrefixWrappedValue(s);
  69.         if(prefix==null) {
  70.             return s;
  71.         }
  72.         if(!prefix.endsWith(".")) {
  73.             prefix = prefix + ".";
  74.         }
  75.         if(s.length>prefix.getBytes().length) {
  76.             byte[] newArchive = new byte[s.length - prefix.getBytes().length];
  77.             System.arraycopy(s, prefix.getBytes().length, newArchive, 0, newArchive.length);
  78.             return newArchive;
  79.         }
  80.         return s;
  81.     }
  82.    
  83.     public static String deletePrefixWrappedValue(String s) {
  84.         if(s!=null && BYOKUtilities.isWrappedValue(s)) {
  85.             String newS = BYOKUtilities.deletePrefixWrappedValueEngine(s);
  86.             // controllo 2 volte perchè l'operazione encrypt tramite 'remote' aggiunge 2 prefissi
  87.             // non serve, eventualmente il servizio jmx supporta questo caso
  88.             /**if(newS!=null && BYOKUtilities.isWrappedValue(newS)) {
  89.                 newS = BYOKUtilities.deletePrefixWrappedValueEngine(newS);
  90.             }*/
  91.             if(newS!=null && newS.length()>0) {
  92.                 return newS;
  93.             }
  94.         }
  95.         return s;
  96.     }
  97.     public static String deletePrefixWrappedValueEngine(String s) {
  98.         String prefix = BYOKUtilities.extractPrefixWrappedValue(s);
  99.         if(prefix==null) {
  100.             return s;
  101.         }
  102.         if(!prefix.endsWith(".")) {
  103.             prefix = prefix + ".";
  104.         }
  105.         if(s.length()>prefix.length()) {
  106.             return s.substring(prefix.length());
  107.         }
  108.         return s;
  109.     }
  110.    
  111.     public static String extractPrefixWrappedValue(byte[] s) {
  112.         if(s==null) {
  113.             return null;
  114.         }
  115.         return extractPrefixWrappedValue(new String(s));
  116.     }
  117.     public static String extractPrefixWrappedValue(String s) {
  118.         if(s!=null && s.startsWith(CostantiDB.ENC_PREFIX) && s.contains(".")) {
  119.             int indexOf = s.indexOf(".");
  120.             if(indexOf>0 && indexOf<s.length()) {
  121.                 return s.substring(0, indexOf);
  122.             }
  123.         }
  124.         return null;
  125.     }
  126.     public static String getPolicy(String s) {
  127.         String header = extractPrefixWrappedValue(s);
  128.         if(header!=null && header.endsWith(CostantiDB.ENC_PREFIX)) {
  129.             return header.substring(CostantiDB.ENC_PREFIX.length(), header.length()-CostantiDB.ENC_PREFIX.length());
  130.         }
  131.         return null;
  132.     }
  133.    
  134.     public static String getRawWrappedValue(String s) {
  135.         if(s!=null && s.startsWith(CostantiDB.ENC_PREFIX) && s.contains(".")) {
  136.             int indexOf = s.indexOf(".");
  137.             if(indexOf>0 && indexOf<s.length()) {
  138.                 String header = s.substring(0, indexOf);
  139.                 if(header.endsWith(CostantiDB.ENC_PREFIX) && s.length()>(header.length()+1)) {
  140.                     return s.substring(header.length()+1); // +1 per il punto
  141.                 }
  142.             }
  143.         }
  144.         return null;
  145.     }
  146. }