CertificateFactory.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.certificate;

  21. import java.security.NoSuchAlgorithmException;
  22. import java.security.Provider;
  23. import java.security.Security;
  24. import java.security.cert.CertPathValidator;
  25. import java.security.cert.CertificateException;

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

  36.     private static boolean useBouncyCastleProvider = false;
  37.     public static boolean isUseBouncyCastleProvider() {
  38.         return useBouncyCastleProvider;
  39.     }
  40.     public static void setUseBouncyCastleProvider(boolean useBouncyCastleProvider) {
  41.         CertificateFactory.useBouncyCastleProvider = useBouncyCastleProvider;
  42.     }
  43.    
  44.     private static Provider provider = null;
  45.     private static synchronized Provider getProviderEngine() {
  46.         if ( provider == null )
  47.             provider = Security.getProvider(org.bouncycastle.jce.provider.BouncyCastleProvider.PROVIDER_NAME);
  48.         return provider;
  49.     }
  50.     private static Provider getProvider() {
  51.         Provider p = null;
  52.         if(!useBouncyCastleProvider) {
  53.             return p;
  54.         }
  55.         if ( provider == null )
  56.             return getProviderEngine();
  57.         return provider;
  58.     }
  59.    
  60.     public static java.security.cert.CertificateFactory getCertificateFactory() throws CertificateException {
  61.         java.security.cert.CertificateFactory certificateFactory = null;
  62.         if(useBouncyCastleProvider) {
  63.             certificateFactory = java.security.cert.CertificateFactory.getInstance("X.509", getProvider());
  64.         }
  65.         else {
  66.             certificateFactory = java.security.cert.CertificateFactory.getInstance("X.509");
  67.         }
  68.         return certificateFactory;
  69.     }
  70.    
  71.     private static String certPathDefaultType = null;
  72.     private static synchronized String getCertPathDefaultTypeEngine() {
  73.         if ( certPathDefaultType == null )
  74.             certPathDefaultType = CertPathValidator.getDefaultType();
  75.         return certPathDefaultType;
  76.     }
  77.     private static String getCertPathDefaultType() {
  78.         if ( certPathDefaultType == null )
  79.             return getCertPathDefaultTypeEngine();
  80.         return certPathDefaultType;
  81.     }
  82.    
  83.     public static CertPathValidator getCertPathValidator() throws NoSuchAlgorithmException {
  84.         CertPathValidator certPathValidator = null;
  85.         if(useBouncyCastleProvider) {
  86.             certPathValidator = CertPathValidator.getInstance(getCertPathDefaultType(), getProvider());
  87.         }
  88.         else {
  89.             certPathValidator = CertPathValidator.getInstance(getCertPathDefaultType());
  90.         }
  91.         return certPathValidator;
  92.     }
  93. }