SubjectAlternativeNames.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.Extension;
  25. import org.bouncycastle.asn1.x509.Extensions;
  26. import org.bouncycastle.asn1.x509.GeneralName;

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

  35.     private List<GeneralName> alternativeNames = new ArrayList<>();
  36.    
  37.    
  38.     public List<GeneralName> getObjectAlternativeNames() {
  39.         return this.alternativeNames;
  40.     }
  41.     public GeneralName getObjectAlternativeName(int index) {
  42.         return this.alternativeNames!=null && (this.alternativeNames.size()>index) ? this.alternativeNames.get(index) : null;
  43.     }
  44.     public List<String> getAlternativeNames() {
  45.         List<String> s = new ArrayList<>();
  46.         if(this.alternativeNames!=null && !this.alternativeNames.isEmpty()) {
  47.             for (GeneralName o : this.alternativeNames) {
  48.                 if(o.getName()!=null) {
  49.                     s.add(o.getName().toString());
  50.                 }
  51.             }
  52.         }
  53.         return s;
  54.     }
  55.     public String getAlternativeName(int index) {
  56.         if(this.alternativeNames!=null && (this.alternativeNames.size()>index)) {
  57.             return (this.alternativeNames.get(index)!=null && this.alternativeNames.get(index).getName()!=null) ? this.alternativeNames.get(index).getName().toString() : null;
  58.         }
  59.         return null;
  60.     }
  61.     public boolean containsAlternativeName(String name) throws CertificateParsingException {
  62.         return containsAlternativeNameEngine(null, name);
  63.     }
  64.     public boolean containsAlternativeName(int tagNum, String name) throws CertificateParsingException {
  65.         return containsAlternativeNameEngine(tagNum, name);
  66.     }
  67.     private boolean containsAlternativeNameEngine(Integer tagNum, String name) throws CertificateParsingException {
  68.         if(name==null) {
  69.             throw new CertificateParsingException("Param name undefined");
  70.         }
  71.         if(this.alternativeNames!=null && !this.alternativeNames.isEmpty()) {
  72.             for (GeneralName o : this.alternativeNames) {
  73.                 if(equalsAlternativeName(tagNum, name, o)) {
  74.                     return true;
  75.                 }
  76.             }
  77.         }
  78.         return false;
  79.     }
  80.     private boolean equalsAlternativeName(Integer tagNum, String name, GeneralName o) {
  81.         if(o.getName()!=null && name.equals(o.getName().toString())) {
  82.             if(tagNum==null) {
  83.                 return true;
  84.             }
  85.             else {
  86.                 if(tagNum.intValue() == o.getTagNo()) {
  87.                     return true;
  88.                 }
  89.             }
  90.         }
  91.         return false;
  92.     }
  93.    
  94.     public static SubjectAlternativeNames getSubjectAlternativeNames(byte[]encoded) {
  95.        
  96.         org.bouncycastle.asn1.x509.Certificate c =org.bouncycastle.asn1.x509.Certificate.getInstance(encoded);
  97.         Extensions exts = c.getTBSCertificate().getExtensions();
  98.         if (exts != null){
  99.             org.bouncycastle.asn1.x509.GeneralNames gns = org.bouncycastle.asn1.x509.GeneralNames.fromExtensions(exts, Extension.subjectAlternativeName);
  100.             if(gns!=null) {
  101.                
  102.                 SubjectAlternativeNames san = null;
  103.                
  104.                 org.bouncycastle.asn1.x509.GeneralName [] names = gns.getNames();
  105.                 if(names!=null && names.length>0) {
  106.                    
  107.                     san = new SubjectAlternativeNames();
  108.                    
  109.                     for (GeneralName generalName : names) {
  110.                         san.alternativeNames.add(generalName);
  111.                     }
  112.                 }
  113.                
  114.                 /**System.out.println("======================");
  115.                 System.out.println("GeneralNames '"+gns.toString()+"'");
  116.                 if(names!=null && names.length>0) {
  117.                     System.out.println("Len '"+names.length+"'");
  118.                     for (int i = 0; i < names.length; i++) {
  119.                         GeneralName gn = names[i];
  120.                         System.out.println("gn["+i+"]=["+gn.getName()+"]["+gn.getTagNo()+"]");
  121.                     }
  122.                 }
  123.                 System.out.println("======================");*/
  124.                
  125.                 return san;
  126.             }
  127.         }
  128.         return null;
  129.        
  130.     }
  131. }