SecurityProvider.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.xml;

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

  25. import org.openspcoop2.core.mvc.properties.provider.ProviderException;
  26. import org.openspcoop2.core.mvc.properties.provider.ProviderValidationException;
  27. import org.openspcoop2.security.message.constants.EncryptionAlgorithm;
  28. import org.openspcoop2.security.message.constants.EncryptionC14NAlgorithm;
  29. import org.openspcoop2.security.message.constants.EncryptionDigestAlgorithm;
  30. import org.openspcoop2.security.message.constants.EncryptionKeyTransportAlgorithm;
  31. import org.openspcoop2.security.message.constants.EncryptionSymmetricKeyWrapAlgorithm;
  32. import org.openspcoop2.security.message.constants.KeyAlgorithm;
  33. import org.openspcoop2.security.message.constants.SignatureAlgorithm;
  34. import org.openspcoop2.security.message.constants.SignatureC14NAlgorithm;
  35. import org.openspcoop2.security.message.constants.SignatureDigestAlgorithm;
  36. import org.openspcoop2.security.message.utils.AbstractSecurityProvider;

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

  45.     @Override
  46.     public void validate(Map<String, Properties> mapProperties) throws ProviderException, ProviderValidationException {
  47.         super.validate(mapProperties);
  48.     }
  49.    
  50.     @Override
  51.     public List<String> getValues(String id) throws ProviderException {
  52.         if(XMLCostanti.ID_SIGNATURE_ALGORITHM.equals(id)) {
  53.             List<String> l = new ArrayList<>();
  54.             SignatureAlgorithm [] tmp = SignatureAlgorithm.values();
  55.             for (int i = 0; i < tmp.length; i++) {
  56.                 l.add(tmp[i].getUri());
  57.             }
  58.             return l;
  59.         }
  60.         else if(XMLCostanti.ID_SIGNATURE_DIGEST_ALGORITHM.equals(id)) {
  61.             List<String> l = new ArrayList<>();
  62.             SignatureDigestAlgorithm [] tmp = SignatureDigestAlgorithm.values();
  63.             for (int i = 0; i < tmp.length; i++) {
  64.                 l.add(tmp[i].getUri());
  65.             }
  66.             return l;
  67.         }
  68.         else if(XMLCostanti.ID_SIGNATURE_C14N_ALGORITHM.equals(id)) {
  69.             List<String> l = new ArrayList<>();
  70.             SignatureC14NAlgorithm [] tmp = SignatureC14NAlgorithm.values();
  71.             for (int i = 0; i < tmp.length; i++) {
  72.                 l.add(tmp[i].getUri());
  73.             }
  74.             return l;
  75.         }
  76.         else if(XMLCostanti.ID_SIGNATURE_C14N_ALGORITHM_EXCLUSIVE.equals(id)) {
  77.             List<String> l = new ArrayList<>();
  78.             SignatureC14NAlgorithm [] tmp = SignatureC14NAlgorithm.values();
  79.             for (int i = 0; i < tmp.length; i++) {
  80.                 if(tmp[i].name().startsWith("EXCLUSIVE")) {
  81.                     l.add(tmp[i].getUri());
  82.                 }
  83.             }
  84.             return l;
  85.         }
  86.         else if(XMLCostanti.ID_SIGNATURE_C14N_ALGORITHM_INCLUSIVE.equals(id)) {
  87.             List<String> l = new ArrayList<>();
  88.             SignatureC14NAlgorithm [] tmp = SignatureC14NAlgorithm.values();
  89.             for (int i = 0; i < tmp.length; i++) {
  90.                 if(tmp[i].name().startsWith("INCLUSIVE")) {
  91.                     l.add(tmp[i].getUri());
  92.                 }
  93.             }
  94.             return l;
  95.         }
  96.        
  97.         else if(XMLCostanti.ID_ENCRYPT_KEY_ALGORITHM.equals(id)) {
  98.             List<String> l = new ArrayList<>();
  99.             KeyAlgorithm [] tmp = KeyAlgorithm.values();
  100.             for (int i = 0; i < tmp.length; i++) {
  101.                 l.add(tmp[i].name());
  102.             }
  103.             return l;
  104.         }
  105.         else if(XMLCostanti.ID_ENCRYPT_SYMMETRIC_KEY_WRAP_ALGORITHM.equals(id)) {
  106.             List<String> l = new ArrayList<>();
  107.             EncryptionSymmetricKeyWrapAlgorithm [] tmp = EncryptionSymmetricKeyWrapAlgorithm.values();
  108.             for (int i = 0; i < tmp.length; i++) {
  109.                 l.add(tmp[i].getUri());
  110.             }
  111.             return l;
  112.         }
  113.         else if(XMLCostanti.ID_ENCRYPT_TRANSPORT_KEY_WRAP_ALGORITHM.equals(id)) {
  114.             List<String> l = new ArrayList<>();
  115.             EncryptionKeyTransportAlgorithm [] tmp = EncryptionKeyTransportAlgorithm.values();
  116.             for (int i = 0; i < tmp.length; i++) {
  117.                 l.add(tmp[i].getUri());
  118.             }
  119.             return l;
  120.         }
  121.         else if(XMLCostanti.ID_ENCRYPT_ALGORITHM.equals(id)) {
  122.             List<String> l = new ArrayList<>();
  123.             EncryptionAlgorithm [] tmp = EncryptionAlgorithm.values();
  124.             for (int i = 0; i < tmp.length; i++) {
  125.                 l.add(tmp[i].getUri());
  126.             }
  127.             return l;
  128.         }
  129.         else if(XMLCostanti.ID_ENCRYPT_DIGEST_ALGORITHM.equals(id)) {
  130.             List<String> l = new ArrayList<>();
  131.             EncryptionDigestAlgorithm [] tmp = EncryptionDigestAlgorithm.values();
  132.             for (int i = 0; i < tmp.length; i++) {
  133.                 l.add(tmp[i].getUri());
  134.             }
  135.             return l;
  136.         }
  137.         else if(XMLCostanti.ID_ENCRYPT_C14N_ALGORITHM.equals(id)) {
  138.             List<String> l = new ArrayList<>();
  139.             EncryptionC14NAlgorithm [] tmp = EncryptionC14NAlgorithm.values();
  140.             for (int i = 0; i < tmp.length; i++) {
  141.                 l.add(tmp[i].getUri());
  142.             }
  143.             return l;
  144.         }
  145.         else if(XMLCostanti.ID_ENCRYPT_C14N_ALGORITHM_EXCLUSIVE.equals(id)) {
  146.             List<String> l = new ArrayList<>();
  147.             EncryptionC14NAlgorithm [] tmp = EncryptionC14NAlgorithm.values();
  148.             for (int i = 0; i < tmp.length; i++) {
  149.                 if(tmp[i].name().startsWith("EXCLUSIVE")) {
  150.                     l.add(tmp[i].getUri());
  151.                 }
  152.             }
  153.             return l;
  154.         }
  155.         else if(XMLCostanti.ID_ENCRYPT_C14N_ALGORITHM_INCLUSIVE.equals(id)) {
  156.             List<String> l = new ArrayList<>();
  157.             EncryptionC14NAlgorithm [] tmp = EncryptionC14NAlgorithm.values();
  158.             for (int i = 0; i < tmp.length; i++) {
  159.                 if(tmp[i].name().startsWith("INCLUSIVE")) {
  160.                     l.add(tmp[i].getUri());
  161.                 }
  162.             }
  163.             return l;
  164.         }
  165.         else {
  166.             return super.getValues(id);
  167.         }
  168.     }

  169.     @Override
  170.     public List<String> getLabels(String id) throws ProviderException {
  171.         if(XMLCostanti.ID_SIGNATURE_ALGORITHM.equals(id)) {
  172.             List<String> l = new ArrayList<>();
  173.             SignatureAlgorithm [] tmp = SignatureAlgorithm.values();
  174.             for (int i = 0; i < tmp.length; i++) {
  175.                 l.add(this.convertEnumName(tmp[i].name()));
  176.             }
  177.             return l;
  178.         }
  179.         else if(XMLCostanti.ID_SIGNATURE_DIGEST_ALGORITHM.equals(id)) {
  180.             List<String> l = new ArrayList<>();
  181.             SignatureDigestAlgorithm [] tmp = SignatureDigestAlgorithm.values();
  182.             for (int i = 0; i < tmp.length; i++) {
  183.                 l.add(this.convertEnumName(tmp[i].name()));
  184.             }
  185.             return l;
  186.         }
  187.         else if(XMLCostanti.ID_SIGNATURE_C14N_ALGORITHM.equals(id)) {
  188.             List<String> l = new ArrayList<>();
  189.             SignatureC14NAlgorithm [] tmp = SignatureC14NAlgorithm.values();
  190.             for (int i = 0; i < tmp.length; i++) {
  191.                 l.add(tmp[i].getLabel());
  192.             }
  193.             return l;
  194.         }
  195.        
  196.         else if(XMLCostanti.ID_ENCRYPT_KEY_ALGORITHM.equals(id)) {
  197.             return this.getValues(id);
  198.         }
  199.         else if(XMLCostanti.ID_ENCRYPT_SYMMETRIC_KEY_WRAP_ALGORITHM.equals(id)) {
  200.             List<String> l = new ArrayList<>();
  201.             EncryptionSymmetricKeyWrapAlgorithm [] tmp = EncryptionSymmetricKeyWrapAlgorithm.values();
  202.             for (int i = 0; i < tmp.length; i++) {
  203.                 l.add(this.convertEnumName(tmp[i].name()));
  204.             }
  205.             return l;
  206.         }
  207.         else if(XMLCostanti.ID_ENCRYPT_TRANSPORT_KEY_WRAP_ALGORITHM.equals(id)) {
  208.             List<String> l = new ArrayList<>();
  209.             EncryptionKeyTransportAlgorithm [] tmp = EncryptionKeyTransportAlgorithm.values();
  210.             for (int i = 0; i < tmp.length; i++) {
  211.                 l.add(this.convertEnumName(tmp[i].name()));
  212.             }
  213.             return l;
  214.         }
  215.         else if(XMLCostanti.ID_ENCRYPT_ALGORITHM.equals(id)) {
  216.             List<String> l = new ArrayList<>();
  217.             EncryptionAlgorithm [] tmp = EncryptionAlgorithm.values();
  218.             for (int i = 0; i < tmp.length; i++) {
  219.                 l.add(this.convertEnumName(tmp[i].name()));
  220.             }
  221.             return l;
  222.         }
  223.         else if(XMLCostanti.ID_ENCRYPT_DIGEST_ALGORITHM.equals(id)) {
  224.             List<String> l = new ArrayList<>();
  225.             EncryptionDigestAlgorithm [] tmp = EncryptionDigestAlgorithm.values();
  226.             for (int i = 0; i < tmp.length; i++) {
  227.                 l.add(this.convertEnumName(tmp[i].name()));
  228.             }
  229.             return l;
  230.         }
  231.         else if(XMLCostanti.ID_ENCRYPT_C14N_ALGORITHM.equals(id)) {
  232.             List<String> l = new ArrayList<>();
  233.             EncryptionC14NAlgorithm [] tmp = EncryptionC14NAlgorithm.values();
  234.             for (int i = 0; i < tmp.length; i++) {
  235.                 l.add(tmp[i].getLabel());
  236.             }
  237.             return l;
  238.         }
  239.         else {
  240.             return super.getLabels(id);
  241.         }
  242.     }
  243.    
  244.     private String convertEnumName(String name) {
  245.         if(name.contains("_")) {
  246.             String t = new String(name);
  247.             while(t.contains("_")) {
  248.                 t = t.replace("_", "-");
  249.             }
  250.             return t;
  251.         }
  252.         else {
  253.             return name;
  254.         }
  255.     }

  256.     @Override
  257.     public String getDefault(String id) throws ProviderException {
  258.         if(XMLCostanti.ID_SIGNATURE_ALGORITHM.equals(id)) {
  259.             return SignatureAlgorithm.RSA_SHA256.getUri();
  260.         }
  261.         else if(XMLCostanti.ID_SIGNATURE_DIGEST_ALGORITHM.equals(id)) {
  262.             return SignatureDigestAlgorithm.SHA256.getUri();
  263.         }
  264.         else if(XMLCostanti.ID_SIGNATURE_C14N_ALGORITHM.equals(id)) {
  265.             return SignatureC14NAlgorithm.EXCLUSIVE_C14N_10_OMITS_COMMENTS.getUri(); // richiesto da WSI-BasicProfile
  266.         }
  267.        
  268.         else if(XMLCostanti.ID_ENCRYPT_KEY_ALGORITHM.equals(id)) {
  269.             return KeyAlgorithm.AES.name();
  270.         }
  271.         else if(XMLCostanti.ID_ENCRYPT_SYMMETRIC_KEY_WRAP_ALGORITHM.equals(id)) {
  272.             return EncryptionSymmetricKeyWrapAlgorithm.AES_256.getUri();
  273.         }
  274.         else if(XMLCostanti.ID_ENCRYPT_ALGORITHM.equals(id)) {
  275.             return EncryptionAlgorithm.AES_256.getUri();
  276.         }
  277.         else if(XMLCostanti.ID_ENCRYPT_TRANSPORT_KEY_WRAP_ALGORITHM.equals(id)) {
  278.             return EncryptionKeyTransportAlgorithm.RSA_v1dot5.getUri();
  279.         }
  280.         else if(XMLCostanti.ID_ENCRYPT_DIGEST_ALGORITHM.equals(id)) {
  281.             return EncryptionDigestAlgorithm.SHA256.getUri();
  282.         }
  283.         else if(XMLCostanti.ID_ENCRYPT_C14N_ALGORITHM.equals(id)) {
  284.             return EncryptionC14NAlgorithm.INCLUSIVE_C14N_10_WITH_COMMENTS.getUri();
  285.         }
  286.         else {
  287.             return super.getDefault(id);
  288.         }
  289.     }

  290. }