001/*
002 * nimbus-jose-jwt
003 *
004 * Copyright 2012-2016, Connect2id Ltd.
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
007 * this file except in compliance with the License. You may obtain a copy of the
008 * License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software distributed
013 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
014 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
015 * specific language governing permissions and limitations under the License.
016 */
017
018package com.nimbusds.jose.jca;
019
020
021import java.security.Provider;
022import java.security.SecureRandom;
023
024
025/**
026 * Java Cryptography Architecture (JCA) context intended specifically for
027 * JSON Web Encryption (JWE) providers. Allows setting of more specific JCA
028 * providers for key encryption, content encryption and MAC computation.
029 *
030 * @author Vladimir Dzhuvinov
031 * @version 2015-06-08
032 */
033public final class JWEJCAContext extends JCAContext {
034
035
036        /**
037         * The key encryption provider.
038         */
039        private Provider keProvider;
040
041
042        /**
043         * The content encryption provider.
044         */
045        private Provider ceProvider;
046
047
048        /**
049         * The MAC provider.
050         */
051        private Provider macProvider;
052
053
054        /**
055         * Creates a new default JCA context for JWE.
056         */
057        public JWEJCAContext() {
058
059                this(null, null, null, null, null);
060        }
061
062
063        /**
064         * Creates a new JCA context for JWE with the specified JCA providers
065         * and secure random generator.
066         *
067         * @param generalProvider The general JCA provider to be used for all
068         *                        operations where a more specific one is
069         *                        absent, {@code null} to use the default
070         *                        system provider.
071         * @param keProvider      The specific JCA provider to be used for the
072         *                        key encryption, {@code null} to fall back to
073         *                        the general one, and if that is not specified
074         *                        to the default system provider.
075         * @param ceProvider      The specific JCA provider to be used for the
076         *                        content encryption, {@code null} to fall back
077         *                        to the general one, and if that is not
078         *                        specified to the default system provider.
079         * @param macProvider     The specific JCA provider to be used for the
080         *                        MAC computation (where required by the JWE
081         *                        encryption method), {@code null} to fall back
082         *                        to the general one, and if that is not
083         *                        specified to the default system provider.
084         * @param randomGen       The specific secure random generator for the
085         *                        initialisation vector and other purposes
086         *                        requiring a random number, {@code null} to
087         *                        use the default system one.
088         */
089        public JWEJCAContext(final Provider generalProvider,
090                             final Provider keProvider,
091                             final Provider ceProvider,
092                             final Provider macProvider,
093                             final SecureRandom randomGen) {
094
095                super(generalProvider, randomGen);
096                this.keProvider = keProvider;
097                this.ceProvider = ceProvider;
098                this.macProvider = macProvider;
099        }
100
101
102
103        /**
104         * Sets a specific JCA provider for the key encryption.
105         *
106         * @param keProvider The specific JCA provider to be used for the key
107         *                   encryption, {@code null} to fall back to the
108         *                   general one, and if that is not specified to the
109         *                   default system provider.
110         */
111        public void setKeyEncryptionProvider(final Provider keProvider) {
112
113                this.keProvider = keProvider;
114        }
115
116
117        /**
118         * Gets the specific JCA provider for the key encryption.
119         *
120         * @return The applicable JCA provider, {@code null} implies the
121         *         default system provider.
122         */
123        public Provider getKeyEncryptionProvider() {
124
125                return keProvider != null ? keProvider : getProvider();
126        }
127
128
129        /**
130         * Sets a specific JCA provider for the content encryption.
131         *
132         * @param ceProvider The specific JCA provider to be used for the
133         *                   content encryption, {@code null} to fall back to
134         *                   the general one, and if that is not specified to
135         *                   the default system provider.
136         */
137        public void setContentEncryptionProvider(final Provider ceProvider) {
138
139                this.ceProvider = ceProvider;
140        }
141
142
143        /**
144         * Gets the specific JCA provider for the content encryption.
145         *
146         * @return The applicable JCA provider, {@code null} implies the
147         *         default system provider.
148         */
149        public Provider getContentEncryptionProvider() {
150
151                return ceProvider != null ? ceProvider : getProvider();
152        }
153
154
155        /**
156         * Sets a specific JCA provider for the MAC computation (where required
157         * by the JWE encryption method).
158         *
159         * @param macProvider The specific JCA provider to be used for the MAC
160         *                    computation (where required by the JWE encryption
161         *                    method), {@code null} to fall back to the general
162         *                    one, and if that is not specified to the default
163         *                    system provider.
164         */
165        public void setMACProvider(final Provider macProvider) {
166
167                this.macProvider = macProvider;
168        }
169
170
171        /**
172         * Gets the specific JCA provider for the MAC computation (where
173         * required by the JWE encryption method).
174         *
175         * @return The applicable JCA provider, {@code null} implies the
176         *         default system provider.
177         */
178        public Provider getMACProvider() {
179
180                return macProvider != null ? macProvider : getProvider();
181        }
182}