SSLSavingTrustManager.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. /**
  26.  * SSLSavingTrustManager
  27.  *
  28.  * @author Poli Andrea (apoli@link.it)
  29.  * @author $Author$
  30.  * @version $Rev$, $Date$
  31.  */
  32. public class SSLSavingTrustManager implements X509TrustManager {

  33.     private X509TrustManager tm;
  34.     private X509Certificate[] chain;
  35.     private boolean processClient;

  36.     public X509Certificate[] getPeerCertificates() {
  37.         return this.chain;
  38.     }

  39.     public SSLSavingTrustManager(X509TrustManager tm) {
  40.         this(tm, false);
  41.     }
  42.     public SSLSavingTrustManager(X509TrustManager tm, boolean processClient) {
  43.         this.tm = tm;
  44.         this.processClient = processClient;
  45.     }

  46.     @Override
  47.     public X509Certificate[] getAcceptedIssuers() {
  48.         if(this.processClient) {
  49.             return this.tm.getAcceptedIssuers();
  50.         }
  51.         else {
  52.             throw new UnsupportedOperationException();
  53.         }
  54.     }

  55.     @Override
  56.     public void checkClientTrusted(X509Certificate[] chain, String authType)
  57.             throws CertificateException {
  58.         if(this.processClient) {
  59.             this.tm.checkClientTrusted(chain, authType);
  60.         }
  61.         else {
  62.             throw new UnsupportedOperationException();
  63.         }
  64.     }

  65.     @Override
  66.     public void checkServerTrusted(X509Certificate[] chain, String authType)
  67.             throws CertificateException {
  68.         this.chain = chain;
  69.         this.tm.checkServerTrusted(chain, authType);
  70.     }

  71.     public static TrustManager[] wrap(TrustManager[] tmArray) {
  72.         if(tmArray!=null && tmArray.length>0) {
  73.             for (int i = 0; i < tmArray.length; i++) {
  74.                 TrustManager tm = tmArray[i];
  75.                 if(tm!=null && tm instanceof X509TrustManager) {
  76.                     // wrap

  77.                     // clono perche' nel caso di SSLTrustAll si tratta di una istanza statica
  78.                     TrustManager[] cloned = new TrustManager[tmArray.length];
  79.                     for (int j = 0; j < tmArray.length; j++) {
  80.                         if(j == i) {
  81.                             cloned[j] = new SSLSavingTrustManager((X509TrustManager)tm, true);
  82.                         }
  83.                         else {
  84.                             cloned[j] = tmArray[j];
  85.                         }
  86.                     }
  87.                     return cloned;
  88.                 }
  89.             }
  90.         }
  91.         return tmArray;
  92.     }
  93.    
  94.     public static SSLSavingTrustManager read(TrustManager[] tmArray) {
  95.         if(tmArray!=null && tmArray.length>0) {
  96.             for (int i = 0; i < tmArray.length; i++) {
  97.                 TrustManager tm = tmArray[i];
  98.                 if(tm!=null && tm instanceof SSLSavingTrustManager) {
  99.                     return (SSLSavingTrustManager) tm;
  100.                 }
  101.             }
  102.         }
  103.         return null;
  104.     }
  105. }