001/*
002 * Copyright 2017-2018 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright (C) 2017-2018 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.ThreadSafety;
026import com.unboundid.util.ThreadSafetyLevel;
027
028
029
030/**
031 * This enum defines a set of supported X.509 certificate versions.
032 */
033@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
034public enum X509CertificateVersion
035{
036  /**
037   * The X.509 v1 certificate version.
038   */
039  V1(0, "v1"),
040
041
042
043  /**
044   * The X.509 v2 certificate version.
045   */
046  V2(1, "v2"),
047
048
049
050  /**
051   * The X.509 v3 certificate version.
052   */
053  V3(2, "v3");
054
055
056
057  // The integer value for this certificate version, as used in the encoded
058  // X.509 certificate.
059  private final int intValue;
060
061  // The name for this X.509 certificate version.
062  private final String name;
063
064
065
066  /**
067   * Creates a new X.509 certificate version with the provided information.
068   *
069   * @param  intValue  The integer value for the certificate version.  Note that
070   *                   this is the integer value that is used in the encoded
071   *                   certificate, and not the logical version number that the
072   *                   encoded value represents (for example, the "v1"
073   *                   certificate version has an integer value of 0 rather than
074   *                   1).
075   * @param  name      The name for this certificate version.  It must not be
076   *                   {@code null}.
077   */
078  X509CertificateVersion(final int intValue, final String name)
079  {
080    this.intValue = intValue;
081    this.name = name;
082  }
083
084
085
086  /**
087   * Retrieves the integer value for this certificate version.  Note that this
088   * is the integer value that is used in the encoded certificate, and not the
089   * logical version number that the encoded value represents (for example, the
090   * "v1" certificate version has an integer value of 0 rather than 1).
091   *
092   * @return  The integer value for this certificate version.
093   */
094  int getIntValue()
095  {
096    return intValue;
097  }
098
099
100
101  /**
102   * Retrieves the name for this certificate version.
103   *
104   * @return  The name for this certificate version.
105   */
106  public String getName()
107  {
108    return name;
109  }
110
111
112
113  /**
114   * Retrieves the certificate version for the provided integer value.
115   *
116   * @param  intValue  The integer value for the certificate version to
117   *                   retrieve.  Note that this is the integer value that is
118   *                   used in the encoded certificate, and not the logical
119   *                   version number that the encoded value represents (for
120   *                   example, the "v1" certificate version has an integer
121   *                   value of 0 rather than 1).
122   *
123   * @return  The certificate version for the provided integer value, or
124   *          {@code null} if the provided version does not correspond to any
125   *          known certificate value.
126   */
127  static X509CertificateVersion valueOf(final int intValue)
128  {
129    for (final X509CertificateVersion v : values())
130    {
131      if (v.intValue == intValue)
132      {
133        return v;
134      }
135    }
136
137    return null;
138  }
139}