]>
Commit | Line | Data |
---|---|---|
28f7d9d0 | 1 | /* AlgorithmParameterGenerator.java --- Algorithm Parameter Generator |
7451c155 | 2 | Copyright (C) 1999, 2003 Free Software Foundation, Inc. |
28f7d9d0 BM |
3 | |
4 | This file is part of GNU Classpath. | |
5 | ||
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) | |
9 | any later version. | |
10 | ||
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. | |
15 | ||
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., 59 Temple Place, Suite 330, Boston, MA | |
19 | 02111-1307 USA. | |
20 | ||
92aaa246 MW |
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 | |
24 | combination. | |
25 | ||
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. */ | |
28f7d9d0 BM |
37 | |
38 | package java.security; | |
7451c155 | 39 | |
28f7d9d0 BM |
40 | import java.security.spec.AlgorithmParameterSpec; |
41 | ||
42 | /** | |
7451c155 TT |
43 | * <p>The <code>AlgorithmParameterGenerator</code> class is used to generate a |
44 | * set of parameters to be used with a certain algorithm. Parameter generators | |
45 | * are constructed using the <code>getInstance()</code> factory methods (static | |
46 | * methods that return instances of a given class).</p> | |
47 | * | |
48 | * <p>The object that will generate the parameters can be initialized in two | |
49 | * different ways: in an algorithm-independent manner, or in an | |
50 | * algorithm-specific manner:</p> | |
51 | * | |
52 | * <ul> | |
53 | * <li>The algorithm-independent approach uses the fact that all parameter | |
54 | * generators share the concept of a <i>"size"</i> and a <i>source of | |
55 | * randomness</i>. The measure of <i>size</i> is universally shared by all | |
56 | * algorithm parameters, though it is interpreted differently for different | |
57 | * algorithms. For example, in the case of parameters for the <i>DSA</i> | |
58 | * algorithm, <i>"size"</i> corresponds to the size of the prime modulus (in | |
59 | * bits). When using this approach, algorithm-specific parameter generation | |
60 | * values - if any - default to some standard values, unless they can be | |
61 | * derived from the specified size.</li> | |
62 | * <li>The other approach initializes a parameter generator object using | |
63 | * algorithm-specific semantics, which are represented by a set of | |
64 | * algorithm-specific parameter generation values. To generate Diffie-Hellman | |
65 | * system parameters, for example, the parameter generation values usually | |
66 | * consist of the size of the prime modulus and the size of the random | |
67 | * exponent, both specified in number of bits.</li> | |
68 | * <ul> | |
69 | * | |
70 | * <p>In case the client does not explicitly initialize the | |
71 | * <code>AlgorithmParameterGenerator</code> (via a call to an <code>init()</code> | |
72 | * method), each provider must supply (and document) a default initialization. | |
73 | * For example, the <b>GNU</b> provider uses a default modulus prime size of | |
74 | * <code>1024</code> bits for the generation of <i>DSA</i> parameters. | |
75 | * | |
76 | * @author Mark Benvenuto | |
77 | * @since 1.2 | |
78 | * @see AlgorithmParameters | |
79 | * @see AlgorithmParameterSpec | |
28f7d9d0 BM |
80 | */ |
81 | public class AlgorithmParameterGenerator | |
82 | { | |
7451c155 TT |
83 | /** Service name for algorithm parameter generators. */ |
84 | private static final String ALGORITHM_PARAMETER_GENERATOR = | |
85 | "AlgorithmParameterGenerator"; | |
86 | ||
28f7d9d0 BM |
87 | private AlgorithmParameterGeneratorSpi paramGenSpi; |
88 | private Provider provider; | |
89 | private String algorithm; | |
90 | ||
91 | /** | |
7451c155 TT |
92 | * Creates an <code>AlgorithmParameterGenerator</code> object. |
93 | * | |
94 | * @param paramGenSpi the delegate. | |
95 | * @param provider the provider. | |
96 | * @param algorithm the algorithm. | |
28f7d9d0 BM |
97 | */ |
98 | protected AlgorithmParameterGenerator(AlgorithmParameterGeneratorSpi | |
99 | paramGenSpi, Provider provider, | |
100 | String algorithm) | |
101 | { | |
102 | this.paramGenSpi = paramGenSpi; | |
103 | this.provider = provider; | |
104 | this.algorithm = algorithm; | |
105 | } | |
106 | ||
107 | /** | |
7451c155 TT |
108 | * Returns the standard name of the algorithm this parameter generator is |
109 | * associated with. | |
110 | * | |
111 | * @return the string name of the algorithm. | |
28f7d9d0 BM |
112 | */ |
113 | public final String getAlgorithm() | |
114 | { | |
115 | return algorithm; | |
116 | } | |
117 | ||
7451c155 TT |
118 | /** |
119 | * Generates an <code>AlgorithmParameterGenerator</code> object that | |
120 | * implements the specified digest algorithm. If the default provider package | |
121 | * provides an implementation of the requested digest algorithm, an instance | |
122 | * of <code>AlgorithmParameterGenerator</code> containing that implementation | |
123 | * is returned. If the algorithm is not available in the default package, | |
124 | * other packages are searched. | |
125 | * | |
126 | * @param algorithm the string name of the algorithm this parameter generator | |
127 | * is associated with. | |
128 | * @return the new <code>AlgorithmParameterGenerator</code> object. | |
129 | * @throws NoSuchAlgorithmException if the algorithm is not available in the | |
130 | * environment. | |
28f7d9d0 BM |
131 | */ |
132 | public static AlgorithmParameterGenerator getInstance(String algorithm) | |
133 | throws NoSuchAlgorithmException | |
134 | { | |
135 | Provider[] p = Security.getProviders(); | |
28f7d9d0 | 136 | for (int i = 0; i < p.length; i++) |
7451c155 TT |
137 | try |
138 | { | |
139 | return getInstance(algorithm, p[i]); | |
140 | } | |
141 | catch (NoSuchAlgorithmException ignored) {} | |
28f7d9d0 BM |
142 | |
143 | throw new NoSuchAlgorithmException(algorithm); | |
144 | } | |
145 | ||
7451c155 TT |
146 | /** |
147 | * Generates an <code>AlgorithmParameterGenerator</code> object for the | |
148 | * requested algorithm, as supplied from the specified provider, if such a | |
149 | * parameter generator is available from the provider. | |
150 | * | |
151 | * @param algorithm the string name of the algorithm. | |
152 | * @param provider the string name of the provider. | |
153 | * @return the new <code>AlgorithmParameterGenerator</code> object. | |
154 | * @throws NoSuchAlgorithmException if the <code>algorithm</code> is not | |
155 | * available from the <code>provider</code>. | |
156 | * @throws NoSuchProviderException if the <code>provider</code> is not | |
157 | * available in the environment. | |
158 | * @throws IllegalArgumentException if the <code>provider</code> name is | |
159 | * <code>null</code> or empty. | |
160 | * @see Provider | |
28f7d9d0 BM |
161 | */ |
162 | public static AlgorithmParameterGenerator getInstance(String algorithm, | |
163 | String provider) | |
164 | throws NoSuchAlgorithmException, NoSuchProviderException | |
165 | { | |
7451c155 TT |
166 | if (provider == null || provider.length() == 0) |
167 | throw new IllegalArgumentException("Illegal provider"); | |
168 | ||
28f7d9d0 BM |
169 | Provider p = Security.getProvider(provider); |
170 | if (p == null) | |
171 | throw new NoSuchProviderException(); | |
172 | ||
7451c155 | 173 | return getInstance(algorithm, p); |
28f7d9d0 BM |
174 | } |
175 | ||
7451c155 TT |
176 | /** |
177 | * Generates an AlgorithmParameterGenerator object for the requested | |
178 | * algorithm, as supplied from the specified provider, if such a parameter | |
179 | * generator is available from the provider. Note: the <code>provider</code> | |
180 | * doesn't have to be registered. | |
181 | * | |
182 | * @param algorithm the string name of the algorithm. | |
183 | * @param provider the provider. | |
184 | * @return the new AlgorithmParameterGenerator object. | |
185 | * @throws NoSuchAlgorithmException if the algorithm is not available from | |
186 | * the provider. | |
187 | * @throws IllegalArgumentException if the provider is null. | |
188 | * @since 1.4 | |
189 | * @see Provider | |
190 | */ | |
191 | public static AlgorithmParameterGenerator getInstance(String algorithm, | |
192 | Provider provider) | |
28f7d9d0 BM |
193 | throws NoSuchAlgorithmException |
194 | { | |
7451c155 TT |
195 | if (provider == null) |
196 | throw new IllegalArgumentException("Illegal provider"); | |
28f7d9d0 BM |
197 | |
198 | try | |
199 | { | |
7451c155 TT |
200 | return new AlgorithmParameterGenerator( |
201 | (AlgorithmParameterGeneratorSpi) Engine.getInstance( | |
202 | ALGORITHM_PARAMETER_GENERATOR, algorithm, provider), | |
203 | provider, algorithm); | |
204 | } | |
205 | catch (ClassCastException cce) | |
28f7d9d0 | 206 | { |
7451c155 | 207 | throw new NoSuchAlgorithmException(algorithm); |
28f7d9d0 BM |
208 | } |
209 | } | |
210 | ||
211 | /** | |
7451c155 TT |
212 | * Returns the provider of this algorithm parameter generator object. |
213 | * | |
214 | * @return the provider of this algorithm parameter generator object. | |
28f7d9d0 BM |
215 | */ |
216 | public final Provider getProvider() | |
217 | { | |
218 | return provider; | |
219 | } | |
220 | ||
221 | /** | |
7451c155 TT |
222 | * Initializes this parameter generator for a certain <i>size</i>. To create |
223 | * the parameters, the {@link SecureRandom} implementation of the | |
224 | * highest-priority installed provider is used as the source of randomness. | |
225 | * (If none of the installed providers supply an implementation of | |
226 | * {@link SecureRandom}, a system-provided source of randomness is used.) | |
227 | * | |
228 | * @param size the size (number of bits). | |
28f7d9d0 BM |
229 | */ |
230 | public final void init(int size) | |
231 | { | |
232 | init(size, new SecureRandom()); | |
233 | } | |
234 | ||
235 | /** | |
7451c155 TT |
236 | * Initializes this parameter generator for a certain size and source of |
237 | * randomness. | |
238 | * | |
239 | * @param size the size (number of bits). | |
240 | * @param random the source of randomness. | |
28f7d9d0 BM |
241 | */ |
242 | public final void init(int size, SecureRandom random) | |
243 | { | |
244 | paramGenSpi.engineInit(size, random); | |
245 | } | |
246 | ||
247 | /** | |
7451c155 TT |
248 | * Initializes this parameter generator with a set of algorithm-specific |
249 | * parameter generation values. To generate the parameters, the {@link | |
250 | * SecureRandom} implementation of the highest-priority installed provider is | |
251 | * used as the source of randomness. (If none of the installed providers | |
252 | * supply an implementation of {@link SecureRandom}, a system-provided source | |
253 | * of randomness is used.) | |
254 | * | |
255 | * @param genParamSpec the set of algorithm-specific parameter generation | |
256 | * values. | |
257 | * @throws InvalidAlgorithmParameterException if the given parameter | |
258 | * generation values are inappropriate for this parameter generator. | |
28f7d9d0 | 259 | */ |
7451c155 TT |
260 | public final void init(AlgorithmParameterSpec genParamSpec) |
261 | throws InvalidAlgorithmParameterException | |
28f7d9d0 BM |
262 | { |
263 | init(genParamSpec, new SecureRandom()); | |
264 | } | |
265 | ||
266 | /** | |
7451c155 TT |
267 | * Initializes this parameter generator with a set of algorithm-specific |
268 | * parameter generation values. | |
269 | * | |
270 | * @param genParamSpec the set of algorithm-specific parameter generation | |
271 | * values. | |
272 | * @param random the source of randomness. | |
273 | * @throws InvalidAlgorithmParameterException if the given parameter | |
274 | * generation values are inappropriate for this parameter generator. | |
28f7d9d0 BM |
275 | */ |
276 | public final void init(AlgorithmParameterSpec genParamSpec, | |
7451c155 TT |
277 | SecureRandom random) |
278 | throws InvalidAlgorithmParameterException | |
28f7d9d0 BM |
279 | { |
280 | paramGenSpi.engineInit(genParamSpec, random); | |
281 | } | |
282 | ||
283 | /** | |
7451c155 TT |
284 | * Generates the parameters. |
285 | * | |
286 | * @return the new {@link AlgorithmParameters} object. | |
28f7d9d0 BM |
287 | */ |
288 | public final AlgorithmParameters generateParameters() | |
289 | { | |
290 | return paramGenSpi.engineGenerateParameters(); | |
291 | } | |
292 | } |