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