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-2016 ForgeRock AS.
15 */
16
17 package org.forgerock.json.jose.jwe;
18
19 import org.forgerock.json.jose.exceptions.JweException;
20 import org.forgerock.json.jose.jwe.handlers.encryption.AESKeyWrapEncryptionHandler;
21 import org.forgerock.json.jose.jwe.handlers.encryption.DirectEncryptionHandler;
22 import org.forgerock.json.jose.jwe.handlers.encryption.EncryptionHandler;
23 import org.forgerock.json.jose.jwe.handlers.encryption.RSAEncryptionHandler;
24
25 /**
26 * A service to get the appropriate EncryptionHandler for a specified Java Cryptographic encryption algorithm.
27 * <p>
28 * For details of all supported algorithms see {@link JweAlgorithm} and for all supported encryption methods see
29 * {@link EncryptionMethod}
30 *
31 * @since 2.0.0
32 */
33 public class EncryptionManager {
34
35 /**
36 * Gets the appropriate EncryptionHandler that can perform the required encryption algorithm, as described by the
37 * JweAlgorithm and EncryptionMethod in the given JweHeader.
38 *
39 * @param header The JweHeader containing the JweAlgorithm and EncryptionMethod to get the EncryptionHandler for.
40 * @return The EncryptionHandler.
41 */
42 public EncryptionHandler getEncryptionHandler(JweHeader header) {
43
44 switch (header.getAlgorithm().getAlgorithmType()) {
45 case RSA:
46 return getEncryptionHandler(header.getAlgorithm(), header.getEncryptionMethod());
47 case DIRECT:
48 return getEncryptionHandler(header.getAlgorithm(), header.getEncryptionMethod());
49 case AES_KEYWRAP:
50 return getEncryptionHandler(header.getAlgorithm(), header.getEncryptionMethod());
51 default: {
52 throw new JweException("No Encryption Handler for unknown encryption algorithm, "
53 + header.getAlgorithm() + ".");
54 }
55 }
56 }
57
58 /**
59 * Gets the appropriate EncryptionHandler that can perform the required encryption algorithm, as described by the
60 * JweAlgorithm and EncryptionMethod.
61 *
62 * @param algorithm The JweAlgorithm.
63 * @param encryptionMethod The EncryptionMethod.
64 * @return The EncryptionHandler.
65 */
66 private EncryptionHandler getEncryptionHandler(JweAlgorithm algorithm, EncryptionMethod encryptionMethod) {
67
68 switch (algorithm) {
69 case RSAES_PKCS1_V1_5:
70 case RSA_OAEP:
71 case RSA_OAEP_256:
72 return new RSAEncryptionHandler(encryptionMethod, algorithm);
73 case DIRECT:
74 return new DirectEncryptionHandler(encryptionMethod);
75 case A128KW:
76 case A192KW:
77 case A256KW:
78 return new AESKeyWrapEncryptionHandler(encryptionMethod);
79 default:
80 throw new JweException("No Encryption Handler for unknown encryption method, "
81 + encryptionMethod + ", with algorithm, " + algorithm + ".");
82 }
83 }
84 }