ProviderUtils.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.security;

  21. import java.security.Security;
  22. import java.util.ArrayList;
  23. import java.util.Arrays;
  24. import java.util.List;

  25. /**
  26.  * ProviderUtils
  27.  *
  28.  * @author Andrea Poli (apoli@link.it)
  29.  * @author  $Author$
  30.  * @version $Rev$, $Date$
  31.  *
  32.  */
  33. public class ProviderUtils {
  34.    
  35.     private ProviderUtils(){}

  36.     public static void addBouncyCastle() {
  37.         add(new org.bouncycastle.jce.provider.BouncyCastleProvider());
  38.     }
  39.     public static void add(java.security.Provider provider) {
  40.         java.security.Security.addProvider(provider);
  41.     }
  42.    
  43.     public static void addBouncyCastleAfterSun(boolean overrideIfExists) {
  44.         /**Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());*/
  45.         if(overrideIfExists) {
  46.             addOverrideIfExists(new org.bouncycastle.jce.provider.BouncyCastleProvider(), 2); // lasciare alla posizione 1 il provider 'SUN'
  47.         }
  48.         else {
  49.             addIfNotExists(new org.bouncycastle.jce.provider.BouncyCastleProvider(), 2); // lasciare alla posizione 1 il provider 'SUN'
  50.         }
  51.     }
  52.    
  53.     public static void addIfNotExists(java.security.Provider provider, int position) {
  54.         if(Security.getProvider(provider.getName())==null) {
  55.             Security.insertProviderAt(provider, position);
  56.         }
  57.     }
  58.     // NOTA: utility Security.insertProviderAt utilizza una posizione vera e non da programmatore che parte da 0!!!!!!!!!!!
  59.     public static void addOverrideIfExists(java.security.Provider provider, int position) {
  60.         if(Security.getProvider(provider.getName())!=null) {
  61.             List<java.security.Provider> l = getProviders();
  62.             if(!l.isEmpty() && l.size()>(position-1) && l.get((position-1)).getName().equals(provider.getName())) {
  63.                 return; // gia presente alla posizione due
  64.             }          
  65.             Security.removeProvider(provider.getName());
  66.         }
  67.         Security.insertProviderAt(provider, position);
  68.     }
  69.    
  70.     public static boolean existsBouncyCastle() {
  71.         return exists(new org.bouncycastle.jce.provider.BouncyCastleProvider());
  72.     }
  73.     public static boolean exists(java.security.Provider provider) {
  74.         return Security.getProvider(provider.getName())!=null;
  75.     }
  76.    
  77.     public static boolean existsBouncyCastle(int position) {
  78.         return exists(new org.bouncycastle.jce.provider.BouncyCastleProvider(), position);
  79.     }
  80.     public static boolean exists(java.security.Provider provider, int position) {
  81.         List<java.security.Provider> l = getProviders();
  82.         if(l.isEmpty() || l.size()<position) {
  83.             return false;
  84.         }
  85.         return l.get(position).getName().equals(provider.getName());
  86.     }
  87.    
  88.     public static void removeBouncyCastle() {
  89.         remove(new org.bouncycastle.jce.provider.BouncyCastleProvider());
  90.     }
  91.     public static void remove(java.security.Provider provider) {
  92.         Security.removeProvider(provider.getName());
  93.     }
  94.    
  95.     public static List<java.security.Provider> getProviders(){
  96.         List<java.security.Provider> l = new ArrayList<>();
  97.         java.security.Provider [] p = Security.getProviders();
  98.         if(p!=null && p.length>0) {
  99.             l.addAll(Arrays.asList(p));
  100.         }
  101.         return l;
  102.     }
  103.     public static List<String> getProviderNames(){
  104.         List<String> l = new ArrayList<>();
  105.         List<java.security.Provider> lp = getProviders();
  106.         if(!lp.isEmpty()) {
  107.             for (java.security.Provider provider : lp) {
  108.                 l.add(provider.getName());
  109.             }
  110.         }
  111.         return l;
  112.     }
  113.  }