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