001/*
002 * Copyright 2017-2024 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2017-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) 2017-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.cert;
037
038
039
040import java.util.ArrayList;
041import java.util.Collections;
042import java.util.Iterator;
043import java.util.List;
044
045import com.unboundid.asn1.ASN1Element;
046import com.unboundid.asn1.ASN1Sequence;
047import com.unboundid.util.Debug;
048import com.unboundid.util.NotMutable;
049import com.unboundid.util.NotNull;
050import com.unboundid.util.OID;
051import com.unboundid.util.StaticUtils;
052import com.unboundid.util.ThreadSafety;
053import com.unboundid.util.ThreadSafetyLevel;
054
055import static com.unboundid.util.ssl.cert.CertMessages.*;
056
057
058
059/**
060 * This class provides an implementation of the CRL distribution points X.509
061 * certificate extension as described in
062 * <A HREF="https://www.ietf.org/rfc/rfc5280.txt">RFC 5280</A> section 4.2.1.13.
063 * This can be used to provide information about the location of certificate
064 * revocation lists (CRLs) that can be examined to check the validity of this
065 * certificate.
066 * <BR><BR>
067 * The OID for this extension is 2.5.29.31 and the value has the following
068 * encoding:
069 * <PRE>
070 *   CRLDistributionPoints ::= SEQUENCE SIZE (1..MAX) OF DistributionPoint
071 *
072 *   DistributionPoint ::= SEQUENCE {
073 *        distributionPoint       [0]     DistributionPointName OPTIONAL,
074 *        reasons                 [1]     ReasonFlags OPTIONAL,
075 *        cRLIssuer               [2]     GeneralNames OPTIONAL }
076 *
077 *   DistributionPointName ::= CHOICE {
078 *        fullName                [0]     GeneralNames,
079 *        nameRelativeToCRLIssuer [1]     RelativeDistinguishedName }
080 *
081 *   ReasonFlags ::= BIT STRING {
082 *        unused                  (0),
083 *        keyCompromise           (1),
084 *        cACompromise            (2),
085 *        affiliationChanged      (3),
086 *        superseded              (4),
087 *        cessationOfOperation    (5),
088 *        certificateHold         (6),
089 *        privilegeWithdrawn      (7),
090 *        aACompromise            (8) }
091 * </PRE>
092 */
093@NotMutable()
094@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
095public final class CRLDistributionPointsExtension
096       extends X509CertificateExtension
097{
098  /**
099   * The OID (2.5.29.31) for CRL distribution points extensions.
100   */
101  @NotNull public static final OID CRL_DISTRIBUTION_POINTS_OID =
102       new OID("2.5.29.31");
103
104
105
106  /**
107   * The serial version UID for this serializable class.
108   */
109  private static final long serialVersionUID = -4710958813506834961L;
110
111
112
113  // The list of CRL distribution points included in this extension.
114  @NotNull private final List<CRLDistributionPoint> crlDistributionPoints;
115
116
117
118  /**
119   * Creates a new CRL distribution points extension with the provided
120   * information.
121   *
122   * @param  isCritical             Indicates whether this extension should be
123   *                                considered critical.
124   * @param  crlDistributionPoints  The distribution points to include in this
125   *                                extension.  It must not be {@code null} or
126   *                                empty.
127   *
128   * @throws  CertException  If a problem is encountered while trying to encode
129   *                         the value for this extension.
130   */
131  CRLDistributionPointsExtension(final boolean isCritical,
132       @NotNull final List<CRLDistributionPoint> crlDistributionPoints)
133       throws CertException
134  {
135    super(CRL_DISTRIBUTION_POINTS_OID, isCritical,
136         encodeValue(crlDistributionPoints));
137
138    this.crlDistributionPoints = crlDistributionPoints;
139  }
140
141
142
143  /**
144   * Creates a new CRL distribution points extension from the provided generic
145   * extension.
146   *
147   * @param  extension  The extension to decode as a CRL distribution points
148   *                    extension.
149   *
150   * @throws  CertException  If the provided extension cannot be decoded as a
151   *                         CRL distribution points extension.
152   */
153  CRLDistributionPointsExtension(
154       @NotNull final X509CertificateExtension extension)
155       throws CertException
156  {
157    super(extension);
158
159    try
160    {
161      final ASN1Element[] elements =
162           ASN1Sequence.decodeAsSequence(extension.getValue()).elements();
163      final ArrayList<CRLDistributionPoint> dps =
164           new ArrayList<>(elements.length);
165      for (final ASN1Element e : elements)
166      {
167        dps.add(new CRLDistributionPoint(e));
168      }
169
170      crlDistributionPoints = Collections.unmodifiableList(dps);
171    }
172    catch (final Exception e)
173    {
174      Debug.debugException(e);
175      throw new CertException(
176           ERR_CRL_DP_EXTENSION_CANNOT_PARSE.get(
177                String.valueOf(extension), StaticUtils.getExceptionMessage(e)),
178           e);
179    }
180  }
181
182
183
184  /**
185   * Encodes the provided information into a form for use as the value for this
186   * extension.
187   *
188   * @param  crlDistributionPoints  The distribution points to include in this
189   *                                extension.  It must not be {@code null} or
190   *                                empty.
191   *
192   * @return  The encoded value.
193   *
194   * @throws  CertException  If a problem is encountered while trying to encode
195   *                         this extension.
196   */
197  @NotNull()
198  private static byte[] encodeValue(
199               @NotNull final List<CRLDistributionPoint> crlDistributionPoints)
200          throws CertException
201  {
202    final ArrayList<ASN1Element> elements =
203         new ArrayList<>(crlDistributionPoints.size());
204    for (final CRLDistributionPoint p : crlDistributionPoints)
205    {
206      elements.add(p.encode());
207    }
208
209    return new ASN1Sequence(elements).encode();
210  }
211
212
213
214  /**
215   * Retrieves the list of CRL distribution points included in this extension.
216   *
217   * @return  The list of CRL distribution points included in this extension.
218   */
219  @NotNull()
220  public List<CRLDistributionPoint> getCRLDistributionPoints()
221  {
222    return crlDistributionPoints;
223  }
224
225
226
227  /**
228   * {@inheritDoc}
229   */
230  @Override()
231  @NotNull()
232  public String getExtensionName()
233  {
234    return INFO_CRL_DP_EXTENSION_NAME.get();
235  }
236
237
238
239  /**
240   * {@inheritDoc}
241   */
242  @Override()
243  public void toString(@NotNull final StringBuilder buffer)
244  {
245    buffer.append("CRLDistributionPointsExtension(oid='");
246    buffer.append(getOID());
247    buffer.append("', isCritical=");
248    buffer.append(isCritical());
249    buffer.append(", distributionPoints={");
250
251    final Iterator<CRLDistributionPoint> iterator =
252         crlDistributionPoints.iterator();
253    while (iterator.hasNext())
254    {
255      iterator.next().toString(buffer);
256      if (iterator.hasNext())
257      {
258        buffer.append(", ");
259      }
260    }
261
262    buffer.append("})");
263  }
264}