001/*
002 * Copyright 2020-2022 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2020-2022 Ping Identity Corporation
007 *
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 *    http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 */
020/*
021 * Copyright (C) 2020-2022 Ping Identity Corporation
022 *
023 * This program is free software; you can redistribute it and/or modify
024 * it under the terms of the GNU General Public License (GPLv2 only)
025 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
026 * as published by the Free Software Foundation.
027 *
028 * This program is distributed in the hope that it will be useful,
029 * but WITHOUT ANY WARRANTY; without even the implied warranty of
030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
031 * GNU General Public License for more details.
032 *
033 * You should have received a copy of the GNU General Public License
034 * along with this program; if not, see <http://www.gnu.org/licenses>.
035 */
036package com.unboundid.util.ssl;
037
038
039
040import java.io.Serializable;
041import java.security.cert.CertificateException;
042import java.security.cert.X509Certificate;
043import javax.net.ssl.X509TrustManager;
044
045import com.unboundid.util.NotMutable;
046import com.unboundid.util.NotNull;
047import com.unboundid.util.ThreadSafety;
048import com.unboundid.util.ThreadSafetyLevel;
049
050import static com.unboundid.util.ssl.SSLMessages.*;
051
052
053
054/**
055 * This class provides an SSL trust manager that will not trust any
056 * certificates.  It is primarily useful for testing purposes.
057 */
058@NotMutable()
059@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
060public final class NullTrustManager
061       implements X509TrustManager, Serializable
062{
063  /**
064   * A pre-allocated empty certificate array.
065   */
066  @NotNull private static final X509Certificate[] NO_CERTIFICATES =
067       new X509Certificate[0];
068
069
070
071  /**
072   * A singleton instance of this trust manager.
073   */
074  @NotNull private static final NullTrustManager INSTANCE =
075       new NullTrustManager();
076
077
078
079  /**
080   * The serial version UID for this serializable class.
081   */
082  private static final long serialVersionUID = 2435783503919293156L;
083
084
085
086  /**
087   * Creates a new instance of this trust all trust manager that will trust
088   * any certificate, including certificates that are expired or not yet valid.
089   */
090  private NullTrustManager()
091  {
092    // No implementation is required.
093  }
094
095
096
097  /**
098   * Retrieves the singleton instance of this class.
099   *
100   * @return  The singleton instance of this class.
101   */
102  @NotNull()
103  public static NullTrustManager getInstance()
104  {
105    return INSTANCE;
106  }
107
108
109
110  /**
111   * Checks to determine whether the provided client certificate chain should be
112   * trusted.  A certificate will only be rejected (by throwing a
113   * {@link CertificateException}) if certificate validity dates should be
114   * examined and the certificate or any of its issuers is outside of the
115   * validity window.
116   *
117   * @param  chain     The client certificate chain for which to make the
118   *                   determination.
119   * @param  authType  The authentication type based on the client certificate.
120   *
121   * @throws  CertificateException  If the provided client certificate chain
122   *                                should not be trusted.
123   */
124  @Override()
125  public void checkClientTrusted(@NotNull final X509Certificate[] chain,
126                                 @NotNull final String authType)
127         throws CertificateException
128  {
129    throw new CertificateException(
130         ERR_NULL_TRUST_MANAGER_CERT_NOT_TRUSTED.get());
131  }
132
133
134
135  /**
136   * Checks to determine whether the provided server certificate chain should be
137   * trusted.  A certificate will only be rejected (by throwing a
138   * {@link CertificateException}) if certificate validity dates should be
139   * examined and the certificate or any of its issuers is outside of the
140   * validity window.
141   *
142   * @param  chain     The server certificate chain for which to make the
143   *                   determination.
144   * @param  authType  The key exchange algorithm used.
145   *
146   * @throws  CertificateException  If the provided server certificate chain
147   *                                should not be trusted.
148   */
149  @Override()
150  public void checkServerTrusted(@NotNull final X509Certificate[] chain,
151                                 @NotNull final String authType)
152         throws CertificateException
153  {
154    throw new CertificateException(
155         ERR_NULL_TRUST_MANAGER_CERT_NOT_TRUSTED.get());
156  }
157
158
159
160  /**
161   * Retrieves the accepted issuer certificates for this trust manager.  This
162   * will always return an empty array.
163   *
164   * @return  The accepted issuer certificates for this trust manager.
165   */
166  @Override()
167  @NotNull()
168  public X509Certificate[] getAcceptedIssuers()
169  {
170    return NO_CERTIFICATES;
171  }
172}