CertificatePolicyEntry.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.ASN1ObjectIdentifier;

  25. /**
  26.  * CertificatePolicyEntry
  27.  *
  28.  * @author Poli Andrea (apoli@link.it)
  29.  * @author $Author$
  30.  * @version $Rev$, $Date$
  31.  */
  32. public class CertificatePolicyEntry {
  33.    
  34.     public CertificatePolicyEntry(org.bouncycastle.asn1.DLSequence dl) {
  35.         this(dl,0);
  36.     }
  37.     private CertificatePolicyEntry(org.bouncycastle.asn1.DLSequence dl, int level) {
  38.         for (int j = 0; j < dl.size(); j++) {
  39.             Object o = dl.getObjectAt(j);
  40.             if(o!=null) {
  41.                 init(o, level);
  42.             }
  43.         }
  44.     }
  45.     private void init(Object o, int level) {
  46.         if(o instanceof org.bouncycastle.asn1.ASN1ObjectIdentifier) {
  47.             this.asn1ObjectIdentifier = (org.bouncycastle.asn1.ASN1ObjectIdentifier) o;
  48.         }
  49.         else {
  50.             if(o instanceof org.bouncycastle.asn1.DLSequence) {
  51.                 if(level<10) {
  52.                     CertificatePolicyEntry cpe = new CertificatePolicyEntry((org.bouncycastle.asn1.DLSequence)o,(level+1));
  53.                     this.entries.add(cpe);
  54.                 }
  55.             }
  56.             else {
  57.                 this.value.add(o);
  58.             }
  59.         }
  60.     }
  61.    
  62.     private ASN1ObjectIdentifier asn1ObjectIdentifier;
  63.     private List<Object> value = new ArrayList<>();
  64.     private List<CertificatePolicyEntry> entries = new ArrayList<>();

  65.     public ASN1ObjectIdentifier getAsn1ObjectIdentifier() {
  66.         return this.asn1ObjectIdentifier;
  67.     }
  68.     public String getOID() {
  69.         return this.asn1ObjectIdentifier!=null ? this.asn1ObjectIdentifier.getId() : null;
  70.     }
  71.    
  72.     public List<Object> getObjectValues() {
  73.         return this.value;
  74.     }
  75.     public Object getObjectValue(int index) {
  76.         return this.value!=null && (this.value.size()>index) ? this.value.get(index) : null;
  77.     }
  78.     public List<String> getValues() {
  79.         List<String> s = new ArrayList<>();
  80.         if(this.value!=null && !this.value.isEmpty()) {
  81.             for (Object o : this.value) {
  82.                 s.add(o.toString());
  83.             }
  84.         }
  85.         return s;
  86.     }
  87.     public String getValue(int index) {
  88.         if(this.value!=null && (this.value.size()>index)) {
  89.             return this.value.get(index)!=null ? this.value.get(index).toString() : null;
  90.         }
  91.         return null;
  92.     }
  93.     public boolean containsValue(String value) throws CertificateParsingException {
  94.         if(value==null) {
  95.             throw new CertificateParsingException("Param value undefined");
  96.         }
  97.         if(this.value!=null && !this.value.isEmpty()) {
  98.             for (Object o : this.value) {
  99.                 if(value.equals(o.toString())) {
  100.                     return true;
  101.                 }
  102.             }
  103.         }
  104.         return false;
  105.     }
  106.    
  107.     public List<CertificatePolicyEntry> getEntries() {
  108.         return this.entries;
  109.     }
  110.     public CertificatePolicyEntry getEntry(int index) {
  111.         return this.entries!=null && (this.entries.size()>index) ? this.entries.get(index) : null;
  112.     }
  113.     public CertificatePolicyEntry getEntry(String oid) throws CertificateParsingException {
  114.         return getEntryByOID(oid);
  115.     }
  116.     public CertificatePolicyEntry getEntryByOID(String oid) throws CertificateParsingException {
  117.         if(oid==null) {
  118.             throw new CertificateParsingException("Param oid undefined");
  119.         }
  120.         if(this.entries!=null && !this.entries.isEmpty()) {
  121.             for (CertificatePolicyEntry certificatePolicyEntry : this.entries) {
  122.                 if(oid.equals(certificatePolicyEntry.getOID())) {
  123.                     return certificatePolicyEntry;
  124.                 }
  125.             }
  126.         }
  127.         return null;
  128.     }
  129.     public boolean hasCertificatePolicyEntry(String oid) throws CertificateParsingException {
  130.         if(oid==null) {
  131.             throw new CertificateParsingException("Param oid undefined");
  132.         }
  133.         return this.getEntryByOID(oid)!=null;
  134.     }
  135.    
  136.     @Override
  137.     public String toString() {
  138.         return toString("");
  139.     }
  140.     public String toString(String prefix) {
  141.         StringBuilder sb = new StringBuilder();
  142.         if(this.asn1ObjectIdentifier!=null) {
  143.             sb.append(prefix);
  144.             sb.append("OID:");
  145.             sb.append(this.asn1ObjectIdentifier.getId());
  146.         }
  147.         if(this.value!=null && !this.value.isEmpty()) {
  148.             int index = 0;
  149.             for (Object o : this.value) {
  150.                 sb.append("\n");
  151.                 sb.append(prefix);
  152.                 sb.append("Value["+index+"]:");
  153.                 sb.append(o.toString());
  154.                 index++;
  155.             }
  156.            
  157.         }
  158.         if(this.entries!=null && !this.entries.isEmpty()) {
  159.             int index = 0;
  160.             for (CertificatePolicyEntry o : this.entries) {
  161.                 sb.append("\n");
  162.                 sb.append(prefix);
  163.                 sb.append("CertificatePolicyEntry["+index+"]{\n");
  164.                 sb.append(o.toString((prefix+"\t")));
  165.                 sb.append("\n"+prefix+"}");
  166.                 index++;
  167.             }
  168.         }
  169.         return sb.toString();
  170.     }
  171. }