GestoreOCSPValidator.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.security.keystore.cache;

  21. import java.security.cert.X509Certificate;

  22. import org.openspcoop2.protocol.sdk.state.RequestInfo;
  23. import org.openspcoop2.security.keystore.OCSPResponse;
  24. import org.openspcoop2.utils.LoggerBuffer;
  25. import org.openspcoop2.utils.UtilsException;
  26. import org.openspcoop2.utils.certificate.KeyStore;
  27. import org.openspcoop2.utils.certificate.ocsp.IOCSPResourceReader;
  28. import org.openspcoop2.utils.certificate.ocsp.OCSPConfig;
  29. import org.openspcoop2.utils.certificate.ocsp.OCSPValidatorImpl;
  30. import org.openspcoop2.utils.transport.http.IOCSPValidator;
  31. import org.openspcoop2.utils.transport.http.OCSPResponseException;
  32. import org.openspcoop2.utils.transport.http.OCSPTrustManager;

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

  41.     private OCSPValidatorImpl validatorImpl;
  42.     private RequestInfo requestInfo;
  43.    
  44.     public GestoreOCSPValidator(RequestInfo requestInfo, LoggerBuffer log,
  45.             String crlInput,
  46.             String configType, IOCSPResourceReader ocspResourceReader) throws UtilsException {
  47.         this.validatorImpl = new OCSPValidatorImpl(log, crlInput, configType, ocspResourceReader);
  48.         this.requestInfo = requestInfo;
  49.     }
  50.     public GestoreOCSPValidator(RequestInfo requestInfo, LoggerBuffer log,
  51.             KeyStore trustStore, String crlInput,
  52.             String configType, IOCSPResourceReader ocspResourceReader) throws UtilsException {
  53.         this.validatorImpl = new OCSPValidatorImpl(log, trustStore, crlInput, configType, ocspResourceReader);
  54.         this.requestInfo = requestInfo;
  55.     }
  56.    
  57.     @Override
  58.     public void valid(X509Certificate cert) throws UtilsException {
  59.         OCSPResponse response = null;
  60.         try {
  61.             response = GestoreKeystoreCache.getOCSPResponse(this.requestInfo, this.validatorImpl, cert);
  62.             if(response==null) {
  63.                 throw new UtilsException("OCSPResponse unavailable");
  64.             }
  65.         }catch(Exception t) {
  66.             throw new UtilsException(t.getMessage(),t);
  67.         }
  68.         if(!response.isValid()) {
  69.             if(response.getException()==null) {
  70.                 throw new UtilsException("Invalid Certificate");
  71.             }
  72.             else {
  73.                 if(response.getException() instanceof OCSPResponseException) {
  74.                     throw (OCSPResponseException) response.getException();
  75.                 }
  76.                 else if(response.getException() instanceof UtilsException) {
  77.                     throw (UtilsException) response.getException();
  78.                 }
  79.                 else {
  80.                     throw new UtilsException(response.getException().getMessage(),response.getException());
  81.                 }
  82.             }
  83.         }
  84.     }
  85.     @Override
  86.     public KeyStore getTrustStore() {
  87.         return this.validatorImpl.getTrustStore();
  88.     }
  89.     @Override
  90.     public void setTrustStore(KeyStore keystore) {
  91.         this.validatorImpl.setTrustStore(keystore);
  92.     }
  93.     @Override
  94.     public OCSPTrustManager getOCSPTrustManager() {
  95.         return this.validatorImpl.getOCSPTrustManager();
  96.     }
  97.     @Override
  98.     public void setOCSPTrustManager(OCSPTrustManager trustManager) {
  99.         this.validatorImpl.setOCSPTrustManager(trustManager);
  100.     }
  101.     public OCSPConfig getOcspConfig() {
  102.         return this.validatorImpl.getOcspConfig();
  103.     }
  104. }