AuthorityInformationAccess.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.cert.CertificateParsingException;
  22. import java.util.ArrayList;
  23. import java.util.List;

  24. import org.bouncycastle.asn1.x509.AccessDescription;
  25. import org.bouncycastle.asn1.x509.Extensions;
  26. import org.bouncycastle.asn1.x509.GeneralName;

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

  35.     private static final String CA_ISSUERS = "1.3.6.1.5.5.7.48.2";
  36.     private static final String OCSP = "1.3.6.1.5.5.7.48.1";
  37.    
  38.     private List<GeneralName> caIssuers = new ArrayList<>();
  39.     private List<GeneralName> ocsps = new ArrayList<>();

  40.    
  41.     public List<GeneralName> getObjectCAIssuers() {
  42.         return this.caIssuers;
  43.     }
  44.     public GeneralName getObjectCAIssuer(int index) {
  45.         return this.caIssuers!=null && (this.caIssuers.size()>index) ? this.caIssuers.get(index) : null;
  46.     }
  47.     public List<String> getCAIssuers() {
  48.         List<String> s = new ArrayList<>();
  49.         if(this.caIssuers!=null && !this.caIssuers.isEmpty()) {
  50.             for (GeneralName o : this.caIssuers) {
  51.                 if(o.getName()!=null) {
  52.                     s.add(o.getName().toString());
  53.                 }
  54.             }
  55.         }
  56.         return s;
  57.     }
  58.     public String getCAIssuer(int index) {
  59.         if(this.caIssuers!=null && (this.caIssuers.size()>index)) {
  60.             return (this.caIssuers.get(index)!=null && this.caIssuers.get(index).getName()!=null) ? this.caIssuers.get(index).getName().toString() : null;
  61.         }
  62.         return null;
  63.     }
  64.     public boolean containsCAIssuer(String name) throws CertificateParsingException {
  65.         return containsCAIssuerEngine(null, name);
  66.     }
  67.     public boolean containsCAIssuer(int tagNum, String name) throws CertificateParsingException {
  68.         return containsCAIssuerEngine(tagNum, name);
  69.     }
  70.     private boolean containsCAIssuerEngine(Integer tagNum, String name) throws CertificateParsingException {
  71.         if(name==null) {
  72.             throw new CertificateParsingException("Param name undefined");
  73.         }
  74.         if(this.caIssuers!=null && !this.caIssuers.isEmpty()) {
  75.             for (GeneralName o : this.caIssuers) {
  76.                 if(isEquals(o, tagNum, name)) {
  77.                     return true;
  78.                 }
  79.             }
  80.         }
  81.         return false;
  82.     }
  83.    
  84.    
  85.     public List<GeneralName> getObjectOCSPs() {
  86.         return this.ocsps;
  87.     }
  88.     public GeneralName getObjectOCSP(int index) {
  89.         return this.ocsps!=null && (this.ocsps.size()>index) ? this.ocsps.get(index) : null;
  90.     }
  91.     public List<String> getOCSPs() {
  92.         List<String> s = new ArrayList<>();
  93.         if(this.ocsps!=null && !this.ocsps.isEmpty()) {
  94.             for (GeneralName o : this.ocsps) {
  95.                 if(o.getName()!=null) {
  96.                     s.add(o.getName().toString());
  97.                 }
  98.             }
  99.         }
  100.         return s;
  101.     }
  102.     public String getOCSP(int index) {
  103.         if(this.ocsps!=null && (this.ocsps.size()>index)) {
  104.             return (this.ocsps.get(index)!=null && this.ocsps.get(index).getName()!=null) ? this.ocsps.get(index).getName().toString() : null;
  105.         }
  106.         return null;
  107.     }
  108.     public boolean containsOCSP(String name) throws CertificateParsingException {
  109.         return containsOCSPEngine(null, name);
  110.     }
  111.     public boolean containsOCSP(int tagNum, String name) throws CertificateParsingException {
  112.         return containsOCSPEngine(tagNum, name);
  113.     }
  114.     private boolean containsOCSPEngine(Integer tagNum, String name) throws CertificateParsingException {
  115.         if(name==null) {
  116.             throw new CertificateParsingException("Param name undefined");
  117.         }
  118.         if(this.ocsps!=null && !this.ocsps.isEmpty()) {
  119.             for (GeneralName o : this.ocsps) {
  120.                 if(isEquals(o, tagNum, name)) {
  121.                     return true;
  122.                 }
  123.             }
  124.         }
  125.         return false;
  126.     }
  127.    
  128.     public static AuthorityInformationAccess getAuthorityInformationAccess(byte[]encoded) {
  129.        
  130.         org.bouncycastle.asn1.x509.Certificate c =org.bouncycastle.asn1.x509.Certificate.getInstance(encoded);
  131.         Extensions exts = c.getTBSCertificate().getExtensions();
  132.         if (exts != null){
  133.             org.bouncycastle.asn1.x509.AuthorityInformationAccess auth = org.bouncycastle.asn1.x509.AuthorityInformationAccess.fromExtensions(exts);
  134.             if(auth!=null) {
  135.                 return readAIA(auth);
  136.                 /**AuthorityInformationAccess aia = readAIA(auth);
  137.                
  138.                 System.out.println("======================");
  139.                 System.out.println("AuthorityInformationAccess '"+auth.toString()+"'");
  140.                 if(auth.getAccessDescriptions()!=null && auth.getAccessDescriptions().length>0) {
  141.                     System.out.println("Len '"+auth.getAccessDescriptions().length+"'");
  142.                     for (int i = 0; i < auth.getAccessDescriptions().length; i++) {
  143.                         AccessDescription ad = auth.getAccessDescriptions()[i];
  144.                         System.out.println("AD["+i+"]=["+ad.getAccessMethod()+"]["+ad.getAccessLocation()+"]["+ad.getAccessLocation().getTagNo()+"]["+ad.getAccessLocation().getName()+"]");
  145.                     }
  146.                 }
  147.                 System.out.println("======================");
  148.                
  149.                 return aia;*/
  150.             }
  151.         }
  152.         return null;
  153.        
  154.     }
  155.     private static AuthorityInformationAccess readAIA(org.bouncycastle.asn1.x509.AuthorityInformationAccess auth) {
  156.         AuthorityInformationAccess aia = null;
  157.         if(auth.getAccessDescriptions()!=null && auth.getAccessDescriptions().length>0) {
  158.            
  159.             aia = new AuthorityInformationAccess();
  160.            
  161.             for (int i = 0; i < auth.getAccessDescriptions().length; i++) {
  162.                 AccessDescription ad = auth.getAccessDescriptions()[i];
  163.                 if(ad.getAccessMethod()!=null) {
  164.                     if(CA_ISSUERS.equals(ad.getAccessMethod().getId())) {
  165.                         aia.caIssuers.add(ad.getAccessLocation());
  166.                     }
  167.                     else if(OCSP.equals(ad.getAccessMethod().getId())) {
  168.                         aia.ocsps.add(ad.getAccessLocation());
  169.                     }
  170.                 }
  171.             }
  172.         }
  173.         return aia;
  174.     }
  175.    
  176.     private boolean isEquals(GeneralName o, Integer tagNum, String name) {
  177.         if(o.getName()!=null && name.equals(o.getName().toString())) {
  178.             if(tagNum==null) {
  179.                 return true;
  180.             }
  181.             else {
  182.                 if(tagNum.intValue() == o.getTagNo()) {
  183.                     return true;
  184.                 }
  185.             }
  186.         }
  187.         return false;
  188.     }
  189. }