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
029import static com.unboundid.util.ssl.cert.CertMessages.*;
030
031
032
033/**
034 * This enum defines a set of OIDs that are known to be used in the
035 * {@link ExtendedKeyUsageExtension}.  Note that extended key usage extensions
036 * may include OIDs that are not included in this enum, and any code that makes
037 * use of the extension should be prepared to handle other key usage IDs.
038 */
039@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
040public enum ExtendedKeyUsageID
041{
042  /**
043   * The extended key usage ID that indicates that the associated certificate
044   * may be used for TLS server authentication.
045   */
046  TLS_SERVER_AUTHENTICATION("1.3.6.1.5.5.7.3.1",
047       INFO_EXTENDED_KEY_USAGE_ID_TLS_SERVER_AUTHENTICATION.get()),
048
049
050
051  /**
052   * The extended key usage ID that indicates that the associated certificate
053   * may be used for TLS client authentication.
054   */
055  TLS_CLIENT_AUTHENTICATION("1.3.6.1.5.5.7.3.2",
056       INFO_EXTENDED_KEY_USAGE_ID_TLS_CLIENT_AUTHENTICATION.get()),
057
058
059
060  /**
061   * The extended key usage ID that indicates that the associated certificate
062   * may be used for code signing.
063   */
064  CODE_SIGNING("1.3.6.1.5.5.7.3.3",
065       INFO_EXTENDED_KEY_USAGE_ID_CODE_SIGNING.get()),
066
067
068
069  /**
070   * The extended key usage ID that indicates that the associated certificate
071   * may be used for email protection.
072   */
073  EMAIL_PROTECTION("1.3.6.1.5.5.7.3.4",
074       INFO_EXTENDED_KEY_USAGE_ID_EMAIL_PROTECTION.get()),
075
076
077
078  /**
079   * The extended key usage ID that indicates that the associated certificate
080   * may be used for time stamping.
081   */
082  TIME_STAMPING("1.3.6.1.5.5.7.3.8",
083       INFO_EXTENDED_KEY_USAGE_ID_TIME_STAMPING.get()),
084
085
086
087  /**
088   * The extended key usage ID that indicates that the associated certificate
089   * may be used for signing OCSP responses.
090   */
091  OCSP_SIGNING("1.3.6.1.5.5.7.3.9",
092       INFO_EXTENDED_KEY_USAGE_ID_OCSP_SIGNING.get());
093
094
095
096  // The OID for this extended key usage ID value.
097  private final OID oid;
098
099  // The human-readable name for this extended key usage ID value.
100  private final String name;
101
102
103
104  /**
105   * Creates a new extended key usage ID value with the provided information.
106   *
107   * @param  oidString  The string representation of the OID for this extended
108   *                    key usage ID value.
109   * @param  name       The human-readable name for this extended key usage ID
110   *                    value.
111   */
112  ExtendedKeyUsageID(final String oidString, final String name)
113  {
114    this.name = name;
115
116    oid = new OID(oidString);
117  }
118
119
120
121  /**
122   * Retrieves the OID for this extended key usage ID value.
123   *
124   * @return  The OID for this extended key usage ID value.
125   */
126  public OID getOID()
127  {
128    return oid;
129  }
130
131
132
133  /**
134   * Retrieves the human-readable name for this extended key usage ID value.
135   *
136   * @return  The human-readable name for this extended key usage ID value.
137   */
138  public String getName()
139  {
140    return name;
141  }
142
143
144
145  /**
146   * Retrieves the extended key usage ID value with the specified OID.
147   *
148   * @param  oid  The OID of the extended key usage ID value to retrieve.  It
149   *              must not be {@code null}.
150   *
151   * @return  The extended key usage ID value with the specified OID, or
152   *          {@code null} if there is no value with the specified OID.
153   */
154  public static ExtendedKeyUsageID forOID(final OID oid)
155  {
156    for (final ExtendedKeyUsageID id : values())
157    {
158      if (id.oid.equals(oid))
159      {
160        return id;
161      }
162    }
163
164    return null;
165  }
166
167
168
169  /**
170   * Retrieves the human-readable name for the extended key usage ID value with
171   * the provided OID, or a string representation of the OID if there is no
172   * value with that OID.
173   *
174   * @param  oid  The OID for the extended key usage ID to retrieve.
175   *
176   * @return  The human-readable name for the extended key usage ID value with
177   *            the provided OID, or a string representation of the OID if there
178   *            is no value with that OID.
179   */
180  public static String getNameOrOID(final OID oid)
181  {
182    final ExtendedKeyUsageID id = forOID(oid);
183    if (id == null)
184    {
185      return oid.toString();
186    }
187    else
188    {
189      return id.name;
190    }
191  }
192}