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, consisting of a JCA
027 * {@link java.security.Provider provider} and
028 * {@link java.security.SecureRandom secure random generator}.
029 *
030 * @author Vladimir Dzhuvinov
031 * @version 2015-06-08
032 */
033public class JCAContext {
034
035
036        /**
037         * The JCA provider.
038         */
039        private Provider provider;
040
041
042        /**
043         * The secure random generator.
044         */
045        private SecureRandom randomGen;
046
047
048        /**
049         * Creates a new default JCA context.
050         */
051        public JCAContext() {
052
053                this(null, null);
054        }
055
056
057        /**
058         * Creates a new JCA context.
059         *
060         * @param provider  The JCA provider, {@code null} to use the default
061         *                  system one.
062         * @param randomGen The specific secure random generator, {@code null}
063         *                  to use the default system one.
064         */
065        public JCAContext(final Provider provider, final SecureRandom randomGen) {
066
067                this.provider = provider;
068                this.randomGen = randomGen;
069        }
070
071
072        /**
073         * Gets the JCA provider to be used for all operations.
074         *
075         * @return The JCA provider to be used for all operations where a more
076         *         specific one is absent, {@code null} implies the default
077         *         system provider.
078         */
079        public Provider getProvider() {
080
081                return provider;
082        }
083
084
085        /**
086         * Sets the JCA provider to be used for all operations.
087         *
088         * @param provider The JCA provider to be used for all operations where
089         *                 a more specific one is absent, {@code null} to use
090         *                 the default system provider.
091         */
092        public void setProvider(final Provider provider) {
093
094                this.provider = provider;
095        }
096
097
098        /**
099         * Gets the secure random generator. Intended for generation of
100         * initialisation vectors and other purposes that require a secure
101         * random generator.
102         *
103         * @return The specific secure random generator (if available), else
104         *         the default system one.
105         */
106        public SecureRandom getSecureRandom() {
107
108                return randomGen != null ? randomGen : new SecureRandom();
109        }
110
111
112        /**
113         * Sets a specific secure random generator for the initialisation
114         * vector and other purposes requiring a random number.
115         *
116         * @param randomGen The secure random generator, {@code null} to use
117         *                  the default system one.
118         */
119        public void setSecureRandom(final SecureRandom randomGen) {
120
121                this.randomGen = randomGen;
122        }
123}