CRLDistributionPoints.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.util.ArrayList;
  22. import java.util.List;

  23. import org.bouncycastle.asn1.x509.DistributionPoint;
  24. import org.bouncycastle.asn1.x509.Extensions;
  25. import org.bouncycastle.asn1.x509.GeneralName;
  26. import org.bouncycastle.asn1.x509.GeneralNames;

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

  35.     private List<CRLDistributionPoint> distributionPoints = new ArrayList<>();

  36.    
  37.     public List<CRLDistributionPoint> getCRLDistributionPoints() {
  38.         return this.distributionPoints;
  39.     }
  40.     public CRLDistributionPoint getCRLDistributionPoint(int index) {
  41.         return this.distributionPoints!=null && (this.distributionPoints.size()>index) ? this.distributionPoints.get(index) : null;
  42.     }
  43.    
  44.     public static CRLDistributionPoints getCRLDistributionPoints(byte[]encoded) {
  45.        
  46.         org.bouncycastle.asn1.x509.Certificate c =org.bouncycastle.asn1.x509.Certificate.getInstance(encoded);
  47.         Extensions exts = c.getTBSCertificate().getExtensions();
  48.         if (exts != null){
  49.             org.bouncycastle.asn1.x509.CRLDistPoint crlDistPoint = org.bouncycastle.asn1.x509.CRLDistPoint.fromExtensions(exts);
  50.             if(crlDistPoint!=null) {
  51.                 return readCRLDistributionPoints(crlDistPoint);
  52.                
  53.                 /**CRLDistributionPoints crls = readCRLDistributionPoints(crlDistPoint);
  54.                
  55.                 System.out.println("======================");
  56.                 //System.out.println("CRLDistributionPoints '"+crlDistPoint.toString()+"'");
  57.                 for (int i = 0; i < crlDistPoint.getDistributionPoints().length; i++) {
  58.                     DistributionPoint dt = crlDistPoint.getDistributionPoints()[i];
  59.                     System.out.println("Issuer '"+dt.getCRLIssuer()+"'");
  60.                     System.out.println("Point '"+dt.getDistributionPoint()+"'");
  61.                     System.out.println("Reasons '"+dt.getReasons()+"'");
  62.                 }
  63.                 System.out.println("======================");
  64.                
  65.                 return crls;*/
  66.             }
  67.         }
  68.         return null;
  69.        
  70.     }
  71.     private static CRLDistributionPoints readCRLDistributionPoints(org.bouncycastle.asn1.x509.CRLDistPoint crlDistPoint) {
  72.         CRLDistributionPoints crls = null;
  73.         if(crlDistPoint.getDistributionPoints()!=null && crlDistPoint.getDistributionPoints().length>0) {
  74.            
  75.             crls = new CRLDistributionPoints();
  76.            
  77.             for (int i = 0; i < crlDistPoint.getDistributionPoints().length; i++) {
  78.                 DistributionPoint dt = crlDistPoint.getDistributionPoints()[i];
  79.                 if(dt!=null) {
  80.                    
  81.                     CRLDistributionPoint crl = readCRLDistributionPoint(dt);
  82.                    
  83.                     crls.distributionPoints.add(crl);
  84.                 }
  85.             }
  86.         }
  87.         return crls;
  88.     }
  89.     private static CRLDistributionPoint readCRLDistributionPoint(DistributionPoint dt) {
  90.         CRLDistributionPoint crl = new CRLDistributionPoint();
  91.        
  92.         if(dt.getCRLIssuer()!=null && dt.getCRLIssuer().getNames()!=null && dt.getCRLIssuer().getNames().length>0) {
  93.             for (GeneralName gn : dt.getCRLIssuer().getNames()) {
  94.                 crl.crlIssuers.add(gn);
  95.             }
  96.         }
  97.         if(dt.getReasons()!=null) {
  98.             crl.reasonFlags = dt.getReasons();
  99.         }
  100.         if(dt.getDistributionPoint()!=null) {
  101.             setCRLDistributionPoint(crl, dt);
  102.         }
  103.         return crl;
  104.     }
  105.     private static void setCRLDistributionPoint(CRLDistributionPoint crl, DistributionPoint dt) {
  106.         crl.distributionPointName = dt.getDistributionPoint();
  107.         if(dt.getDistributionPoint().getName() instanceof GeneralNames) {
  108.             GeneralNames gns = (GeneralNames) dt.getDistributionPoint().getName();
  109.             if(gns.getNames()!=null && gns.getNames().length>0) {
  110.                 for (GeneralName gn : gns.getNames()) {
  111.                     crl.distributionPointNames.add(gn);
  112.                 }
  113.             }
  114.         }
  115.     }
  116. }