AttributeAuthorityUtilities.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.core.token.attribute_authority;

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

  24. import org.apache.cxf.rt.security.rs.RSSecurityConstants;
  25. import org.openspcoop2.core.commons.DBUtils;
  26. import org.openspcoop2.core.config.GenericProperties;
  27. import org.openspcoop2.core.config.Property;
  28. import org.openspcoop2.core.mvc.properties.utils.DBPropertiesUtils;
  29. import org.openspcoop2.pdd.core.token.TokenException;
  30. import org.openspcoop2.security.message.constants.SecurityConstants;
  31. import org.openspcoop2.utils.certificate.KeystoreParams;
  32. import org.openspcoop2.utils.certificate.KeystoreType;

  33. /**    
  34.  * AttributeAuthorityUtilities
  35.  *
  36.  * @author Poli Andrea (poli@link.it)
  37.  * @author $Author$
  38.  * @version $Rev$, $Date$
  39.  */
  40. public class AttributeAuthorityUtilities {
  41.    
  42.     private AttributeAuthorityUtilities() {}
  43.    
  44.     public static KeystoreParams getRequestJwsKeystoreParams(GenericProperties gp) throws TokenException {
  45.         PolicyAttributeAuthority policy = AttributeAuthorityUtilities.convertTo(gp);
  46.         return getRequestJwsKeystoreParams(policy);
  47.     }
  48.     public static KeystoreParams getRequestJwsKeystoreParams(PolicyAttributeAuthority policy) throws TokenException {
  49.    
  50.         if(!policy.isRequestJws()) {
  51.             throw new TokenException("La configurazione nell'AttributeAuthority "+policy.getName()+" non definisce il tipo di richiesta come JWS");
  52.         }
  53.        
  54.         String keystoreType = policy.getRequestJwtSignKeystoreType();
  55.         if(keystoreType==null) {
  56.             throw new TokenException("JWS Signature keystore type undefined");
  57.         }
  58.         String keystoreFile = policy.getRequestJwtSignKeystoreFile();
  59.         if(keystoreFile==null) {
  60.             throw new TokenException("JWS Signature keystore file undefined");
  61.         }
  62.         String keystorePassword = policy.getRequestJwtSignKeystorePassword();
  63.         if(keystorePassword==null &&
  64.                 !SecurityConstants.KEYSTORE_TYPE_JWK_VALUE.equalsIgnoreCase(keystoreType) &&
  65.                 !SecurityConstants.KEYSTORE_TYPE_KEY_PAIR_VALUE.equalsIgnoreCase(keystoreType) &&
  66.                 !SecurityConstants.KEYSTORE_TYPE_PUBLIC_KEY_VALUE.equalsIgnoreCase(keystoreType)) {
  67.             boolean required = true;
  68.             if(KeystoreType.JKS.isType(keystoreType)) {
  69.                 required = DBUtils.isKeystoreJksPasswordRequired();
  70.             }
  71.             else if(KeystoreType.PKCS12.isType(keystoreType)) {
  72.                 required = DBUtils.isKeystorePkcs12PasswordRequired();
  73.             }
  74.             if(required) {
  75.                 throw new TokenException("JWS Signature keystore password undefined");
  76.             }
  77.         }
  78.         String keyAlias = policy.getRequestJwtSignKeyAlias();
  79.         if(keyAlias==null &&
  80.                 !SecurityConstants.KEYSTORE_TYPE_KEY_PAIR_VALUE.equalsIgnoreCase(keystoreType) &&
  81.                 !SecurityConstants.KEYSTORE_TYPE_PUBLIC_KEY_VALUE.equalsIgnoreCase(keystoreType)) {
  82.             throw new TokenException("JWS Signature key alias undefined");
  83.         }
  84.         String keyPassword = policy.getRequestJwtSignKeyPassword();
  85.         if(keyPassword==null &&
  86.                 !SecurityConstants.KEYSTORE_TYPE_JWK_VALUE.equalsIgnoreCase(keystoreType) &&
  87.                 !SecurityConstants.KEYSTORE_TYPE_KEY_PAIR_VALUE.equalsIgnoreCase(keystoreType) &&
  88.                 !SecurityConstants.KEYSTORE_TYPE_PUBLIC_KEY_VALUE.equalsIgnoreCase(keystoreType)) {
  89.             boolean required = true;
  90.             if(KeystoreType.JKS.isType(keystoreType)) {
  91.                 required = DBUtils.isKeystoreJksKeyPasswordRequired();
  92.             }
  93.             else if(KeystoreType.PKCS12.isType(keystoreType)) {
  94.                 required = DBUtils.isKeystorePkcs12KeyPasswordRequired();
  95.             }
  96.             if(required) {
  97.                 throw new TokenException("JWS Signature key password undefined");
  98.             }
  99.         }
  100.        
  101.         String keystoreByokPolicy = policy.getRequestJwtSignKeystoreByokPolicy();
  102.        
  103.         KeystoreParams keystoreParams = new KeystoreParams();
  104.         keystoreParams.setPath(keystoreFile);
  105.         keystoreParams.setType(keystoreType);
  106.         keystoreParams.setPassword(keystorePassword);
  107.         keystoreParams.setKeyAlias(keyAlias);
  108.         keystoreParams.setKeyPassword(keyPassword);
  109.         keystoreParams.setByokPolicy(keystoreByokPolicy);
  110.        
  111.         fillKeyPairParamters(keystoreParams, keystoreType, policy);
  112.        
  113.         return keystoreParams;
  114.        
  115.     }
  116.    
  117.     private static void fillKeyPairParamters(KeystoreParams keystoreParams, String keystoreType, PolicyAttributeAuthority policy) throws TokenException {
  118.         if(SecurityConstants.KEYSTORE_TYPE_KEY_PAIR_VALUE.equalsIgnoreCase(keystoreType)) {
  119.             String keystorePublicKeyFile = policy.getRequestJwtSignKeystoreFilePublicKey();
  120.             if(keystorePublicKeyFile==null) {
  121.                 throw new TokenException("JWT Signature public key file undefined");
  122.             }
  123.             keystoreParams.setKeyPairPublicKeyPath(keystorePublicKeyFile);
  124.         }
  125.        
  126.         if(SecurityConstants.KEYSTORE_TYPE_KEY_PAIR_VALUE.equalsIgnoreCase(keystoreType)
  127.                 ||
  128.             SecurityConstants.KEYSTORE_TYPE_PUBLIC_KEY_VALUE.equalsIgnoreCase(keystoreType)) {
  129.             String keyPairAlgorithm = policy.getRequestJwtSignKeystoreFileAlgorithm();
  130.             if(keyPairAlgorithm==null) {
  131.                 throw new TokenException("JWT Signature key pair algorithm undefined");
  132.             }
  133.             keystoreParams.setKeyPairAlgorithm(keyPairAlgorithm);
  134.         }
  135.     }
  136.    
  137.     public static KeystoreParams getResponseJwsKeystoreParams(GenericProperties gp) throws TokenException {
  138.         PolicyAttributeAuthority policy = AttributeAuthorityUtilities.convertTo(gp);
  139.         return getResponseJwsKeystoreParams(policy);
  140.     }
  141.     public static KeystoreParams getResponseJwsKeystoreParams(PolicyAttributeAuthority policy) throws TokenException {
  142.    
  143.         if(!policy.isResponseJws()) {
  144.             throw new TokenException("La configurazione nell'AttributeAuthority "+policy.getName()+" non definisce il tipo di risposta come JWS");
  145.         }
  146.         Properties p = policy.getProperties().get(org.openspcoop2.pdd.core.token.attribute_authority.Costanti.POLICY_VALIDAZIONE_JWS_VERIFICA_PROP_REF_ID);
  147.         if(p!=null && p.containsKey(SecurityConstants.JOSE_KEYSTORE_FILE)) {
  148.             KeystoreParams keystoreParams = new KeystoreParams();
  149.             keystoreParams.setPath(p.getProperty(SecurityConstants.JOSE_KEYSTORE_FILE));
  150.             String type = p.getProperty(SecurityConstants.JOSE_KEYSTORE_TYPE);
  151.             if(type==null) {
  152.                 type = KeystoreType.JKS.getNome();
  153.             }
  154.             keystoreParams.setType(type);
  155.             keystoreParams.setPassword(p.getProperty(SecurityConstants.JOSE_KEYSTORE_PSWD));
  156.             keystoreParams.setKeyAlias(p.getProperty(SecurityConstants.JOSE_KEYSTORE_KEY_ALIAS));
  157.             keystoreParams.setKeyPassword(p.getProperty(RSSecurityConstants.RSSEC_KEY_PSWD));
  158.            
  159.             fillKeyPairParamters(keystoreParams, type, p);
  160.            
  161.             return keystoreParams;
  162.         }
  163.         return null;
  164.        
  165.     }
  166.     private static void fillKeyPairParamters(KeystoreParams keystoreParams, String type, Properties p) throws TokenException {
  167.         if(SecurityConstants.KEYSTORE_TYPE_KEY_PAIR_VALUE.equalsIgnoreCase(type)) {
  168.             String keystorePublicKeyFile = p.getProperty(SecurityConstants.JOSE_KEYSTORE_PUBLIC_KEY);
  169.             if(keystorePublicKeyFile==null) {
  170.                 throw new TokenException("Public key file undefined");
  171.             }
  172.             keystoreParams.setKeyPairPublicKeyPath(keystorePublicKeyFile);
  173.         }
  174.        
  175.         if(SecurityConstants.KEYSTORE_TYPE_KEY_PAIR_VALUE.equalsIgnoreCase(type)
  176.                 ||
  177.             SecurityConstants.KEYSTORE_TYPE_PUBLIC_KEY_VALUE.equalsIgnoreCase(type)) {
  178.             String keyPairAlgorithm = p.getProperty(SecurityConstants.JOSE_KEYSTORE_KEY_ALGORITHM);
  179.             if(keyPairAlgorithm==null) {
  180.                 throw new TokenException("Key pair algorithm undefined");
  181.             }
  182.             keystoreParams.setKeyPairAlgorithm(keyPairAlgorithm);
  183.         }
  184.     }
  185.    
  186.     public static PolicyAttributeAuthority convertTo(GenericProperties gp) throws TokenException {
  187.        
  188.         PolicyAttributeAuthority policy = new PolicyAttributeAuthority();
  189.         policy.setName(gp.getNome());
  190.         policy.setDescrizione(gp.getDescrizione());
  191.        
  192.         HashMap<String, String> properties = new HashMap<>();
  193.         for (Property pConfig : gp.getPropertyList()) {
  194.             properties.put(pConfig.getNome(), pConfig.getValore());
  195.         }
  196.         try {
  197.             Map<String, Properties> multiProperties = DBPropertiesUtils.toMultiMap(properties);
  198.             policy.setProperties(multiProperties);
  199.         }catch(Exception e) {
  200.             throw new TokenException(e.getMessage(),e);
  201.         }
  202.        
  203.         return policy;

  204.     }
  205. }