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 OIDs that are known to be associated with elliptic
048 * curve keys.
049 */
050@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
051public enum NamedCurve
052{
053  /**
054   * The brainpoolP256r1 curve.
055   */
056  BRAINPOOLP256R1("1.3.36.3.3.2.8.1.1.7", "brainpoolP256r1"),
057
058
059
060  /**
061   * The brainpoolP384r1 curve.
062   */
063  BRAINPOOLP384R1("1.3.36.3.3.2.8.1.1.11", "brainpoolP384r1"),
064
065
066
067  /**
068   * The brainpoolP512r1 curve.
069   */
070  BRAINPOOLP512R1("1.3.36.3.3.2.8.1.1.13", "brainpoolP512r1"),
071
072
073
074  /**
075   * The secP160k1 curve.
076   */
077  SECP160K1("1.3.132.0.9", "secP160k1"),
078
079
080
081  /**
082   * The secP160r1 curve.
083   */
084  SECP160R1("1.3.132.0.8", "secP160r1"),
085
086
087
088  /**
089   * The secP160r2 curve.
090   */
091  SECP160R2("1.3.132.0.30", "secP160r2"),
092
093
094
095  /**
096   * The secP192k1 curve.
097   */
098  SECP192K1("1.3.132.0.31", "secP192k1"),
099
100
101
102  /**
103   * The secP192r1 curve (also known as nistP192).
104   */
105  SECP192R1("1.2.840.10045.3.1.1", "secP192r1"),
106
107
108
109  /**
110   * The secP224k1 curve.
111   */
112  SECP224K1("1.3.132.0.32", "secP224k1"),
113
114
115
116  /**
117   * The secP224r1 curve (also known as nistP224).
118   */
119  SECP224R1("1.3.132.0.33", "secP224r1"),
120
121
122
123  /**
124   * The secP256k1 curve.
125   */
126  SECP256K1("1.3.132.0.10", "secP256k1"),
127
128
129
130  /**
131   * The secP256r1 curve (also known as nistP256).
132   */
133  SECP256R1("1.2.840.10045.3.1.7", "secP256r1"),
134
135
136
137  /**
138   * The secP384r1 curve (also known as nistP384).
139   */
140  SECP384R1("1.3.132.0.34", "secP384r1"),
141
142
143
144  /**
145   * The secP521r1 curve (also known as nistP521).
146   */
147  SECP521R1("1.3.132.0.35", "secP521r1"),
148
149
150
151  /**
152   * The secT163k1 curve.
153   */
154  SECT163K1("1.3.132.0.1", "secT163k1"),
155
156
157
158  /**
159   * The secT163r2 curve.
160   */
161  SECT163R2("1.3.132.0.15", "secT163r2"),
162
163
164
165  /**
166   * The secT233k1 curve.
167   */
168  SECT233K1("1.3.132.0.26", "secT233k1"),
169
170
171
172  /**
173   * The secT233r1 curve.
174   */
175  SECT233R1("1.3.132.0.27", "secT233r1"),
176
177
178
179  /**
180   * The secT283k1 curve.
181   */
182  SECT283K1("1.3.132.0.16", "secT283k1"),
183
184
185
186  /**
187   * The secT283r1 curve.
188   */
189  SECT283R1("1.3.132.0.17", "secT283r1"),
190
191
192
193  /**
194   * The secT409k1 curve.
195   */
196  SECT409K1("1.3.132.0.36", "secT409k1"),
197
198
199
200  /**
201   * The secT409r1 curve.
202   */
203  SECT409R1("1.3.132.0.37", "secT409r1"),
204
205
206
207  /**
208   * The secT571k1 curve.
209   */
210  SECT571K1("1.3.132.0.38", "secT571k1"),
211
212
213
214  /**
215   * The secT571r1 curve.
216   */
217  SECT571R1("1.3.132.0.39", "secT571r1");
218
219
220
221  // The OID for this extended key usage ID value.
222  private final OID oid;
223
224  // The name for this extended key usage ID value.
225  private final String name;
226
227
228
229  /**
230   * Creates a new named curve value with the provided information.
231   *
232   * @param  oidString  The string representation of the OID for this named
233   *                    curve value.
234   * @param  name       The name for this named curve value.
235   */
236  NamedCurve(final String oidString, final String name)
237  {
238    this.name = name;
239
240    oid = new OID(oidString);
241  }
242
243
244
245  /**
246   * Retrieves the OID for this named curve value.
247   *
248   * @return  The OID for this named curve value.
249   */
250  public OID getOID()
251  {
252    return oid;
253  }
254
255
256
257  /**
258   * Retrieves the name for this named curve value.
259   *
260   * @return  The name for this named curve value.
261   */
262  public String getName()
263  {
264    return name;
265  }
266
267
268
269  /**
270   * Retrieves the named curve value with the specified OID.
271   *
272   * @param  oid  The OID of the named curve value to retrieve.  It must not be
273   *              {@code null}.
274   *
275   * @return  The named curve value with the specified OID, or {@code null} if
276   *          there is no value with the specified OID.
277   */
278  public static NamedCurve forOID(final OID oid)
279  {
280    for (final NamedCurve curve : values())
281    {
282      if (curve.oid.equals(oid))
283      {
284        return curve;
285      }
286    }
287
288    return null;
289  }
290
291
292
293  /**
294   * Retrieves the name for the named curve value with the provided OID, or a
295   * string representation of the OID if there is no value with that OID.
296   *
297   * @param  oid  The OID for the named curve to retrieve.
298   *
299   * @return  The name for the named curve value with the provided OID, or a
300   *          string representation of the OID if there is no value with that
301   *          OID.
302   */
303  public static String getNameOrOID(final OID oid)
304  {
305    final NamedCurve curve = forOID(oid);
306    if (curve == null)
307    {
308      return oid.toString();
309    }
310    else
311    {
312      return curve.name;
313    }
314  }
315
316
317
318  /**
319   * Retrieves the named curve with the specified name.
320   *
321   * @param  name  The name of the named curve to retrieve.  It must not be
322   *               {@code null}.
323   *
324   * @return  The requested named curve, or {@code null} if no such curve is
325   *          defined.
326   */
327  public static NamedCurve forName(final String name)
328  {
329    for (final NamedCurve namedCurve : NamedCurve.values())
330    {
331      if (namedCurve.name.equalsIgnoreCase(name) ||
332           namedCurve.name().equalsIgnoreCase(name))
333      {
334        return namedCurve;
335      }
336    }
337
338    return null;
339  }
340}