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 algorithm names and OIDs.
048 */
049@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
050public enum SignatureAlgorithmIdentifier
051{
052  /**
053   * The algorithm identifier for the MD2 message digest with RSA encryption.
054   * This identifier is defined in RFC 3279 section 2.2.1.
055   */
056  MD2_WITH_RSA("1.2.840.113549.1.1.2", "MD2withRSA", "MD2 with RSA"),
057
058
059
060  /**
061   * The algorithm identifier for the MD5 message digest with RSA encryption.
062   * This identifier is defined in RFC 3279 section 2.2.1.
063   */
064  MD5_WITH_RSA("1.2.840.113549.1.1.4", "MD5withRSA", "MD5 with RSA"),
065
066
067
068  /**
069   * The algorithm identifier for the SHA-1 message digest with RSA encryption.
070   * This identifier is defined in RFC 3279 section 2.2.1.
071   */
072  SHA_1_WITH_RSA("1.2.840.113549.1.1.5", "SHA1withRSA", "SHA-1 with RSA"),
073
074
075
076  /**
077   * The algorithm identifier for the 224-bit SHA-2 message digest with RSA
078   * encryption.  This identifier is defined in RFC 4055 section 5.
079   */
080  SHA_224_WITH_RSA("1.2.840.113549.1.1.14", "SHA224withRSA",
081       "SHA-224 with RSA"),
082
083
084
085  /**
086   * The algorithm identifier for the 256-bit SHA-2 message digest with RSA
087   * encryption.  This identifier is defined in RFC 4055 section 5.
088   */
089  SHA_256_WITH_RSA("1.2.840.113549.1.1.11", "SHA256withRSA",
090       "SHA-256 with RSA"),
091
092
093
094  /**
095   * The algorithm identifier for the 384-bit SHA-2 message digest with RSA
096   * encryption.  This identifier is defined in RFC 4055 section 5.
097   */
098  SHA_384_WITH_RSA("1.2.840.113549.1.1.12", "SHA384withRSA",
099       "SHA-384 with RSA"),
100
101
102
103  /**
104   * The algorithm identifier for the 512-bit SHA-2 message digest with RSA
105   * encryption.  This identifier is defined in RFC 4055 section 5.
106   */
107  SHA_512_WITH_RSA("1.2.840.113549.1.1.13", "SHA512withRSA",
108       "SHA-512 with RSA"),
109
110
111
112  /**
113   * The algorithm identifier for the SHA-1 message digest with the DSA
114   * signature algorithm.  This identifier is defined in RFC 3279 section 2.2.2.
115   */
116  SHA_1_WITH_DSA("1.2.840.10040.4.3", "SHA1withDSA", "SHA-1 with DSA"),
117
118
119
120  /**
121   * The algorithm identifier for the 224-bit SHA-2 message digest with the DSA
122   * signature algorithm.  This identifier is defined in RFC 5758 section 3.1.
123   */
124  SHA_224_WITH_DSA("2.16.840.1.101.3.4.3.1", "SHA224withDSA",
125       "SHA-224 with DSA"),
126
127
128
129  /**
130   * The algorithm identifier for the 256-bit SHA-2 message digest with the DSA
131   * signature algorithm.  This identifier is defined in RFC 5758 section 3.1.
132   */
133  SHA_256_WITH_DSA("2.16.840.1.101.3.4.3.2", "SHA256withDSA",
134       "SHA-256 with DSA"),
135
136
137
138  /**
139   * The algorithm identifier for the SHA-1 message digest with the ECDSA
140   * signature algorithm.  This identifier is defined in RFC 3279 section 2.2.3.
141   */
142  SHA_1_WITH_ECDSA("1.2.840.10045.4.1", "SHA1withECDSA", "SHA-1 with ECDSA"),
143
144
145
146  /**
147   * The algorithm identifier for the 224-bit SHA-2 message digest with the
148   * ECDSA signature algorithm.  This identifier is defined in RFC 5758 section
149   * 3.2.
150   */
151  SHA_224_WITH_ECDSA("1.2.840.10045.4.3.1", "SHA224withECDSA",
152       "SHA-224 with ECDSA"),
153
154
155
156  /**
157   * The algorithm identifier for the 256-bit SHA-2 message digest with the
158   * ECDSA signature algorithm.  This identifier is defined in RFC 5758 section
159   * 3.2.
160   */
161  SHA_256_WITH_ECDSA("1.2.840.10045.4.3.2", "SHA256withECDSA",
162       "SHA-256 with ECDSA"),
163
164
165
166  /**
167   * The algorithm identifier for the 384-bit SHA-2 message digest with the
168   * ECDSA signature algorithm.  This identifier is defined in RFC 5758 section
169   * 3.2.
170   */
171  SHA_384_WITH_ECDSA("1.2.840.10045.4.3.3", "SHA384withECDSA",
172       "SHA-384 with ECDSA"),
173
174
175
176  /**
177   * The algorithm identifier for the 512-bit SHA-2 message digest with the
178   * ECDSA signature algorithm.  This identifier is defined in RFC 5758 section
179   * 3.2.
180   */
181  SHA_512_WITH_ECDSA("1.2.840.10045.4.3.4", "SHA512withECDSA",
182       "SHA-512 with ECDSA");
183
184
185
186  // The OID for this signature algorithm.
187  private final OID oid;
188
189  // The name for this signature algorithm as it would be used internally by
190  // Java.
191  private final String javaName;
192
193  // The user-friendly name for this signature algorithm.
194  private final String userFriendlyName;
195
196
197
198  /**
199   * Creates a new signature algorithm with the provided information.
200   *
201   * @param  oidString         The string representation of the OID for this
202   *                           signature algorithm.
203   * @param  javaName          The name for this signature algorithm as it would
204   *                           be used internally by Java.
205   * @param  userFriendlyName  The user-friendly name for this signature
206   *                           algorithm.
207   */
208  SignatureAlgorithmIdentifier(final String oidString, final String javaName,
209                               final String userFriendlyName)
210  {
211    this.javaName = javaName;
212    this.userFriendlyName = userFriendlyName;
213
214    oid = new OID(oidString);
215  }
216
217
218
219  /**
220   * Retrieves the OID for this signature algorithm.
221   *
222   * @return  The OID for this signature algorithm.
223   */
224  public OID getOID()
225  {
226    return oid;
227  }
228
229
230
231  /**
232   * Retrieves the name for this signature algorithm as it would be used
233   * internally by Java.
234   *
235   * @return  The name for this signature algorithm as it would be used
236   *          internally by Java.
237   */
238  public String getJavaName()
239  {
240    return javaName;
241  }
242
243
244
245  /**
246   * Retrieves the user-friendly name for this signature algorithm.
247   *
248   * @return  The user-friendly name for this signature algorithm.
249   */
250  public String getUserFriendlyName()
251  {
252    return userFriendlyName;
253  }
254
255
256
257  /**
258   * Retrieves the signature algorithm identifier instance with the specified
259   * OID.
260   *
261   * @param  oid  The OID for the signature algorithm identifier instance to
262   *              retrieve.
263   *
264   * @return  The appropriate signature algorithm identifier instance, or
265   *          {@code null} if the provided OID does not reference a known
266   *          signature algorithm identifier.
267   */
268  public static SignatureAlgorithmIdentifier forOID(final OID oid)
269  {
270    for (final SignatureAlgorithmIdentifier v : values())
271    {
272      if (v.oid.equals(oid))
273      {
274        return v;
275      }
276    }
277
278    return null;
279  }
280
281
282
283  /**
284   * Retrieves the signature algorithm identifier instance with the specified
285   * name.
286   *
287   * @param  name  The name of the signature algorithm identifier instance to
288   *               retrieve.
289   *
290   * @return  The appropriate signature algorithm identifier instance, or
291   *          {@code null} if the provided name does not reference a known
292   *          signature algorithm identifier.
293   */
294  public static SignatureAlgorithmIdentifier forName(final String name)
295  {
296    final String preparedName = prepareName(name);
297    for (final SignatureAlgorithmIdentifier v : values())
298    {
299      if (v.javaName.equalsIgnoreCase(preparedName))
300      {
301        return v;
302      }
303    }
304
305    return null;
306  }
307
308
309
310  /**
311   * Prepares the provided name to be used by the {@link #forName(String)}
312   * method.  All spaces, dashes, and underscores will be removed.
313   *
314   * @param  name  The name to be compared.
315   *
316   * @return  The prepared version of the provided name.
317   */
318  private static String prepareName(final String name)
319  {
320    final StringBuilder buffer = new StringBuilder(name.length());
321
322    for (final char c : name.toCharArray())
323    {
324      switch (c)
325      {
326        case ' ':
327        case '-':
328        case '_':
329          // This character will be omitted.
330          break;
331        default:
332          // This character will be used.
333          buffer.append(c);
334      }
335    }
336
337    return buffer.toString();
338  }
339
340
341
342  /**
343   * Retrieves the user-friendly name for the signature algorithm identifier
344   * value with the provided OID, or a string representation of the OID if there
345   * is no value with that OID.
346   *
347   * @param  oid  The OID for the signature algorithm identifier to retrieve.
348   *
349   * @return  The user-friendly name for the signature algorithm identifier
350   *          value with the provided OID, or a string representation of the OID
351   *          if there is no value with that OID.
352   */
353  public static String getNameOrOID(final OID oid)
354  {
355    final SignatureAlgorithmIdentifier id = forOID(oid);
356    if (id == null)
357    {
358      return oid.toString();
359    }
360    else
361    {
362      return id.userFriendlyName;
363    }
364  }
365
366
367
368  /**
369   * Retrieves a string representation of this signature algorithm identifier.
370   *
371   * @return  A string representation of this signature algorithm identifier.
372   */
373  @Override()
374  public String toString()
375  {
376    return userFriendlyName;
377  }
378}