001: /*
002: * @(#)AlgorithmParameterGenerator.java 1.43 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package java.security;
029:
030: import java.security.spec.AlgorithmParameterSpec;
031:
032: /**
033: * The <code>AlgorithmParameterGenerator</code> class is used to generate a
034: * set of
035: * parameters to be used with a certain algorithm. Parameter generators
036: * are constructed using the <code>getInstance</code> factory methods
037: * (static methods that return instances of a given class).
038: *
039: * <P>The object that will generate the parameters can be initialized
040: * in two different ways: in an algorithm-independent manner, or in an
041: * algorithm-specific manner:
042: *
043: * <ul>
044: * <li>The algorithm-independent approach uses the fact that all parameter
045: * generators share the concept of a "size" and a
046: * source of randomness. The measure of size is universally shared
047: * by all algorithm parameters, though it is interpreted differently
048: * for different algorithms. For example, in the case of parameters for
049: * the <i>DSA</i> algorithm, "size" corresponds to the size
050: * of the prime modulus (in bits).
051: * When using this approach, algorithm-specific parameter generation
052: * values - if any - default to some standard values, unless they can be
053: * derived from the specified size.<P>
054: *
055: * <li>The other approach initializes a parameter generator object
056: * using algorithm-specific semantics, which are represented by a set of
057: * algorithm-specific parameter generation values. To generate
058: * Diffie-Hellman system parameters, for example, the parameter generation
059: * values usually
060: * consist of the size of the prime modulus and the size of the
061: * random exponent, both specified in number of bits.
062: * </ul>
063: *
064: * <P>In case the client does not explicitly initialize the
065: * AlgorithmParameterGenerator
066: * (via a call to an <code>init</code> method), each provider must supply (and
067: * document) a default initialization. For example, the Sun provider uses a
068: * default modulus prime size of 1024 bits for the generation of DSA
069: * parameters.
070: *
071: * @author Jan Luehe
072: *
073: * @version 1.36, 02/02/00
074: *
075: * @see AlgorithmParameters
076: * @see java.security.spec.AlgorithmParameterSpec
077: *
078: * @since 1.2
079: */
080:
081: public class AlgorithmParameterGenerator {
082:
083: // The provider
084: private Provider provider;
085:
086: // The provider implementation (delegate)
087: private AlgorithmParameterGeneratorSpi paramGenSpi;
088:
089: // The algorithm
090: private String algorithm;
091:
092: /**
093: * Creates an AlgorithmParameterGenerator object.
094: *
095: * @param paramGenSpi the delegate
096: * @param provider the provider
097: * @param algorithm the algorithm
098: */
099: protected AlgorithmParameterGenerator(
100: AlgorithmParameterGeneratorSpi paramGenSpi,
101: Provider provider, String algorithm) {
102: this .paramGenSpi = paramGenSpi;
103: this .provider = provider;
104: this .algorithm = algorithm;
105: }
106:
107: /**
108: * Returns the standard name of the algorithm this parameter
109: * generator is associated with.
110: *
111: * @return the string name of the algorithm.
112: */
113: public final String getAlgorithm() {
114: return this .algorithm;
115: }
116:
117: /**
118: * Generates an AlgorithmParameterGenerator object that implements the
119: * specified digest algorithm. If the default provider package
120: * provides an implementation of the requested digest algorithm,
121: * an instance of AlgorithmParameterGenerator containing that
122: * implementation
123: * is returned. If the algorithm is not available in the default
124: * package, other packages are searched.
125: *
126: * @param algorithm the string name of the algorithm this
127: * parameter generator is associated with.
128: *
129: * @return the new AlgorithmParameterGenerator object.
130: *
131: * @exception NoSuchAlgorithmException if the algorithm is
132: * not available in the environment.
133: */
134: public static AlgorithmParameterGenerator getInstance(
135: String algorithm) throws NoSuchAlgorithmException {
136: try {
137: Object[] objs = Security.getImpl(algorithm,
138: "AlgorithmParameterGenerator", (String) null);
139: return new AlgorithmParameterGenerator(
140: (AlgorithmParameterGeneratorSpi) objs[0],
141: (Provider) objs[1], algorithm);
142: } catch (NoSuchProviderException e) {
143: throw new NoSuchAlgorithmException(algorithm + " not found");
144: }
145: }
146:
147: /**
148: * Generates an AlgorithmParameterGenerator object for the requested
149: * algorithm, as supplied from the specified provider,
150: * if such a parameter generator is available from the provider.
151: *
152: * @param algorithm the string name of the algorithm.
153: *
154: * @param provider the string name of the provider.
155: *
156: * @return the new AlgorithmParameterGenerator object.
157: *
158: * @exception NoSuchAlgorithmException if the algorithm is
159: * not available from the provider.
160: *
161: * @exception NoSuchProviderException if the provider is not
162: * available in the environment.
163: *
164: * @exception IllegalArgumentException if the provider name is null
165: * or empty.
166: *
167: * @see Provider
168: */
169: public static AlgorithmParameterGenerator getInstance(
170: String algorithm, String provider)
171: throws NoSuchAlgorithmException, NoSuchProviderException {
172: if (provider == null || provider.length() == 0)
173: throw new IllegalArgumentException("missing provider");
174: Object[] objs = Security.getImpl(algorithm,
175: "AlgorithmParameterGenerator", provider);
176: return new AlgorithmParameterGenerator(
177: (AlgorithmParameterGeneratorSpi) objs[0],
178: (Provider) objs[1], algorithm);
179: }
180:
181: /**
182: * Generates an AlgorithmParameterGenerator object for the requested
183: * algorithm, as supplied from the specified provider,
184: * if such a parameter generator is available from the provider.
185: * Note: the <code>provider</code> doesn't have to be registered.
186: *
187: * @param algorithm the string name of the algorithm.
188: *
189: * @param provider the provider.
190: *
191: * @return the new AlgorithmParameterGenerator object.
192: *
193: * @exception NoSuchAlgorithmException if the algorithm is
194: * not available from the provider.
195: *
196: * @exception IllegalArgumentException if the <code>provider</code> is
197: * null.
198: *
199: * @see Provider
200: *
201: * @since 1.4
202: */
203: public static AlgorithmParameterGenerator getInstance(
204: String algorithm, Provider provider)
205: throws NoSuchAlgorithmException {
206: if (provider == null)
207: throw new IllegalArgumentException("missing provider");
208: Object[] objs = Security.getImpl(algorithm,
209: "AlgorithmParameterGenerator", provider);
210: return new AlgorithmParameterGenerator(
211: (AlgorithmParameterGeneratorSpi) objs[0],
212: (Provider) objs[1], algorithm);
213: }
214:
215: /**
216: * Returns the provider of this algorithm parameter generator object.
217: *
218: * @return the provider of this algorithm parameter generator object
219: */
220: public final Provider getProvider() {
221: return this .provider;
222: }
223:
224: /**
225: * Initializes this parameter generator for a certain size.
226: * To create the parameters, the <code>SecureRandom</code>
227: * implementation of the highest-priority installed provider is used as
228: * the source of randomness.
229: * (If none of the installed providers supply an implementation of
230: * <code>SecureRandom</code>, a system-provided source of randomness is
231: * used.)
232: *
233: * @param size the size (number of bits).
234: */
235: public final void init(int size) {
236: paramGenSpi.engineInit(size, new SecureRandom());
237: }
238:
239: /**
240: * Initializes this parameter generator for a certain size and source
241: * of randomness.
242: *
243: * @param size the size (number of bits).
244: * @param random the source of randomness.
245: */
246: public final void init(int size, SecureRandom random) {
247: paramGenSpi.engineInit(size, random);
248: }
249:
250: /**
251: * Initializes this parameter generator with a set of algorithm-specific
252: * parameter generation values.
253: * To generate the parameters, the <code>SecureRandom</code>
254: * implementation of the highest-priority installed provider is used as
255: * the source of randomness.
256: * (If none of the installed providers supply an implementation of
257: * <code>SecureRandom</code>, a system-provided source of randomness is
258: * used.)
259: *
260: * @param genParamSpec the set of algorithm-specific parameter generation values.
261: *
262: * @exception InvalidAlgorithmParameterException if the given parameter
263: * generation values are inappropriate for this parameter generator.
264: */
265: public final void init(AlgorithmParameterSpec genParamSpec)
266: throws InvalidAlgorithmParameterException {
267: paramGenSpi.engineInit(genParamSpec, new SecureRandom());
268: }
269:
270: /**
271: * Initializes this parameter generator with a set of algorithm-specific
272: * parameter generation values.
273: *
274: * @param genParamSpec the set of algorithm-specific parameter generation values.
275: * @param random the source of randomness.
276: *
277: * @exception InvalidAlgorithmParameterException if the given parameter
278: * generation values are inappropriate for this parameter generator.
279: */
280: public final void init(AlgorithmParameterSpec genParamSpec,
281: SecureRandom random)
282: throws InvalidAlgorithmParameterException {
283: paramGenSpi.engineInit(genParamSpec, random);
284: }
285:
286: /**
287: * Generates the parameters.
288: *
289: * @return the new AlgorithmParameters object.
290: */
291: public final AlgorithmParameters generateParameters() {
292: return paramGenSpi.engineGenerateParameters();
293: }
294: }
|