001/*
002 * Copyright 2012-2024 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2012-2024 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) 2012-2024 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.security.cert.CertificateException;
041import java.security.cert.X509Certificate;
042import java.util.Date;
043import javax.net.ssl.X509TrustManager;
044import javax.security.auth.x500.X500Principal;
045
046import com.unboundid.util.NotMutable;
047import com.unboundid.util.NotNull;
048import com.unboundid.util.ThreadSafety;
049import com.unboundid.util.ThreadSafetyLevel;
050
051import static com.unboundid.util.ssl.SSLMessages.*;
052
053
054
055/**
056 * This class provides an SSL trust manager that merely checks to see whether
057 * a presented certificate is currently within its validity time window (i.e.,
058 * the current time is not earlier than the certificate's notBefore timestamp
059 * and not later than the certificate's notAfter timestamp).
060 * <BR><BR>
061 * Note that no other elements of the certificate are examined, so it is
062 * strongly recommended that this trust manager be used in an
063 * {@link AggregateTrustManager} in conjunction with other trust managers that
064 * perform other forms of validation.
065 */
066@NotMutable()
067@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
068public final class ValidityDateTrustManager
069       implements X509TrustManager
070{
071  /**
072   * A pre-allocated empty certificate array.
073   */
074  @NotNull private static final X509Certificate[] NO_CERTIFICATES =
075       new X509Certificate[0];
076
077
078
079  /**
080   * Creates a new validity date trust manager.
081   */
082  public ValidityDateTrustManager()
083  {
084    // No implementation is required.
085  }
086
087
088
089  /**
090   * Checks to determine whether the provided client certificate chain should be
091   * trusted.
092   *
093   * @param  chain     The client certificate chain for which to make the
094   *                   determination.
095   * @param  authType  The authentication type based on the client certificate.
096   *
097   * @throws  CertificateException  If the provided client certificate chain
098   *                                should not be trusted.
099   */
100  @Override()
101  public void checkClientTrusted(@NotNull final X509Certificate[] chain,
102                                 @NotNull final String authType)
103         throws CertificateException
104  {
105    checkCertificateValidity(chain[0]);
106  }
107
108
109
110  /**
111   * Checks to determine whether the provided server certificate chain should be
112   * trusted.
113   *
114   * @param  chain     The server certificate chain for which to make the
115   *                   determination.
116   * @param  authType  The key exchange algorithm used.
117   *
118   * @throws  CertificateException  If the provided server certificate chain
119   *                                should not be trusted.
120   */
121  @Override()
122  public void checkServerTrusted(@NotNull final X509Certificate[] chain,
123                                 @NotNull final String authType)
124         throws CertificateException
125  {
126    checkCertificateValidity(chain[0]);
127  }
128
129
130
131  /**
132   * Checks the provided certificate to determine whether the current time is
133   * within the certificate's validity window.
134   *
135   * @param  c  The certificate to be checked.
136   *
137   * @throws  CertificateException  If the presented certificate is outside the
138   *                                validity window.
139   */
140  private static void checkCertificateValidity(@NotNull final X509Certificate c)
141         throws CertificateException
142  {
143    final Date currentTime = new Date();
144    final Date notBefore   = c.getNotBefore();
145    final Date notAfter    = c.getNotAfter();
146
147    if (currentTime.before(notBefore))
148    {
149      throw new CertificateException(ERR_VALIDITY_TOO_EARLY.get(
150           c.getSubjectX500Principal().getName(X500Principal.RFC2253),
151           String.valueOf(notBefore)));
152    }
153
154    if (currentTime.after(c.getNotAfter()))
155    {
156      throw new CertificateException(ERR_VALIDITY_TOO_LATE.get(
157           c.getSubjectX500Principal().getName(X500Principal.RFC2253),
158           String.valueOf(notAfter)));
159    }
160  }
161
162
163
164  /**
165   * Retrieves the accepted issuer certificates for this trust manager.  This
166   * will always return an empty array.
167   *
168   * @return  The accepted issuer certificates for this trust manager.
169   */
170  @Override()
171  @NotNull()
172  public X509Certificate[] getAcceptedIssuers()
173  {
174    return NO_CERTIFICATES;
175  }
176}