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-2016 ForgeRock AS.
015 */
016
017package org.forgerock.json.jose.jws;
018
019import java.security.Key;
020import java.security.interfaces.ECPrivateKey;
021import java.security.interfaces.ECPublicKey;
022
023import org.forgerock.json.jose.jws.handlers.ECDSASigningHandler;
024import org.forgerock.json.jose.jws.handlers.HmacSigningHandler;
025import org.forgerock.json.jose.jws.handlers.NOPSigningHandler;
026import org.forgerock.json.jose.jws.handlers.RSASigningHandler;
027import org.forgerock.json.jose.jws.handlers.SigningHandler;
028import org.forgerock.util.SignatureUtil;
029
030/**
031 * A service to get the appropriate SigningHandler for a specific Java Cryptographic signing algorithm.
032 * <p>
033 * For details of all supported signing algorithms see {@link JwsAlgorithm}
034 *
035 * @since 2.0.0
036 */
037public class SigningManager {
038
039    private final SignatureUtil signatureUtil = SignatureUtil.getInstance();
040
041    /**
042     * Constructs an implementation of the SigningHandler which does not perform
043     * any signing or verifying.
044     *
045     * @return an implementation of the SigningHandler which does not perform
046     *         any signing or verifying.
047     */
048    public SigningHandler newNopSigningHandler() {
049        return new NOPSigningHandler();
050    }
051
052    /**
053     * Constructs a new HmacSigningHandler.
054     *
055     * @param sharedSecret
056     *            The shared secret to use to sign the data.
057     * @return a new HmacSigningHandler.
058     */
059    public SigningHandler newHmacSigningHandler(byte[] sharedSecret) {
060        return new HmacSigningHandler(sharedSecret);
061    }
062
063    /**
064     * Constructs a new RSASigningHandler, with a SignatureUtil instance to
065     * delegate the signing and verifying calls to.
066     *
067     * @param key
068     *            The key used to sign and verify the signature.
069     * @return a new RSASigningHandler, with a SignatureUtil instance to
070     *         delegate the signing and verifying calls to.
071     */
072    public SigningHandler newRsaSigningHandler(Key key) {
073        return new RSASigningHandler(key, signatureUtil);
074    }
075
076    /**
077     * Constructs a new handler for signing ES256 signatures.
078     *
079     * @param key the elliptic curve private key. Should use the required curve for the given signing algorithm
080     *            (P-256 for ES256).
081     * @return the signing handler.
082     */
083    public SigningHandler newEcdsaSigningHandler(ECPrivateKey key) {
084        return new ECDSASigningHandler(key);
085    }
086
087    /**
088     * Constructs a new handler for verifying ES256 signatures.
089     * @param key the elliptic curve public key. Should use the required curve for the given signing algorithm (P-256
090     *            for ES256).
091     * @return the signing handler configured for verification.
092     */
093    public SigningHandler newEcdsaVerificationHandler(ECPublicKey key) {
094        return new ECDSASigningHandler(key);
095    }
096
097}