OCSPTrustManager.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.transport.http;

  21. import java.security.cert.CertificateException;
  22. import java.security.cert.X509Certificate;

  23. import javax.net.ssl.TrustManager;
  24. import javax.net.ssl.X509TrustManager;

  25. import org.openspcoop2.utils.certificate.KeyStore;
  26. import org.openspcoop2.utils.certificate.KeystoreType;

  27. /**
  28.  * OCSPTrustManager
  29.  *
  30.  * @author Poli Andrea (apoli@link.it)
  31.  * @author $Author$
  32.  * @version $Rev$, $Date$
  33.  */
  34. public class OCSPTrustManager implements X509TrustManager {

  35.     private IOCSPValidator ocspValidator;
  36.     private X509TrustManager tm;
  37.     private X509Certificate[] chain;
  38.     private boolean trustAll;
  39.    
  40.     public X509Certificate[] getPeerCertificates() {
  41.         return this.chain;
  42.     }

  43.     public OCSPTrustManager(X509TrustManager tm, IOCSPValidator ocspValidator) {
  44.         this.tm = tm;
  45.         this.ocspValidator = ocspValidator;
  46.         this.trustAll = tm instanceof SSLTrustAllManager;
  47.     }

  48.     @Override
  49.     public X509Certificate[] getAcceptedIssuers() {
  50.         return this.tm.getAcceptedIssuers();
  51.     }

  52.     @Override
  53.     public void checkClientTrusted(X509Certificate[] chain, String authType)
  54.             throws CertificateException {
  55.         this.tm.checkClientTrusted(chain, authType);
  56.     }

  57.     @Override
  58.     public void checkServerTrusted(X509Certificate[] chain, String authType)
  59.             throws CertificateException {
  60.         this.chain = chain;
  61.         this.tm.checkServerTrusted(chain, authType);
  62.        
  63.         if(chain!=null && chain.length>0) {
  64.            
  65.             if(this.trustAll) {
  66.                 // non ho usato un truststore, ne genero uno con i certificati server ritornati dal server, che ho accettato
  67.                 // il truststore serve per porte generare la richiesta OCSP
  68.                 if(this.ocspValidator.getTrustStore()==null) {
  69.                     try {
  70.                         java.security.KeyStore ks = java.security.KeyStore.getInstance(KeystoreType.JKS.getNome());
  71.                         ks.load(null, null);
  72.                         KeyStore trustStore = new KeyStore(ks);
  73.                         int index = 0;
  74.                         for (X509Certificate x509Certificate : chain) {
  75.                             trustStore.putCertificate("cert-"+index, x509Certificate, false);
  76.                             index++;
  77.                         }
  78. //                      java.util.Enumeration<String> en = trustStore.aliases();
  79. //                      while (en.hasMoreElements()) {
  80. //                          String alias = (String) en.nextElement();
  81. //                          System.out.println(alias);  
  82. //                      }
  83.                         this.ocspValidator.setTrustStore(trustStore);
  84.                     }catch(Throwable t) {
  85.                         throw new CertificateException(t.getMessage(),t);
  86.                     }
  87.                 }
  88.             }
  89.            
  90.             try {
  91.                 // valido il certificato principale nell'OCSP (la validazione a catena avviene all'interno del motore se abilitato a partire dal certificato principale)
  92.                 this.ocspValidator.valid(chain[0]);
  93.             }catch(Throwable t) {
  94.                 throw new CertificateException(t.getMessage(),t);
  95.             }
  96.         }
  97.     }

  98.     public static TrustManager[] wrap(TrustManager[] tmArray, IOCSPValidator ocspValidator) {
  99.         if(tmArray!=null && tmArray.length>0) {
  100.             for (int i = 0; i < tmArray.length; i++) {
  101.                 TrustManager tm = tmArray[i];
  102.                 if(tm!=null && tm instanceof X509TrustManager) {
  103.                     // wrap
  104.                     // clono perche' nel caso di SSLTrustAll si tratta di una istanza statica
  105.                     TrustManager[] cloned = new TrustManager[tmArray.length];
  106.                     for (int j = 0; j < tmArray.length; j++) {
  107.                         if(j == i) {
  108.                             cloned[j] = new OCSPTrustManager((X509TrustManager)tm, ocspValidator);
  109.                         }
  110.                         else {
  111.                             cloned[j] = tmArray[j];
  112.                         }
  113.                     }
  114.                     return cloned;
  115.                 }
  116.             }
  117.         }
  118.         return tmArray;
  119.     }
  120.    
  121.     public static OCSPTrustManager read(TrustManager[] tmArray) {
  122.         if(tmArray!=null && tmArray.length>0) {
  123.             for (int i = 0; i < tmArray.length; i++) {
  124.                 TrustManager tm = tmArray[i];
  125.                 if(tm!=null && tm instanceof OCSPTrustManager) {
  126.                     return (OCSPTrustManager) tm;
  127.                 }
  128.             }
  129.         }
  130.         return null;
  131.     }
  132. }