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 com.unboundid.util.NotNull;
041import com.unboundid.util.Nullable;
042import com.unboundid.util.StaticUtils;
043import com.unboundid.util.ThreadSafety;
044import com.unboundid.util.ThreadSafetyLevel;
045
046
047
048/**
049 * This enum defines a set of supported X.509 certificate versions.
050 */
051@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
052public enum X509CertificateVersion
053{
054  /**
055   * The X.509 v1 certificate version.
056   */
057  V1(0, "v1"),
058
059
060
061  /**
062   * The X.509 v2 certificate version.
063   */
064  V2(1, "v2"),
065
066
067
068  /**
069   * The X.509 v3 certificate version.
070   */
071  V3(2, "v3");
072
073
074
075  // The integer value for this certificate version, as used in the encoded
076  // X.509 certificate.
077  private final int intValue;
078
079  // The name for this X.509 certificate version.
080  @NotNull private final String name;
081
082
083
084  /**
085   * Creates a new X.509 certificate version with the provided information.
086   *
087   * @param  intValue  The integer value for the certificate version.  Note that
088   *                   this is the integer value that is used in the encoded
089   *                   certificate, and not the logical version number that the
090   *                   encoded value represents (for example, the "v1"
091   *                   certificate version has an integer value of 0 rather than
092   *                   1).
093   * @param  name      The name for this certificate version.  It must not be
094   *                   {@code null}.
095   */
096  X509CertificateVersion(final int intValue, @NotNull final String name)
097  {
098    this.intValue = intValue;
099    this.name = name;
100  }
101
102
103
104  /**
105   * Retrieves the integer value for this certificate version.  Note that this
106   * is the integer value that is used in the encoded certificate, and not the
107   * logical version number that the encoded value represents (for example, the
108   * "v1" certificate version has an integer value of 0 rather than 1).
109   *
110   * @return  The integer value for this certificate version.
111   */
112  int getIntValue()
113  {
114    return intValue;
115  }
116
117
118
119  /**
120   * Retrieves the name for this certificate version.
121   *
122   * @return  The name for this certificate version.
123   */
124  @NotNull()
125  public String getName()
126  {
127    return name;
128  }
129
130
131
132  /**
133   * Retrieves the certificate version for the provided integer value.
134   *
135   * @param  intValue  The integer value for the certificate version to
136   *                   retrieve.  Note that this is the integer value that is
137   *                   used in the encoded certificate, and not the logical
138   *                   version number that the encoded value represents (for
139   *                   example, the "v1" certificate version has an integer
140   *                   value of 0 rather than 1).
141   *
142   * @return  The certificate version for the provided integer value, or
143   *          {@code null} if the provided version does not correspond to any
144   *          known certificate value.
145   */
146  @Nullable()
147  static X509CertificateVersion valueOf(final int intValue)
148  {
149    for (final X509CertificateVersion v : values())
150    {
151      if (v.intValue == intValue)
152      {
153        return v;
154      }
155    }
156
157    return null;
158  }
159
160
161
162  /**
163   * Retrieves the X.509 certificate version with the specified name.
164   *
165   * @param  name  The name of the X.509 certificate version to retrieve.  It
166   *               must not be {@code null}.
167   *
168   * @return  The requested X.509 certificate version, or {@code null} if no
169   *          such version is defined.
170   */
171  @Nullable()
172  public static X509CertificateVersion forName(@NotNull final String name)
173  {
174    switch (StaticUtils.toLowerCase(name))
175    {
176      case "1":
177      case "v1":
178        return V1;
179      case "2":
180      case "v2":
181        return V2;
182      case "3":
183      case "v3":
184        return V3;
185      default:
186        return null;
187    }
188  }
189}