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