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.OID;
041import com.unboundid.util.ThreadSafety;
042import com.unboundid.util.ThreadSafetyLevel;
043
044
045
046/**
047 * This enum defines a set of public key algorithm names and OIDs.
048 */
049@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
050public enum PublicKeyAlgorithmIdentifier
051{
052  /**
053   * The algorithm identifier for the RSA public key algorithm.  This identifier
054   * is defined in RFC 3279 section 2.3.1.
055   */
056  RSA("1.2.840.113549.1.1.1", "RSA"),
057
058
059
060  /**
061   * The algorithm identifier for the DSA public key algorithm.  This identifier
062   * is defined in RFC 3279 section 2.3.2.
063   */
064  DSA("1.2.840.10040.4.1", "DSA"),
065
066
067
068  /**
069   * The algorithm identifier for the Diffie-Hellman public key algorithm.  This
070   * identifier is defined in RFC 3279 section 2.3.3.
071   */
072  DIFFIE_HELLMAN("1.2.840.10046.2.1", "DiffieHellman"),
073
074
075
076  /**
077   * The algorithm identifier for the elliptic curve public key algorithm.  This
078   * identifier is defined in RFC 3279 section 2.3.5.
079   */
080  EC("1.2.840.10045.2.1", "EC");
081
082
083
084  // The OID for this public key algorithm.
085  private final OID oid;
086
087  // The name for this public key algorithm.
088  private final String name;
089
090
091
092  /**
093   * Creates a new public key algorithm identifier with the provided
094   * information.
095   *
096   * @param  oidString  The string representation of the OID for this public key
097   *                    algorithm.
098   * @param  name       The name for this public key algorithm.
099   */
100  PublicKeyAlgorithmIdentifier(final String oidString, final String name)
101  {
102    this.name = name;
103
104    oid = new OID(oidString);
105  }
106
107
108
109  /**
110   * Retrieves the OID for this public key algorithm.
111   *
112   * @return  The OID for this public key algorithm.
113   */
114  public OID getOID()
115  {
116    return oid;
117  }
118
119
120
121  /**
122   * Retrieves the name for this public key algorithm.
123   *
124   * @return  The name for this public key algorithm.
125   */
126  public String getName()
127  {
128    return name;
129  }
130
131
132
133  /**
134   * Retrieves the public key algorithm identifier instance with the specified
135   * OID.
136   *
137   * @param  oid  The OID for the public key algorithm identifier instance to
138   *              retrieve.
139   *
140   * @return  The appropriate public key algorithm identifier instance, or
141   *          {@code null} if the provided OID does not reference a known
142   *          public key algorithm identifier.
143   */
144  public static PublicKeyAlgorithmIdentifier forOID(final OID oid)
145  {
146    for (final PublicKeyAlgorithmIdentifier v : values())
147    {
148      if (v.oid.equals(oid))
149      {
150        return v;
151      }
152    }
153
154    return null;
155  }
156
157
158
159  /**
160   * Retrieves the public key algorithm identifier instance with the specified
161   * name.
162   *
163   * @param  name  The name of the public key algorithm identifier instance to
164   *               retrieve.
165   *
166   * @return  The appropriate public key algorithm identifier instance, or
167   *          {@code null} if the provided name does not reference a known
168   *          public key algorithm identifier.
169   */
170  public static PublicKeyAlgorithmIdentifier forName(final String name)
171  {
172    final String preparedName = prepareName(name);
173    for (final PublicKeyAlgorithmIdentifier v : values())
174    {
175      if (v.name.equalsIgnoreCase(preparedName))
176      {
177        return v;
178      }
179    }
180
181    return null;
182  }
183
184
185
186  /**
187   * Prepares the provided name to be used by the {@link #forName(String)}
188   * method.  All spaces, dashes, and underscores will be removed.
189   *
190   * @param  name  The name to be compared.
191   *
192   * @return  The prepared version of the provided name.
193   */
194  private static String prepareName(final String name)
195  {
196    final StringBuilder buffer = new StringBuilder(name.length());
197
198    for (final char c : name.toCharArray())
199    {
200      switch (c)
201      {
202        case ' ':
203        case '-':
204        case '_':
205          // This character will be omitted.
206          break;
207        default:
208          // This character will be used.
209          buffer.append(c);
210      }
211    }
212
213    return buffer.toString();
214  }
215
216
217
218  /**
219   * Retrieves the human-readable name for the public key algorithm identifier
220   * value with the provided OID, or a string representation of the OID if there
221   * is no value with that OID.
222   *
223   * @param  oid  The OID for the public key algorithm identifier to retrieve.
224   *
225   * @return  The human-readable name for the public key algorithm identifier
226   *          value with the provided OID, or a string representation of the OID
227   *          if there is no value with that OID.
228   */
229  public static String getNameOrOID(final OID oid)
230  {
231    final PublicKeyAlgorithmIdentifier id = forOID(oid);
232    if (id == null)
233    {
234      return oid.toString();
235    }
236    else
237    {
238      return id.name;
239    }
240  }
241
242
243
244  /**
245   * Retrieves a string representation of this public key algorithm identifier.
246   *
247   * @return  A string representation of this public key algorithm identifier.
248   */
249  @Override()
250  public String toString()
251  {
252    return name;
253  }
254}