1 /* 2 * The contents of this file are subject to the terms of the Common Development and 3 * Distribution License (the License). You may not use this file except in compliance with the 4 * License. 5 * 6 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the 7 * specific language governing permission and limitations under the License. 8 * 9 * When distributing Covered Software, include this CDDL Header Notice in each file and include 10 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL 11 * Header, with the fields enclosed by brackets [] replaced by your own identifying 12 * information: "Portions copyright [year] [name of copyright owner]". 13 * 14 * Copyright 2013-2017 ForgeRock AS. 15 */ 16 17 package org.forgerock.json.jose.jws; 18 19 import org.forgerock.json.jose.jwt.Algorithm; 20 21 /** 22 * An Enum of the possible signing algorithms that can be used to sign a JWT. 23 * <p> 24 * @see <a href="http://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-11#section-3.1">JWS Algorithms</a> 25 * 26 * @since 2.0.0 27 */ 28 public enum JwsAlgorithm implements Algorithm { 29 30 /** No digital signature or MAC value included. */ 31 NONE(null, null, JwsAlgorithmType.NONE), 32 /** HMAC using SHA-256 hash algorithm. */ 33 HS256("HmacSHA256", "SHA-256", JwsAlgorithmType.HMAC), 34 /** HMAC using SHA-384 hash algorithm. */ 35 HS384("HmacSHA384", "SHA-384", JwsAlgorithmType.HMAC), 36 /** HMAC using SHA-512 hash algorithm. */ 37 HS512("HmacSHA512", "SHA-512", JwsAlgorithmType.HMAC), 38 /** RSA using SHA-256 hash algorithm. **/ 39 RS256("SHA256withRSA", "SHA-256", JwsAlgorithmType.RSA), 40 /** ECDSA using SHA-256 hash algorithm. */ 41 ES256("SHA256WithECDSA", "SHA-256", JwsAlgorithmType.ECDSA), 42 /** ECDSA using SHA-384 hash algorithm. */ 43 ES384("SHA384WithECDSA", "SHA-384", JwsAlgorithmType.ECDSA), 44 /** ECDSA using SHA-512 hash algorithm. */ 45 ES512("SHA512WithECDSA", "SHA-512", JwsAlgorithmType.ECDSA); 46 47 private final String algorithm; 48 private final String mdAlgorithm; 49 private final JwsAlgorithmType algorithmType; 50 51 /** 52 * Constructs a new JwsAlgorithm with the Java Cryptographic string name of the algorithm and the JwsAlgorithmType 53 * of the algorithm. 54 * 55 * @param algorithm The Java Cryptographic algorithm name. 56 * @param mdAlgorithm The MessageDigest algorithm. 57 * @param algorithmType The JwsAlgorithmType of the JwsAlgorithm. 58 */ 59 JwsAlgorithm(String algorithm, String mdAlgorithm, JwsAlgorithmType algorithmType) { 60 this.algorithm = algorithm; 61 this.mdAlgorithm = mdAlgorithm; 62 this.algorithmType = algorithmType; 63 } 64 65 @Override 66 public String getAlgorithm() { 67 return algorithm; 68 } 69 70 @Override 71 public String getJwaAlgorithmName() { 72 return name(); 73 } 74 75 /** 76 * Returns the Java-friendly name of the message digest algorithm 77 * implementation. 78 * 79 * @return the Java-friendly name of the message digest algorithm 80 * implementation. 81 * @see <a 82 * href="http://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html">Standard 83 * Names</a> 84 */ 85 public String getMdAlgorithm() { 86 return mdAlgorithm; 87 } 88 89 /** 90 * Return the standard name of the elliptic curve definition. Only applicable for ECDSA algorithms. 91 * 92 * @return the curve name or null if not applicable. 93 */ 94 public String getEllipticCurveName() { 95 switch (this) { 96 case ES256: 97 return "P-256"; 98 case ES384: 99 return "P-384"; 100 case ES512: 101 return "P-521"; // Not a typo! 102 default: 103 return null; 104 } 105 } 106 107 /** 108 * Gets the JwsAlgorithmType of the JwsAlgorithm. 109 * 110 * @return The JwsAlgorithmType. 111 */ 112 public JwsAlgorithmType getAlgorithmType() { 113 return algorithmType; 114 } 115 116 /** 117 * See {@link #parseCryptographicAlgorithm(String)}}. 118 * @deprecated Replaced by {@link #parseCryptographicAlgorithm(String)} 119 * 120 * @param algorithm The Java Cryptographic string algorithm name. 121 * @return The matching JwsAlgorithm. 122 */ 123 @Deprecated 124 public static JwsAlgorithm getJwsAlgorithm(String algorithm) { 125 return parseCryptographicAlgorithm(algorithm); 126 } 127 128 /** 129 * Parses the given algorithm string to find the matching Java Cryptographic algorithm name. 130 * <p> 131 * If the given algorithm name does not match the algorithm name of any of the constants, then an 132 * IllegalArgumentException will be thrown. 133 * 134 * @param algorithm The Java Cryptographic string algorithm name. 135 * @return The matching JwsAlgorithm. 136 */ 137 public static JwsAlgorithm parseCryptographicAlgorithm(String algorithm) { 138 for (JwsAlgorithm jwsAlgorithm : JwsAlgorithm.values()) { 139 if (algorithm.equalsIgnoreCase(jwsAlgorithm.getAlgorithm())) { 140 return jwsAlgorithm; 141 } 142 } 143 throw new IllegalArgumentException("Unknown JwsAlgorithm, " + algorithm); 144 } 145 146 /** 147 * Turns the JwsAlgorithm constant into a JSON value string. 148 * 149 * @return {@inheritDoc} 150 */ 151 @Override 152 public String toString() { 153 return super.toString(); 154 } 155 }