001package com.nimbusds.jose.jca;
002
003
004import java.security.Provider;
005import java.security.SecureRandom;
006
007
008/**
009 * Java Cryptography Architecture (JCA) context intended specifically for
010 * JSON Web Encryption (JWE) providers. Allows setting of more specific JCA
011 * providers for key encryption, content encryption and MAC computation.
012 *
013 * @author Vladimir Dzhuvinov
014 * @version 2015-06-08
015 */
016public final class JWEJCAContext extends JCAContext {
017
018
019        /**
020         * The key encryption provider.
021         */
022        private Provider keProvider;
023
024
025        /**
026         * The content encryption provider.
027         */
028        private Provider ceProvider;
029
030
031        /**
032         * The MAC provider.
033         */
034        private Provider macProvider;
035
036
037        /**
038         * Creates a new default JCA context for JWE.
039         */
040        public JWEJCAContext() {
041
042                this(null, null, null, null, null);
043        }
044
045
046        /**
047         * Creates a new JCA context for JWE with the specified JCA providers
048         * and secure random generator.
049         *
050         * @param generalProvider The general JCA provider to be used for all
051         *                        operations where a more specific one is
052         *                        absent, {@code null} to use the default
053         *                        system provider.
054         * @param keProvider      The specific JCA provider to be used for the
055         *                        key encryption, {@code null} to fall back to
056         *                        the general one, and if that is not specified
057         *                        to the default system provider.
058         * @param ceProvider      The specific JCA provider to be used for the
059         *                        content encryption, {@code null} to fall back
060         *                        to the general one, and if that is not
061         *                        specified to the default system provider.
062         * @param macProvider     The specific JCA provider to be used for the
063         *                        MAC computation (where required by the JWE
064         *                        encryption method), {@code null} to fall back
065         *                        to the general one, and if that is not
066         *                        specified to the default system provider.
067         * @param randomGen       The specific secure random generator for the
068         *                        initialisation vector and other purposes
069         *                        requiring a random number, {@code null} to
070         *                        use the default system one.
071         */
072        public JWEJCAContext(final Provider generalProvider,
073                             final Provider keProvider,
074                             final Provider ceProvider,
075                             final Provider macProvider,
076                             final SecureRandom randomGen) {
077
078                super(generalProvider, randomGen);
079                this.keProvider = keProvider;
080                this.ceProvider = ceProvider;
081                this.macProvider = macProvider;
082        }
083
084
085
086        /**
087         * Sets a specific JCA provider for the key encryption.
088         *
089         * @param keProvider The specific JCA provider to be used for the key
090         *                   encryption, {@code null} to fall back to the
091         *                   general one, and if that is not specified to the
092         *                   default system provider.
093         */
094        public void setKeyEncryptionProvider(final Provider keProvider) {
095
096                this.keProvider = keProvider;
097        }
098
099
100        /**
101         * Gets the specific JCA provider for the key encryption.
102         *
103         * @return The applicable JCA provider, {@code null} implies the
104         *         default system provider.
105         */
106        public Provider getKeyEncryptionProvider() {
107
108                return keProvider != null ? keProvider : getProvider();
109        }
110
111
112        /**
113         * Sets a specific JCA provider for the content encryption.
114         *
115         * @param ceProvider The specific JCA provider to be used for the
116         *                   content encryption, {@code null} to fall back to
117         *                   the general one, and if that is not specified to
118         *                   the default system provider.
119         */
120        public void setContentEncryptionProvider(final Provider ceProvider) {
121
122                this.ceProvider = ceProvider;
123        }
124
125
126        /**
127         * Gets the specific JCA provider for the content encryption.
128         *
129         * @return The applicable JCA provider, {@code null} implies the
130         *         default system provider.
131         */
132        public Provider getContentEncryptionProvider() {
133
134                return ceProvider != null ? ceProvider : getProvider();
135        }
136
137
138        /**
139         * Sets a specific JCA provider for the MAC computation (where required
140         * by the JWE encryption method).
141         *
142         * @param macProvider The specific JCA provider to be used for the MAC
143         *                    computation (where required by the JWE encryption
144         *                    method), {@code null} to fall back to the general
145         *                    one, and if that is not specified to the default
146         *                    system provider.
147         */
148        public void setMACProvider(final Provider macProvider) {
149
150                this.macProvider = macProvider;
151        }
152
153
154        /**
155         * Gets the specific JCA provider for the MAC computation (where
156         * required by the JWE encryption method).
157         *
158         * @return The applicable JCA provider, {@code null} implies the
159         *         default system provider.
160         */
161        public Provider getMACProvider() {
162
163                return macProvider != null ? macProvider : getProvider();
164        }
165}