Extensions.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.Arrays;
  23. import java.util.List;

  24. import org.bouncycastle.asn1.ASN1ObjectIdentifier;

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

  33.     private org.bouncycastle.asn1.x509.Extensions exts;

  34.     public org.bouncycastle.asn1.x509.Extensions getAll() {
  35.         return this.exts;
  36.     }
  37.     public org.bouncycastle.asn1.x509.Extensions getExtensions() {
  38.         return this.exts;
  39.     }

  40.     public org.bouncycastle.asn1.x509.Extension getExtension(String id) {
  41.         org.bouncycastle.asn1.ASN1ObjectIdentifier asn1 = new ASN1ObjectIdentifier(id);
  42.         return getExtension(asn1);
  43.     }
  44.     public org.bouncycastle.asn1.x509.Extension getExtension(org.bouncycastle.asn1.ASN1ObjectIdentifier id) {
  45.         if(this.exts!=null) {
  46.             return this.exts.getExtension(id);
  47.         }
  48.         return null;
  49.     }
  50.    
  51.     public List<String> getOIDs(){
  52.         return getExtensionOIDsEngine(this.getASN1OIDs());
  53.     }
  54.     public List<org.bouncycastle.asn1.ASN1ObjectIdentifier> getASN1OIDs(){
  55.         return getExtensionASN1OIDsEngine(this.exts!=null ? this.exts.getExtensionOIDs() : null);
  56.     }
  57.    
  58.     public List<String> getCriticalOIDs(){
  59.         return getExtensionOIDsEngine(this.getCriticalASN1OIDs());
  60.     }
  61.     public List<org.bouncycastle.asn1.ASN1ObjectIdentifier> getCriticalASN1OIDs(){
  62.         return getExtensionASN1OIDsEngine(this.exts!=null ? this.exts.getCriticalExtensionOIDs() : null);
  63.     }
  64.    
  65.     public List<String> getNonCriticalOIDs(){
  66.         return getExtensionOIDsEngine(this.getNonCriticalASN1OIDs());
  67.     }
  68.     public List<org.bouncycastle.asn1.ASN1ObjectIdentifier> getNonCriticalASN1OIDs(){
  69.         return getExtensionASN1OIDsEngine(this.exts!=null ? this.exts.getNonCriticalExtensionOIDs() : null);
  70.     }
  71.    
  72.     private List<String> getExtensionOIDsEngine(List<org.bouncycastle.asn1.ASN1ObjectIdentifier> l){
  73.         List<String> lR = null;
  74.         if(l!=null && !l.isEmpty()) {
  75.             lR = new ArrayList<>();
  76.             for (org.bouncycastle.asn1.ASN1ObjectIdentifier asn1 : l) {
  77.                 lR.add(asn1.getId());
  78.             }
  79.         }
  80.         return lR;
  81.     }
  82.     private List<org.bouncycastle.asn1.ASN1ObjectIdentifier> getExtensionASN1OIDsEngine(org.bouncycastle.asn1.ASN1ObjectIdentifier [] ids){
  83.         List<org.bouncycastle.asn1.ASN1ObjectIdentifier> l = null;
  84.         if(ids!=null && ids.length>0) {
  85.             l = new ArrayList<>();
  86.             l.addAll(Arrays.asList(ids));
  87.         }
  88.         return l;
  89.     }
  90.    
  91.     public boolean hasExtension(String id) {
  92.         return this.getExtension(id)!=null;
  93.     }
  94.     public boolean hasExtension(org.bouncycastle.asn1.ASN1ObjectIdentifier id) {
  95.         return this.getExtension(id)!=null;
  96.     }
  97.    
  98.     public boolean hasCriticalExtension(String id) {
  99.         List<String> criticalList = this.getCriticalOIDs();
  100.         if(criticalList!=null) {
  101.             return criticalList.contains(id);
  102.         }
  103.         return false;
  104.     }
  105.     public boolean hasCriticalExtension(org.bouncycastle.asn1.ASN1ObjectIdentifier id) {
  106.         List<org.bouncycastle.asn1.ASN1ObjectIdentifier> criticalList = this.getCriticalASN1OIDs();
  107.         if(criticalList!=null) {
  108.             return criticalList.contains(id);
  109.         }
  110.         return false;
  111.     }
  112.    
  113.     public boolean hasNonCriticalExtension(String id) {
  114.         List<String> nonCriticalList = this.getNonCriticalOIDs();
  115.         if(nonCriticalList!=null) {
  116.             return nonCriticalList.contains(id);
  117.         }
  118.         return false;
  119.     }
  120.     public boolean hasNonCriticalExtension(org.bouncycastle.asn1.ASN1ObjectIdentifier id) {
  121.         List<org.bouncycastle.asn1.ASN1ObjectIdentifier> nonCriticalList = this.getNonCriticalASN1OIDs();
  122.         if(nonCriticalList!=null) {
  123.             return nonCriticalList.contains(id);
  124.         }
  125.         return false;
  126.     }
  127.    
  128.     public static Extensions getExtensions(byte[]encoded) {
  129.        
  130.         Extensions extObj = new Extensions();
  131.        
  132.         org.bouncycastle.asn1.x509.Certificate c =org.bouncycastle.asn1.x509.Certificate.getInstance(encoded);
  133.         extObj.exts = c.getTBSCertificate().getExtensions();

  134.         return extObj;
  135.        
  136.     }
  137. }