CertificatePolicy.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.CertificateEncodingException;
  22. import java.security.cert.CertificateParsingException;
  23. import java.security.cert.X509Certificate;
  24. import java.util.ArrayList;
  25. import java.util.List;

  26. import org.bouncycastle.asn1.ASN1Encodable;
  27. import org.bouncycastle.asn1.ASN1ObjectIdentifier;
  28. import org.bouncycastle.asn1.x509.Extensions;
  29. import org.bouncycastle.asn1.x509.PolicyInformation;

  30. /**
  31.  * CertificatePolicy
  32.  *
  33.  * @author Poli Andrea (apoli@link.it)
  34.  * @author $Author$
  35.  * @version $Rev$, $Date$
  36.  */
  37. public class CertificatePolicy {

  38.     private static final String PARAM_OID_UNDEFINED = "Param oid undefined";
  39.    
  40.     private ASN1ObjectIdentifier asn1ObjectIdentifier;
  41.     private List<CertificatePolicyEntry> qualifiers = new ArrayList<>();
  42.     public ASN1ObjectIdentifier getAsn1ObjectIdentifier() {
  43.         return this.asn1ObjectIdentifier;
  44.     }
  45.     public String getOID() {
  46.         return this.asn1ObjectIdentifier!=null ? this.asn1ObjectIdentifier.getId() : null;
  47.     }
  48.    
  49.     public List<CertificatePolicyEntry> getQualifiers() {
  50.         return this.qualifiers;
  51.     }
  52.     public CertificatePolicyEntry getQualifier(int index) {
  53.         return this.qualifiers!=null ? this.qualifiers.get(index) : null;
  54.     }
  55.    
  56.     public CertificatePolicyEntry getQualifier(String oid) throws CertificateParsingException {
  57.         return getQualifierByOID(oid);
  58.     }
  59.     public CertificatePolicyEntry getQualifierByOID(String oid) throws CertificateParsingException {
  60.         if(oid==null) {
  61.             throw new CertificateParsingException(PARAM_OID_UNDEFINED);
  62.         }
  63.         if(this.qualifiers!=null && !this.qualifiers.isEmpty()) {
  64.             for (CertificatePolicyEntry certificatePolicyQualifier : this.qualifiers) {
  65.                 if(oid.equals(certificatePolicyQualifier.getOID())) {
  66.                     return certificatePolicyQualifier;
  67.                 }
  68.             }
  69.         }
  70.         return null;
  71.     }
  72.     public boolean hasCertificatePolicyQualifier(String oid) throws CertificateParsingException {
  73.         if(oid==null) {
  74.             throw new CertificateParsingException(PARAM_OID_UNDEFINED);
  75.         }
  76.         return this.getQualifierByOID(oid)!=null;
  77.     }
  78.    
  79.    
  80.     public static boolean existsCertificatePolicy(X509Certificate x509, String oid) throws CertificateParsingException, CertificateEncodingException {
  81.         if(oid==null) {
  82.             throw new CertificateParsingException(PARAM_OID_UNDEFINED);
  83.         }
  84.         List<CertificatePolicy> list = CertificatePolicy.getCertificatePolicies(x509.getEncoded());
  85.         if(!list.isEmpty()) {
  86.             for (CertificatePolicy certificatePolicy : list) {
  87.                 if(oid.equals(certificatePolicy.getOID())) {
  88.                     return true;
  89.                 }
  90.             }
  91.         }
  92.         return false;
  93.     }
  94.    
  95.     @Override
  96.     public String toString() {
  97.         StringBuilder sb = new StringBuilder();
  98.         if(this.asn1ObjectIdentifier!=null) {
  99.             sb.append("OID:");
  100.             sb.append(this.asn1ObjectIdentifier.getId());
  101.         }
  102.         if(this.qualifiers!=null && !this.qualifiers.isEmpty()) {
  103.             int index = 0;
  104.             for (CertificatePolicyEntry o : this.qualifiers) {
  105.                 if(sb.length()>0) {
  106.                     sb.append("\n");
  107.                 }
  108.                 sb.append("Qualifier["+index+"]{\n");
  109.                 sb.append(o.toString("\t"));
  110.                 sb.append("}");
  111.                 index++;
  112.             }
  113.         }
  114.         return sb.toString();
  115.     }
  116.    
  117.     public static List<CertificatePolicy> getCertificatePolicies(byte[]encoded) {
  118.        
  119.         List<CertificatePolicy> l = new ArrayList<>();
  120.        
  121.         org.bouncycastle.asn1.x509.Certificate c =org.bouncycastle.asn1.x509.Certificate.getInstance(encoded);
  122.         Extensions exts = c.getTBSCertificate().getExtensions();
  123.         if (exts != null){
  124.             org.bouncycastle.asn1.x509.CertificatePolicies certPolicies = org.bouncycastle.asn1.x509.CertificatePolicies.fromExtensions(exts);
  125.             if(certPolicies!=null) {
  126.                 PolicyInformation [] pi = certPolicies.getPolicyInformation();
  127.                 if(pi!=null && pi.length>0) {
  128.                     read(pi, l);
  129.                 }
  130.             }
  131.         }
  132.         return l;
  133.        
  134.     }
  135.     private static void read(PolicyInformation [] pi, List<CertificatePolicy> l) {
  136.         for (PolicyInformation policyInformation : pi) {
  137.             CertificatePolicy cp = new CertificatePolicy();
  138.             cp.asn1ObjectIdentifier = policyInformation.getPolicyIdentifier();
  139.             if(policyInformation.getPolicyQualifiers()!=null) {
  140.                 for (int i = 0; i < policyInformation.getPolicyQualifiers().size(); i++) {
  141.                     ASN1Encodable e = policyInformation.getPolicyQualifiers().getObjectAt(i);
  142.                     if(e instanceof org.bouncycastle.asn1.DLSequence) {
  143.                         org.bouncycastle.asn1.DLSequence dl = (org.bouncycastle.asn1.DLSequence) e;
  144.                         CertificatePolicyEntry cpe = new CertificatePolicyEntry(dl);
  145.                         cp.qualifiers.add(cpe);
  146.                     }                              
  147.                 }
  148.             }
  149.             l.add(cp);
  150.              
  151.             /**System.out.println("======================");
  152.             System.out.println("PolicyInformation '"+policyInformation.toString()+"'");
  153.             System.out.println("Id '"+policyInformation.getPolicyIdentifier()+"'");
  154.             System.out.println("Qual '"+policyInformation.getPolicyQualifiers()+"'");
  155.             System.out.println("======================");*/
  156.         }
  157.     }
  158. }