BYOKInstance.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.byok;

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

  24. import org.apache.commons.lang.StringUtils;
  25. import org.openspcoop2.utils.DynamicStringReplace;
  26. import org.openspcoop2.utils.UtilsException;
  27. import org.openspcoop2.utils.digest.DigestEncoding;
  28. import org.openspcoop2.utils.io.Base64Utilities;
  29. import org.openspcoop2.utils.io.HexBinaryUtilities;
  30. import org.openspcoop2.utils.resources.Charset;
  31. import org.openspcoop2.utils.resources.FileSystemUtilities;
  32. import org.openspcoop2.utils.transport.TransportUtils;
  33. import org.openspcoop2.utils.transport.http.HttpConstants;
  34. import org.openspcoop2.utils.transport.http.HttpRequest;
  35. import org.openspcoop2.utils.transport.http.HttpRequestMethod;
  36. import org.slf4j.Logger;

  37. /**
  38.  * BYOKInstance
  39.  *
  40.  * @author Poli Andrea (apoli@link.it)
  41.  * @author $Author$
  42.  * @version $Rev$, $Date$
  43.  */
  44. public class BYOKInstance {

  45.     private BYOKConfig config;
  46.    
  47.     private HttpRequest httpRequest;
  48.    
  49.     private byte [] localKey;
  50.     private BYOKLocalConfig localConfigResolved;
  51.    
  52.     private String keyCache;
  53.    
  54.     public BYOKInstance(BYOKConfig config, HttpRequest httpRequest, String keyCache) {
  55.         this.config = config;
  56.         this.httpRequest = httpRequest;
  57.         this.keyCache = keyCache;
  58.     }
  59.     public BYOKInstance(BYOKConfig config, BYOKLocalConfig localConfigResolved, byte[] key, String keyCache) {
  60.         this.config = config;
  61.         this.localConfigResolved = localConfigResolved;
  62.         this.localKey = key;
  63.         this.keyCache = keyCache;
  64.     }
  65.    
  66.     public BYOKConfig getConfig() {
  67.         return this.config;
  68.     }
  69.    
  70.     public BYOKLocalConfig getLocalConfigResolved() {
  71.         return this.localConfigResolved;
  72.     }
  73.     public byte[] getLocalKey() {
  74.         return this.localKey;
  75.     }
  76.    
  77.     public HttpRequest getHttpRequest() {
  78.         return this.httpRequest;
  79.     }
  80.    
  81.     public String getKeyCache() {
  82.         return this.keyCache;
  83.     }
  84.    
  85.     private static final String BYOK_REQUEST_PARAMS_UNDEFINED = "BYOKRequestParams undefined";
  86.     private static final String BYOK_REQUEST_PARAMS_CONFIG_UNDEFINED = "BYOKRequestParams config undefined";

  87.     public static BYOKInstance newInstance(Logger log, BYOKRequestParams requestParams, byte[] key) throws UtilsException {
  88.         if(requestParams==null) {
  89.             throw new UtilsException(BYOK_REQUEST_PARAMS_UNDEFINED);
  90.         }
  91.         if(requestParams.getConfig()==null) {
  92.             throw new UtilsException(BYOK_REQUEST_PARAMS_CONFIG_UNDEFINED);
  93.         }
  94.         if(BYOKEncryptionMode.LOCAL.equals(requestParams.getConfig().getEncryptionMode())) {
  95.             return BYOKInstance.newLocalInstance(log, requestParams, key);
  96.         }
  97.         else {
  98.             return BYOKInstance.newRemoteInstance(log, requestParams, key);
  99.         }
  100.     }
  101.     public static BYOKInstance newInstance(Logger log, Map<String,Object> dynamicMap, BYOKConfig config, Map<String,String> inputMap, String keyCache, byte[] key) throws UtilsException {
  102.         if(config==null) {
  103.             throw new UtilsException(BYOK_REQUEST_PARAMS_CONFIG_UNDEFINED);
  104.         }
  105.         if(BYOKEncryptionMode.LOCAL.equals(config.getEncryptionMode())) {
  106.             return BYOKInstance.newLocalInstance(log, dynamicMap, config, inputMap, keyCache, key);
  107.         }
  108.         else {
  109.             return BYOKInstance.newRemoteInstance(log, dynamicMap, config, inputMap, key);
  110.         }
  111.     }
  112.    
  113.     public static BYOKInstance newRemoteInstance(Logger log, BYOKRequestParams requestParams, byte[] key) throws UtilsException {
  114.         if(requestParams==null) {
  115.             throw new UtilsException(BYOK_REQUEST_PARAMS_UNDEFINED);
  116.         }
  117.         return newRemoteInstance(log, requestParams.getDynamicMap(), requestParams.getConfig(), requestParams.getInputMap(), key);
  118.     }
  119.     public static BYOKInstance newRemoteInstance(Logger log, Map<String,Object> dynamicMap, BYOKConfig config, Map<String,String> inputMap, byte[] key) throws UtilsException {
  120.         HttpRequest httpRequest = buildHttpRequest(log, config, dynamicMap, inputMap, key);
  121.         String keyCache = buildKeyCache(httpRequest);
  122.         return new BYOKInstance(config, httpRequest, keyCache);
  123.     }
  124.    
  125.     public static BYOKInstance newLocalInstance(Logger log, BYOKRequestParams requestParams, byte[] key) throws UtilsException {
  126.         if(requestParams==null) {
  127.             throw new UtilsException(BYOK_REQUEST_PARAMS_UNDEFINED);
  128.         }
  129.         if(requestParams.getKeyIdentity()==null) {
  130.             throw new UtilsException("BYOKRequestParams key identity undefined");
  131.         }
  132.         return newLocalInstance(log, requestParams.getDynamicMap(), requestParams.getConfig(), requestParams.getInputMap(), requestParams.getKeyIdentity(), key);
  133.     }
  134.     public static BYOKInstance newLocalInstance(Logger log, Map<String,Object> dynamicMap, BYOKConfig config, Map<String,String> inputMap, String keyCache, byte[] key) throws UtilsException {
  135.         if(log!=null) {
  136.              // nop
  137.         }
  138.         BYOKLocalConfig localConfig = buildBYOKLocalConfig(config, dynamicMap, inputMap, null);
  139.         return new BYOKInstance(config, localConfig, key, keyCache);
  140.     }
  141.    
  142.     private static BYOKLocalConfig buildBYOKLocalConfig(BYOKConfig config, Map<String,Object> dynamicMap, Map<String,String> inputMap, byte[] key) throws UtilsException {
  143.        
  144.         BYOKLocalConfig localConfig = new BYOKLocalConfig();
  145.        
  146.         List<BYOKConfigParameter> inputParameters = config.getInputParameters();
  147.        
  148.         localConfig.encryptionEngine = resolveKmsConstants(dynamicMap, inputParameters, inputMap, key,
  149.                 BYOKCostanti.PROPERTY_SUFFIX_LOCAL_IMPL, config.getLocalConfig().encryptionEngine);
  150.        
  151.         localConfig.keystoreType = config.getLocalConfig().keystoreType;
  152.         localConfig.keystoreHsmType = config.getLocalConfig().keystoreHsmType;
  153.         localConfig.keystorePath = resolveKmsConstants(dynamicMap, inputParameters, inputMap, key,
  154.                 BYOKCostanti.PROPERTY_SUFFIX_LOCAL_KEYSTORE_PATH, config.getLocalConfig().keystorePath);
  155.         localConfig.keystorePassword = resolveKmsConstants(dynamicMap, inputParameters, inputMap, key,
  156.                 BYOKCostanti.PROPERTY_SUFFIX_LOCAL_KEYSTORE_PASSWORD, config.getLocalConfig().keystorePassword);
  157.        
  158.         localConfig.keyPath = resolveKmsConstants(dynamicMap, inputParameters, inputMap, key,
  159.                 BYOKCostanti.PROPERTY_SUFFIX_LOCAL_KEY_PATH, config.getLocalConfig().keyPath);
  160.         localConfig.keyInline = resolveKmsConstants(dynamicMap, inputParameters, inputMap, key,
  161.                 BYOKCostanti.PROPERTY_SUFFIX_LOCAL_KEY_INLINE, config.getLocalConfig().keyInline);
  162.         localConfig.keyEncoding = resolveKmsConstants(dynamicMap, inputParameters, inputMap, key,
  163.                 BYOKCostanti.PROPERTY_SUFFIX_LOCAL_KEY_ENCODING, config.getLocalConfig().keyEncoding);
  164.         localConfig.keyAlgorithm = resolveKmsConstants(dynamicMap, inputParameters, inputMap, key,
  165.                 BYOKCostanti.PROPERTY_SUFFIX_LOCAL_KEY_ALGORITHM, config.getLocalConfig().keyAlgorithm);
  166.         localConfig.keyAlias = resolveKmsConstants(dynamicMap, inputParameters, inputMap, key,
  167.                 BYOKCostanti.PROPERTY_SUFFIX_LOCAL_KEY_ALIAS, config.getLocalConfig().keyAlias);
  168.         localConfig.keyPassword = resolveKmsConstants(dynamicMap, inputParameters, inputMap, key,
  169.                 BYOKCostanti.PROPERTY_SUFFIX_LOCAL_KEY_PASSWORD, config.getLocalConfig().keyPassword);
  170.         localConfig.keyId = resolveKmsConstants(dynamicMap, inputParameters, inputMap, key,
  171.                 BYOKCostanti.PROPERTY_SUFFIX_LOCAL_KEY_ID, config.getLocalConfig().keyId);
  172.         localConfig.keyWrap = config.getLocalConfig().keyWrap;
  173.        
  174.         localConfig.publicKeyPath = resolveKmsConstants(dynamicMap, inputParameters, inputMap, key,
  175.                 BYOKCostanti.PROPERTY_SUFFIX_LOCAL_PUBLIC_KEY_PATH, config.getLocalConfig().publicKeyPath);
  176.         localConfig.publicKeyInline = resolveKmsConstants(dynamicMap, inputParameters, inputMap, key,
  177.                 BYOKCostanti.PROPERTY_SUFFIX_LOCAL_PUBLIC_KEY_INLINE, config.getLocalConfig().publicKeyInline);
  178.         localConfig.publicKeyEncoding = resolveKmsConstants(dynamicMap, inputParameters, inputMap, key,
  179.                 BYOKCostanti.PROPERTY_SUFFIX_LOCAL_PUBLIC_KEY_ENCODING, config.getLocalConfig().publicKeyEncoding);
  180.        
  181.         localConfig.pw = resolveKmsConstants(dynamicMap, inputParameters, inputMap, key,
  182.                 BYOKCostanti.PROPERTY_SUFFIX_LOCAL_PW, config.getLocalConfig().pw);
  183.         localConfig.pwType = resolveKmsConstants(dynamicMap, inputParameters, inputMap, key,
  184.                 BYOKCostanti.PROPERTY_SUFFIX_LOCAL_PW_TYPE, config.getLocalConfig().pwType);
  185.         localConfig.pwIteration = config.getLocalConfig().pwIteration;
  186.        
  187.         localConfig.contentAlgorithm = resolveKmsConstants(dynamicMap, inputParameters, inputMap, key,
  188.                 BYOKCostanti.PROPERTY_SUFFIX_LOCAL_CONTENT_ALGORITHM, config.getLocalConfig().contentAlgorithm);
  189.        
  190.         localConfig.encoding = resolveKmsConstants(dynamicMap, inputParameters, inputMap, key,
  191.                 BYOKCostanti.PROPERTY_SUFFIX_LOCAL_ENCODING, config.getLocalConfig().encoding);

  192.         localConfig.joseIncludeCert = config.getLocalConfig().joseIncludeCert;
  193.         localConfig.joseIncludePublicKey = config.getLocalConfig().joseIncludePublicKey;
  194.         localConfig.joseIncludeKeyId = config.getLocalConfig().joseIncludeKeyId;
  195.         localConfig.joseIncludeCertSha1 = config.getLocalConfig().joseIncludeCertSha1;
  196.         localConfig.joseIncludeCertSha256 = config.getLocalConfig().joseIncludeCertSha256;
  197.        
  198.         return localConfig;
  199.     }
  200.    
  201.     private static HttpRequest buildHttpRequest(Logger log, BYOKConfig configParam, Map<String,Object> dynamicMap, Map<String,String> inputMap, byte[] key) throws UtilsException {
  202.        
  203.         HttpRequest http = new HttpRequest();
  204.        
  205.         BYOKRemoteConfig config = configParam.getRemoteConfig();
  206.         List<BYOKConfigParameter> inputParameters = configParam.getInputParameters();
  207.        
  208.         http.setUrl(resolveKmsConstants(dynamicMap, inputParameters, inputMap, key,
  209.             BYOKCostanti.PROPERTY_SUFFIX_HTTP_ENDPOINT, config.getHttpEndpoint()));
  210.         String m = null;
  211.         try {
  212.             m = resolveKmsConstants(dynamicMap, inputParameters, inputMap, key,
  213.                     BYOKCostanti.PROPERTY_SUFFIX_HTTP_METHOD, config.getHttpMethod());
  214.             if(m==null) {
  215.                 throw new UtilsException("Undefined");
  216.             }
  217.             http.setMethod(HttpRequestMethod.valueOf(m.toUpperCase()));
  218.         }catch(Exception e) {
  219.             throw new UtilsException("Invalid request http method ("+config.getHttpEndpoint()+"; resolved:"+m+"): "+e.getMessage(),e);
  220.         }
  221.        
  222.         if(config.getHttpConnectionTimeout()!=null) {
  223.             http.setConnectTimeout(config.getHttpConnectionTimeout());
  224.         }
  225.         if(config.getHttpReadTimeout()!=null) {
  226.             http.setReadTimeout(config.getHttpReadTimeout());
  227.         }
  228.        
  229.         setHttpHeader(config, dynamicMap,
  230.                 inputParameters, inputMap, key, http);
  231.        
  232.         setPayload(config, dynamicMap,
  233.                 inputParameters, inputMap, key, http);
  234.        
  235.         setHttps(log, config, dynamicMap,
  236.                 inputParameters, inputMap, key, http);
  237.        
  238.         return http;
  239.     }
  240.     private static void setHttpHeader(BYOKRemoteConfig config, Map<String,Object> dynamicMap,
  241.             List<BYOKConfigParameter> inputParameters, Map<String,String> inputMap, byte[] key,
  242.             HttpRequest http) throws UtilsException {
  243.         if(config.getHttpHeaders()!=null && !config.getHttpHeaders().isEmpty()) {
  244.             for(Map.Entry<String,String> entry : config.getHttpHeaders().entrySet()) {
  245.                 String nome = resolveKmsConstants(dynamicMap, inputParameters, inputMap, key,
  246.                         BYOKCostanti.PROPERTY_SUFFIX_HTTP_HEADER+"<name>", entry.getKey());
  247.                 String valore = resolveKmsConstants(dynamicMap, inputParameters, inputMap, key,
  248.                         BYOKCostanti.PROPERTY_SUFFIX_HTTP_HEADER+entry.getKey(), entry.getValue());
  249.                 if(nome!=null && valore!=null) {
  250.                     http.addHeader(nome, valore);
  251.                 }
  252.             }
  253.         }
  254.         if(config.getHttpUsername()!=null) {
  255.             http.setUsername(resolveKmsConstants(dynamicMap, inputParameters, inputMap, key,
  256.                         BYOKCostanti.PROPERTY_SUFFIX_HTTP_USERNAME, config.getHttpUsername()));
  257.         }
  258.         if(config.getHttpPassword()!=null) {
  259.             http.setPassword(resolveKmsConstants(dynamicMap, inputParameters, inputMap, key,
  260.                         BYOKCostanti.PROPERTY_SUFFIX_HTTP_PASSWORD, config.getHttpPassword()));
  261.         }
  262.     }
  263.     private static void setPayload(BYOKRemoteConfig config, Map<String,Object> dynamicMap,
  264.             List<BYOKConfigParameter> inputParameters, Map<String,String> inputMap, byte[] key,
  265.             HttpRequest http) throws UtilsException {
  266.         if(config.getHttpPayloadInLine()!=null) {
  267.             String content = resolveKmsConstants(dynamicMap, inputParameters, inputMap, key,
  268.                     BYOKCostanti.PROPERTY_SUFFIX_HTTP_PAYLOAD_INLINE, config.getHttpPayloadInLine());
  269.             if(content!=null) {
  270.                 http.setContent(content.getBytes());
  271.             }
  272.         }
  273.         else if(config.getHttpPayloadPath()!=null) {
  274.             byte[]fileContent = null;
  275.             try {
  276.                 fileContent = FileSystemUtilities.readBytesFromFile(config.getHttpPayloadPath());
  277.             }catch(Exception e) {
  278.                 throw new UtilsException("Invalid request payload file ("+config.getHttpPayloadPath()+"): "+e.getMessage(),e);
  279.             }
  280.             http.setContent(resolveKmsConstants(dynamicMap, inputParameters, inputMap, key,
  281.                     BYOKCostanti.PROPERTY_SUFFIX_HTTP_PAYLOAD_PATH, fileContent));
  282.         }
  283.        
  284.         if(http.getContent()!=null) {
  285.             String ct = http.getHeaderFirstValue(HttpConstants.CONTENT_TYPE);
  286.             if(ct==null || StringUtils.isEmpty(ct)) {
  287.                 http.setContentType(HttpConstants.CONTENT_TYPE_APPLICATION_OCTET_STREAM);
  288.             }
  289.         }
  290.     }
  291.    
  292.     private static void setHttps(Logger log, BYOKRemoteConfig config, Map<String,Object> dynamicMap,
  293.             List<BYOKConfigParameter> inputParameters, Map<String,String> inputMap, byte[] key,
  294.             HttpRequest http) throws UtilsException {
  295.        
  296.         if(log!=null) {
  297.             // nop
  298.         }
  299.        
  300.         if(config.isHttps()) {
  301.            
  302.             http.setHostnameVerifier(config.isHttpsHostnameVerifier());
  303.            
  304.             setHttpsServer(config, dynamicMap, inputParameters, inputMap, key, http);
  305.            
  306.             setHttpsClient(config, dynamicMap, inputParameters, inputMap, key, http);
  307.            
  308.         }
  309.        
  310.     }
  311.     private static void setHttpsServer(BYOKRemoteConfig config, Map<String,Object> dynamicMap,
  312.             List<BYOKConfigParameter> inputParameters, Map<String,String> inputMap, byte[] key,
  313.             HttpRequest http) throws UtilsException {
  314.         if(config.isHttpsServerAuth()) {
  315.            
  316.             if(config.getHttpsServerAuthTrustStorePath()!=null) {
  317.                 http.setTrustStorePath(resolveKmsConstants(dynamicMap, inputParameters, inputMap, key,
  318.                         BYOKCostanti.PROPERTY_SUFFIX_HTTPS_AUTENTICAZIONE_SERVER_TRUSTSTORE_PATH, config.getHttpsServerAuthTrustStorePath()));
  319.             }
  320.             if(config.getHttpsServerAuthTrustStoreType()!=null) {
  321.                 http.setTrustStoreType(resolveKmsConstants(dynamicMap, inputParameters, inputMap, key,
  322.                         BYOKCostanti.PROPERTY_SUFFIX_HTTPS_AUTENTICAZIONE_SERVER_TRUSTSTORE_TYPE, config.getHttpsServerAuthTrustStoreType()));
  323.             }
  324.             if(config.getHttpsServerAuthTrustStorePassword()!=null) {
  325.                 http.setTrustStorePassword(resolveKmsConstants(dynamicMap, inputParameters, inputMap, key,
  326.                         BYOKCostanti.PROPERTY_SUFFIX_HTTPS_AUTENTICAZIONE_SERVER_TRUSTSTORE_PASSWORD, config.getHttpsServerAuthTrustStorePassword()));
  327.             }
  328.             if(config.getHttpsServerAuthTrustStoreCrls()!=null) {
  329.                 http.setCrlPath(resolveKmsConstants(dynamicMap, inputParameters, inputMap, key,
  330.                         BYOKCostanti.PROPERTY_SUFFIX_HTTPS_AUTENTICAZIONE_SERVER_CRLS, config.getHttpsServerAuthTrustStoreCrls()));
  331.             }
  332.             if(config.getHttpsServerAuthTrustStoreOcspPolicy()!=null) {
  333.                 http.setOcspPolicy(resolveKmsConstants(dynamicMap, inputParameters, inputMap, key,
  334.                         BYOKCostanti.PROPERTY_SUFFIX_HTTPS_AUTENTICAZIONE_SERVER_OCSP_POLICY, config.getHttpsServerAuthTrustStoreOcspPolicy()));
  335.             }
  336.            
  337.         }
  338.         else {
  339.             http.setTrustAllCerts(true);
  340.         }
  341.     }
  342.     private static void setHttpsClient(BYOKRemoteConfig config, Map<String,Object> dynamicMap,
  343.             List<BYOKConfigParameter> inputParameters, Map<String,String> inputMap, byte[] key,
  344.             HttpRequest http) throws UtilsException {
  345.         if(config.isHttpsClientAuth()) {
  346.            
  347.             if(config.getHttpsClientAuthKeyStorePath()!=null) {
  348.                 http.setKeyStorePath(resolveKmsConstants(dynamicMap, inputParameters, inputMap, key,
  349.                         BYOKCostanti.PROPERTY_SUFFIX_HTTPS_AUTENTICAZIONE_CLIENT_KEYSTORE_PATH, config.getHttpsClientAuthKeyStorePath()));
  350.             }
  351.             if(config.getHttpsClientAuthKeyStoreType()!=null) {
  352.                 http.setKeyStoreType(resolveKmsConstants(dynamicMap, inputParameters, inputMap, key,
  353.                         BYOKCostanti.PROPERTY_SUFFIX_HTTPS_AUTENTICAZIONE_CLIENT_KEYSTORE_TYPE, config.getHttpsClientAuthKeyStoreType()));
  354.             }
  355.             if(config.getHttpsClientAuthKeyStorePassword()!=null) {
  356.                 http.setKeyStorePassword(resolveKmsConstants(dynamicMap, inputParameters, inputMap, key,
  357.                         BYOKCostanti.PROPERTY_SUFFIX_HTTPS_AUTENTICAZIONE_CLIENT_KEYSTORE_PASSWORD, config.getHttpsClientAuthKeyStorePassword()));
  358.             }
  359.            
  360.            
  361.             if(config.getHttpsClientAuthKeyAlias()!=null) {
  362.                 http.setKeyAlias(resolveKmsConstants(dynamicMap, inputParameters, inputMap, key,
  363.                         BYOKCostanti.PROPERTY_SUFFIX_HTTPS_AUTENTICAZIONE_CLIENT_KEY_ALIAS, config.getHttpsClientAuthKeyAlias()));
  364.             }
  365.             if(config.getHttpsClientAuthKeyPassword()!=null) {
  366.                 http.setKeyPassword(resolveKmsConstants(dynamicMap, inputParameters, inputMap, key,
  367.                         BYOKCostanti.PROPERTY_SUFFIX_HTTPS_AUTENTICAZIONE_CLIENT_KEY_PASSWORD, config.getHttpsClientAuthKeyPassword()));
  368.             }
  369.         }
  370.     }
  371.    
  372.     private static String buildKeyCache(HttpRequest httpRequest) throws UtilsException {
  373.         StringBuilder sb = new StringBuilder();
  374.         if(httpRequest!=null) {
  375.            
  376.             sb.append(httpRequest.getMethod()).append("_").append(httpRequest.getUrl());
  377.        
  378.             addKeyCacheHttpHeader(httpRequest, sb);
  379.            
  380.             if(httpRequest.getContent()!=null && httpRequest.getContent().length>0) {
  381.                 String digestAlgorithm = "SHA256";
  382.                 String digestAuditValue = org.openspcoop2.utils.digest.DigestUtils.getDigestValue(httpRequest.getContent(), digestAlgorithm, DigestEncoding.HEX,
  383.                         true); // se rfc3230 true aggiunge prefisso algoritmo=
  384.                 sb.append("_");
  385.                 sb.append(digestAuditValue);
  386.             }
  387.         }
  388.         return sb.toString();
  389.     }
  390.     private static void addKeyCacheHttpHeader(HttpRequest httpRequest, StringBuilder sb) {
  391.         if(httpRequest.getHeadersValues()!=null && !httpRequest.getHeadersValues().isEmpty()) {
  392.             for(String name : httpRequest.getHeadersValues().keySet()) {
  393.                 List<String> values = httpRequest.getHeadersValues().get(name);
  394.                 if(values!=null && !values.isEmpty()) {
  395.                     for (String v : values) {
  396.                         sb.append("_");
  397.                         sb.append(name).append(":").append(v);
  398.                     }
  399.                 }
  400.             }
  401.         }
  402.     }
  403.    
  404.     private static byte[] resolveKmsConstants(Map<String,Object> dynamicMap, List<BYOKConfigParameter> inputParameters,
  405.             Map<String,String> inputMap, byte[] key,
  406.             String name, byte[] value) throws UtilsException {
  407.         byte[] returnArray = null;
  408.         if(value!=null) {
  409.             String v = new String(value);
  410.             if(BYOKCostanti.VARIABILE_KMS_KEY.equals(v) || BYOKCostanti.VARIABILE_KSM_KEY_DEPRECATED.equals(v)) {
  411.                 returnArray = key;
  412.             }
  413.             else if(BYOKCostanti.VARIABILE_KMS_KEY_URL_ENCODED.equals(v) || BYOKCostanti.VARIABILE_KSM_KEY_URL_ENCODED_DEPRECATED.equals(v)) {
  414.                 returnArray = TransportUtils.urlEncodeParam(v, Charset.UTF_8.getValue()).getBytes();
  415.             }
  416.             else if(BYOKCostanti.VARIABILE_KMS_KEY_BASE64.equals(v) || BYOKCostanti.VARIABILE_KSM_KEY_BASE64_DEPRECATED.equals(v)) {
  417.                 returnArray = Base64Utilities.encode(key);
  418.             }
  419.             else if(BYOKCostanti.VARIABILE_KMS_KEY_BASE64_URL_ENCODED.equals(v) || BYOKCostanti.VARIABILE_KSM_KEY_BASE64_URL_ENCODED_DEPRECATED.equals(v)) {
  420.                 String base64 = Base64Utilities.encodeAsString(key);
  421.                 returnArray = TransportUtils.urlEncodeParam(base64, Charset.UTF_8.getValue()).getBytes();
  422.             }
  423.             else if(BYOKCostanti.VARIABILE_KMS_KEY_HEX.equals(v) || BYOKCostanti.VARIABILE_KSM_KEY_HEX_DEPRECATED.equals(v)) {
  424.                 returnArray = HexBinaryUtilities.encodeAsString(key).getBytes();
  425.             }
  426.             else if(BYOKCostanti.VARIABILE_KMS_KEY_HEX_URL_ENCODED.equals(v) || BYOKCostanti.VARIABILE_KSM_KEY_HEX_URL_ENCODED_DEPRECATED.equals(v)) {
  427.                 String hex =  HexBinaryUtilities.encodeAsString(key);
  428.                 returnArray = TransportUtils.urlEncodeParam(hex, Charset.UTF_8.getValue()).getBytes();
  429.             }
  430.             else {
  431.                 returnArray = resolveKmsConstants(dynamicMap, inputParameters,
  432.                         inputMap, key,
  433.                         name, v).getBytes();
  434.             }
  435.         }
  436.         return returnArray;
  437.     }
  438.     private static String resolveKmsConstants(Map<String,Object> dynamicMap, List<BYOKConfigParameter> inputParameters,
  439.             Map<String,String> inputMap, byte[] key,
  440.             String name, String value) throws UtilsException {
  441.        
  442.         if(value==null) {
  443.             return value;
  444.         }
  445.        
  446.         String newValue = resolveKmsConstant(value, BYOKCostanti.VARIABILE_KMS_KEY, key);
  447.         newValue = resolveKmsConstant(newValue, BYOKCostanti.VARIABILE_KSM_KEY_DEPRECATED, key);
  448.         newValue = resolveKmsConstant(newValue, BYOKCostanti.VARIABILE_KMS_KEY_URL_ENCODED, key);
  449.         newValue = resolveKmsConstant(newValue, BYOKCostanti.VARIABILE_KSM_KEY_URL_ENCODED_DEPRECATED, key);
  450.        
  451.         newValue = resolveKmsConstant(newValue, BYOKCostanti.VARIABILE_KMS_KEY_BASE64, key);
  452.         newValue = resolveKmsConstant(newValue, BYOKCostanti.VARIABILE_KSM_KEY_BASE64_DEPRECATED, key);
  453.         newValue = resolveKmsConstant(newValue, BYOKCostanti.VARIABILE_KMS_KEY_BASE64_URL_ENCODED, key);
  454.         newValue = resolveKmsConstant(newValue, BYOKCostanti.VARIABILE_KSM_KEY_BASE64_URL_ENCODED_DEPRECATED, key);
  455.        
  456.         newValue = resolveKmsConstant(newValue, BYOKCostanti.VARIABILE_KMS_KEY_HEX, key);
  457.         newValue = resolveKmsConstant(newValue, BYOKCostanti.VARIABILE_KSM_KEY_HEX_DEPRECATED, key);
  458.         newValue = resolveKmsConstant(newValue, BYOKCostanti.VARIABILE_KMS_KEY_HEX_URL_ENCODED, key);
  459.         newValue = resolveKmsConstant(newValue, BYOKCostanti.VARIABILE_KSM_KEY_HEX_URL_ENCODED_DEPRECATED, key);
  460.        
  461.         String kmsMapId = null;
  462.         if(newValue.contains(BYOKCostanti.VARIABILE_KMS_KEY_PREFIX) && !dynamicMap.containsKey(BYOKCostanti.VARIABILE_KMS)) {
  463.             kmsMapId = BYOKCostanti.VARIABILE_KMS;
  464.         }
  465.         else if(newValue.contains(BYOKCostanti.VARIABILE_KSM_KEY_PREFIX_DEPRECATED) && !dynamicMap.containsKey(BYOKCostanti.VARIABILE_KSM_DEPRECATED)) {
  466.             kmsMapId = BYOKCostanti.VARIABILE_KSM_DEPRECATED;
  467.         }
  468.         if(kmsMapId!=null) {
  469.             Map<String, String> k = new HashMap<>();
  470.             for (BYOKConfigParameter parameter: inputParameters) {
  471.                 if(inputMap!=null && inputMap.containsKey(parameter.getName())) {
  472.                     String paramValue = inputMap.get(parameter.getName());
  473.                     String valueResolved = resolve(parameter.getName(), paramValue, dynamicMap); // potrebbe essere a sua volta dinamico
  474.                     k.put(parameter.getName(), valueResolved);
  475.                 }
  476.             }
  477.             dynamicMap.put(kmsMapId, k);
  478.         }
  479.        
  480.         return resolve(name, newValue, dynamicMap);
  481.     }
  482.     private static String resolveKmsConstant(String value, String constant, byte[] key) throws UtilsException {
  483.         if(value!=null && value.contains(constant)){
  484.            
  485.             String replaceValue = null;
  486.             if(BYOKCostanti.VARIABILE_KMS_KEY.equals(constant) || BYOKCostanti.VARIABILE_KSM_KEY_DEPRECATED.equals(constant) ||
  487.                     BYOKCostanti.VARIABILE_KMS_KEY_URL_ENCODED.equals(constant) || BYOKCostanti.VARIABILE_KSM_KEY_URL_ENCODED_DEPRECATED.equals(constant)) {
  488.                 replaceValue = new String(key);
  489.             }
  490.             else if(BYOKCostanti.VARIABILE_KMS_KEY_BASE64.equals(constant) || BYOKCostanti.VARIABILE_KSM_KEY_BASE64_DEPRECATED.equals(constant) ||
  491.                     BYOKCostanti.VARIABILE_KMS_KEY_BASE64_URL_ENCODED.equals(constant) || BYOKCostanti.VARIABILE_KSM_KEY_BASE64_URL_ENCODED_DEPRECATED.equals(constant)) {
  492.                 replaceValue = Base64Utilities.encodeAsString(key);
  493.             }
  494.             else if(BYOKCostanti.VARIABILE_KMS_KEY_HEX.equals(constant) || BYOKCostanti.VARIABILE_KSM_KEY_HEX_DEPRECATED.equals(constant) ||
  495.                     BYOKCostanti.VARIABILE_KMS_KEY_HEX_URL_ENCODED.equals(constant) || BYOKCostanti.VARIABILE_KSM_KEY_HEX_URL_ENCODED_DEPRECATED.equals(constant)) {
  496.                 replaceValue = HexBinaryUtilities.encodeAsString(key);
  497.             }
  498.            
  499.             if(BYOKCostanti.VARIABILE_KMS_KEY_URL_ENCODED.equals(constant) || BYOKCostanti.VARIABILE_KSM_KEY_URL_ENCODED_DEPRECATED.equals(constant) ||  
  500.                     BYOKCostanti.VARIABILE_KMS_KEY_BASE64_URL_ENCODED.equals(constant) || BYOKCostanti.VARIABILE_KSM_KEY_BASE64_URL_ENCODED_DEPRECATED.equals(constant) ||
  501.                     BYOKCostanti.VARIABILE_KMS_KEY_HEX_URL_ENCODED.equals(constant) || BYOKCostanti.VARIABILE_KSM_KEY_HEX_URL_ENCODED_DEPRECATED.equals(constant)) {
  502.                 replaceValue = TransportUtils.urlEncodeParam(replaceValue,Charset.UTF_8.getValue());
  503.             }
  504.            
  505.             while(value.contains(constant)){
  506.                 value = value.replace(constant, replaceValue);
  507.             }
  508.         }
  509.         return value;
  510.     }
  511.    
  512.     private static String resolve(String name, String value, Map<String,Object> dynamicMap) throws UtilsException {
  513.         try{
  514.             return DynamicStringReplace.replace(value, dynamicMap, true);
  515.         }catch(Exception e){
  516.             String prefix = "["+name+"] contiene un valore non corretto: ";
  517.             throw new UtilsException(prefix+e.getMessage(),e);
  518.         }
  519.     }
  520. }