OCSPResponseCode.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.ocsp;

  21. import org.bouncycastle.cert.ocsp.OCSPResp;

  22. /**
  23.  * OCSPResponseCode
  24.  *
  25.  * @author Poli Andrea (apoli@link.it)
  26.  * @author $Author$
  27.  * @version $Rev$, $Date$
  28.  */
  29. public enum OCSPResponseCode {
  30.    
  31.     SUCCESSFUL(OCSPResp.SUCCESSFUL), // 0

  32.     // https://www.rfc-editor.org/rfc/rfc6960#section-2.3

  33.     // A server produces the "malformedRequest" response if the request
  34.     //received does not conform to the OCSP syntax.
  35.     MALFORMED_REQUEST(OCSPResp.MALFORMED_REQUEST),  // 1
  36.    
  37.     // The response "internalError" indicates that the OCSP responder
  38.     //  reached an inconsistent internal state.  The query should be retried,
  39.     //   potentially with another responder.
  40.     INTERNAL_ERROR(OCSPResp.INTERNAL_ERROR),  // 2
  41.    
  42.     // In the event that the OCSP responder is operational but unable to
  43.     //   return a status for the requested certificate, the "tryLater"
  44.     //   response can be used to indicate that the service exists but is
  45.     //   temporarily unable to respond.
  46.     TRY_LATER(OCSPResp.TRY_LATER),  // 3
  47.    
  48.     // The response "sigRequired" is returned in cases where the server
  49.     //   requires that the client sign the request in order to construct a
  50.     //   response.
  51.     SIG_REQUIRED(OCSPResp.SIG_REQUIRED),  // 5
  52.    
  53.     // The response "unauthorized" is returned in cases where the client is
  54.     //   not authorized to make this query to this server or the server is not
  55.     //   capable of responding authoritatively
  56.     UNAUTHORIZED(OCSPResp.UNAUTHORIZED),  // 6
  57.    
  58.     // Unknown
  59.     UNKNOWN(-1),
  60.    
  61.     // Situazioni limite prima di avere una risposta
  62.     OCSP_BUILD_REQUEST_FAILED(-2),
  63.     OCSP_INVOKE_FAILED(-3);
  64.    
  65.    
  66.     private int code;
  67.    
  68.     OCSPResponseCode(int code)
  69.     {
  70.         this.code = code;
  71.     }

  72.     public int getCode()
  73.     {
  74.         return this.code;
  75.     }
  76.    
  77.     public String getMessage() {
  78.         switch (this) {
  79.         case SUCCESSFUL:
  80.            return "Successful";
  81.         case INTERNAL_ERROR:
  82.             return "Internal error";
  83.         case TRY_LATER:
  84.             return "Internal error, try later.";
  85.         case SIG_REQUIRED:
  86.             return "Invalid or missing signature";
  87.         case UNAUTHORIZED:
  88.             return "Unauthorized request";
  89.         case MALFORMED_REQUEST:
  90.             return "Malformed request";
  91.         case OCSP_BUILD_REQUEST_FAILED:
  92.             return "OCSP build request signed failed";
  93.         case OCSP_INVOKE_FAILED:
  94.             return "OCSP invoke failed";
  95.         default:
  96.             return "Error";
  97.         }
  98.     }
  99.    
  100.     @Override
  101.     public String toString(){
  102.         return this.name();
  103.     }
  104.    
  105.     public boolean equals(int code){
  106.         return this.code == code;
  107.     }
  108.    
  109.     public static final OCSPResponseCode toOCSPResponseCode(int code){
  110.         if(code == SUCCESSFUL.code) {
  111.             return OCSPResponseCode.SUCCESSFUL;
  112.         }
  113.         else if(code == MALFORMED_REQUEST.code) {
  114.             return OCSPResponseCode.MALFORMED_REQUEST;
  115.         }
  116.         else if(code == INTERNAL_ERROR.code) {
  117.             return OCSPResponseCode.INTERNAL_ERROR;
  118.         }
  119.         else if(code == TRY_LATER.code) {
  120.             return OCSPResponseCode.TRY_LATER;
  121.         }
  122.         else if(code == SIG_REQUIRED.code) {
  123.             return OCSPResponseCode.SIG_REQUIRED;
  124.         }
  125.         else if(code == UNAUTHORIZED.code) {
  126.             return OCSPResponseCode.UNAUTHORIZED;
  127.         }
  128.         else if(code == UNKNOWN.code) {
  129.             return OCSPResponseCode.UNKNOWN;
  130.         }
  131.         else if(code == OCSP_BUILD_REQUEST_FAILED.code) {
  132.             return OCSPResponseCode.OCSP_BUILD_REQUEST_FAILED;
  133.         }
  134.         else if(code == OCSP_INVOKE_FAILED.code) {
  135.             return OCSPResponseCode.OCSP_INVOKE_FAILED;
  136.         }
  137.        
  138.         return OCSPResponseCode.UNKNOWN;
  139.     }
  140. }