SAMLUtilities.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.security.message.saml;

  21. import java.util.ArrayList;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.Properties;

  25. import org.joda.time.DateTime;
  26. import org.openspcoop2.core.commons.DBUtils;
  27. import org.openspcoop2.security.SecurityException;
  28. import org.openspcoop2.security.keystore.KeystoreConstants;
  29. import org.openspcoop2.security.keystore.MerlinProvider;
  30. import org.openspcoop2.security.message.MessageSecurityContext;
  31. import org.openspcoop2.security.message.constants.SecurityConstants;
  32. import org.openspcoop2.utils.certificate.KeystoreType;



  33. /**
  34.  * SAMLUtilities
  35.  *  
  36.  * @author Andrea Poli (apoli@link.it)
  37.  * @author $Author$
  38.  * @version $Rev$, $Date$
  39.  */
  40. public class SAMLUtilities {
  41.    
  42.     private SAMLUtilities() {}

  43.     public static DateTime minutesOperator(DateTime dateTime,Integer intValue){
  44.         if(intValue!=null){
  45.             DateTime dateTimeRes = null;
  46.             if(intValue.intValue() == 0){
  47.                 dateTimeRes = dateTime;
  48.             }
  49.             else if(intValue.intValue() > 0){
  50.                 dateTimeRes = dateTime.plusMinutes(intValue.intValue());
  51.             }
  52.             else {/**if(intValue.intValue() < 0){*/
  53.                 dateTimeRes = dateTime.minusMinutes(intValue.intValue()*-1);
  54.             }
  55.             /**else {
  56.                 dateTimeRes = dateTime;
  57.             }*/
  58.             return dateTimeRes;
  59.         }
  60.         else {
  61.             return dateTime;
  62.         }
  63.     }

  64.     public static void injectSignaturePropRefIdIntoSamlConfig(Map<String,Object> wssProperties) throws SecurityException {
  65.        
  66.         if (wssProperties != null && wssProperties.size() > 0) {
  67.            
  68.             // preprocess per saml
  69.             List<String> keys = new ArrayList<>();
  70.             keys.addAll(wssProperties.keySet());
  71.             for (String key : keys) {
  72.                 Object oValue = wssProperties.get(key);
  73.                 String value = null;
  74.                 if(oValue instanceof String) {
  75.                     value = (String) oValue;
  76.                 }
  77.                
  78.                 if(SecurityConstants.SIGNATURE_PARTS.equals(key)) {
  79.                     if(value!=null && value.contains(SecurityConstants.SAML_NAMESPACE_TEMPLATE)) {
  80.                         String samlVersion = null;
  81.                         if(wssProperties.containsKey(SecurityConstants.SAML_PROF_REF_ID)) {
  82.                             Object o = wssProperties.get(SecurityConstants.SAML_PROF_REF_ID);
  83.                             if(o instanceof Properties) {
  84.                                 Properties samlConfig = (Properties) o;
  85.                                 if(samlConfig.containsKey(SAMLBuilderConfigConstants.SAML_CONFIG_BUILDER_VERSION)) {
  86.                                     samlVersion = samlConfig.getProperty(SAMLBuilderConfigConstants.SAML_CONFIG_BUILDER_VERSION);
  87.                                 }
  88.                             }
  89.                         }
  90.                         if(samlVersion==null) {
  91.                             throw new SecurityException("SAML version undefined");
  92.                         }
  93.                         if(SecurityConstants.SAML_VERSION_XMLCONFIG_ID_VALUE_20.equals(samlVersion)) {
  94.                             while(value.contains(SecurityConstants.SAML_NAMESPACE_TEMPLATE)) {
  95.                                 value = value.replace(SecurityConstants.SAML_NAMESPACE_TEMPLATE, SAMLConstants.SAML_20_NAMESPACE);
  96.                             }
  97.                         }
  98.                         else {
  99.                             while(value.contains(SecurityConstants.SAML_NAMESPACE_TEMPLATE)) {
  100.                                 value = value.replace(SecurityConstants.SAML_NAMESPACE_TEMPLATE, SAMLConstants.SAML_11_NAMESPACE);
  101.                             }
  102.                         }
  103.                         wssProperties.remove(key);
  104.                         wssProperties.put(key, value);
  105.                     }
  106.                 }
  107.                 else if(
  108.                         (SecurityConstants.SAML_SIGNATURE_PARAM_CONVERTO_INTO_SAML_CONFIG.equals(key) ||
  109.                         SecurityConstants.SAML_SIGNATURE_PARAM_CONVERTO_INTO_SAML_CONFIG_HOLDER_OF_KEY.equals(key))
  110.                         &&
  111.                         (value!=null && wssProperties.containsKey(value))
  112.                         ){
  113.                     injectSignaturePropRefIdIntoSamlConfig(wssProperties, key, value);
  114.                 }
  115.             }
  116.         }
  117.     }
  118.    
  119.     private static void injectSignaturePropRefIdIntoSamlConfig(Map<String,Object> wssProperties, String key, String value) throws SecurityException {
  120.         // raccolgo parametri
  121.         Properties signaturePropRefiId = null;
  122.         String signatureAlias = null;
  123.         String signaturePassword = null;
  124.         if(wssProperties.containsKey(SecurityConstants.SIGNATURE_PROPERTY_REF_ID)) {
  125.             Object o = wssProperties.get(SecurityConstants.SIGNATURE_PROPERTY_REF_ID);
  126.             if(o instanceof Properties) {
  127.                 signaturePropRefiId = (Properties) o;
  128.             }
  129.         }
  130.         if(wssProperties.containsKey(SecurityConstants.SIGNATURE_USER)) {
  131.             Object o = wssProperties.get(SecurityConstants.SIGNATURE_USER);
  132.             if(o instanceof String) {
  133.                 signatureAlias = (String) o;
  134.             }
  135.         }
  136.         else if(wssProperties.containsKey(SecurityConstants.USER)) {
  137.             Object o = wssProperties.get(SecurityConstants.USER);
  138.             if(o instanceof String) {
  139.                 signatureAlias = (String) o;
  140.             }
  141.         }
  142.         if(wssProperties.containsKey(SecurityConstants.SIGNATURE_PASSWORD)) {
  143.             Object o = wssProperties.get(SecurityConstants.SIGNATURE_PASSWORD);
  144.             if(o instanceof String) {
  145.                 signaturePassword = (String) o;
  146.             }
  147.         }
  148.        
  149.         // algoritmi
  150.         String signatureAlgorithm = null;
  151.         String signatureDigestAlgorithm = null;
  152.         String signatureC14nAlgorithmExclusive = null;
  153.         if(SecurityConstants.SAML_SIGNATURE_PARAM_CONVERTO_INTO_SAML_CONFIG.equals(key)) {
  154.             if(wssProperties.containsKey(SecurityConstants.SIGNATURE_ALGORITHM)) {
  155.                 Object o = wssProperties.get(SecurityConstants.SIGNATURE_ALGORITHM);
  156.                 if(o instanceof String) {
  157.                     signatureAlgorithm = (String) o;
  158.                 }
  159.             }
  160.             if(wssProperties.containsKey(SecurityConstants.SIGNATURE_DIGEST_ALGORITHM)) {
  161.                 Object o = wssProperties.get(SecurityConstants.SIGNATURE_DIGEST_ALGORITHM);
  162.                 if(o instanceof String) {
  163.                     signatureDigestAlgorithm = (String) o;
  164.                 }
  165.             }
  166.             if(wssProperties.containsKey(SecurityConstants.SIGNATURE_C14N_ALGORITHM)) {
  167.                 Object o = wssProperties.get(SecurityConstants.SIGNATURE_C14N_ALGORITHM);
  168.                 if(o instanceof String) {
  169.                     signatureC14nAlgorithmExclusive = (String) o;
  170.                 }
  171.             }
  172.         }
  173.        
  174.         // Prelevo saml comfig
  175.         Object o = wssProperties.get(value);
  176.         if(o instanceof Properties) {
  177.             Properties samlConfig = (Properties) o;
  178.            
  179.             String type = signaturePropRefiId.getProperty("org.apache.ws.security.crypto.merlin.keystore.type");
  180.             if(type!=null) {
  181.                 if(SecurityConstants.SAML_SIGNATURE_PARAM_CONVERTO_INTO_SAML_CONFIG.equals(key)) {
  182.                     samlConfig.put(SAMLBuilderConfigConstants.SAML_CONFIG_BUILDER_SIGN_ASSERTION_CRYPTO_PROP_KEYSTORE_TYPE, type);
  183.                 }
  184.                 else {
  185.                     samlConfig.put(SAMLBuilderConfigConstants.SAML_CONFIG_BUILDER_SUBJECT_CONFIRMATION_METHOD_HOLDER_OF_KEY_CRYPTO_PROPERTIES_KEYSTORE_TYPE, type);
  186.                 }
  187.             }
  188.             String file = signaturePropRefiId.getProperty("org.apache.ws.security.crypto.merlin.file");
  189.             if(file==null) {
  190.                 throw new SecurityException("Keystore file in signaturePropRefId undefined");
  191.             }
  192.             if(SecurityConstants.SAML_SIGNATURE_PARAM_CONVERTO_INTO_SAML_CONFIG.equals(key)) {
  193.                 samlConfig.put(SAMLBuilderConfigConstants.SAML_CONFIG_BUILDER_SIGN_ASSERTION_CRYPTO_PROP_KEYSTORE_FILE, file);
  194.             }
  195.             else {
  196.                 samlConfig.put(SAMLBuilderConfigConstants.SAML_CONFIG_BUILDER_SUBJECT_CONFIRMATION_METHOD_HOLDER_OF_KEY_CRYPTO_PROPERTIES_KEYSTORE_FILE, file);
  197.             }
  198.            
  199.             String byokPolicy = signaturePropRefiId.getProperty("org.apache.ws.security.crypto.merlin.file"+MerlinProvider.SUFFIX_BYOK);
  200.             if(byokPolicy!=null) {
  201.                 if(SecurityConstants.SAML_SIGNATURE_PARAM_CONVERTO_INTO_SAML_CONFIG.equals(key)) {
  202.                     samlConfig.put(SAMLBuilderConfigConstants.SAML_CONFIG_BUILDER_SIGN_ASSERTION_CRYPTO_PROP_KEYSTORE_BYOK_POLICY, byokPolicy);
  203.                 }
  204.                 else {
  205.                     samlConfig.put(SAMLBuilderConfigConstants.SAML_CONFIG_BUILDER_SUBJECT_CONFIRMATION_METHOD_HOLDER_OF_KEY_CRYPTO_PROPERTIES_KEYSTORE_BYOK_POLICY, byokPolicy);
  206.                 }
  207.             }
  208.            
  209.             String password = signaturePropRefiId.getProperty("org.apache.ws.security.crypto.merlin.keystore.password");
  210.             if(password==null && MessageSecurityContext.isPasswordRequired(signaturePropRefiId, false)) {
  211.                 throw new SecurityException("Keystore password in signaturePropRefId undefined");
  212.             }
  213.             if(password!=null) {
  214.                 if(SecurityConstants.SAML_SIGNATURE_PARAM_CONVERTO_INTO_SAML_CONFIG.equals(key)) {
  215.                     samlConfig.put(SAMLBuilderConfigConstants.SAML_CONFIG_BUILDER_SIGN_ASSERTION_CRYPTO_PROP_KEYSTORE_PASSWORD, password);
  216.                 }
  217.                 else {
  218.                     samlConfig.put(SAMLBuilderConfigConstants.SAML_CONFIG_BUILDER_SUBJECT_CONFIRMATION_METHOD_HOLDER_OF_KEY_CRYPTO_PROPERTIES_KEYSTORE_PASSWORD, password);
  219.                 }
  220.             }
  221.            
  222.             if(signatureAlias==null) {
  223.                 throw new SecurityException("Signature alias undefined");
  224.             }
  225.             if(SecurityConstants.SAML_SIGNATURE_PARAM_CONVERTO_INTO_SAML_CONFIG.equals(key)) {
  226.                 samlConfig.put(SAMLBuilderConfigConstants.SAML_CONFIG_BUILDER_SIGN_ASSERTION_KEY_NAME, signatureAlias);
  227.             }
  228.             else {
  229.                 samlConfig.put(SAMLBuilderConfigConstants.SAML_CONFIG_BUILDER_SUBJECT_CONFIRMATION_METHOD_HOLDER_OF_KEY_CRYPTO_ALIAS, signatureAlias);
  230.             }
  231.            
  232.             if(SecurityConstants.SAML_SIGNATURE_PARAM_CONVERTO_INTO_SAML_CONFIG.equals(key)) {
  233.                 if(signaturePassword==null && MessageSecurityContext.isPasswordRequired(signaturePropRefiId, true)) {
  234.                     throw new SecurityException("Signature password undefined");
  235.                 }
  236.                 if(signaturePassword!=null) {
  237.                     samlConfig.put(SAMLBuilderConfigConstants.SAML_CONFIG_BUILDER_SIGN_ASSERTION_KEY_PASSWORD, signaturePassword);
  238.                 }
  239.                
  240.                 if(signatureAlgorithm!=null) {
  241.                     samlConfig.put(SAMLBuilderConfigConstants.SAML_CONFIG_BUILDER_SIGN_ASSERTION_SIGNATURE_ALGORITHM, signatureAlgorithm);
  242.                 }
  243.                 if(signatureDigestAlgorithm!=null) {
  244.                     samlConfig.put(SAMLBuilderConfigConstants.SAML_CONFIG_BUILDER_SIGN_ASSERTION_SIGNATURE_DIGEST_ALGORITHM, signatureDigestAlgorithm);
  245.                 }
  246.                 if(signatureC14nAlgorithmExclusive!=null) {
  247.                     samlConfig.put(SAMLBuilderConfigConstants.SAML_CONFIG_BUILDER_SIGN_ASSERTION_SIGNATURE_CANONICALIZATION_ALGORITHM, signatureC14nAlgorithmExclusive);
  248.                 }
  249.             }
  250.         }
  251.     }
  252.    
  253.     public static Properties convertToMerlinProperties(String keystoreType,String keystoreFile, String keystorePassword, boolean useKeystoreCache, String byokPolicy) throws SecurityException {
  254.         Properties p = new Properties();
  255.         if(useKeystoreCache) {
  256.             p.put(KeystoreConstants.PROPERTY_PROVIDER, KeystoreConstants.PROVIDER_GOVWAY);
  257.         }
  258.         else {
  259.             p.put(KeystoreConstants.PROPERTY_PROVIDER, KeystoreConstants.OLD_PROVIDER_DEFAULT);
  260.         }
  261.         if(keystoreType!=null) {
  262.             p.put(KeystoreConstants.PROPERTY_KEYSTORE_TYPE, keystoreType);
  263.         }
  264.         if(keystoreFile==null) {
  265.             throw new SecurityException("Keystore file undefined");
  266.         }
  267.         p.put(KeystoreConstants.PROPERTY_KEYSTORE_PATH, keystoreFile);
  268.         if(keystorePassword==null) {
  269.             boolean required = true;
  270.             if(KeystoreType.JKS.isType(keystoreType)) {
  271.                 required = DBUtils.isKeystoreJksPasswordRequired();
  272.             }
  273.             else if(KeystoreType.PKCS12.isType(keystoreType)) {
  274.                 required = DBUtils.isKeystorePkcs12PasswordRequired();
  275.             }
  276.             if(required) {
  277.                 throw new SecurityException("Keystore password undefined");
  278.             }
  279.         }
  280.         if(keystorePassword!=null) {
  281.             p.put(KeystoreConstants.PROPERTY_KEYSTORE_PASSWORD, keystorePassword);
  282.         }
  283.         if(byokPolicy!=null) {
  284.             p.put(KeystoreConstants.PROPERTY_KEYSTORE_PATH_BYOK, byokPolicy);
  285.         }
  286.         return p;
  287.     }
  288. }