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