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         * @param randomGen The specific secure random generator, {@code null}
062         *                  to use the default system one.
063         */
064        public JCAContext(final Provider provider, final SecureRandom randomGen) {
065
066                this.provider = provider;
067                this.randomGen = randomGen;
068        }
069
070
071        /**
072         * Gets the JCA provider to be used for all operations.
073         *
074         * @return The JCA provider to be used for all operations where a more
075         *         specific one is absent, {@code null} implies the default
076         *         system provider.
077         */
078        public Provider getProvider() {
079
080                return provider;
081        }
082
083
084        /**
085         * Sets the JCA provider to be used for all operations.
086         *
087         * @param provider The JCA provider to be used for all operations where
088         *                 a more specific one is absent, {@code null} to use
089         *                 the default.
090         */
091        public void setProvider(final Provider provider) {
092
093                this.provider = provider;
094        }
095
096
097        /**
098         * Gets the secure random generator. Intended for generation of
099         * initialisation vectors and other purposes that require a secure
100         * random generator.
101         *
102         * @return The specific secure random generator (if available), else
103         *         the default system one.
104         */
105        public SecureRandom getSecureRandom() {
106
107                return randomGen != null ? randomGen : new SecureRandom();
108        }
109
110
111        /**
112         * Sets a specific secure random generator for the initialisation
113         * vector and other purposes requiring a random number.
114         *
115         * @param randomGen The secure random generator, {@code null} to use
116         *                  the default system one.
117         */
118        public void setSecureRandom(final SecureRandom randomGen) {
119
120                this.randomGen = randomGen;
121        }
122}