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