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 PKCS #8 private key versions.
033 */
034@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
035public enum PKCS8PrivateKeyVersion
036{
037  /**
038   * The PKCS #8 v1 private key version.
039   */
040  V1(0, "v1"),
041
042
043
044  /**
045   * The PKCS #8 v2 private key version.
046   */
047  V2(1, "v2");
048
049
050
051  // The integer value for this private key version, as used in the encoded
052  // PKCS #8 private key.
053  private final int intValue;
054
055  // The name for this PKCS #8 private key version.
056  private final String name;
057
058
059
060  /**
061   * Creates a new PKCS #8 private key version with the provided information.
062   *
063   * @param  intValue  The integer value for the private key version.  Note that
064   *                   this is the integer value that is used in the encoded
065   *                   private key, and not the logical version number that the
066   *                   encoded value represents (for example, the "v1" private
067   *                   key version has an integer value of 0 rather than 1).
068   * @param  name      The name for this private key version.  It must not be
069   *                   {@code null}.
070   */
071  PKCS8PrivateKeyVersion(final int intValue, final String name)
072  {
073    this.intValue = intValue;
074    this.name = name;
075  }
076
077
078
079  /**
080   * Retrieves the integer value for this private key version.  Note that this
081   * is the integer value that is used in the encoded private key, and not the
082   * logical version number that the encoded value represents (for example, the
083   * "v1" private key version has an integer value of 0 rather than 1).
084   *
085   * @return  The integer value for this private key version.
086   */
087  int getIntValue()
088  {
089    return intValue;
090  }
091
092
093
094  /**
095   * Retrieves the name for this private key version.
096   *
097   * @return  The name for this private key version.
098   */
099  public String getName()
100  {
101    return name;
102  }
103
104
105
106  /**
107   * Retrieves the private key version for the provided integer value.
108   *
109   * @param  intValue  The integer value for the private key version to
110   *                   retrieve.  Note that this is the integer value that is
111   *                   used in the encoded private key, and not the logical
112   *                   version number that the encoded value represents (for
113   *                   example, the "v1" private key version has an integer
114   *                   value of 0 rather than 1).
115   *
116   * @return  The private key version for the provided integer value, or
117   *          {@code null} if the provided version does not correspond to any
118   *          known private key version value.
119   */
120  static PKCS8PrivateKeyVersion valueOf(final int intValue)
121  {
122    for (final PKCS8PrivateKeyVersion v : values())
123    {
124      if (v.intValue == intValue)
125      {
126        return v;
127      }
128    }
129
130    return null;
131  }
132
133
134
135  /**
136   * Retrieves the PKCS #8 private key version with the specified name.
137   *
138   * @param  name  The name of the PKCS #8 private key version to retrieve.  It
139   *               must not be {@code null}.
140   *
141   * @return  The requested PKCS #8 private key version, or {@code null} if no
142   *          such version is defined.
143   */
144  public static PKCS8PrivateKeyVersion forName(final String name)
145  {
146    switch (StaticUtils.toLowerCase(name))
147    {
148      case "1":
149      case "v1":
150        return V1;
151      case "2":
152      case "v2":
153        return V2;
154      default:
155        return null;
156    }
157  }
158}