ExtendedKeyUsage.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.security.cert.X509Certificate;
  23. import java.util.ArrayList;
  24. import java.util.List;

  25. import org.bouncycastle.asn1.x509.Extensions;

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

  34.     ANY_EXTENDED_KEY_USAGE(org.bouncycastle.asn1.x509.KeyPurposeId.anyExtendedKeyUsage), // [2.5.29.37.0]
  35.    
  36.     SERVER_AUTH(org.bouncycastle.asn1.x509.KeyPurposeId.id_kp_serverAuth), // [1.3.6.1.5.5.7.3.1]
  37.    
  38.     CLIENT_AUTH(org.bouncycastle.asn1.x509.KeyPurposeId.id_kp_clientAuth), // [1.3.6.1.5.5.7.3.2]
  39.    
  40.     CODE_SIGNING(org.bouncycastle.asn1.x509.KeyPurposeId.id_kp_codeSigning), // [1.3.6.1.5.5.7.3.3]

  41.     EMAIL_PROTECTION(org.bouncycastle.asn1.x509.KeyPurposeId.id_kp_emailProtection), // [1.3.6.1.5.5.7.3.4]
  42.    
  43.     IPSEC_END_SYSTEM(org.bouncycastle.asn1.x509.KeyPurposeId.id_kp_ipsecEndSystem), // [1.3.6.1.5.5.7.3.5]

  44.     IPSEC_TUNNEL(org.bouncycastle.asn1.x509.KeyPurposeId.id_kp_ipsecTunnel), // [1.3.6.1.5.5.7.3.6]
  45.    
  46.     IPSEC_USER(org.bouncycastle.asn1.x509.KeyPurposeId.id_kp_ipsecUser), // [1.3.6.1.5.5.7.3.7]

  47.     TIME_STAMPING(org.bouncycastle.asn1.x509.KeyPurposeId.id_kp_timeStamping), // [1.3.6.1.5.5.7.3.8]
  48.    
  49.     OCSP_SIGNING(org.bouncycastle.asn1.x509.KeyPurposeId.id_kp_OCSPSigning), // [1.3.6.1.5.5.7.3.9]
  50.    
  51.     DVCS(org.bouncycastle.asn1.x509.KeyPurposeId.id_kp_dvcs), // [1.3.6.1.5.5.7.3.10]
  52.    
  53.     SBGP_CERT_AA_SERVER_AUTH(org.bouncycastle.asn1.x509.KeyPurposeId.id_kp_sbgpCertAAServerAuth), // [1.3.6.1.5.5.7.3.11]
  54.    
  55.     SCVP_RESPONDER(org.bouncycastle.asn1.x509.KeyPurposeId.id_kp_scvp_responder), // [1.3.6.1.5.5.7.3.12]

  56.     EAP_OVER_PPP(org.bouncycastle.asn1.x509.KeyPurposeId.id_kp_eapOverPPP), // [1.3.6.1.5.5.7.3.13]
  57.    
  58.     EAP_OVER_LAN(org.bouncycastle.asn1.x509.KeyPurposeId.id_kp_eapOverLAN), // [1.3.6.1.5.5.7.3.14]
  59.    
  60.     SCVP_SERVER(org.bouncycastle.asn1.x509.KeyPurposeId.id_kp_scvpServer), // [1.3.6.1.5.5.7.3.15]
  61.    
  62.     SCVP_CLIENT(org.bouncycastle.asn1.x509.KeyPurposeId.id_kp_scvpClient), // [1.3.6.1.5.5.7.3.16]
  63.    
  64.     IPSEC_IKE(org.bouncycastle.asn1.x509.KeyPurposeId.id_kp_ipsecIKE), // [1.3.6.1.5.5.7.3.17]
  65.    
  66.     CAPWAP_AC(org.bouncycastle.asn1.x509.KeyPurposeId.id_kp_capwapAC), // [1.3.6.1.5.5.7.3.18]
  67.    
  68.     CAPWAP_WTP(org.bouncycastle.asn1.x509.KeyPurposeId.id_kp_capwapWTP), // [1.3.6.1.5.5.7.3.19]
  69.    
  70.     SMART_CARD_LOGON(org.bouncycastle.asn1.x509.KeyPurposeId.id_kp_smartcardlogon), // [1.3.6.1.4.1.311.20.2.2]

  71.     MAC_ADDRESS(org.bouncycastle.asn1.x509.KeyPurposeId.id_kp_macAddress), // [1.3.6.1.1.1.1.22]

  72.     MS_SGC(org.bouncycastle.asn1.x509.KeyPurposeId.id_kp_msSGC), // [1.3.6.1.4.1.311.10.3.3]
  73.    
  74.     NS_SGC(org.bouncycastle.asn1.x509.KeyPurposeId.id_kp_nsSGC); // [2.16.840.1.113730.4.1]

  75.    
  76.     ExtendedKeyUsage(org.bouncycastle.asn1.x509.KeyPurposeId purposeId ) {
  77.         this.oid = purposeId.getId();
  78.         this.purposeId = purposeId;
  79.     }
  80.    
  81.     private String oid;
  82.     private org.bouncycastle.asn1.x509.KeyPurposeId purposeId;
  83.     public String getId() {
  84.         return this.oid;
  85.     }
  86.     public org.bouncycastle.asn1.x509.KeyPurposeId getPurposeId() {
  87.         if(this.purposeId!=null) {
  88.             return this.purposeId;
  89.         }
  90.         else {
  91.             return org.bouncycastle.asn1.x509.KeyPurposeId.getInstance(new org.bouncycastle.asn1.ASN1ObjectIdentifier(this.oid));
  92.         }
  93.     }
  94.    
  95.     @Override
  96.     public String toString() {
  97.         return toString(false);
  98.     }
  99.     public String toString(boolean printOID) {
  100.         if(printOID) {
  101.             return this.name()+" ("+this.oid+")";
  102.         }
  103.         else {
  104.             return this.name();
  105.         }
  106.     }
  107.    
  108.     public boolean hasKeyUsage(Certificate x509) throws CertificateParsingException {
  109.         if(x509.getCertificate()!=null) {
  110.             return hasKeyUsage(x509.getCertificate());
  111.         }
  112.         return false;
  113.     }
  114.     public boolean hasKeyUsage(CertificateInfo x509) throws CertificateParsingException {
  115.         return hasKeyUsage(x509.getCertificate());
  116.     }
  117.     public boolean hasKeyUsage(X509Certificate x509) throws CertificateParsingException {
  118.         return existsKeyUsageByOID(x509, this.oid);
  119.     }
  120.     public static boolean existsKeyUsageByOID(X509Certificate x509, String oid) throws CertificateParsingException {
  121.         if(x509.getExtendedKeyUsage()!=null) {
  122.             return x509.getExtendedKeyUsage().contains(oid);
  123.         }
  124.         return false;
  125.     }
  126.    
  127.     public boolean hasKeyUsage(byte[]encoded) {
  128.         return existsKeyUsageByBouncycastleKeyPurposeId(encoded, this.purposeId);
  129.     }
  130.     public static boolean existsKeyUsageByBouncycastleKeyPurposeId(byte[]encoded, String keyPurposeId)  {
  131.         return existsKeyUsageByBouncycastleKeyPurposeId(encoded, org.bouncycastle.asn1.x509.KeyPurposeId.getInstance(new org.bouncycastle.asn1.ASN1ObjectIdentifier(keyPurposeId)));
  132.     }
  133.     public static boolean existsKeyUsageByBouncycastleKeyPurposeId(byte[]encoded, org.bouncycastle.asn1.x509.KeyPurposeId keyPurposeId)  {
  134.         org.bouncycastle.asn1.x509.Certificate c =org.bouncycastle.asn1.x509.Certificate.getInstance(encoded);
  135.         Extensions exts = c.getTBSCertificate().getExtensions();
  136.         if (exts != null){
  137.             org.bouncycastle.asn1.x509.ExtendedKeyUsage eKey = org.bouncycastle.asn1.x509.ExtendedKeyUsage.fromExtensions(exts);
  138.             if(eKey!=null) {
  139.                 return eKey.hasKeyPurposeId(keyPurposeId);
  140.             }
  141.         }
  142.         return false;
  143.     }
  144.    
  145.     public static ExtendedKeyUsage toExtendedKeyUsage(String id) {
  146.         ExtendedKeyUsage [] v = ExtendedKeyUsage.values();
  147.         for (ExtendedKeyUsage usage : v) {
  148.             if(usage.oid.equals(id)) {
  149.                 return usage;
  150.             }
  151.         }
  152.         return null;
  153.     }
  154.     public static ExtendedKeyUsage toExtendedKeyUsage(org.bouncycastle.asn1.x509.KeyPurposeId purposeId) {
  155.         ExtendedKeyUsage [] v = ExtendedKeyUsage.values();
  156.         for (ExtendedKeyUsage usage : v) {
  157.             if(usage.purposeId.equals(purposeId)) {
  158.                 return usage;
  159.             }
  160.         }
  161.         return null;
  162.     }
  163.    
  164.     public static List<ExtendedKeyUsage> getKeyUsage(Certificate x509) throws CertificateParsingException{
  165.         if(x509.getCertificate()!=null) {
  166.             return getKeyUsage(x509.getCertificate());
  167.         }
  168.         return new ArrayList<>();
  169.     }
  170.     public static List<ExtendedKeyUsage> getKeyUsage(CertificateInfo x509) throws CertificateParsingException{
  171.         return getKeyUsage(x509.getCertificate());
  172.     }
  173.     public static List<ExtendedKeyUsage> getKeyUsage(X509Certificate x509) throws CertificateParsingException{
  174.         List<ExtendedKeyUsage> l = new ArrayList<>();
  175.         ExtendedKeyUsage [] values = ExtendedKeyUsage.values();
  176.         for (ExtendedKeyUsage keyUsage : values) {
  177.             if(keyUsage.hasKeyUsage(x509)) {
  178.                 l.add(keyUsage);
  179.             }
  180.         }
  181.         return l;
  182.     }
  183.     public static List<ExtendedKeyUsage> getKeyUsage(byte[]encoded){
  184.         List<ExtendedKeyUsage> l = new ArrayList<>();
  185.         ExtendedKeyUsage [] values = ExtendedKeyUsage.values();
  186.         for (ExtendedKeyUsage keyUsage : values) {
  187.             if(keyUsage.hasKeyUsage(encoded)) {
  188.                 l.add(keyUsage);
  189.             }
  190.         }
  191.         return l;
  192.     }
  193. }