KeyUsage.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.X509Certificate;
  22. import java.util.ArrayList;
  23. import java.util.List;

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

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

  33.     /**
  34.      *  KeyUsage ::= BIT STRING {
  35.              digitalSignature        (0),
  36.              nonRepudiation          (1),
  37.              keyEncipherment         (2),
  38.              dataEncipherment        (3),
  39.              keyAgreement            (4),
  40.              keyCertSign             (5),
  41.              cRLSign                 (6),
  42.              encipherOnly            (7),
  43.              decipherOnly            (8)
  44.      **/
  45.    
  46.     DIGITAL_SIGNATURE(0, org.bouncycastle.asn1.x509.KeyUsage.digitalSignature),
  47.    
  48.     NON_REPUDIATION(1, org.bouncycastle.asn1.x509.KeyUsage.nonRepudiation),

  49.     KEY_ENCIPHERMENT(2, org.bouncycastle.asn1.x509.KeyUsage.keyEncipherment),
  50.    
  51.     DATA_ENCIPHERMENT(3, org.bouncycastle.asn1.x509.KeyUsage.dataEncipherment),
  52.    
  53.     KEY_AGREEMENT(4, org.bouncycastle.asn1.x509.KeyUsage.keyAgreement),
  54.    
  55.     KEY_CERT_SIGN(5, org.bouncycastle.asn1.x509.KeyUsage.keyCertSign),
  56.    
  57.     CRL_SIGN(6, org.bouncycastle.asn1.x509.KeyUsage.cRLSign),
  58.    
  59.     ENCIPHER_ONLY(7, org.bouncycastle.asn1.x509.KeyUsage.encipherOnly),
  60.    
  61.     DECIPHER_ONLY(8, org.bouncycastle.asn1.x509.KeyUsage.decipherOnly);
  62.      
  63.    
  64.     KeyUsage(int arrayBooleanPosition, int bouncyValue ) {
  65.         this.arrayBooleanPosition = arrayBooleanPosition;
  66.         this.bouncyValue = bouncyValue;
  67.     }
  68.    
  69.     private int arrayBooleanPosition;
  70.     private int bouncyValue;

  71.     public int getX509CertificatePosition() {
  72.         return this.arrayBooleanPosition;
  73.     }
  74.     public int getBouncyCastleCode() {
  75.         return this.bouncyValue;
  76.     }
  77.    
  78.     public boolean hasKeyUsage(Certificate x509) {
  79.         if(x509.getCertificate()!=null) {
  80.             return hasKeyUsage(x509.getCertificate());
  81.         }
  82.         return false;
  83.     }
  84.     public boolean hasKeyUsage(CertificateInfo x509) {
  85.         return hasKeyUsage(x509.getCertificate());
  86.     }
  87.     public boolean hasKeyUsage(X509Certificate x509) {
  88.         return existsKeyUsageByArrayBooleanPosition(x509, this.arrayBooleanPosition);
  89.     }
  90.     public static boolean existsKeyUsageByArrayBooleanPosition(X509Certificate x509, int arrayBooleanPosition) {
  91.         if(x509.getKeyUsage()!=null && x509.getKeyUsage().length>arrayBooleanPosition) {
  92.             return x509.getKeyUsage()[arrayBooleanPosition];
  93.         }
  94.         return false;
  95.     }
  96.    
  97.     @Override
  98.     public String toString() {
  99.         return toString(false);
  100.     }
  101.     public String toString(boolean printArrayPosition) {
  102.         if(printArrayPosition) {
  103.             return this.name()+" ("+this.arrayBooleanPosition+")";
  104.         }
  105.         else {
  106.             return this.name();
  107.         }
  108.     }
  109.    
  110.     public boolean hasKeyUsage(byte[]encoded) {
  111.         return existsKeyUsageByBouncycastleCode(encoded, this.bouncyValue);
  112.     }
  113.     public static boolean existsKeyUsageByBouncycastleCode(byte[]encoded, int bouncyValue) {
  114.         org.bouncycastle.asn1.x509.Certificate c =org.bouncycastle.asn1.x509.Certificate.getInstance(encoded);
  115.         Extensions exts = c.getTBSCertificate().getExtensions();
  116.         if (exts != null){
  117.             org.bouncycastle.asn1.x509.KeyUsage ku = org.bouncycastle.asn1.x509.KeyUsage.fromExtensions(exts);
  118.             if(ku!=null) {
  119.                 return ku.hasUsages(bouncyValue);
  120.             }
  121.         }
  122.         return false;
  123.     }
  124.        
  125.     public static List<KeyUsage> getKeyUsage(Certificate x509){
  126.         if(x509.getCertificate()!=null) {
  127.             return getKeyUsage(x509.getCertificate());
  128.         }
  129.         return new ArrayList<>();
  130.     }
  131.     public static List<KeyUsage> getKeyUsage(CertificateInfo x509){
  132.         return getKeyUsage(x509.getCertificate());
  133.     }
  134.     public static List<KeyUsage> getKeyUsage(X509Certificate x509){
  135.         List<KeyUsage> l = new ArrayList<>();
  136.         KeyUsage [] values = KeyUsage.values();
  137.         for (KeyUsage keyUsage : values) {
  138.             if(keyUsage.hasKeyUsage(x509)) {
  139.                 l.add(keyUsage);
  140.             }
  141.         }
  142.         return l;
  143.     }
  144.     public static List<KeyUsage> getKeyUsage(byte[]encoded){
  145.         List<KeyUsage> l = new ArrayList<>();
  146.         KeyUsage [] values = KeyUsage.values();
  147.         for (KeyUsage keyUsage : values) {
  148.             if(keyUsage.hasKeyUsage(encoded)) {
  149.                 l.add(keyUsage);
  150.             }
  151.         }
  152.         return l;
  153.     }
  154.    
  155. }