BYOKLocalConfig.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.io.Serializable;
  22. import java.util.Properties;
  23. import java.util.UUID;

  24. import org.apache.commons.lang.StringUtils;
  25. import org.openspcoop2.utils.UtilsException;
  26. import org.openspcoop2.utils.certificate.KeystoreType;
  27. import org.openspcoop2.utils.certificate.hsm.HSMManager;
  28. import org.openspcoop2.utils.certificate.hsm.HSMUtils;
  29. import org.slf4j.Logger;

  30. /**
  31.  * BYOKLocalConfig
  32.  *
  33.  * @author Poli Andrea (apoli@link.it)
  34.  * @author $Author$
  35.  * @version $Rev$, $Date$
  36.  */
  37. public class BYOKLocalConfig implements Serializable {

  38.     /**
  39.      *
  40.      */
  41.     private static final long serialVersionUID = -3572589461109860459L;
  42.            
  43.     protected String encryptionEngine;
  44.    
  45.     protected KeystoreType keystoreType;
  46.     protected String keystoreHsmType;
  47.     protected String keystorePath;
  48.     protected String keystorePassword;
  49.    
  50.     protected String keyPath;
  51.     protected String keyInline;
  52.     protected String keyEncoding;
  53.     protected String keyAlgorithm;
  54.     protected String keyAlias;
  55.     protected String keyPassword;
  56.     protected String keyId;
  57.     protected boolean keyWrap = false;
  58.    
  59.     protected String publicKeyPath;
  60.     protected String publicKeyInline;
  61.     protected String publicKeyEncoding;
  62.    
  63.     protected String pw; // password
  64.     protected String pwType; // passwordType
  65.     protected Integer pwIteration; // passwordIteration
  66.    
  67.     protected String contentAlgorithm;
  68.    
  69.     protected String encoding;

  70.     protected boolean joseIncludeCert;
  71.     protected boolean joseIncludePublicKey;
  72.     protected boolean joseIncludeKeyId;
  73.     protected boolean joseIncludeCertSha1;
  74.     protected boolean joseIncludeCertSha256;
  75.    
  76.     protected BYOKLocalConfig() {}
  77.     protected BYOKLocalConfig(String id, Properties p, Logger log, BYOKConfig config, String byokPropertyPrefix) throws UtilsException {
  78.                
  79.         if(p==null || p.isEmpty()) {
  80.             log.error("Properties is null");
  81.             throw new UtilsException("Properties '"+byokPropertyPrefix+id+".*' undefined");
  82.         }
  83.        
  84.         initEngine(id, p, config, byokPropertyPrefix);
  85.        
  86.         initKeystore(id, p, config, byokPropertyPrefix);
  87.     }

  88.     private static final String UNSUPPORTED_KEYSTORE_PREFIX = "Unsupported keystore '";
  89.     private static final String UNSUPPORTED_PROPERTY_PREFIX = "Unsupported property '";
  90.     private static final String VALUE_SEPARATOR = "' value '";
  91.     private static final String TYPE_SEPARATOR = "' type '";
  92.    
  93.     private void initEngine(String id, Properties p, BYOKConfig config, String byokPropertyPrefix) throws UtilsException {
  94.        
  95.         if(config!=null) {
  96.             // nop
  97.         }
  98.        
  99.         this.encryptionEngine = BYOKConfig.getProperty(id, p, BYOKCostanti.PROPERTY_SUFFIX_LOCAL_IMPL, true, byokPropertyPrefix);
  100.         if(!BYOKCostanti.PROPERTY_LOCAL_ENCRYPTION_ENGINE_JAVA.equals(this.encryptionEngine) &&
  101.                 !BYOKCostanti.PROPERTY_LOCAL_ENCRYPTION_ENGINE_JOSE.equals(this.encryptionEngine) &&
  102.                 !BYOKCostanti.PROPERTY_LOCAL_ENCRYPTION_ENGINE_OPENSSL.equals(this.encryptionEngine)) {
  103.             throw new UtilsException(UNSUPPORTED_PROPERTY_PREFIX+byokPropertyPrefix+id+"."+BYOKCostanti.PROPERTY_SUFFIX_LOCAL_IMPL+VALUE_SEPARATOR+this.encryptionEngine+"'");
  104.         }
  105.        
  106.         this.contentAlgorithm = BYOKConfig.getProperty(id, p, BYOKCostanti.PROPERTY_SUFFIX_LOCAL_CONTENT_ALGORITHM,
  107.                 !BYOKCostanti.PROPERTY_LOCAL_ENCRYPTION_ENGINE_OPENSSL.equals(this.encryptionEngine), byokPropertyPrefix);
  108.        
  109.         if(BYOKCostanti.PROPERTY_LOCAL_ENCRYPTION_ENGINE_JAVA.equals(this.encryptionEngine) ||
  110.                 BYOKCostanti.PROPERTY_LOCAL_ENCRYPTION_ENGINE_OPENSSL.equals(this.encryptionEngine)) {
  111.             boolean required = BYOKCostanti.PROPERTY_LOCAL_ENCRYPTION_ENGINE_JAVA.equals(this.encryptionEngine);
  112.                     /**||
  113.                     BYOKMode.WRAP.equals(config.getMode()); // il wrap richiede e produce un valore leggibile per l'unwrap*/
  114.             this.encoding = BYOKConfig.getProperty(id, p, BYOKCostanti.PROPERTY_SUFFIX_LOCAL_ENCODING, required, byokPropertyPrefix);
  115.             if(this.encoding!=null && StringUtils.isNotEmpty(this.encoding) &&
  116.                     !BYOKCostanti.PROPERTY_LOCAL_ENCODING_BASE64.equals(this.encoding) &&
  117.                     !BYOKCostanti.PROPERTY_LOCAL_ENCODING_HEX.equals(this.encoding)) {
  118.                 throw new UtilsException(UNSUPPORTED_PROPERTY_PREFIX+byokPropertyPrefix+id+"."+BYOKCostanti.PROPERTY_SUFFIX_LOCAL_ENCODING+VALUE_SEPARATOR+this.encoding+"'");
  119.             }
  120.         }
  121.        
  122.         if(BYOKCostanti.PROPERTY_LOCAL_ENCRYPTION_ENGINE_JAVA.equals(this.encryptionEngine)) {      
  123.             this.keyWrap = BYOKConfig.getBooleanProperty(id, p, BYOKCostanti.PROPERTY_SUFFIX_LOCAL_KEY_WRAP, false, false, byokPropertyPrefix);
  124.         }
  125.        
  126.         if(BYOKCostanti.PROPERTY_LOCAL_ENCRYPTION_ENGINE_JOSE.equals(this.encryptionEngine)) {      
  127.            
  128.             this.joseIncludeCert = BYOKConfig.getBooleanProperty(id, p, BYOKCostanti.PROPERTY_SUFFIX_LOCAL_JOSE_INCLUDE_CERT, false, false, byokPropertyPrefix);
  129.             this.joseIncludePublicKey = BYOKConfig.getBooleanProperty(id, p, BYOKCostanti.PROPERTY_SUFFIX_LOCAL_JOSE_INCLUDE_PUBLIC_KEY, false, false, byokPropertyPrefix);
  130.             this.joseIncludeKeyId = BYOKConfig.getBooleanProperty(id, p, BYOKCostanti.PROPERTY_SUFFIX_LOCAL_JOSE_INCLUDE_KEY_ID, false, false, byokPropertyPrefix);
  131.             this.joseIncludeCertSha1 = BYOKConfig.getBooleanProperty(id, p, BYOKCostanti.PROPERTY_SUFFIX_LOCAL_JOSE_INCLUDE_CERT_SHA1, false, false, byokPropertyPrefix);
  132.             this.joseIncludeCertSha256 = BYOKConfig.getBooleanProperty(id, p, BYOKCostanti.PROPERTY_SUFFIX_LOCAL_JOSE_INCLUDE_CERT_SHA256, false, false, byokPropertyPrefix);
  133.            
  134.             String keyIdTmp = BYOKConfig.getProperty(id, p, BYOKCostanti.PROPERTY_SUFFIX_LOCAL_KEY_ID, false, byokPropertyPrefix);
  135.             if(keyIdTmp!=null) {
  136.                 this.keyId = keyIdTmp.trim();
  137.             }
  138.            
  139.         }
  140.     }
  141.    
  142.     private void initKeystore(String id, Properties p, BYOKConfig config, String byokPropertyPrefix) throws UtilsException {
  143.        
  144.         String tmpKeystoreType = BYOKConfig.getProperty(id, p, BYOKCostanti.PROPERTY_SUFFIX_LOCAL_KEYSTORE_TYPE, true, byokPropertyPrefix);
  145.         this.keystoreType = KeystoreType.toEnumFromName(tmpKeystoreType);
  146.         if(this.keystoreType==null) {
  147.             HSMManager hsmManager = HSMManager.getInstance();
  148.             if(hsmManager.existsKeystoreType(tmpKeystoreType)) {
  149.                 this.keystoreType = KeystoreType.PKCS11;
  150.                 this.keystoreHsmType = tmpKeystoreType;
  151.             }
  152.             else {
  153.                 throw new UtilsException(UNSUPPORTED_KEYSTORE_PREFIX+byokPropertyPrefix+id+"."+BYOKCostanti.PROPERTY_SUFFIX_LOCAL_KEYSTORE_TYPE+TYPE_SEPARATOR+tmpKeystoreType+"'");
  154.             }
  155.         }
  156.         String unsupportedError = UNSUPPORTED_KEYSTORE_PREFIX+byokPropertyPrefix+id+"."+BYOKCostanti.PROPERTY_SUFFIX_LOCAL_KEYSTORE_TYPE+TYPE_SEPARATOR+tmpKeystoreType+"' with "+
  157.                 byokPropertyPrefix+id+"."+BYOKCostanti.PROPERTY_SUFFIX_LOCAL_IMPL+" '";
  158.         switch (this.keystoreType) {
  159.         case JKS:
  160.         case PKCS12:
  161.         case PKCS11:
  162.         case JWK_SET:
  163.         case JCEKS:
  164.            
  165.             if(config!=null && config.getLocalConfig()!=null && config.getLocalConfig().isOpenSSLEngine()) {
  166.                 throw new UtilsException(unsupportedError+BYOKCostanti.PROPERTY_LOCAL_ENCRYPTION_ENGINE_OPENSSL+"'");
  167.             }
  168.            
  169.             initKeystoreEngine(id, p, config, byokPropertyPrefix);
  170.             break;
  171.         case KEY_PAIR:
  172.         case PUBLIC_KEY:
  173.         case SYMMETRIC_KEY:
  174.            
  175.             if(config!=null && config.getLocalConfig()!=null && config.getLocalConfig().isOpenSSLEngine()) {
  176.                 throw new UtilsException(unsupportedError+BYOKCostanti.PROPERTY_LOCAL_ENCRYPTION_ENGINE_OPENSSL+"'");
  177.             }
  178.            
  179.             initKeyEngine(id, p, byokPropertyPrefix);
  180.             break;
  181.         case PASSWORD_KEY_DERIVATION:
  182.            
  183.             if(BYOKCostanti.PROPERTY_LOCAL_ENCRYPTION_ENGINE_JOSE.equals(this.encryptionEngine)) {
  184.                 throw new UtilsException(unsupportedError+BYOKCostanti.PROPERTY_LOCAL_ENCRYPTION_ENGINE_JOSE+"'");
  185.             }
  186.            
  187.             initPasswordEngine(id, p, byokPropertyPrefix);
  188.             break;
  189.         default:
  190.             throw new UtilsException(UNSUPPORTED_KEYSTORE_PREFIX+byokPropertyPrefix+id+"."+BYOKCostanti.PROPERTY_SUFFIX_LOCAL_KEYSTORE_TYPE+TYPE_SEPARATOR+this.keystoreType+"'");
  191.         }
  192.     }
  193.     private void initKeystoreEngine(String id, Properties p, BYOKConfig config, String byokPropertyPrefix) throws UtilsException {
  194.        
  195.         if(KeystoreType.PKCS11.equals(this.keystoreType)) {
  196.             this.keystorePath = HSMUtils.KEYSTORE_HSM_STORE_PASSWORD_UNDEFINED;
  197.         }
  198.         else {
  199.             this.keystorePath = BYOKConfig.getProperty(id, p, BYOKCostanti.PROPERTY_SUFFIX_LOCAL_KEYSTORE_PATH, true, byokPropertyPrefix);
  200.         }
  201.        
  202.         if(KeystoreType.PKCS11.equals(this.keystoreType)) {
  203.             this.keystorePassword = HSMUtils.KEYSTORE_HSM_STORE_PASSWORD_UNDEFINED;
  204.         }
  205.         else if(!KeystoreType.JWK_SET.equals(this.keystoreType)) {
  206.             this.keystorePassword = BYOKConfig.getProperty(id, p, BYOKCostanti.PROPERTY_SUFFIX_LOCAL_KEYSTORE_PASSWORD, true, byokPropertyPrefix);
  207.         }
  208.        
  209.         initKeystoreKey(id, p, config, byokPropertyPrefix);
  210.     }
  211.     private void initKeystoreKey(String id, Properties p, BYOKConfig config, String byokPropertyPrefix) throws UtilsException {
  212.        
  213.         this.keyAlias = BYOKConfig.getProperty(id, p, BYOKCostanti.PROPERTY_SUFFIX_LOCAL_KEY_ALIAS, true, byokPropertyPrefix);
  214.                
  215.         if(KeystoreType.JCEKS.equals(this.keystoreType) || BYOKMode.UNWRAP.equals(config.getMode())) {
  216.            
  217.             this.keyPassword = BYOKConfig.getProperty(id, p, BYOKCostanti.PROPERTY_SUFFIX_LOCAL_KEY_PASSWORD, false, byokPropertyPrefix);
  218.            
  219.         }
  220.        
  221.         this.keyAlgorithm = BYOKConfig.getProperty(id, p, BYOKCostanti.PROPERTY_SUFFIX_LOCAL_KEY_ALGORITHM, true, byokPropertyPrefix);
  222.        
  223.     }
  224.    
  225.     private void initKeyEngine(String id, Properties p, String byokPropertyPrefix) throws UtilsException {
  226.        
  227.         this.keyInline = BYOKConfig.getProperty(id, p, BYOKCostanti.PROPERTY_SUFFIX_LOCAL_KEY_INLINE, false, byokPropertyPrefix);
  228.         this.keyPath = BYOKConfig.getProperty(id, p, BYOKCostanti.PROPERTY_SUFFIX_LOCAL_KEY_PATH, (this.keyInline==null || StringUtils.isEmpty(this.keyInline)), byokPropertyPrefix);
  229.        
  230.         this.keyEncoding = BYOKConfig.getProperty(id, p, BYOKCostanti.PROPERTY_SUFFIX_LOCAL_KEY_ENCODING, false, byokPropertyPrefix);
  231.         if(this.keyEncoding!=null && StringUtils.isNotEmpty(this.keyEncoding) &&
  232.             !BYOKCostanti.PROPERTY_LOCAL_ENCODING_BASE64.equals(this.keyEncoding) &&
  233.             !BYOKCostanti.PROPERTY_LOCAL_ENCODING_HEX.equals(this.keyEncoding)) {
  234.             throw new UtilsException(UNSUPPORTED_PROPERTY_PREFIX+byokPropertyPrefix+id+"."+BYOKCostanti.PROPERTY_SUFFIX_LOCAL_KEY_ENCODING+VALUE_SEPARATOR+this.keyEncoding+"'");
  235.         }
  236.        
  237.         this.keyAlgorithm = BYOKConfig.getProperty(id, p, BYOKCostanti.PROPERTY_SUFFIX_LOCAL_KEY_ALGORITHM, true, byokPropertyPrefix);
  238.        
  239.         if(KeystoreType.KEY_PAIR.equals(this.keystoreType)) {
  240.             this.publicKeyInline = BYOKConfig.getProperty(id, p, BYOKCostanti.PROPERTY_SUFFIX_LOCAL_PUBLIC_KEY_INLINE, false, byokPropertyPrefix);
  241.             this.publicKeyPath = BYOKConfig.getProperty(id, p, BYOKCostanti.PROPERTY_SUFFIX_LOCAL_PUBLIC_KEY_PATH, (this.publicKeyInline==null || StringUtils.isEmpty(this.publicKeyInline)), byokPropertyPrefix);
  242.            
  243.             this.publicKeyEncoding = BYOKConfig.getProperty(id, p, BYOKCostanti.PROPERTY_SUFFIX_LOCAL_PUBLIC_KEY_ENCODING, false, byokPropertyPrefix);
  244.             if(this.publicKeyEncoding!=null && StringUtils.isNotEmpty(this.publicKeyEncoding) &&
  245.                 !BYOKCostanti.PROPERTY_LOCAL_ENCODING_BASE64.equals(this.publicKeyEncoding) &&
  246.                 !BYOKCostanti.PROPERTY_LOCAL_ENCODING_HEX.equals(this.publicKeyEncoding)) {
  247.                 throw new UtilsException(UNSUPPORTED_PROPERTY_PREFIX+byokPropertyPrefix+id+"."+BYOKCostanti.PROPERTY_SUFFIX_LOCAL_PUBLIC_KEY_ENCODING+VALUE_SEPARATOR+this.publicKeyEncoding+"'");
  248.             }
  249.            
  250.             this.keyPassword = BYOKConfig.getProperty(id, p, BYOKCostanti.PROPERTY_SUFFIX_LOCAL_KEY_PASSWORD, false, byokPropertyPrefix);
  251.         }
  252.    
  253.     }
  254.    
  255.     private void initPasswordEngine(String id, Properties p, String byokPropertyPrefix) throws UtilsException {
  256.        
  257.         this.pw = BYOKConfig.getProperty(id, p, BYOKCostanti.PROPERTY_SUFFIX_LOCAL_PW, true, byokPropertyPrefix);
  258.        
  259.         this.pwType = BYOKConfig.getProperty(id, p, BYOKCostanti.PROPERTY_SUFFIX_LOCAL_PW_TYPE, false, byokPropertyPrefix);
  260.         if(this.pwType==null || StringUtils.isEmpty(this.pwType.trim())) {
  261.             this.pwType = BYOKCostanti.PROPERTY_LOCAL_PW_TYPE_DEFAULT;
  262.         }
  263.         else if(!BYOKCostanti.getLocalPasswordTypes().contains(this.pwType)) {
  264.             throw new UtilsException(UNSUPPORTED_PROPERTY_PREFIX+byokPropertyPrefix+id+"."+BYOKCostanti.PROPERTY_SUFFIX_LOCAL_PW_TYPE+VALUE_SEPARATOR+this.pwType+"'");
  265.         }
  266.        
  267.         this.pwIteration = BYOKConfig.getIntegerProperty(id, p, BYOKCostanti.PROPERTY_SUFFIX_LOCAL_PWD_ITERATION, false, byokPropertyPrefix);
  268.     }
  269.    
  270.    
  271.     public boolean isJavaEngine() {
  272.         return BYOKCostanti.PROPERTY_LOCAL_ENCRYPTION_ENGINE_JAVA.equals(this.encryptionEngine);
  273.     }
  274.     public boolean isJoseEngine() {
  275.         return BYOKCostanti.PROPERTY_LOCAL_ENCRYPTION_ENGINE_JOSE.equals(this.encryptionEngine);
  276.     }
  277.     public boolean isOpenSSLEngine() {
  278.         return BYOKCostanti.PROPERTY_LOCAL_ENCRYPTION_ENGINE_OPENSSL.equals(this.encryptionEngine);
  279.     }
  280.    
  281.     public KeystoreType getKeystoreType() {
  282.         return this.keystoreType;
  283.     }
  284.    
  285.     public String getKeystoreHsmType() {
  286.         return this.keystoreHsmType;
  287.     }

  288.     public String getKeystorePath() {
  289.         return this.keystorePath;
  290.     }
  291.    
  292.     public String getKeystorePassword() {
  293.         return this.keystorePassword;
  294.     }

  295.     public String getKeyPath() {
  296.         return this.keyPath;
  297.     }
  298.     public String getKeyInline() {
  299.         return this.keyInline;
  300.     }
  301.     public boolean isKeyBase64Encoding() {
  302.         return BYOKCostanti.PROPERTY_LOCAL_ENCODING_BASE64.equals(this.keyEncoding);
  303.     }
  304.     public boolean isKeyHexEncoding() {
  305.         return BYOKCostanti.PROPERTY_LOCAL_ENCODING_HEX.equals(this.keyEncoding);
  306.     }
  307.     public String getKeyEncoding() {
  308.         return this.keyEncoding;
  309.     }

  310.     public String getKeyAlgorithm() {
  311.         return this.keyAlgorithm;
  312.     }

  313.     public String getKeyAlias() {
  314.         return this.keyAlias;
  315.     }
  316.     public void setKeyAlias(String keyAlias) {
  317.         this.keyAlias = keyAlias;
  318.     }
  319.     public void generateKeyAlias() {
  320.         this.keyAlias = UUID.randomUUID().toString();
  321.     }
  322.    
  323.     public String getKeyPassword() {
  324.         return this.keyPassword;
  325.     }
  326.    
  327.     public String getKeyId() {
  328.         return this.keyId;
  329.     }
  330.    
  331.     public boolean isKeyWrap() {
  332.         return this.keyWrap;
  333.     }
  334.    
  335.     public String getPublicKeyPath() {
  336.         return this.publicKeyPath;
  337.     }
  338.     public String getPublicKeyInline() {
  339.         return this.publicKeyInline;
  340.     }
  341.     public boolean isPublicKeyBase64Encoding() {
  342.         return BYOKCostanti.PROPERTY_LOCAL_ENCODING_BASE64.equals(this.publicKeyEncoding);
  343.     }
  344.     public boolean isPublicKeyHexEncoding() {
  345.         return BYOKCostanti.PROPERTY_LOCAL_ENCODING_HEX.equals(this.publicKeyEncoding);
  346.     }
  347.     public String getPublicKeyEncoding() {
  348.         return this.publicKeyEncoding;
  349.     }
  350.    
  351.     public String getPassword() {
  352.         return this.pw;
  353.     }
  354.    
  355.     public String getPasswordType() {
  356.         return this.pwType;
  357.     }
  358.    
  359.     public Integer getPasswordIteration() {
  360.         return this.pwIteration;
  361.     }
  362.    
  363.     public String getContentAlgorithm() {
  364.         return this.contentAlgorithm;
  365.     }
  366.    
  367.     public boolean isBase64Encoding() {
  368.         return BYOKCostanti.PROPERTY_LOCAL_ENCODING_BASE64.equals(this.encoding);
  369.     }
  370.     public boolean isHexEncoding() {
  371.         return BYOKCostanti.PROPERTY_LOCAL_ENCODING_HEX.equals(this.encoding);
  372.     }
  373.     public String getEncoding() {
  374.         return this.encoding;
  375.     }

  376.     public boolean isJoseIncludeCert() {
  377.         return this.joseIncludeCert;
  378.     }
  379.     public boolean isJoseIncludePublicKey() {
  380.         return this.joseIncludePublicKey;
  381.     }
  382.     public boolean isJoseIncludeKeyId() {
  383.         return this.joseIncludeKeyId;
  384.     }
  385.     public boolean isJoseIncludeCertSha1() {
  386.         return this.joseIncludeCertSha1;
  387.     }
  388.     public boolean isJoseIncludeCertSha256() {
  389.         return this.joseIncludeCertSha256;
  390.     }
  391. }