001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2013-2015 ForgeRock AS.
015 */
016
017package org.forgerock.json.jose.jws;
018
019import org.forgerock.json.jose.jwt.Algorithm;
020
021/**
022 * An Enum of the possible signing algorithms that can be used to sign a JWT.
023 * <p>
024 * @see <a href="http://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-11#section-3.1">JWS Algorithms</a>
025 *
026 * @since 2.0.0
027 */
028public enum JwsAlgorithm implements Algorithm {
029
030    /** No digital signature or MAC value included. */
031    NONE(null, null, JwsAlgorithmType.NONE),
032    /** HMAC using SHA-256 hash algorithm. */
033    HS256("HmacSHA256", "SHA-256", JwsAlgorithmType.HMAC),
034    /** HMAC using SHA-384 hash algorithm. */
035    HS384("HmacSHA384", "SHA-384", JwsAlgorithmType.HMAC),
036    /** HMAC using SHA-512 hash algorithm. */
037    HS512("HmacSHA512", "SHA-512", JwsAlgorithmType.HMAC),
038    /** RSA using SHA-256 hash algorithm. **/
039    RS256("SHA256withRSA", "SHA-256", JwsAlgorithmType.RSA);
040
041    private final String algorithm;
042    private final String mdAlgorithm;
043    private final JwsAlgorithmType algorithmType;
044
045    /**
046     * Constructs a new JwsAlgorithm with the Java Cryptographic string name of the algorithm and the JwsAlgorithmType
047     * of the algorithm.
048     *
049     * @param algorithm The Java Cryptographic algorithm name.
050     * @param mdAlgorithm The MessageDigest algorithm.
051     * @param algorithmType The JwsAlgorithmType of the JwsAlgorithm.
052     */
053    private JwsAlgorithm(String algorithm, String mdAlgorithm, JwsAlgorithmType algorithmType) {
054        this.algorithm = algorithm;
055        this.mdAlgorithm = mdAlgorithm;
056        this.algorithmType = algorithmType;
057    }
058
059    /**
060     * {@inheritDoc}
061     */
062    @Override
063    public String getAlgorithm() {
064        return algorithm;
065    }
066
067    /**
068     * Returns the Java-friendly name of the message digest algorithm
069     * implementation.
070     *
071     * @return the Java-friendly name of the message digest algorithm
072     *         implementation.
073     * @see <a
074     *      href="http://docs.oracle.com/javase/7/docs/technotes/guides/security/StandardNames.html">Standard
075     *      Names</a>
076     */
077    public String getMdAlgorithm() {
078        return mdAlgorithm;
079    }
080
081    /**
082     * Gets the JwsAlgorithmType of the JwsAlgorithm.
083     *
084     * @return The JwsAlgorithmType.
085     */
086    public JwsAlgorithmType getAlgorithmType() {
087        return algorithmType;
088    }
089
090    /**
091     * Gets the JwsAlgorithm constant that matches the given Java Cryptographic algorithm name.
092     * <p>
093     * If the given algorithm name does not match the algorithm name of any of the constants, then an
094     * IllegalArgumentException will be thrown.
095     *
096     * @param algorithm The Java Cryptographic string algorithm name.
097     * @return The matching JwsAlgorithm.
098     */
099    public static JwsAlgorithm getJwsAlgorithm(String algorithm) {
100        for (JwsAlgorithm jwsAlgorithm : JwsAlgorithm.values()) {
101            if (algorithm.equalsIgnoreCase(jwsAlgorithm.getAlgorithm())) {
102                return jwsAlgorithm;
103            }
104        }
105        throw new IllegalArgumentException("Unknown JwsAlgorithm, " + algorithm);
106    }
107
108    /**
109     * Turns the JwsAlgorithm constant into a JSON value string.
110     *
111     * @return {@inheritDoc}
112     */
113    @Override
114    public String toString() {
115        return super.toString();
116    }
117}