001/*
002 * Copyright 2017-2019 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright (C) 2017-2019 Ping Identity Corporation
007 *
008 * This program is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU General Public License (GPLv2 only)
010 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
011 * as published by the Free Software Foundation.
012 *
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016 * GNU General Public License for more details.
017 *
018 * You should have received a copy of the GNU General Public License
019 * along with this program; if not, see <http://www.gnu.org/licenses>.
020 */
021package com.unboundid.util.ssl.cert;
022
023
024
025import com.unboundid.util.StaticUtils;
026import com.unboundid.util.ThreadSafety;
027import com.unboundid.util.ThreadSafetyLevel;
028
029
030
031/**
032 * This enum defines a set of supported X.509 certificate versions.
033 */
034@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
035public enum X509CertificateVersion
036{
037  /**
038   * The X.509 v1 certificate version.
039   */
040  V1(0, "v1"),
041
042
043
044  /**
045   * The X.509 v2 certificate version.
046   */
047  V2(1, "v2"),
048
049
050
051  /**
052   * The X.509 v3 certificate version.
053   */
054  V3(2, "v3");
055
056
057
058  // The integer value for this certificate version, as used in the encoded
059  // X.509 certificate.
060  private final int intValue;
061
062  // The name for this X.509 certificate version.
063  private final String name;
064
065
066
067  /**
068   * Creates a new X.509 certificate version with the provided information.
069   *
070   * @param  intValue  The integer value for the certificate version.  Note that
071   *                   this is the integer value that is used in the encoded
072   *                   certificate, and not the logical version number that the
073   *                   encoded value represents (for example, the "v1"
074   *                   certificate version has an integer value of 0 rather than
075   *                   1).
076   * @param  name      The name for this certificate version.  It must not be
077   *                   {@code null}.
078   */
079  X509CertificateVersion(final int intValue, final String name)
080  {
081    this.intValue = intValue;
082    this.name = name;
083  }
084
085
086
087  /**
088   * Retrieves the integer value for this certificate version.  Note that this
089   * is the integer value that is used in the encoded certificate, and not the
090   * logical version number that the encoded value represents (for example, the
091   * "v1" certificate version has an integer value of 0 rather than 1).
092   *
093   * @return  The integer value for this certificate version.
094   */
095  int getIntValue()
096  {
097    return intValue;
098  }
099
100
101
102  /**
103   * Retrieves the name for this certificate version.
104   *
105   * @return  The name for this certificate version.
106   */
107  public String getName()
108  {
109    return name;
110  }
111
112
113
114  /**
115   * Retrieves the certificate version for the provided integer value.
116   *
117   * @param  intValue  The integer value for the certificate version to
118   *                   retrieve.  Note that this is the integer value that is
119   *                   used in the encoded certificate, and not the logical
120   *                   version number that the encoded value represents (for
121   *                   example, the "v1" certificate version has an integer
122   *                   value of 0 rather than 1).
123   *
124   * @return  The certificate version for the provided integer value, or
125   *          {@code null} if the provided version does not correspond to any
126   *          known certificate value.
127   */
128  static X509CertificateVersion valueOf(final int intValue)
129  {
130    for (final X509CertificateVersion v : values())
131    {
132      if (v.intValue == intValue)
133      {
134        return v;
135      }
136    }
137
138    return null;
139  }
140
141
142
143  /**
144   * Retrieves the X.509 certificate version with the specified name.
145   *
146   * @param  name  The name of the X.509 certificate version to retrieve.  It
147   *               must not be {@code null}.
148   *
149   * @return  The requested X.509 certificate version, or {@code null} if no
150   *          such version is defined.
151   */
152  public static X509CertificateVersion forName(final String name)
153  {
154    switch (StaticUtils.toLowerCase(name))
155    {
156      case "1":
157      case "v1":
158        return V1;
159      case "2":
160      case "v2":
161        return V2;
162      case "3":
163      case "v3":
164        return V3;
165      default:
166        return null;
167    }
168  }
169}