1 |
771 |
jeremybenn |
/* AlgorithmParameters.java --- Algorithm Parameters Implementation Class
|
2 |
|
|
Copyright (C) 1999, 2003, 2004 Free Software Foundation, Inc.
|
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., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
|
|
02110-1301 USA.
|
20 |
|
|
|
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. */
|
37 |
|
|
|
38 |
|
|
|
39 |
|
|
package java.security;
|
40 |
|
|
|
41 |
|
|
import gnu.java.lang.CPStringBuilder;
|
42 |
|
|
|
43 |
|
|
import gnu.java.security.Engine;
|
44 |
|
|
|
45 |
|
|
import java.io.IOException;
|
46 |
|
|
import java.lang.reflect.InvocationTargetException;
|
47 |
|
|
import java.security.spec.AlgorithmParameterSpec;
|
48 |
|
|
import java.security.spec.InvalidParameterSpecException;
|
49 |
|
|
|
50 |
|
|
/**
|
51 |
|
|
* <code>AlgorithmParameters</code> is an Algorithm Parameters class which
|
52 |
|
|
* provides an interface through which the user can manage the parameters of an
|
53 |
|
|
* Algorithm.
|
54 |
|
|
*
|
55 |
|
|
* @author Mark Benvenuto
|
56 |
|
|
* @since 1.2
|
57 |
|
|
* @see AlgorithmParameterSpec
|
58 |
|
|
* @see java.security.spec.DSAParameterSpec
|
59 |
|
|
* @see KeyPairGenerator
|
60 |
|
|
*/
|
61 |
|
|
public class AlgorithmParameters
|
62 |
|
|
{
|
63 |
|
|
/** Service name for algorithm parameters. */
|
64 |
|
|
private static final String ALGORITHM_PARAMETERS = "AlgorithmParameters";
|
65 |
|
|
|
66 |
|
|
private AlgorithmParametersSpi paramSpi;
|
67 |
|
|
private Provider provider;
|
68 |
|
|
private String algorithm;
|
69 |
|
|
|
70 |
|
|
/**
|
71 |
|
|
* Constructs a new instance of <code>AlgorithmParameters</code>.
|
72 |
|
|
*
|
73 |
|
|
* @param paramSpi
|
74 |
|
|
* the engine to use.
|
75 |
|
|
* @param provider
|
76 |
|
|
* the provider to use.
|
77 |
|
|
* @param algorithm
|
78 |
|
|
* the algorithm to use.
|
79 |
|
|
*/
|
80 |
|
|
protected AlgorithmParameters(AlgorithmParametersSpi paramSpi,
|
81 |
|
|
Provider provider, String algorithm)
|
82 |
|
|
{
|
83 |
|
|
this.paramSpi = paramSpi;
|
84 |
|
|
this.provider = provider;
|
85 |
|
|
this.algorithm = algorithm;
|
86 |
|
|
}
|
87 |
|
|
|
88 |
|
|
/** @return A string with the name of the algorithm used. */
|
89 |
|
|
public final String getAlgorithm()
|
90 |
|
|
{
|
91 |
|
|
return algorithm;
|
92 |
|
|
}
|
93 |
|
|
|
94 |
|
|
/**
|
95 |
|
|
* Returns a new instance of <code>AlgorithmParameters</code> representing
|
96 |
|
|
* the specified algorithm parameters.
|
97 |
|
|
* <p>
|
98 |
|
|
* The returned <code>AlgorithmParameters</code> must still be initialized
|
99 |
|
|
* with an <code>init()</code> method.
|
100 |
|
|
*
|
101 |
|
|
* @param algorithm the algorithm to use.
|
102 |
|
|
* @return the new instance repesenting the desired algorithm.
|
103 |
|
|
* @throws NoSuchAlgorithmException if the algorithm is not implemented by any
|
104 |
|
|
* provider.
|
105 |
|
|
* @throws IllegalArgumentException if <code>algorithm</code> is
|
106 |
|
|
* <code>null</code> or is an empty string.
|
107 |
|
|
*/
|
108 |
|
|
public static AlgorithmParameters getInstance(String algorithm)
|
109 |
|
|
throws NoSuchAlgorithmException
|
110 |
|
|
{
|
111 |
|
|
Provider[] p = Security.getProviders();
|
112 |
|
|
NoSuchAlgorithmException lastException = null;
|
113 |
|
|
for (int i = 0; i < p.length; i++)
|
114 |
|
|
try
|
115 |
|
|
{
|
116 |
|
|
return getInstance(algorithm, p[i]);
|
117 |
|
|
}
|
118 |
|
|
catch (NoSuchAlgorithmException x)
|
119 |
|
|
{
|
120 |
|
|
lastException = x;
|
121 |
|
|
}
|
122 |
|
|
if (lastException != null)
|
123 |
|
|
throw lastException;
|
124 |
|
|
throw new NoSuchAlgorithmException(algorithm);
|
125 |
|
|
}
|
126 |
|
|
|
127 |
|
|
/**
|
128 |
|
|
* Returns a new instance of <code>AlgorithmParameters</code> representing
|
129 |
|
|
* the specified algorithm parameters from a named provider.
|
130 |
|
|
* <p>
|
131 |
|
|
* The returned <code>AlgorithmParameters</code> must still be intialized
|
132 |
|
|
* with an <code>init()</code> method.
|
133 |
|
|
* </p>
|
134 |
|
|
*
|
135 |
|
|
* @param algorithm the algorithm to use.
|
136 |
|
|
* @param provider the name of the {@link Provider} to use.
|
137 |
|
|
* @return the new instance repesenting the desired algorithm.
|
138 |
|
|
* @throws NoSuchAlgorithmException if the algorithm is not implemented by the
|
139 |
|
|
* named provider.
|
140 |
|
|
* @throws NoSuchProviderException if the named provider was not found.
|
141 |
|
|
* @throws IllegalArgumentException if either <code>algorithm</code> or
|
142 |
|
|
* <code>provider</code> is <code>null</code> or empty.
|
143 |
|
|
*/
|
144 |
|
|
public static AlgorithmParameters getInstance(String algorithm,
|
145 |
|
|
String provider)
|
146 |
|
|
throws NoSuchAlgorithmException, NoSuchProviderException
|
147 |
|
|
{
|
148 |
|
|
if (provider == null)
|
149 |
|
|
throw new IllegalArgumentException("provider MUST NOT be null");
|
150 |
|
|
provider = provider.trim();
|
151 |
|
|
if (provider.length() == 0)
|
152 |
|
|
throw new IllegalArgumentException("provider MUST NOT be empty");
|
153 |
|
|
Provider p = Security.getProvider(provider);
|
154 |
|
|
if (p == null)
|
155 |
|
|
throw new NoSuchProviderException(provider);
|
156 |
|
|
return getInstance(algorithm, p);
|
157 |
|
|
}
|
158 |
|
|
|
159 |
|
|
/**
|
160 |
|
|
* Returns a new instance of <code>AlgorithmParameters</code> representing
|
161 |
|
|
* the specified algorithm parameters from the specified {@link Provider}.
|
162 |
|
|
* <p>
|
163 |
|
|
* The returned <code>AlgorithmParameters</code> must still be intialized
|
164 |
|
|
* with an <code>init()</code> method.
|
165 |
|
|
*
|
166 |
|
|
* @param algorithm the algorithm to use.
|
167 |
|
|
* @param provider the {@link Provider} to use.
|
168 |
|
|
* @return the new instance repesenting the desired algorithm.
|
169 |
|
|
* @throws NoSuchAlgorithmException if the algorithm is not implemented by the
|
170 |
|
|
* {@link Provider}.
|
171 |
|
|
* @throws IllegalArgumentException if either <code>algorithm</code> or
|
172 |
|
|
* <code>provider</code> is <code>null</code>, or if
|
173 |
|
|
* <code>algorithm</code> is an empty string.
|
174 |
|
|
* @since 1.4
|
175 |
|
|
*/
|
176 |
|
|
public static AlgorithmParameters getInstance(String algorithm,
|
177 |
|
|
Provider provider)
|
178 |
|
|
throws NoSuchAlgorithmException
|
179 |
|
|
{
|
180 |
|
|
CPStringBuilder sb = new CPStringBuilder("AlgorithmParameters for algorithm [")
|
181 |
|
|
.append(algorithm).append("] from provider[")
|
182 |
|
|
.append(provider).append("] could not be created");
|
183 |
|
|
Throwable cause;
|
184 |
|
|
try
|
185 |
|
|
{
|
186 |
|
|
Object spi = Engine.getInstance(ALGORITHM_PARAMETERS, algorithm, provider);
|
187 |
|
|
return new AlgorithmParameters((AlgorithmParametersSpi) spi,
|
188 |
|
|
provider,
|
189 |
|
|
algorithm);
|
190 |
|
|
}
|
191 |
|
|
catch (InvocationTargetException x)
|
192 |
|
|
{
|
193 |
|
|
cause = x.getCause();
|
194 |
|
|
if (cause instanceof NoSuchAlgorithmException)
|
195 |
|
|
throw (NoSuchAlgorithmException) cause;
|
196 |
|
|
if (cause == null)
|
197 |
|
|
cause = x;
|
198 |
|
|
}
|
199 |
|
|
catch (ClassCastException x)
|
200 |
|
|
{
|
201 |
|
|
cause = x;
|
202 |
|
|
}
|
203 |
|
|
NoSuchAlgorithmException x = new NoSuchAlgorithmException(sb.toString());
|
204 |
|
|
x.initCause(cause);
|
205 |
|
|
throw x;
|
206 |
|
|
}
|
207 |
|
|
|
208 |
|
|
/** @return the provider of this parameter object. */
|
209 |
|
|
public final Provider getProvider()
|
210 |
|
|
{
|
211 |
|
|
return provider;
|
212 |
|
|
}
|
213 |
|
|
|
214 |
|
|
/**
|
215 |
|
|
* Initializes the engine with the specified {@link AlgorithmParameterSpec}.
|
216 |
|
|
*
|
217 |
|
|
* @param paramSpec
|
218 |
|
|
* A {@link AlgorithmParameterSpec} to use.
|
219 |
|
|
* @throws InvalidParameterSpecException
|
220 |
|
|
* if <code>paramSpec</code> is invalid.
|
221 |
|
|
*/
|
222 |
|
|
public final void init(AlgorithmParameterSpec paramSpec)
|
223 |
|
|
throws InvalidParameterSpecException
|
224 |
|
|
{
|
225 |
|
|
paramSpi.engineInit(paramSpec);
|
226 |
|
|
}
|
227 |
|
|
|
228 |
|
|
/**
|
229 |
|
|
* Initializes the engine with the specified parameters stored in the byte
|
230 |
|
|
* array and decodes them according to the ASN.1 specification. If the ASN.1
|
231 |
|
|
* specification exists then it succeeds otherwise an {@link IOException} is
|
232 |
|
|
* thrown.
|
233 |
|
|
*
|
234 |
|
|
* @param params
|
235 |
|
|
* the parameters to use.
|
236 |
|
|
* @throws IOException
|
237 |
|
|
* if a decoding error occurs.
|
238 |
|
|
*/
|
239 |
|
|
public final void init(byte[]params) throws IOException
|
240 |
|
|
{
|
241 |
|
|
paramSpi.engineInit(params);
|
242 |
|
|
}
|
243 |
|
|
|
244 |
|
|
/**
|
245 |
|
|
* Initializes the engine with the specified parameters stored in the byte
|
246 |
|
|
* array and decodes them according to the specified decoding specification.
|
247 |
|
|
* If <code>format</code> is <code>null</code>, then this method decodes the
|
248 |
|
|
* byte array using the ASN.1 specification if it exists, otherwise it throws
|
249 |
|
|
* an {@link IOException}.
|
250 |
|
|
*
|
251 |
|
|
* @param params
|
252 |
|
|
* the parameters to use.
|
253 |
|
|
* @param format
|
254 |
|
|
* the name of decoding format to use.
|
255 |
|
|
* @throws IOException
|
256 |
|
|
* if a decoding error occurs.
|
257 |
|
|
*/
|
258 |
|
|
public final void init(byte[]params, String format) throws IOException
|
259 |
|
|
{
|
260 |
|
|
paramSpi.engineInit(params, format);
|
261 |
|
|
}
|
262 |
|
|
|
263 |
|
|
/**
|
264 |
|
|
* Returns a new instance of <code>AlgorithmParameters</code> as a
|
265 |
|
|
* designated parameter specification {@link Class}.
|
266 |
|
|
*
|
267 |
|
|
* @param paramSpec
|
268 |
|
|
* the {@link Class} to use.
|
269 |
|
|
* @return the parameter specification.
|
270 |
|
|
* @throws InvalidParameterSpecException
|
271 |
|
|
* if <code>paramSpec</code> is invalid.
|
272 |
|
|
*/
|
273 |
|
|
public final <T extends AlgorithmParameterSpec>
|
274 |
|
|
T getParameterSpec(Class<T> paramSpec)
|
275 |
|
|
throws InvalidParameterSpecException
|
276 |
|
|
{
|
277 |
|
|
return paramSpi.engineGetParameterSpec(paramSpec);
|
278 |
|
|
}
|
279 |
|
|
|
280 |
|
|
/**
|
281 |
|
|
* Returns the parameters in the default encoding format. The primary encoding
|
282 |
|
|
* format is ASN.1 if it exists for the specified type.
|
283 |
|
|
*
|
284 |
|
|
* @return byte array representing the parameters.
|
285 |
|
|
*/
|
286 |
|
|
public final byte[] getEncoded() throws IOException
|
287 |
|
|
{
|
288 |
|
|
return paramSpi.engineGetEncoded();
|
289 |
|
|
}
|
290 |
|
|
|
291 |
|
|
/**
|
292 |
|
|
* Returns the parameters in the specified encoding format. If
|
293 |
|
|
* <code>format</code> is <code>null</code> then the ASN.1 encoding
|
294 |
|
|
* format is used if it exists for the specified type.
|
295 |
|
|
*
|
296 |
|
|
* @param format
|
297 |
|
|
* the name of the encoding format to use.
|
298 |
|
|
* @return the parameters encoded using the specified encoding scheme.
|
299 |
|
|
* @throws IOException
|
300 |
|
|
* if an encoding exception occurs, or if this parameter object has
|
301 |
|
|
* not been initialized.
|
302 |
|
|
*/
|
303 |
|
|
public final byte[] getEncoded(String format) throws IOException
|
304 |
|
|
{
|
305 |
|
|
return paramSpi.engineGetEncoded(format);
|
306 |
|
|
}
|
307 |
|
|
|
308 |
|
|
/**
|
309 |
|
|
* Returns a string representation of the encoded form.
|
310 |
|
|
*
|
311 |
|
|
* @return a string representation of the encoded form.
|
312 |
|
|
*/
|
313 |
|
|
public final String toString()
|
314 |
|
|
{
|
315 |
|
|
return paramSpi.engineToString();
|
316 |
|
|
}
|
317 |
|
|
}
|