OCSPRequestParams.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 java.security.cert.X509Certificate;
  22. import java.util.ArrayList;
  23. import java.util.HashMap;
  24. import java.util.List;
  25. import java.util.Map;

  26. import org.openspcoop2.utils.LoggerBuffer;
  27. import org.openspcoop2.utils.UtilsException;
  28. import org.openspcoop2.utils.UtilsMultiException;
  29. import org.openspcoop2.utils.certificate.ArchiveLoader;
  30. import org.openspcoop2.utils.certificate.AuthorityInformationAccess;
  31. import org.openspcoop2.utils.certificate.Certificate;
  32. import org.openspcoop2.utils.certificate.CertificateInfo;
  33. import org.openspcoop2.utils.certificate.KeyStore;

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

  42.     private X509Certificate certificate;
  43.     private X509Certificate issuerCertificate;
  44.     private KeyStore issuerTrustStore;
  45.     private X509Certificate signerCertificate;
  46.     private KeyStore signerTrustStore;
  47.     private List<String> responderURIs;
  48.     private KeyStore httpsTrustStore;
  49.     private KeyStore httpsKeyStore;
  50.     private OCSPConfig config;
  51.     private boolean isSelfSigned;
  52.     private boolean isCA;
  53.    
  54.     // serve per validare la catena
  55.     private KeyStore configTrustStore;
  56.     private IOCSPResourceReader reader;
  57.    
  58.     public boolean isSelfSigned() {
  59.         return this.isSelfSigned;
  60.     }
  61.     public void setSelfSigned(boolean isSelfSigned) {
  62.         this.isSelfSigned = isSelfSigned;
  63.     }
  64.     public boolean isCA() {
  65.         return this.isCA;
  66.     }
  67.     public void setCA(boolean isCA) {
  68.         this.isCA = isCA;
  69.     }
  70.     public OCSPConfig getConfig() {
  71.         return this.config;
  72.     }
  73.     public void setConfig(OCSPConfig config) {
  74.         this.config = config;
  75.     }
  76.     public X509Certificate getCertificate() {
  77.         return this.certificate;
  78.     }
  79.     public void setCertificate(X509Certificate certificate) {
  80.         this.certificate = certificate;
  81.     }
  82.     public X509Certificate getIssuerCertificate() {
  83.         return this.issuerCertificate;
  84.     }
  85.     public void setIssuerCertificate(X509Certificate issuerCertificate) {
  86.         this.issuerCertificate = issuerCertificate;
  87.     }
  88.     public KeyStore getIssuerTrustStore() {
  89.         return this.issuerTrustStore;
  90.     }
  91.     public void setIssuerTrustStore(KeyStore issuerTrustStore) {
  92.         this.issuerTrustStore = issuerTrustStore;
  93.     }
  94.     public X509Certificate getSignerCertificate() {
  95.         return this.signerCertificate;
  96.     }
  97.     public void setSignerCertificate(X509Certificate signerCertificate) {
  98.         this.signerCertificate = signerCertificate;
  99.     }
  100.     public KeyStore getSignerTrustStore() {
  101.         return this.signerTrustStore;
  102.     }
  103.     public void setSignerTrustStore(KeyStore signerTrustStore) {
  104.         this.signerTrustStore = signerTrustStore;
  105.     }
  106.     public List<String> getResponderURIs() {
  107.         return this.responderURIs;
  108.     }
  109.     public void setResponderURIs(List<String> responderURIs) {
  110.         this.responderURIs = responderURIs;
  111.     }
  112.     public KeyStore getHttpsTrustStore() {
  113.         return this.httpsTrustStore;
  114.     }
  115.     public void setHttpsTrustStore(KeyStore httpsTrustStore) {
  116.         this.httpsTrustStore = httpsTrustStore;
  117.     }
  118.     public KeyStore getHttpsKeyStore() {
  119.         return this.httpsKeyStore;
  120.     }
  121.     public void setHttpsKeyStore(KeyStore httpsKeyStore) {
  122.         this.httpsKeyStore = httpsKeyStore;
  123.     }
  124.    
  125.     public KeyStore getConfigTrustStore() {
  126.         return this.configTrustStore;
  127.     }
  128.     public void setConfigTrustStore(KeyStore configTrustStore) {
  129.         this.configTrustStore = configTrustStore;
  130.     }
  131.     public IOCSPResourceReader getReader() {
  132.         return this.reader;
  133.     }
  134.     public void setReader(IOCSPResourceReader reader) {
  135.         this.reader = reader;
  136.     }
  137.    
  138.     public static OCSPRequestParams build(LoggerBuffer log, X509Certificate certificate, KeyStore trustStore, OCSPConfig config, IOCSPResourceReader reader) throws UtilsException {
  139.         CertificateInfo cer = new CertificateInfo(certificate, "ocspVerifica");
  140.         return build(log, cer, trustStore, config, reader);
  141.     }
  142.     public static OCSPRequestParams build(LoggerBuffer log, CertificateInfo certificate, KeyStore trustStore, OCSPConfig config, IOCSPResourceReader reader) throws UtilsException {
  143.    
  144.         if(config==null) {
  145.             throw new UtilsException("Param config is null");
  146.         }
  147.         if(certificate==null) {
  148.             throw new UtilsException("Param certificate is null");
  149.         }
  150.        
  151.         OCSPRequestParams params = new OCSPRequestParams();
  152.         params.config = config;
  153.         params.certificate = certificate.getCertificate();
  154.         try {
  155.             params.isCA = certificate.isCA();
  156.         }catch(Exception t) {
  157.             throw new UtilsException(t.getMessage(),t);
  158.         }
  159.         params.isSelfSigned = certificate.isSelfSigned();
  160.        
  161.         params.configTrustStore = trustStore;
  162.         params.reader = reader;
  163.        
  164.         if(!params.isSelfSigned) {
  165.        
  166.             reader.initConfig(config);
  167.            
  168.             // comprendo issuer del certificato da validare
  169.             if(config.getCaSource()!=null && !config.getCaSource().isEmpty()) {
  170.                 for (CertificateSource s : config.getCaSource()) {
  171.                     if(CertificateSource.CONFIG.equals(s)) {
  172.                         if(trustStore!=null) {
  173.                             params.issuerCertificate = (X509Certificate) trustStore.getCertificateBySubject(params.certificate.getIssuerX500Principal());
  174.                             if(params.issuerCertificate!=null) {
  175.                                 params.issuerTrustStore = trustStore;
  176.                                 log.debug("OCSP IssuerCertificate: use truststore config");
  177.                                 break;
  178.                             }
  179.                         }
  180.                     }
  181.                     else if(CertificateSource.ALTERNATIVE_CONFIG.equals(s)) {
  182.                         KeyStore alternativeTrustStore = null;
  183.                         if(config.getAlternativeTrustStoreCAPath()!=null) {
  184.                             alternativeTrustStore = reader.getIssuerAlternativeTrustStore();
  185.                         }
  186.                         if(alternativeTrustStore!=null) {
  187.                             params.issuerCertificate = (X509Certificate) alternativeTrustStore.getCertificateBySubject(params.certificate.getIssuerX500Principal());
  188.                             if(params.issuerCertificate!=null) {
  189.                                 params.issuerTrustStore = alternativeTrustStore;
  190.                                 log.debug("OCSP IssuerCertificate: use alternative truststore config");
  191.                                 break;
  192.                             }
  193.                         }
  194.                     }
  195.                     else if(CertificateSource.AUTHORITY_INFORMATION_ACCESS.equals(s)) {
  196.                         try {
  197.                             AuthorityInformationAccess aia = certificate.getAuthorityInformationAccess();
  198.                             if(aia!=null) {
  199.                                 List<String> issuer = aia.getCAIssuers();
  200.                                 if(issuer!=null && !issuer.isEmpty()) {
  201.                                     List<Exception> listExceptions = new ArrayList<>();
  202.                                     StringBuilder sbError = new StringBuilder();
  203.                                     boolean find = false;
  204.                                     for (String urlIssuer : issuer) {
  205.                                         Map<String, byte[]> map = new HashMap<>();
  206.                                         try {
  207.                                             reader.readExternalResource(urlIssuer, map);
  208.                                             if(!map.isEmpty()) {
  209.                                                 byte [] issCert = map.get(urlIssuer);
  210.                                                 if(issCert!=null) {
  211.                                                     Certificate cer = ArchiveLoader.load(issCert);
  212.                                                     if(cer!=null && cer.getCertificate()!=null) {
  213.                                                         params.issuerCertificate = cer.getCertificate().getCertificate();
  214.                                                         if(params.issuerCertificate!=null) {
  215.                                                             log.debug("OCSP IssuerCertificate: retrieved from external url '"+urlIssuer+"'");
  216.                                                             find = true;
  217.                                                             break;
  218.                                                         }
  219.                                                     }
  220.                                                 }
  221.                                             }
  222.                                         }catch(Exception t) {
  223.                                             String msgError = "[AuthorityInformationAccess-CAIssuer: "+urlIssuer+"] retrieve failed: "+t.getMessage();
  224.                                             if(sbError.length()>0) {
  225.                                                 sbError.append("\n");
  226.                                             }
  227.                                             sbError.append(msgError);
  228.                                             log.debug("OCSP "+msgError,t);
  229.                                             listExceptions.add(new Exception(msgError,t));
  230.                                         }
  231.                                     }
  232.                                     if(find) {
  233.                                         break;
  234.                                     }
  235.                                     if(!listExceptions.isEmpty()) {
  236.                                         throw new UtilsMultiException("OCSP IssuerCertificate retrieve failed: "+sbError.toString(), listExceptions.toArray(new Throwable[1]));
  237.                                     }
  238.                                 }
  239.                             }
  240.                         }catch(Exception t) {
  241.                             throw new UtilsException(t.getMessage(),t);
  242.                         }
  243.                     }
  244.                 }
  245.             }
  246.            
  247.             // comprendo signer
  248.             KeyStore signerTrustStore = reader.getSignerTrustStore();
  249.             if(config.getAliasCertificateSigner()!=null) {
  250.                 try {
  251.                     params.signerCertificate = (X509Certificate) signerTrustStore.getCertificate(config.getAliasCertificateSigner());
  252.                     if(params.signerCertificate==null) {
  253.                         throw new UtilsException("Not found");
  254.                     }
  255.                     log.debug("OCSP SignerCertificate: retrieved from truststore config (alias:"+config.getAliasCertificateSigner()+")");
  256.                 }catch(Exception t) {
  257.                     throw new UtilsException("Get signer certificate failed: "+t.getMessage(),t);
  258.                 }
  259.             }
  260.             else {
  261.                 params.signerTrustStore = signerTrustStore;
  262.                 if(signerTrustStore!=null) {
  263.                     log.debug("OCSP SignerCertificate: use truststore config");
  264.                 }
  265.             }
  266.            
  267.             // responder url
  268.             params.responderURIs = new ArrayList<>();
  269.             if(config.getResponderUrlSource()!=null && !config.getResponderUrlSource().isEmpty()) {
  270.                 for (CertificateSource s : config.getResponderUrlSource()) {
  271.                     if(CertificateSource.CONFIG.equals(s)) {
  272.                         throw new UtilsException("Unsupported");
  273.                     }
  274.                     else if(CertificateSource.ALTERNATIVE_CONFIG.equals(s)) {
  275.                         List<String> alternativeUrl = params.isCA ? config.getAlternativeResponderUrlCA() : config.getAlternativeResponderUrl();
  276.                         if(alternativeUrl!=null && !alternativeUrl.isEmpty()) {
  277.                             for (String url : alternativeUrl) {
  278.                                 if(!params.responderURIs.contains(url)) {
  279.                                     params.responderURIs.add(url);
  280.                                     log.debug("OCSP ResponderURL: add alternative url '"+url+"'");
  281.                                 }
  282.                             }
  283.                         }
  284.                     }
  285.                     else if(CertificateSource.AUTHORITY_INFORMATION_ACCESS.equals(s)) {
  286.                         try {
  287.                             AuthorityInformationAccess aia = certificate.getAuthorityInformationAccess();
  288.                             if(aia!=null) {
  289.                                 List<String> ocsps = aia.getOCSPs();
  290.                                 if(ocsps!=null && !ocsps.isEmpty()) {
  291.                                     String urlOcsp = ocsps.get(0);
  292.                                     if(!params.responderURIs.contains(urlOcsp)) {
  293.                                         params.responderURIs.add(urlOcsp);
  294.                                         log.debug("OCSP ResponderURL: add external url '"+urlOcsp+"'");
  295.                                     }
  296.                                 }
  297.                             }
  298.                         }catch(Exception t) {
  299.                             throw new UtilsException(t.getMessage(),t);
  300.                         }
  301.                     }
  302.                 }
  303.             }
  304.            
  305.             // https truststore
  306.             params.httpsTrustStore = reader.getHttpsTrustStore();
  307.            
  308.             // https keystore
  309.             params.httpsKeyStore = reader.getHttpsKeyStore();
  310.         }
  311.        
  312.         return params;
  313.     }
  314.    
  315. }