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.jwe; 018 019import org.forgerock.json.jose.exceptions.JweException; 020import org.forgerock.json.jose.jwe.handlers.encryption.AESKeyWrapEncryptionHandler; 021import org.forgerock.json.jose.jwe.handlers.encryption.DirectEncryptionHandler; 022import org.forgerock.json.jose.jwe.handlers.encryption.EncryptionHandler; 023import org.forgerock.json.jose.jwe.handlers.encryption.RSAEncryptionHandler; 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().getAlgorithmType()) { 045 case RSA: 046 return getEncryptionHandler(header.getAlgorithm(), header.getEncryptionMethod()); 047 case DIRECT: 048 return getEncryptionHandler(header.getAlgorithm(), header.getEncryptionMethod()); 049 case AES_KEYWRAP: 050 return getEncryptionHandler(header.getAlgorithm(), header.getEncryptionMethod()); 051 default: { 052 throw new JweException("No Encryption Handler for unknown encryption algorithm, " 053 + header.getAlgorithm() + "."); 054 } 055 } 056 } 057 058 /** 059 * Gets the appropriate EncryptionHandler that can perform the required encryption algorithm, as described by the 060 * JweAlgorithm and EncryptionMethod. 061 * 062 * @param algorithm The JweAlgorithm. 063 * @param encryptionMethod The EncryptionMethod. 064 * @return The EncryptionHandler. 065 */ 066 private EncryptionHandler getEncryptionHandler(JweAlgorithm algorithm, EncryptionMethod encryptionMethod) { 067 068 switch (algorithm) { 069 case RSAES_PKCS1_V1_5: 070 case RSA_OAEP: 071 case RSA_OAEP_256: 072 return new RSAEncryptionHandler(encryptionMethod, algorithm); 073 case DIRECT: 074 return new DirectEncryptionHandler(encryptionMethod); 075 case A128KW: 076 case A192KW: 077 case A256KW: 078 return new AESKeyWrapEncryptionHandler(encryptionMethod); 079 default: 080 throw new JweException("No Encryption Handler for unknown encryption method, " 081 + encryptionMethod + ", with algorithm, " + algorithm + "."); 082 } 083 } 084}