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