Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/java/security/AlgorithmParameterGenerator.java
41152 views
1
/*
2
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package java.security;
27
28
import java.security.spec.AlgorithmParameterSpec;
29
import java.util.Objects;
30
import sun.security.jca.JCAUtil;
31
32
/**
33
* The {@code AlgorithmParameterGenerator} class is used to generate a
34
* set of
35
* parameters to be used with a certain algorithm. Parameter generators
36
* are constructed using the {@code getInstance} factory methods
37
* (static methods that return instances of a given class).
38
*
39
* <P>The object that will generate the parameters can be initialized
40
* in two different ways: in an algorithm-independent manner, or in an
41
* algorithm-specific manner:
42
*
43
* <ul>
44
* <li>The algorithm-independent approach uses the fact that all parameter
45
* generators share the concept of a "size" and a
46
* source of randomness. The measure of size is universally shared
47
* by all algorithm parameters, though it is interpreted differently
48
* for different algorithms. For example, in the case of parameters for
49
* the <i>DSA</i> algorithm, "size" corresponds to the size
50
* of the prime modulus (in bits).
51
* When using this approach, algorithm-specific parameter generation
52
* values - if any - default to some standard values, unless they can be
53
* derived from the specified size.
54
*
55
* <li>The other approach initializes a parameter generator object
56
* using algorithm-specific semantics, which are represented by a set of
57
* algorithm-specific parameter generation values. To generate
58
* Diffie-Hellman system parameters, for example, the parameter generation
59
* values usually
60
* consist of the size of the prime modulus and the size of the
61
* random exponent, both specified in number of bits.
62
* </ul>
63
*
64
* <P>In case the client does not explicitly initialize the
65
* AlgorithmParameterGenerator (via a call to an {@code init} method),
66
* each provider must supply (and document) a default initialization.
67
* See the Keysize Restriction sections of the
68
* {@extLink security_guide_jdk_providers JDK Providers}
69
* document for information on the AlgorithmParameterGenerator defaults
70
* used by JDK providers.
71
* However, note that defaults may vary across different providers.
72
* Additionally, the default value for a provider may change in a future
73
* version. Therefore, it is recommended to explicitly initialize the
74
* AlgorithmParameterGenerator instead of relying on provider-specific defaults.
75
*
76
* <p> Every implementation of the Java platform is required to support the
77
* following standard {@code AlgorithmParameterGenerator} algorithms and
78
* keysizes in parentheses:
79
* <ul>
80
* <li>{@code DiffieHellman} (1024, 2048)</li>
81
* <li>{@code DSA} (1024, 2048)</li>
82
* </ul>
83
* These algorithms are described in the <a href=
84
* "{@docRoot}/../specs/security/standard-names.html#algorithmparametergenerator-algorithms">
85
* AlgorithmParameterGenerator section</a> of the
86
* Java Security Standard Algorithm Names Specification.
87
* Consult the release documentation for your implementation to see if any
88
* other algorithms are supported.
89
*
90
* @author Jan Luehe
91
*
92
*
93
* @see AlgorithmParameters
94
* @see java.security.spec.AlgorithmParameterSpec
95
*
96
* @since 1.2
97
*/
98
99
public class AlgorithmParameterGenerator {
100
101
// The provider
102
private Provider provider;
103
104
// The provider implementation (delegate)
105
private AlgorithmParameterGeneratorSpi paramGenSpi;
106
107
// The algorithm
108
private String algorithm;
109
110
/**
111
* Creates an AlgorithmParameterGenerator object.
112
*
113
* @param paramGenSpi the delegate
114
* @param provider the provider
115
* @param algorithm the algorithm
116
*/
117
protected AlgorithmParameterGenerator
118
(AlgorithmParameterGeneratorSpi paramGenSpi, Provider provider,
119
String algorithm) {
120
this.paramGenSpi = paramGenSpi;
121
this.provider = provider;
122
this.algorithm = algorithm;
123
}
124
125
/**
126
* Returns the standard name of the algorithm this parameter
127
* generator is associated with.
128
*
129
* @return the string name of the algorithm.
130
*/
131
public final String getAlgorithm() {
132
return this.algorithm;
133
}
134
135
/**
136
* Returns an AlgorithmParameterGenerator object for generating
137
* a set of parameters to be used with the specified algorithm.
138
*
139
* <p> This method traverses the list of registered security Providers,
140
* starting with the most preferred Provider.
141
* A new AlgorithmParameterGenerator object encapsulating the
142
* AlgorithmParameterGeneratorSpi implementation from the first
143
* Provider that supports the specified algorithm is returned.
144
*
145
* <p> Note that the list of registered providers may be retrieved via
146
* the {@link Security#getProviders() Security.getProviders()} method.
147
*
148
* @implNote
149
* The JDK Reference Implementation additionally uses the
150
* {@code jdk.security.provider.preferred}
151
* {@link Security#getProperty(String) Security} property to determine
152
* the preferred provider order for the specified algorithm. This
153
* may be different than the order of providers returned by
154
* {@link Security#getProviders() Security.getProviders()}.
155
*
156
* @param algorithm the name of the algorithm this
157
* parameter generator is associated with.
158
* See the AlgorithmParameterGenerator section in the <a href=
159
* "{@docRoot}/../specs/security/standard-names.html#algorithmparametergenerator-algorithms">
160
* Java Security Standard Algorithm Names Specification</a>
161
* for information about standard algorithm names.
162
*
163
* @return the new {@code AlgorithmParameterGenerator} object
164
*
165
* @throws NoSuchAlgorithmException if no {@code Provider} supports an
166
* {@code AlgorithmParameterGeneratorSpi} implementation for the
167
* specified algorithm
168
*
169
* @throws NullPointerException if {@code algorithm} is {@code null}
170
*
171
* @see Provider
172
*/
173
public static AlgorithmParameterGenerator getInstance(String algorithm)
174
throws NoSuchAlgorithmException {
175
Objects.requireNonNull(algorithm, "null algorithm name");
176
try {
177
Object[] objs = Security.getImpl(algorithm,
178
"AlgorithmParameterGenerator",
179
(String)null);
180
return new AlgorithmParameterGenerator
181
((AlgorithmParameterGeneratorSpi)objs[0],
182
(Provider)objs[1],
183
algorithm);
184
} catch(NoSuchProviderException e) {
185
throw new NoSuchAlgorithmException(algorithm + " not found");
186
}
187
}
188
189
/**
190
* Returns an AlgorithmParameterGenerator object for generating
191
* a set of parameters to be used with the specified algorithm.
192
*
193
* <p> A new AlgorithmParameterGenerator object encapsulating the
194
* AlgorithmParameterGeneratorSpi implementation from the specified provider
195
* is returned. The specified provider must be registered
196
* in the security provider list.
197
*
198
* <p> Note that the list of registered providers may be retrieved via
199
* the {@link Security#getProviders() Security.getProviders()} method.
200
*
201
* @param algorithm the name of the algorithm this
202
* parameter generator is associated with.
203
* See the AlgorithmParameterGenerator section in the <a href=
204
* "{@docRoot}/../specs/security/standard-names.html#algorithmparametergenerator-algorithms">
205
* Java Security Standard Algorithm Names Specification</a>
206
* for information about standard algorithm names.
207
*
208
* @param provider the string name of the Provider.
209
*
210
* @return the new {@code AlgorithmParameterGenerator} object
211
*
212
* @throws IllegalArgumentException if the provider name is {@code null}
213
* or empty
214
*
215
* @throws NoSuchAlgorithmException if an
216
* {@code AlgorithmParameterGeneratorSpi}
217
* implementation for the specified algorithm is not
218
* available from the specified provider
219
*
220
* @throws NoSuchProviderException if the specified provider is not
221
* registered in the security provider list
222
*
223
* @throws NullPointerException if {@code algorithm} is {@code null}
224
*
225
* @see Provider
226
*/
227
public static AlgorithmParameterGenerator getInstance(String algorithm,
228
String provider)
229
throws NoSuchAlgorithmException, NoSuchProviderException
230
{
231
Objects.requireNonNull(algorithm, "null algorithm name");
232
if (provider == null || provider.isEmpty())
233
throw new IllegalArgumentException("missing provider");
234
Object[] objs = Security.getImpl(algorithm,
235
"AlgorithmParameterGenerator",
236
provider);
237
return new AlgorithmParameterGenerator
238
((AlgorithmParameterGeneratorSpi)objs[0], (Provider)objs[1],
239
algorithm);
240
}
241
242
/**
243
* Returns an AlgorithmParameterGenerator object for generating
244
* a set of parameters to be used with the specified algorithm.
245
*
246
* <p> A new AlgorithmParameterGenerator object encapsulating the
247
* AlgorithmParameterGeneratorSpi implementation from the specified Provider
248
* object is returned. Note that the specified Provider object
249
* does not have to be registered in the provider list.
250
*
251
* @param algorithm the string name of the algorithm this
252
* parameter generator is associated with.
253
* See the AlgorithmParameterGenerator section in the <a href=
254
* "{@docRoot}/../specs/security/standard-names.html#algorithmparametergenerator-algorithms">
255
* Java Security Standard Algorithm Names Specification</a>
256
* for information about standard algorithm names.
257
*
258
* @param provider the {@code Provider} object.
259
*
260
* @return the new {@code AlgorithmParameterGenerator} object
261
*
262
* @throws IllegalArgumentException if the specified provider is
263
* {@code null}
264
*
265
* @throws NoSuchAlgorithmException if an
266
* {@code AlgorithmParameterGeneratorSpi}
267
* implementation for the specified algorithm is not available
268
* from the specified {@code Provider} object
269
*
270
* @throws NullPointerException if {@code algorithm} is {@code null}
271
*
272
* @see Provider
273
*
274
* @since 1.4
275
*/
276
public static AlgorithmParameterGenerator getInstance(String algorithm,
277
Provider provider)
278
throws NoSuchAlgorithmException
279
{
280
Objects.requireNonNull(algorithm, "null algorithm name");
281
if (provider == null)
282
throw new IllegalArgumentException("missing provider");
283
Object[] objs = Security.getImpl(algorithm,
284
"AlgorithmParameterGenerator",
285
provider);
286
return new AlgorithmParameterGenerator
287
((AlgorithmParameterGeneratorSpi)objs[0], (Provider)objs[1],
288
algorithm);
289
}
290
291
/**
292
* Returns the provider of this algorithm parameter generator object.
293
*
294
* @return the provider of this algorithm parameter generator object
295
*/
296
public final Provider getProvider() {
297
return this.provider;
298
}
299
300
/**
301
* Initializes this parameter generator for a certain size.
302
* To create the parameters, the {@code SecureRandom}
303
* implementation of the highest-priority installed provider is used as
304
* the source of randomness.
305
* (If none of the installed providers supply an implementation of
306
* {@code SecureRandom}, a system-provided source of randomness is
307
* used.)
308
*
309
* @param size the size (number of bits).
310
*/
311
public final void init(int size) {
312
paramGenSpi.engineInit(size, JCAUtil.getDefSecureRandom());
313
}
314
315
/**
316
* Initializes this parameter generator for a certain size and source
317
* of randomness.
318
*
319
* @param size the size (number of bits).
320
* @param random the source of randomness.
321
*/
322
public final void init(int size, SecureRandom random) {
323
paramGenSpi.engineInit(size, random);
324
}
325
326
/**
327
* Initializes this parameter generator with a set of algorithm-specific
328
* parameter generation values.
329
* To generate the parameters, the {@code SecureRandom}
330
* implementation of the highest-priority installed provider is used as
331
* the source of randomness.
332
* (If none of the installed providers supply an implementation of
333
* {@code SecureRandom}, a system-provided source of randomness is
334
* used.)
335
*
336
* @param genParamSpec the set of algorithm-specific parameter generation values.
337
*
338
* @throws InvalidAlgorithmParameterException if the given parameter
339
* generation values are inappropriate for this parameter generator.
340
*/
341
public final void init(AlgorithmParameterSpec genParamSpec)
342
throws InvalidAlgorithmParameterException {
343
paramGenSpi.engineInit(genParamSpec, JCAUtil.getDefSecureRandom());
344
}
345
346
/**
347
* Initializes this parameter generator with a set of algorithm-specific
348
* parameter generation values.
349
*
350
* @param genParamSpec the set of algorithm-specific parameter generation values.
351
* @param random the source of randomness.
352
*
353
* @throws InvalidAlgorithmParameterException if the given parameter
354
* generation values are inappropriate for this parameter generator.
355
*/
356
public final void init(AlgorithmParameterSpec genParamSpec,
357
SecureRandom random)
358
throws InvalidAlgorithmParameterException {
359
paramGenSpi.engineInit(genParamSpec, random);
360
}
361
362
/**
363
* Generates the parameters.
364
*
365
* @return the new AlgorithmParameters object.
366
*/
367
public final AlgorithmParameters generateParameters() {
368
return paramGenSpi.engineGenerateParameters();
369
}
370
}
371
372