TrustManagerUtils.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.net.util;

  18. import java.security.GeneralSecurityException;
  19. import java.security.KeyStore;
  20. import java.security.cert.CertificateException;
  21. import java.security.cert.X509Certificate;

  22. import javax.net.ssl.TrustManagerFactory;
  23. import javax.net.ssl.X509TrustManager;

  24. /**
  25.  * TrustManager utilities for generating TrustManagers.
  26.  *
  27.  * @since 3.0
  28.  */
  29. public final class TrustManagerUtils {

  30.     private static final class TrustManager implements X509TrustManager {

  31.         private final boolean checkServerValidity;

  32.         TrustManager(final boolean checkServerValidity) {
  33.             this.checkServerValidity = checkServerValidity;
  34.         }

  35.         /**
  36.          * Never generates a CertificateException.
  37.          */
  38.         @Override
  39.         public void checkClientTrusted(final X509Certificate[] certificates, final String authType) {
  40.             // empty
  41.         }

  42.         @Override
  43.         public void checkServerTrusted(final X509Certificate[] certificates, final String authType) throws CertificateException {
  44.             if (checkServerValidity) {
  45.                 for (final X509Certificate certificate : certificates) {
  46.                     certificate.checkValidity();
  47.                 }
  48.             }
  49.         }

  50.         /**
  51.          * @return an empty array of certificates
  52.          */
  53.         @Override
  54.         public X509Certificate[] getAcceptedIssuers() {
  55.             return NetConstants.EMPTY_X509_CERTIFICATE_ARRAY;
  56.         }
  57.     }

  58.     private static final X509TrustManager ACCEPT_ALL = new TrustManager(false);

  59.     private static final X509TrustManager CHECK_SERVER_VALIDITY = new TrustManager(true);

  60.     /**
  61.      * Generate a TrustManager that performs no checks.
  62.      *
  63.      * @return the TrustManager
  64.      */
  65.     public static X509TrustManager getAcceptAllTrustManager() {
  66.         return ACCEPT_ALL;
  67.     }

  68.     /**
  69.      * Return the default TrustManager provided by the JVM.
  70.      * <p>
  71.      * This should be the same as the default used by
  72.      * {@link javax.net.ssl.SSLContext#init(javax.net.ssl.KeyManager[], javax.net.ssl.TrustManager[], java.security.SecureRandom) SSLContext#init(KeyManager[],
  73.      * TrustManager[], SecureRandom)} when the TrustManager parameter is set to {@code null}
  74.      *
  75.      * @param keyStore the KeyStore to use, may be {@code null}
  76.      * @return the default TrustManager
  77.      * @throws GeneralSecurityException if an error occurs
  78.      */
  79.     public static X509TrustManager getDefaultTrustManager(final KeyStore keyStore) throws GeneralSecurityException {
  80.         final String defaultAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
  81.         final TrustManagerFactory instance = TrustManagerFactory.getInstance(defaultAlgorithm);
  82.         instance.init(keyStore);
  83.         return (X509TrustManager) instance.getTrustManagers()[0];
  84.     }

  85.     /**
  86.      * Generate a TrustManager that checks server certificates for validity, but otherwise performs no checks.
  87.      *
  88.      * @return the validating TrustManager
  89.      */
  90.     public static X509TrustManager getValidateServerCertificateTrustManager() {
  91.         return CHECK_SERVER_VALIDITY;
  92.     }

  93.     /**
  94.      * Depreacted.
  95.      *
  96.      * @deprecated Will be removed in 2.0.
  97.      */
  98.     @Deprecated
  99.     public TrustManagerUtils() {
  100.         // empty
  101.     }

  102. }