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