1 /* AlgorithmParameterGenerator.java --- Algorithm Parameter Generator
2 Copyright (C) 1999, 2003, 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.security;
41 import gnu.java.lang.CPStringBuilder;
43 import gnu.java.security.Engine;
45 import java.lang.reflect.InvocationTargetException;
46 import java.security.spec.AlgorithmParameterSpec;
49 * <code>AlgorithmParameterGenerator</code> is used to generate algorithm
50 * parameters for specified algorithms.
52 * <p>In case the client does not explicitly initialize the
53 * <code>AlgorithmParameterGenerator</code> (via a call to an
54 * <code>init()</code> method), each provider must supply (and document) a
55 * default initialization. For example, the <b>GNU</b> provider uses a default
56 * modulus prime size of <code>1024</code> bits for the generation of <i>DSA</i>
59 * @author Mark Benvenuto
61 * @see AlgorithmParameters
62 * @see AlgorithmParameterSpec
64 public class AlgorithmParameterGenerator
66 /** Service name for algorithm parameter generators. */
67 private static final String ALGORITHM_PARAMETER_GENERATOR =
68 "AlgorithmParameterGenerator";
70 private AlgorithmParameterGeneratorSpi paramGenSpi;
71 private Provider provider;
72 private String algorithm;
75 * Constructs a new instance of <code>AlgorithmParameterGenerator</code>.
78 * the generator to use.
80 * the provider to use.
82 * the algorithm to use.
84 protected AlgorithmParameterGenerator(AlgorithmParameterGeneratorSpi
85 paramGenSpi, Provider provider,
88 this.paramGenSpi = paramGenSpi;
89 this.provider = provider;
90 this.algorithm = algorithm;
93 /** @return the name of the algorithm. */
94 public final String getAlgorithm()
100 * Returns a new <code>AlgorithmParameterGenerator</code> instance which
101 * generates algorithm parameters for the specified algorithm.
103 * @param algorithm the name of algorithm to use.
104 * @return the new instance.
105 * @throws NoSuchAlgorithmException if <code>algorithm</code> is not
106 * implemented by any provider.
107 * @throws IllegalArgumentException if <code>algorithm</code> is
108 * <code>null</code> or is an empty string.
110 public static AlgorithmParameterGenerator getInstance(String algorithm)
111 throws NoSuchAlgorithmException
113 Provider[] p = Security.getProviders();
114 NoSuchAlgorithmException lastException = null;
115 for (int i = 0; i < p.length; i++)
118 return getInstance(algorithm, p[i]);
120 catch (NoSuchAlgorithmException x)
124 if (lastException != null)
126 throw new NoSuchAlgorithmException(algorithm);
130 * Returns a new <code>AlgorithmParameterGenerator</code> instance which
131 * generates algorithm parameters for the specified algorithm.
133 * @param algorithm the name of algorithm to use.
134 * @param provider the name of the {@link Provider} to use.
135 * @return the new instance.
136 * @throws NoSuchAlgorithmException if the algorithm is not implemented by the
138 * @throws NoSuchProviderException if the named provider was not found.
139 * @throws IllegalArgumentException if either <code>algorithm</code> or
140 * <code>provider</code> is <code>null</code> or empty.
142 public static AlgorithmParameterGenerator getInstance(String algorithm,
144 throws NoSuchAlgorithmException, NoSuchProviderException
146 if (provider == null)
147 throw new IllegalArgumentException("provider MUST NOT be null");
148 provider = provider.trim();
149 if (provider.length() == 0)
150 throw new IllegalArgumentException("provider MUST NOT be empty");
151 Provider p = Security.getProvider(provider);
153 throw new NoSuchProviderException(provider);
154 return getInstance(algorithm, p);
158 * Returns a new <code>AlgorithmParameterGenerator</code> instance which
159 * generates algorithm parameters for the specified algorithm.
161 * @param algorithm the name of algorithm to use.
162 * @param provider the {@link Provider} to use.
163 * @return the new instance.
164 * @throws NoSuchAlgorithmException if the algorithm is not implemented by
166 * @throws IllegalArgumentException if either <code>algorithm</code> or
167 * <code>provider</code> is <code>null</code>, or if
168 * <code>algorithm</code> is an empty string.
172 public static AlgorithmParameterGenerator getInstance(String algorithm,
174 throws NoSuchAlgorithmException
176 CPStringBuilder sb = new CPStringBuilder()
177 .append("AlgorithmParameterGenerator for algorithm [")
178 .append(algorithm).append("] from provider[")
179 .append(provider).append("] could not be created");
183 Object spi = Engine.getInstance(ALGORITHM_PARAMETER_GENERATOR,
186 return new AlgorithmParameterGenerator((AlgorithmParameterGeneratorSpi) spi,
190 catch (InvocationTargetException x)
192 cause = x.getCause();
193 if (cause instanceof NoSuchAlgorithmException)
194 throw (NoSuchAlgorithmException) cause;
198 catch (ClassCastException x)
202 NoSuchAlgorithmException x = new NoSuchAlgorithmException(sb.toString());
207 /** @return the {@link Provider} of this generator. */
208 public final Provider getProvider()
214 * Initializes this instance with the specified size. Since no source of
215 * randomness is supplied, a default one will be used.
218 * size (in bits) to use.
220 public final void init(int size)
222 init(size, new SecureRandom());
226 * Initializes this instance with the specified key-size and source of
230 * the size (in bits) to use.
232 * the {@link SecureRandom} to use.
234 public final void init(int size, SecureRandom random)
236 paramGenSpi.engineInit(size, random);
240 * Initializes this instance with the specified {@link AlgorithmParameterSpec}.
241 * Since no source of randomness is supplied, a default one will be used.
243 * @param genParamSpec
244 * the {@link AlgorithmParameterSpec} to use.
245 * @throws InvalidAlgorithmParameterException
246 * if <code>genParamSpec</code> is invalid.
248 public final void init(AlgorithmParameterSpec genParamSpec)
249 throws InvalidAlgorithmParameterException
251 init(genParamSpec, new SecureRandom());
255 * Initializes this instance with the specified {@link AlgorithmParameterSpec}
256 * and source of randomness.
258 * @param genParamSpec
259 * the {@link AlgorithmParameterSpec} to use.
261 * the {@link SecureRandom} to use.
262 * @throws InvalidAlgorithmParameterException
263 * if <code>genParamSpec</code> is invalid.
265 public final void init(AlgorithmParameterSpec genParamSpec,
267 throws InvalidAlgorithmParameterException
269 paramGenSpi.engineInit(genParamSpec, random);
272 /** @return a new instance of {@link AlgorithmParameters}. */
273 public final AlgorithmParameters generateParameters()
275 return paramGenSpi.engineGenerateParameters();