CRLCertstore.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.File;
  23. import java.io.FileInputStream;
  24. import java.io.IOException;
  25. import java.io.InputStream;
  26. import java.io.Serializable;
  27. import java.security.cert.CertStore;
  28. import java.security.cert.CertificateFactory;
  29. import java.security.cert.CollectionCertStoreParameters;
  30. import java.security.cert.X509CRL;
  31. import java.util.ArrayList;
  32. import java.util.List;
  33. import java.util.Map;

  34. import org.apache.commons.lang.StringUtils;
  35. import org.openspcoop2.utils.Utilities;
  36. import org.openspcoop2.utils.UtilsException;

  37. /**
  38.  * CRLCertstore
  39.  *
  40.  * @author Andrea Poli (apoli@link.it)
  41.  * @author $Author$
  42.  * @version $Rev$, $Date$
  43.  */
  44. public class CRLCertstore implements Serializable {

  45.     /**
  46.      *
  47.      */
  48.     private static final long serialVersionUID = 1L;

  49.     private List<byte []> crlBytes = null;
  50.     private List<String> crlPaths = null;
  51.    
  52.     private transient CertificateFactory certFactory = null;
  53.     private transient List<X509CRL> caCrls = null;
  54.     private transient CertStore certStore = null;
  55.    
  56.     @Override
  57.     public String toString() {
  58.         StringBuilder bf = new StringBuilder();
  59.         bf.append("CRLCertstore (");
  60.         boolean first = true;
  61.         if(this.crlPaths!=null) {
  62.             for (String crlPath : this.crlPaths) {
  63.                 if(!first) {
  64.                     bf.append(", ");
  65.                 }
  66.                 bf.append(crlPath);
  67.                 first=false;
  68.             }
  69.         }
  70.         else {
  71.             bf.append("Nessuna crl definita");
  72.         }
  73.         bf.append(")");
  74.         return bf.toString();
  75.     }
  76.    
  77.     public static List<String> readCrlPaths(String crlPaths){
  78.         List<String> crlPathsList = new ArrayList<>();
  79.         if(crlPaths!=null && StringUtils.isNotEmpty(crlPaths)) {
  80.             if(crlPaths.contains(",")) {
  81.                 String [] tmp = crlPaths.split(",");
  82.                 for (String crlPath : tmp) {
  83.                     crlPathsList.add(crlPath.trim());
  84.                 }
  85.             }
  86.             else {
  87.                 crlPathsList.add(crlPaths.trim());
  88.             }
  89.         }
  90.         return crlPathsList;
  91.     }
  92.    
  93.     public static String convertToCrlPaths(List<String> crlPathsList) {
  94.         StringBuilder sb = new StringBuilder();
  95.         if(crlPathsList==null || crlPathsList.isEmpty()) {
  96.             return null;
  97.         }
  98.         for (String path : crlPathsList) {
  99.             if(sb.length()>0) {
  100.                 sb.append(",");
  101.             }
  102.             sb.append(path);
  103.         }
  104.         return sb.toString();
  105.     }
  106.    
  107.     public CRLCertstore(String crlPaths) throws UtilsException {
  108.         this(crlPaths, null);
  109.     }
  110.     public CRLCertstore(String crlPaths, Map<String, byte[]> localResources) throws UtilsException {
  111.         List<String> crlPathsList = readCrlPaths(crlPaths);
  112.         this.initEngine(crlPathsList, localResources);
  113.     }
  114.    
  115.     public CRLCertstore(List<String> crlPaths) throws UtilsException{
  116.         this(crlPaths, null);
  117.     }
  118.     public CRLCertstore(List<String> crlPaths, Map<String, byte[]> localResources) throws UtilsException{
  119.         this.initEngine(crlPaths, localResources);
  120.     }
  121.    
  122.     private void initEngine(List<String> crlPaths, Map<String, byte[]> localResources) throws UtilsException{
  123.        
  124.         try{
  125.             if(crlPaths==null || crlPaths.isEmpty()){
  126.                 throw new UtilsException("crlPaths non indicato");
  127.             }
  128.        
  129.             this.crlPaths = crlPaths;
  130.             this.crlBytes = new ArrayList<>();
  131.            
  132.             for (String crlPath : crlPaths) {

  133.                 if(localResources!=null && !localResources.isEmpty() && localResources.containsKey(crlPath)) {
  134.                    
  135.                     byte[] r = localResources.get(crlPath);
  136.                     if(r!=null && r.length>0) {
  137.                         this.crlBytes.add(r);
  138.                     }
  139.                    
  140.                     continue;
  141.                 }
  142.                
  143.                 initEngineCrl(crlPath);
  144.             }
  145.            

  146.             this.initCRL();
  147.            
  148.         }catch(Exception e){
  149.             throw new UtilsException(e.getMessage(),e);
  150.         }
  151.        
  152.     }
  153.     private void initEngineCrl(String crlPath) throws UtilsException, IOException {
  154.        
  155.         byte[] crlBytesAdd = null;
  156.         File fStore = new File(crlPath);
  157.         boolean fStoreExists = fStore.exists();
  158.         try(InputStream isStore = fStoreExists ? new FileInputStream(fStore) : CRLCertstore.class.getResourceAsStream(crlPath);){
  159.             if(isStore!=null) {
  160.                 crlBytesAdd = Utilities.getAsByteArray(isStore);
  161.             }
  162.         }
  163.         if(crlBytesAdd==null && !fStoreExists) {
  164.             try(InputStream isStore = CRLCertstore.class.getResourceAsStream("/"+crlPath);){
  165.                 if(isStore!=null) {
  166.                     crlBytesAdd = Utilities.getAsByteArray(isStore);
  167.                 }
  168.             }
  169.         }
  170.         if(crlBytesAdd==null) {
  171.             throw new UtilsException("Store ["+crlPath+"] not found");
  172.         }
  173.         this.crlBytes.add(crlBytesAdd);
  174.     }
  175.    
  176.    
  177.     private void checkInit() throws UtilsException{
  178.         if(this.caCrls==null) {
  179.             this.initCRL();
  180.         }
  181.     }
  182.     private synchronized void initCRL() throws UtilsException{
  183.         if(this.caCrls==null) {
  184.            
  185.             // create a X509 certificate factory for later use
  186.             try {
  187.                 this.certFactory = org.openspcoop2.utils.certificate.CertificateFactory.getCertificateFactory();
  188.             }catch(Exception e){
  189.                 throw new UtilsException("Error getInstance CertificateFactory: "+e.getMessage(),e);
  190.             }
  191.            
  192.             this.caCrls = new ArrayList<>();
  193.             for (int i = 0; i < this.crlBytes.size(); i++) {
  194.                 byte [] crl = this.crlBytes.get(i);
  195.                 try(ByteArrayInputStream bin = new ByteArrayInputStream(crl)){
  196.                     X509CRL caCrl = (X509CRL) this.certFactory.generateCRL(bin);
  197.                     this.caCrls.add(caCrl);
  198.                 }
  199.                 catch(Exception e){
  200.                     throw new UtilsException("Error loading CRL '"+this.crlPaths.get(i)+"': "+e.getMessage(),e);
  201.                 }
  202.             }
  203.            
  204.             try {
  205.                 CollectionCertStoreParameters certStoreParams =
  206.                             new CollectionCertStoreParameters(this.caCrls);
  207.                 this.certStore =
  208.                             CertStore.getInstance("Collection", certStoreParams);
  209.             }catch(Exception e){
  210.                 throw new UtilsException("Build CertStore failed: "+e.getMessage(),e);
  211.             }
  212.         }
  213.     }
  214.    

  215.     public CertificateFactory getCertFactory() throws UtilsException {
  216.         this.checkInit(); // per ripristino da Serializable
  217.         return this.certFactory;
  218.     }

  219.     public List<X509CRL> getCaCrls() throws UtilsException {
  220.         this.checkInit(); // per ripristino da Serializable
  221.         return this.caCrls;
  222.     }

  223.     public CertStore getCertStore() throws UtilsException {
  224.         this.checkInit(); // per ripristino da Serializable
  225.         return this.certStore;
  226.     }
  227.    
  228.     public int countCrls() {
  229.         return this.crlBytes!=null ? this.crlBytes.size() : 0;
  230.     }
  231.    
  232. }