FixTrustAnchorsNotEmpty.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.io.ByteArrayInputStream;
  22. import java.io.InputStream;
  23. import java.security.KeyStore;
  24. import java.security.cert.CertificateFactory;
  25. import java.util.Enumeration;

  26. import org.openspcoop2.utils.LoggerWrapperFactory;
  27. import org.openspcoop2.utils.UtilsException;
  28. import org.slf4j.Logger;

  29. /**
  30.  * FixTrustAnchorsNotEmpty
  31.  *
  32.  * @author Andrea Poli (apoli@link.it)
  33.  * @author $Author$
  34.  * @version $Rev$, $Date$
  35.  */
  36. public class FixTrustAnchorsNotEmpty {
  37.    
  38.     private FixTrustAnchorsNotEmpty() {}
  39.    
  40.     private static Logger log = LoggerWrapperFactory.getLogger(FixTrustAnchorsNotEmpty.class);
  41.    
  42.     private static CertificateFactory cf = null;
  43.     private static java.security.cert.Certificate certFix = null;
  44.     private static final String PEM = "-----BEGIN CERTIFICATE-----\n"+
  45.             "MIIC9TCCAd0CBE/Ior8wDQYJKoZIhvcNAQEFBQAwPjELMAkGA1UEBhMCSVQxLzAt\n"+
  46.             "BgNVBAMMJkZpeFRydXN0QW5jaG9yc1BhcmFtZXRlck11c3RCZU5vbkVtcHR5MCAX\n"+
  47.             "DTEyMDYwMTExMDg0N1oYDzIyODYwMzE2MTEwODQ3WjA+MQswCQYDVQQGEwJJVDEv\n"+
  48.             "MC0GA1UEAwwmRml4VHJ1c3RBbmNob3JzUGFyYW1ldGVyTXVzdEJlTm9uRW1wdHkw\n"+
  49.             "ggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDDoLP0pdU4WM5DalQHPBhC\n"+
  50.             "ewg2vuS/S6gnG9gLr3N1FzH0efpBuUwX8R7tjX4bEAHnkJdfy+SVAOargNBJnIje\n"+
  51.             "HFmfIwWVjqnHSahZR2IbR8v9LnPgWFljmyu6rtqlSKpL2qhU0pyVaJCtzBUqsTia\n"+
  52.             "BLyQVj+aJJZIjT/BrbNogHds3Ez8Jsh24XbJWA/GcrqFOPW8VP4hg8RS+v7/rr5o\n"+
  53.             "epNQ1l5kc7lg/G4ABpBi3krqCRP2ZVlLdYdv3UbOWyUSOzn6PP17BAgaUAdHJNxg\n"+
  54.             "yxN3F39xMElgO6q9ZykgCDwlAjQtP/ejo5vvRNy7ZEnkY5st7FuUOErOReDQRnFZ\n"+
  55.             "AgMBAAEwDQYJKoZIhvcNAQEFBQADggEBAIq3c5tpfU+L3843mT1iVC2I7D85l2aG\n"+
  56.             "UgxMtBm+yYf428SR/ziL/9Z0DVA8G/6dOLpT2MdKQWEeAR8D3V0VnFZ7quysCug8\n"+
  57.             "XdmSW1kL5Z/rtXHfvQ6FOVYCaOV2f5Th9Hl0Z2dKVLNYBHzTL65OoB+yYMNGXv5Q\n"+
  58.             "ITke2pz4vP3LYv4RDFl80g1Qofpeqa4AFuCkmxlntL7SBCJMtlQutRtJ99ZsxgYG\n"+
  59.             "Mp0Afz8RQ2nSpTW6gUEcYx80oWRSipR7+dmMyeESYnv9l5K6raw3GIrNtzqkfRbx\n"+
  60.             "mZaYPs2bYuX52F4fNc3jqQ8Zd2XnH2nMtHShjFVVfvAY9/LueO2LTuk=\n"+
  61.             "-----END CERTIFICATE-----";
  62.     private static synchronized void initCertificateFactory(){
  63.         if(FixTrustAnchorsNotEmpty.cf==null){
  64.             try (InputStream is = new ByteArrayInputStream(PEM.getBytes());){
  65.                 FixTrustAnchorsNotEmpty.cf = org.openspcoop2.utils.certificate.CertificateFactory.getCertificateFactory();
  66.                 FixTrustAnchorsNotEmpty.certFix = FixTrustAnchorsNotEmpty.cf.generateCertificate(is);
  67.             }catch(Exception e){
  68.                 log.error(e.getMessage(),e);
  69.             }
  70.         }
  71.     }
  72.    
  73.     public static void addCertificate(KeyStore keystore) throws UtilsException{

  74.         try{
  75.            
  76.             // Se esiste un certificato non devo fare altro
  77.             Enumeration<String> aliases = keystore.aliases();
  78.             while (aliases.hasMoreElements()) {
  79.                 String alias = aliases.nextElement();
  80.                 /** System.out.println("ALIAS["+alias+"] ..."); */
  81.                 if(keystore.isCertificateEntry(alias)){
  82.                     /** System.out.println("TROVATO!!!!"); */
  83.                     return;
  84.                 }
  85.             }
  86.             /** System.out.println("NON TROVATO!"); */
  87.            
  88.             if(FixTrustAnchorsNotEmpty.cf==null){
  89.                 FixTrustAnchorsNotEmpty.initCertificateFactory();
  90.             }
  91.             keystore.setCertificateEntry("FixTrustAnchorsParameterMustBeNonEmpty", FixTrustAnchorsNotEmpty.certFix);
  92.             /** System.out.println("AGGIUNTO"); */

  93.         }catch(Exception e){
  94.             throw new UtilsException(e.getMessage(),e);
  95.         }

  96.     }

  97. }