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.jwe;
018
019import org.forgerock.json.jose.exceptions.JweException;
020import org.forgerock.json.jose.jwe.handlers.encryption.EncryptionHandler;
021import org.forgerock.json.jose.jwe.handlers.encryption.RSA15AES128CBCHS256EncryptionHandler;
022import org.forgerock.json.jose.jwe.handlers.encryption.RSA15AES256CBCHS512EncryptionHandler;
023import org.forgerock.json.jose.jws.SigningManager;
024
025/**
026 * A service to get the appropriate EncryptionHandler for a specified Java Cryptographic encryption algorithm.
027 * <p>
028 * For details of all supported algorithms see {@link JweAlgorithm} and for all supported encryption methods see
029 * {@link EncryptionMethod}
030 *
031 * @since 2.0.0
032 */
033public class EncryptionManager {
034
035    /**
036     * Gets the appropriate EncryptionHandler that can perform the required encryption algorithm, as described by the
037     * JweAlgorithm and EncryptionMethod in the given JweHeader.
038     *
039     * @param header The JweHeader containing the JweAlgorithm and EncryptionMethod to get the EncryptionHandler for.
040     * @return The EncryptionHandler.
041     */
042    public EncryptionHandler getEncryptionHandler(JweHeader header) {
043
044        switch (header.getAlgorithm()) {
045        case RSAES_PKCS1_V1_5: {
046            return getEncryptionHandler(header.getAlgorithm(), header.getEncryptionMethod());
047        }
048        default: {
049            throw new JweException("No Encryption Handler for unknown encryption algorithm, "
050                    + header.getAlgorithm() + ".");
051        }
052        }
053    }
054
055    /**
056     * Gets the appropriate EncryptionHandler that can perform the required encryption algorithm, as described by the
057     * JweAlgorithm and EncryptionMethod.
058     *
059     * @param algorithm The JweAlgorithm.
060     * @param encryptionMethod The EncryptionMethod.
061     * @return The EncryptionHandler.
062     */
063    private EncryptionHandler getEncryptionHandler(JweAlgorithm algorithm, EncryptionMethod encryptionMethod) {
064
065        switch (encryptionMethod) {
066        case A128CBC_HS256: {
067            return new RSA15AES128CBCHS256EncryptionHandler(new SigningManager());
068        }
069        case A256CBC_HS512: {
070            return new RSA15AES256CBCHS512EncryptionHandler(new SigningManager());
071        }
072        default: {
073            throw new JweException("No Encryption Handler for unknown encryption method, "
074                    + encryptionMethod + ", with algorithm,  " + algorithm + ".");
075        }
076        }
077    }
078}