AuthorityKeyIdentifier.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.Extensions;
  25. import org.bouncycastle.asn1.x509.GeneralName;
  26. import org.openspcoop2.utils.UtilsException;
  27. import org.openspcoop2.utils.io.Base64Utilities;
  28. import org.openspcoop2.utils.io.HexBinaryUtilities;

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

  37.     private org.bouncycastle.asn1.x509.AuthorityKeyIdentifier authorityKeyIdentifierBC;
  38.     private long certSerialNumber;
  39.     private byte[] keyIdentifier;
  40.     private List<GeneralName> certIssuers = new ArrayList<>();

  41.     public org.bouncycastle.asn1.x509.AuthorityKeyIdentifier getAuthorityKeyIdentifier() {
  42.         return this.authorityKeyIdentifierBC;
  43.     }

  44.     public long getCertSerialNumber() {
  45.         return this.certSerialNumber;
  46.     }

  47.     public byte[] getKeyIdentifier() {
  48.         return this.keyIdentifier;
  49.     }
  50.     public String getBase64KeyIdentifier() {
  51.         return this.keyIdentifier!=null ? Base64Utilities.encodeAsString(this.keyIdentifier) : null;
  52.     }
  53.     public String getHexKeyIdentifier() throws UtilsException {
  54.         return this.keyIdentifier!=null ? HexBinaryUtilities.encodeAsString(this.keyIdentifier) : null;
  55.     }
  56.    
  57.     public List<GeneralName> getObjectCertIssuers() {
  58.         return this.certIssuers;
  59.     }
  60.     public GeneralName getObjectCertIssuer(int index) {
  61.         return this.certIssuers!=null && (this.certIssuers.size()>index) ? this.certIssuers.get(index) : null;
  62.     }
  63.     public List<String> getCertIssuers() {
  64.         List<String> s = new ArrayList<>();
  65.         if(this.certIssuers!=null && !this.certIssuers.isEmpty()) {
  66.             for (GeneralName o : this.certIssuers) {
  67.                 if(o.getName()!=null) {
  68.                     s.add(o.getName().toString());
  69.                 }
  70.             }
  71.         }
  72.         return s;
  73.     }
  74.     public String getCertIssuer(int index) {
  75.         if( this.certIssuers!=null && (this.certIssuers.size()>index) ) {
  76.             return (this.certIssuers.get(index)!=null && this.certIssuers.get(index).getName()!=null) ? this.certIssuers.get(index).getName().toString() : null;
  77.         }
  78.         return null;
  79.     }
  80.     public boolean containsCertIssuer(String name) throws CertificateParsingException {
  81.         return containsCertIssuerEngine(null, name);
  82.     }
  83.     public boolean containsCertIssuer(int tagNum, String name) throws CertificateParsingException {
  84.         return containsCertIssuerEngine(tagNum, name);
  85.     }
  86.     private boolean containsCertIssuerEngine(Integer tagNum, String name) throws CertificateParsingException {
  87.         if(name==null) {
  88.             throw new CertificateParsingException("Param name undefined");
  89.         }
  90.         if(this.certIssuers!=null && !this.certIssuers.isEmpty()) {
  91.             for (GeneralName o : this.certIssuers) {
  92.                 if(isEquals(o, tagNum, name)) {
  93.                     return true;
  94.                 }
  95.             }
  96.         }
  97.         return false;
  98.     }
  99.     private boolean isEquals(GeneralName o, Integer tagNum, String name) {
  100.         if(o.getName()!=null && name.equals(o.getName().toString())) {
  101.             if(tagNum==null) {
  102.                 return true;
  103.             }
  104.             else {
  105.                 if(tagNum.intValue() == o.getTagNo()) {
  106.                     return true;
  107.                 }
  108.             }
  109.         }
  110.         return false;
  111.     }
  112.    
  113.     public static AuthorityKeyIdentifier getAuthorityKeyIdentifier(byte[]encoded) {
  114.        
  115.         org.bouncycastle.asn1.x509.Certificate c =org.bouncycastle.asn1.x509.Certificate.getInstance(encoded);
  116.         Extensions exts = c.getTBSCertificate().getExtensions();
  117.         if (exts != null){
  118.             org.bouncycastle.asn1.x509.AuthorityKeyIdentifier authorityKeyIdentifier = org.bouncycastle.asn1.x509.AuthorityKeyIdentifier.fromExtensions(exts);
  119.             if(authorityKeyIdentifier!=null) {
  120.                 /**System.out.println("======================");
  121.                 System.out.println("AuthorityKeyIdentifier '"+authorityKeyIdentifier.toString()+"'");
  122.                 System.out.println("======================");*/
  123.                
  124.                 AuthorityKeyIdentifier aki = new AuthorityKeyIdentifier();
  125.                 aki.authorityKeyIdentifierBC = authorityKeyIdentifier;
  126.                 if(authorityKeyIdentifier.getAuthorityCertSerialNumber()!=null) {
  127.                     aki.certSerialNumber = authorityKeyIdentifier.getAuthorityCertSerialNumber().longValue();
  128.                 }
  129.                 aki.keyIdentifier = authorityKeyIdentifier.getKeyIdentifier();
  130.                 if(authorityKeyIdentifier.getAuthorityCertIssuer()!=null && authorityKeyIdentifier.getAuthorityCertIssuer().getNames()!=null && authorityKeyIdentifier.getAuthorityCertIssuer().getNames().length>0) {
  131.                     for (GeneralName gn : authorityKeyIdentifier.getAuthorityCertIssuer().getNames()) {
  132.                         aki.certIssuers.add(gn);
  133.                     }
  134.                 }
  135.                 return aki;
  136.             }
  137.         }
  138.         return null;
  139.        
  140.     }
  141. }