CertificatePrincipal.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.List;
  23. import java.util.Map;

  24. import javax.security.auth.x500.X500Principal;

  25. import org.bouncycastle.asn1.x500.RDN;
  26. import org.bouncycastle.asn1.x500.X500Name;
  27. import org.bouncycastle.asn1.x500.style.BCStyle;
  28. import org.bouncycastle.cert.selector.jcajce.JcaX509CertificateHolderSelector;
  29. import org.openspcoop2.utils.UtilsException;
  30. import org.openspcoop2.utils.regexp.RegExpNotFoundException;
  31. import org.openspcoop2.utils.regexp.RegularExpressionEngine;

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

  40.     private X500Principal principal;
  41.     private PrincipalType type;
  42.     private X500Name x500name;

  43.     public CertificatePrincipal(X500Principal principal, PrincipalType type) {
  44.         this.principal = principal;
  45.         this.type = type;
  46.     }

  47.     @Override
  48.     public String toString() {
  49.         return this.principal.toString();
  50.     }
  51.     public String toString(String regexp) throws UtilsException {
  52.         String dn = this.toString();
  53.         return getRegexpValue(dn, regexp);
  54.     }
  55.    
  56.     public String getName() {
  57.         return this.principal.getName();
  58.     }
  59.     public String getNameByRegExp(String regexp) throws UtilsException {
  60.         String dn = this.getName();
  61.         return getRegexpValue(dn, regexp);
  62.     }
  63.    
  64.     public String getName(String format) {
  65.         return this.principal.getName(format);
  66.     }
  67.     public String getName(String format, String regexp) throws UtilsException {
  68.         String dn = this.getName(format);
  69.         return getRegexpValue(dn, regexp);
  70.     }
  71.    
  72.     public String getCanonicalName() {
  73.         return this.principal.getName(X500Principal.CANONICAL);
  74.     }
  75.     public String getCanonicalName(String regexp) throws UtilsException {
  76.         String dn = this.getCanonicalName();
  77.         return getRegexpValue(dn, regexp);
  78.     }
  79.    
  80.     public String getRFC1779Name() {
  81.         return this.principal.getName(X500Principal.RFC1779);
  82.     }
  83.     public String getRFC1779Name(String regexp) throws UtilsException {
  84.         String dn = this.getRFC1779Name();
  85.         return getRegexpValue(dn, regexp);
  86.     }
  87.    
  88.     public String getRFC2253Name() {
  89.         return this.principal.getName(X500Principal.RFC2253);
  90.     }
  91.     public String getRFC2253Name(String regexp) throws UtilsException {
  92.         String dn = this.getRFC2253Name();
  93.         return getRegexpValue(dn, regexp);
  94.     }
  95.    
  96.     public String getNameNormalized() throws UtilsException {
  97.         return CertificateUtils.formatPrincipal(this.toString(), this.type);
  98.     }
  99.     public Map<String, List<String>> toMap() throws UtilsException {
  100.         return CertificateUtils.getPrincipalIntoMap(this.toString(), this.type);
  101.     }
  102.     public Map<String, String> toSimpleMap() throws UtilsException {
  103.         return CertificateUtils.formatPrincipalToMap(this.toString(), this.type);
  104.     }
  105.     public String getNameNormalized(String regexp) throws UtilsException {
  106.         String dn = this.getNameNormalized();
  107.         return getRegexpValue(dn, regexp);
  108.     }
  109.    
  110.    
  111.     private synchronized void initX500Name() {
  112.         if(this.x500name==null) {
  113.             this.x500name=new JcaX509CertificateHolderSelector(this.principal,null).getIssuer();
  114.         }
  115.     }
  116.    
  117.     public static final String CN_EMPTY = "__undefined__";
  118.     public String getCN() {
  119.         return getInfoByOID(BCStyle.CN, CN_EMPTY);
  120.     }
  121.     public String getCN(String regexp) throws UtilsException {
  122.         String cn = this.getCN();
  123.         return getRegexpValue(cn, regexp);
  124.     }
  125.    
  126.    
  127.     private String getRegexpValue(String value, String regexp) throws UtilsException {
  128.         try {
  129.             return RegularExpressionEngine.getStringMatchPattern(value, regexp);
  130.         }catch(RegExpNotFoundException notFound) {
  131.             return null;
  132.         }catch(Exception e) {
  133.             throw new UtilsException(e.getMessage(),e);
  134.         }
  135.     }
  136.    
  137.    
  138.    
  139.     // ******* OID **********
  140.    
  141.     public List<OID> getOID(){
  142.         List<OID> l = new ArrayList<OID>();
  143.         if(this.x500name==null) {
  144.             this.initX500Name();
  145.         }
  146.         RDN [] rdnArray = this.x500name.getRDNs();
  147.         if(rdnArray!=null && rdnArray.length>0) {
  148.             for (RDN rdn : rdnArray) {
  149.                 if(rdn!=null) {
  150.                     OID oid = OID.toOID(rdn.getFirst().getType());
  151. //                  if(oid==null) {
  152. //                      System.out.println("NULLLL ["+rdn.getFirst().getType()+"]: "+rdn.getFirst().getType().getId());
  153. //                  }
  154.                     if(oid!=null) { // custom
  155.                         l.add(oid);
  156.                     }
  157.                 }
  158.             }
  159.         }
  160.         return l;
  161.     }
  162.        
  163.     public String getInfo(String oid) {
  164.         return getInfoByOID(OID.valueOf(oid.toUpperCase()));
  165.     }
  166.     public String getInfo(String oid, String defaultEmptyValue) {
  167.         return getInfoByOID(OID.valueOf(oid.toUpperCase()),defaultEmptyValue);
  168.     }
  169.    
  170.     public String getInfoByOID(String oid) {
  171.         return getInfoByOID(new org.bouncycastle.asn1.ASN1ObjectIdentifier(oid));
  172.     }
  173.     public String getInfoByOID(String oid, String defaultEmptyValue) {
  174.         return getInfoByOID(new org.bouncycastle.asn1.ASN1ObjectIdentifier(oid),defaultEmptyValue);
  175.     }
  176.     public String getInfoByOID(OID oid) {
  177.         return getInfoByOID(oid.getOID());
  178.     }
  179.     public String getInfoByOID(OID oid, String defaultEmptyValue) {
  180.         return getInfoByOID(oid.getOID(),defaultEmptyValue);
  181.     }
  182.     public String getInfoByOID(org.bouncycastle.asn1.ASN1ObjectIdentifier oid) {
  183.         return getInfoByOID(oid, null);
  184.     }
  185.     public String getInfoByOID(org.bouncycastle.asn1.ASN1ObjectIdentifier oid, String defaultEmptyValue) {
  186.         if(this.x500name==null) {
  187.             this.initX500Name();
  188.         }
  189.         RDN [] rdnArray = this.x500name.getRDNs(oid);
  190.         if(rdnArray!=null && rdnArray.length>0 && rdnArray[0]!=null) {
  191.             RDN rdn = rdnArray[0];
  192.             if(rdn.getFirst()!=null && rdn.getFirst().getValue()!=null) {
  193.                 return rdn.getFirst().getValue().toString();
  194.             }
  195.             else {
  196.                 return defaultEmptyValue;
  197.             }
  198.         }
  199.         else {
  200.             return defaultEmptyValue;
  201.         }
  202.     }
  203.    
  204.     public String getInfo(String oid, int position) {
  205.         return getInfoByOID(OID.valueOf(oid.toUpperCase()), position);
  206.     }
  207.     public String getInfo(String oid, String defaultEmptyValue, int position) {
  208.         return getInfoByOID(OID.valueOf(oid.toUpperCase()),defaultEmptyValue, position);
  209.     }
  210.    
  211.     public String getInfoByOID(String oid, int position) {
  212.         return getInfoByOID(new org.bouncycastle.asn1.ASN1ObjectIdentifier(oid), position);
  213.     }
  214.     public String getInfoByOID(String oid, String defaultEmptyValue, int position) {
  215.         return getInfoByOID(new org.bouncycastle.asn1.ASN1ObjectIdentifier(oid),defaultEmptyValue, position);
  216.     }
  217.     public String getInfoByOID(OID oid, int position) {
  218.         return getInfoByOID(oid.getOID(), position);
  219.     }
  220.     public String getInfoByOID(OID oid, String defaultEmptyValue, int position) {
  221.         return getInfoByOID(oid.getOID(),defaultEmptyValue, position);
  222.     }
  223.     public String getInfoByOID(org.bouncycastle.asn1.ASN1ObjectIdentifier oid, int position) {
  224.         return getInfoByOID(oid, null, position);
  225.     }
  226.     public String getInfoByOID(org.bouncycastle.asn1.ASN1ObjectIdentifier oid, String defaultEmptyValue, int position) {
  227.         List<String> l = getInfosByOID(oid, defaultEmptyValue);
  228.         if(l!=null && l.size()>position) {
  229.             return l.get(position);
  230.         }
  231.         return defaultEmptyValue;
  232.     }
  233.    
  234.     public List<String> getInfos(String oid) {
  235.         return getInfosByOID(OID.valueOf(oid.toUpperCase()));
  236.     }
  237.     public List<String> getInfos(String oid, String defaultEmptyValue) {
  238.         return getInfosByOID(OID.valueOf(oid.toUpperCase()),defaultEmptyValue);
  239.     }
  240.    
  241.     public List<String> getInfosByOID(String oid) {
  242.         return getInfosByOID(new org.bouncycastle.asn1.ASN1ObjectIdentifier(oid));
  243.     }
  244.     public List<String> getInfosByOID(String oid, String defaultEmptyValue) {
  245.         return getInfosByOID(new org.bouncycastle.asn1.ASN1ObjectIdentifier(oid),defaultEmptyValue);
  246.     }
  247.     public List<String> getInfosByOID(OID oid) {
  248.         return getInfosByOID(oid.getOID());
  249.     }
  250.     public List<String> getInfosByOID(OID oid, String defaultEmptyValue) {
  251.         return getInfosByOID(oid.getOID(),defaultEmptyValue);
  252.     }
  253.     public List<String> getInfosByOID(org.bouncycastle.asn1.ASN1ObjectIdentifier oid) {
  254.         return getInfosByOID(oid, null);
  255.     }
  256.     public List<String> getInfosByOID(org.bouncycastle.asn1.ASN1ObjectIdentifier oid, String defaultEmptyValue) {
  257.         if(this.x500name==null) {
  258.             this.initX500Name();
  259.         }
  260.         RDN [] rdnArray = this.x500name.getRDNs(oid);
  261.         List<String> l = null;
  262.         if(rdnArray!=null && rdnArray.length>0) {
  263.             for (RDN rdn : rdnArray) {
  264.                 if(rdn!=null && rdn.getFirst()!=null && rdn.getFirst().getValue()!=null) {
  265.                     if(l==null) {
  266.                         l = new ArrayList<>();
  267.                     }
  268.                     l.add(rdn.getFirst().getValue().toString());
  269.                 }
  270.             }
  271.         }
  272.        
  273.         if(l==null && defaultEmptyValue!=null) {
  274.             l = new ArrayList<>();
  275.             l.add(defaultEmptyValue);
  276.         }
  277.         return l;
  278.     }
  279.    
  280.    
  281.     // ******* ID OID **********
  282.    
  283.     public List<String> getIdOID(){
  284.         List<String> l = new ArrayList<>();
  285.         if(this.x500name==null) {
  286.             this.initX500Name();
  287.         }
  288.         RDN [] rdnArray = this.x500name.getRDNs();
  289.         if(rdnArray!=null && rdnArray.length>0) {
  290.             for (RDN rdn : rdnArray) {
  291.                 if(rdn!=null) {
  292.                     l.add(rdn.getFirst().getType().getId());
  293.                 }
  294.             }
  295.         }
  296.         return l;
  297.     }
  298.    
  299.     public String getInfoByIdOID(String oid) {
  300.         return getInfoByIdOID(new org.bouncycastle.asn1.ASN1ObjectIdentifier(oid));
  301.     }
  302.     public String getInfoByIdOID(String oid, String defaultEmptyValue) {
  303.         return getInfoByIdOID(new org.bouncycastle.asn1.ASN1ObjectIdentifier(oid),defaultEmptyValue);
  304.     }
  305.     public String getInfoByIdOID(org.bouncycastle.asn1.ASN1ObjectIdentifier oid) {
  306.         return getInfoByIdOID(oid, null);
  307.     }
  308.     public String getInfoByIdOID(org.bouncycastle.asn1.ASN1ObjectIdentifier idOID, String defaultEmptyValue) {
  309.         if(this.x500name==null) {
  310.             this.initX500Name();
  311.         }
  312.         RDN [] rdnArray = this.x500name.getRDNs(idOID);
  313.         if(rdnArray!=null && rdnArray.length>0 && rdnArray[0]!=null) {
  314.             RDN rdn = rdnArray[0];
  315.             if(rdn.getFirst()!=null && rdn.getFirst().getValue()!=null) {
  316.                 return rdn.getFirst().getValue().toString();
  317.             }
  318.             else {
  319.                 return defaultEmptyValue;
  320.             }
  321.         }
  322.         else {
  323.             return defaultEmptyValue;
  324.         }
  325.     }
  326.    
  327.     public String getInfoByIdOID(String oid, int position) {
  328.         return getInfoByIdOID(new org.bouncycastle.asn1.ASN1ObjectIdentifier(oid), position);
  329.     }
  330.     public String getInfoByIdOID(String oid, String defaultEmptyValue, int position) {
  331.         return getInfoByIdOID(new org.bouncycastle.asn1.ASN1ObjectIdentifier(oid),defaultEmptyValue, position);
  332.     }
  333.     public String getInfoByIdOID(org.bouncycastle.asn1.ASN1ObjectIdentifier oid, int position) {
  334.         return getInfoByIdOID(oid, null, position);
  335.     }
  336.     public String getInfoByIdOID(org.bouncycastle.asn1.ASN1ObjectIdentifier oid, String defaultEmptyValue, int position) {
  337.         List<String> l = getInfosByIdOID(oid, defaultEmptyValue);
  338.         if(l!=null && l.size()>position) {
  339.             return l.get(position);
  340.         }
  341.         return defaultEmptyValue;
  342.     }
  343.    
  344.     public List<String> getInfosByIdOID(String oid) {
  345.         return getInfosByIdOID(new org.bouncycastle.asn1.ASN1ObjectIdentifier(oid));
  346.     }
  347.     public List<String> getInfosByIdOID(String oid, String defaultEmptyValue) {
  348.         return getInfosByIdOID(new org.bouncycastle.asn1.ASN1ObjectIdentifier(oid),defaultEmptyValue);
  349.     }
  350.     public List<String> getInfosByIdOID(org.bouncycastle.asn1.ASN1ObjectIdentifier oid) {
  351.         return getInfosByIdOID(oid, null);
  352.     }
  353.     public List<String> getInfosByIdOID(org.bouncycastle.asn1.ASN1ObjectIdentifier oid, String defaultEmptyValue) {
  354.         return getInfosByOID(oid, defaultEmptyValue);
  355.         /**if(this.x500name==null) {
  356.             this.initX500Name();
  357.         }
  358.         RDN [] rdnArray = this.x500name.getRDNs(oid);
  359.         List<String> l = null;
  360.         if(rdnArray!=null && rdnArray.length>0) {
  361.             for (RDN rdn : rdnArray) {
  362.                 if(rdn!=null && rdn.getFirst()!=null && rdn.getFirst().getValue()!=null) {
  363.                     if(l==null) {
  364.                         l = new ArrayList<>();
  365.                     }
  366.                     l.add(rdn.getFirst().getValue().toString());
  367.                 }
  368.             }
  369.         }
  370.        
  371.         if(l==null && defaultEmptyValue!=null) {
  372.             l = new ArrayList<>();
  373.             l.add(defaultEmptyValue);
  374.         }
  375.         return l;*/
  376.     }
  377. }