CryptConfig.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.crypt;

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

  26. import org.apache.commons.lang.StringUtils;
  27. import org.openspcoop2.utils.UtilsException;
  28. import org.openspcoop2.utils.resources.Charset;

  29. /**
  30.  * PasswordCryptConfig
  31.  *
  32.  * @author Andrea Poli (poli@link.it)
  33.  * @author $Author$
  34.  * @version $Rev$, $Date$
  35.  */

  36. public class CryptConfig implements Serializable {

  37.     /**
  38.      *
  39.      */
  40.     private static final long serialVersionUID = 1L;

  41.     // http://www.jasypt.org/howtoencryptuserpasswords.html
  42.    
  43.     protected static final String CRYPT_SPECIAL_PATH_PLAIN = "PLAIN";
  44.    
  45.     protected static final String CRYPT_TYPE = "passwordEncrypt.type";
  46.     protected static final String CRYPT_CUSTOM_TYPE = "passwordEncrypt.customType";
  47.    
  48.     private static final String CRYPT_CHARSET_NAME = "passwordEncrypt.charsetName";
  49.    
  50.     private static final String CRYPT_SALT_LENGTH = "passwordEncrypt.salt.length";
  51.     private static final String CRYPT_SALT_SECURE_RANDOM = "passwordEncrypt.salt.secureRandom";
  52.     private static final String CRYPT_SALT_SECURE_RANDOM_ALGORITHM = "passwordEncrypt.salt.secureRandomAlgorithm";
  53.    
  54.     private static final String CRYPT_DIGEST_ALGORITHM = "passwordEncrypt.digestAlgorithm";
  55.     private static final String CRYPT_ITERATION = "passwordEncrypt.iteration";
  56.    
  57.     private static final String CRYPT_BASE64_ENCODING = "passwordEncrypt.base64Encoding";

  58.     private static final String CRYPT_BACKWARD_COMPATIBILITY = "passwordEncrypt.backwardCompatibility";
  59.    
  60.     private CryptType cryptType = null;
  61.    
  62.     private String cryptCustomType = null;
  63.    
  64.     private String digestAlgorithm = null;

  65.     private String charsetName = Charset.UTF_8.getValue(); // per la password
  66.    
  67.     private Integer iteration = null;
  68.    
  69.     private Integer saltLength = null;
  70.     private boolean useSecureRandom = true; // default
  71.     private String algorithmSecureRandom = null;
  72.    
  73.     private boolean useBase64Encoding = true; // in alternativa hex
  74.    
  75.     private boolean backwardCompatibility = false;


  76.     public CryptConfig() {}
  77.    
  78.     public CryptConfig(String resource) throws UtilsException{
  79.         this(resource, true);
  80.     }
  81.     public CryptConfig(String resource, boolean useDefaultIfNotFound) throws UtilsException{
  82.        
  83.         if(CRYPT_SPECIAL_PATH_PLAIN.equals(resource)) {
  84.             this.cryptType = CryptType.PLAIN;
  85.             return;
  86.         }
  87.        
  88.         InputStream is = null;
  89.         try{
  90.             File f = new File(resource);
  91.             if(f.exists()){
  92.                 is = new FileInputStream(f);
  93.             }
  94.             else{
  95.                 is = PasswordVerifier.class.getResourceAsStream(resource);
  96.             }
  97.             if(is==null) {
  98.                 is = PasswordVerifier.class.getResourceAsStream("/org/openspcoop2/utils/crypt/consolePassword.properties");
  99.             }
  100.             if(is!=null){
  101.                 Properties p = new Properties();
  102.                 p.load(is);
  103.                 this._init(p);
  104.             }
  105.             else{
  106.                 throw new Exception("Resource ["+resource+"] not found");
  107.             }
  108.         }catch(Exception e){
  109.             throw new UtilsException(e.getMessage(),e);
  110.         }finally{
  111.             try{
  112.                 if(is!=null){
  113.                     is.close();
  114.                 }
  115.             }catch(Exception eClose){
  116.                 // close
  117.             }
  118.         }
  119.     }
  120.     public CryptConfig(InputStream is) throws UtilsException{
  121.         try{
  122.             Properties p = new Properties();
  123.             p.load(is);
  124.             this._init(p);
  125.         }catch(Exception e){
  126.             throw new UtilsException(e.getMessage(),e);
  127.         }
  128.     }
  129.     public CryptConfig(Properties p) throws UtilsException {
  130.         this._init(p);
  131.     }
  132.     private void _init(Properties p) throws UtilsException {
  133.        
  134.         String tipo = CryptConfig.getProperty(p, CryptConfig.CRYPT_TYPE, false);
  135.         if(StringUtils.isNotEmpty(tipo)) {
  136.             CryptType cryptType = null;
  137.             try {
  138.                 cryptType = CryptType.valueOf(tipo);
  139.             }catch(Throwable e) {
  140.                 throw new UtilsException("Property '"+CryptConfig.CRYPT_TYPE+"' value '"+tipo+"' uncorrect: "+e.getMessage(), e);
  141.             }
  142.             this.cryptType = cryptType;
  143.         }
  144.         else {
  145.             String className = CryptConfig.getProperty(p, CryptConfig.CRYPT_CUSTOM_TYPE, false);
  146.             if(StringUtils.isEmpty(className)) {
  147.                 throw new UtilsException("Property '"+CryptConfig.CRYPT_TYPE+"' or '"+CryptConfig.CRYPT_CUSTOM_TYPE+"' are required");
  148.             }
  149.             this.cryptCustomType = className;
  150.         }
  151.        
  152.         String charset = getProperty(p, CRYPT_CHARSET_NAME, false);
  153.         if(StringUtils.isNotEmpty(charset)) {
  154.             this.charsetName = charset;
  155.         }
  156.        
  157.         String saltLength = getProperty(p, CRYPT_SALT_LENGTH, false);
  158.         if(StringUtils.isNotEmpty(saltLength)) {
  159.             try {
  160.                 this.saltLength = Integer.valueOf(saltLength);
  161.             }catch(Throwable e) {
  162.                 throw new UtilsException("Property '"+CRYPT_SALT_LENGTH+"' value '"+saltLength+"' uncorrect: "+e.getMessage(), e);
  163.             }
  164.         }
  165.        
  166.         String secureRandom = getProperty(p, CRYPT_SALT_SECURE_RANDOM, false);
  167.         if(StringUtils.isNotEmpty(secureRandom)) {
  168.             try {
  169.                 this.useSecureRandom = Boolean.valueOf(secureRandom);
  170.             }catch(Throwable e) {
  171.                 throw new UtilsException("Property '"+CRYPT_SALT_SECURE_RANDOM+"' value '"+secureRandom+"' uncorrect: "+e.getMessage(), e);
  172.             }
  173.         }
  174.        
  175.         String secureRandomAlgo = getProperty(p, CRYPT_SALT_SECURE_RANDOM_ALGORITHM, false);
  176.         if(StringUtils.isNotEmpty(secureRandomAlgo)) {
  177.             this.algorithmSecureRandom = secureRandomAlgo;
  178.         }
  179.        
  180.        
  181.         String digest = getProperty(p, CRYPT_DIGEST_ALGORITHM, false);
  182.         if(StringUtils.isNotEmpty(digest)) {
  183.             this.digestAlgorithm = digest;
  184.         }
  185.        
  186.         String iteration = getProperty(p, CRYPT_ITERATION, false);
  187.         if(StringUtils.isNotEmpty(iteration)) {
  188.             try {
  189.                 this.iteration = Integer.valueOf(iteration);
  190.             }catch(Throwable e) {
  191.                 throw new UtilsException("Property '"+CRYPT_ITERATION+"' value '"+iteration+"' uncorrect: "+e.getMessage(), e);
  192.             }
  193.         }
  194.        
  195.        
  196.         String base64 = getProperty(p, CRYPT_BASE64_ENCODING, false);
  197.         if(StringUtils.isNotEmpty(base64)) {
  198.             try {
  199.                 this.useBase64Encoding = Boolean.valueOf(base64);
  200.             }catch(Throwable e) {
  201.                 throw new UtilsException("Property '"+CRYPT_BASE64_ENCODING+"' value '"+base64+"' uncorrect: "+e.getMessage(), e);
  202.             }
  203.         }
  204.        
  205.        
  206.         String back = getProperty(p, CRYPT_BACKWARD_COMPATIBILITY, false);
  207.         if(StringUtils.isNotEmpty(back)) {
  208.             try {
  209.                 this.backwardCompatibility = Boolean.valueOf(back);
  210.             }catch(Throwable e) {
  211.                 throw new UtilsException("Property '"+CRYPT_BACKWARD_COMPATIBILITY+"' value '"+back+"' uncorrect: "+e.getMessage(), e);
  212.             }
  213.         }
  214.     }
  215.    
  216.     protected static String getProperty(Properties p, String name, boolean  required) throws UtilsException {
  217.         String pValue = p.getProperty(name);
  218.         if(pValue==null) {
  219.             if(required) {
  220.                 throw new UtilsException("Property '"+name+"' not found");
  221.             }
  222.             return null;
  223.         }
  224.         else {
  225.             return pValue.trim();
  226.         }
  227.     }
  228.    
  229.     public CryptType getCryptType() {
  230.         return this.cryptType;
  231.     }

  232.     public void setCryptType(CryptType cryptType) {
  233.         this.cryptType = cryptType;
  234.     }

  235.     public String getCryptCustomType() {
  236.         return this.cryptCustomType;
  237.     }

  238.     public void setCryptCustomType(String cryptCustomType) {
  239.         this.cryptCustomType = cryptCustomType;
  240.     }
  241.    
  242.     public boolean isUseBase64Encoding() {
  243.         return this.useBase64Encoding;
  244.     }

  245.     public void setUseBase64Encoding(boolean useBase64Encoding) {
  246.         this.useBase64Encoding = useBase64Encoding;
  247.     }

  248.     public String getDigestAlgorithm() {
  249.         return this.digestAlgorithm;
  250.     }

  251.     public void setDigestAlgorithm(String digestAlgorithm) {
  252.         this.digestAlgorithm = digestAlgorithm;
  253.     }

  254.     public String getCharsetName() {
  255.         return this.charsetName;
  256.     }

  257.     public void setCharsetName(String charsetName) {
  258.         this.charsetName = charsetName;
  259.     }

  260.     public Integer getIteration() {
  261.         return this.iteration;
  262.     }

  263.     public void setIteration(Integer iteration) {
  264.         this.iteration = iteration;
  265.     }
  266.    
  267.     public Integer getSaltLength() {
  268.         return this.saltLength;
  269.     }

  270.     public void setSaltLength(Integer saltLength) {
  271.         this.saltLength = saltLength;
  272.     }

  273.     public boolean isUseSecureRandom() {
  274.         return this.useSecureRandom;
  275.     }

  276.     public void setUseSecureRandom(boolean useSecureRandom) {
  277.         this.useSecureRandom = useSecureRandom;
  278.     }

  279.     public String getAlgorithmSecureRandom() {
  280.         return this.algorithmSecureRandom;
  281.     }

  282.     public void setAlgorithmSecureRandom(String algorithmSecureRandom) {
  283.         this.algorithmSecureRandom = algorithmSecureRandom;
  284.     }

  285.     public boolean isBackwardCompatibility() {
  286.         return this.backwardCompatibility;
  287.     }

  288.     public void setBackwardCompatibility(boolean backwardCompatibility) {
  289.         this.backwardCompatibility = backwardCompatibility;
  290.     }
  291. }