001 /* AlgorithmParameters.java --- Algorithm Parameters Implementation Class
002 Copyright (C) 1999, 2003, 2004 Free Software Foundation, Inc.
003
004 This file is part of GNU Classpath.
005
006 GNU Classpath is free software; you can redistribute it and/or modify
007 it under the terms of the GNU General Public License as published by
008 the Free Software Foundation; either version 2, or (at your option)
009 any later version.
010
011 GNU Classpath 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 for more details.
015
016 You should have received a copy of the GNU General Public License
017 along with GNU Classpath; see the file COPYING. If not, write to the
018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019 02110-1301 USA.
020
021 Linking this library statically or dynamically with other modules is
022 making a combined work based on this library. Thus, the terms and
023 conditions of the GNU General Public License cover the whole
024 combination.
025
026 As a special exception, the copyright holders of this library give you
027 permission to link this library with independent modules to produce an
028 executable, regardless of the license terms of these independent
029 modules, and to copy and distribute the resulting executable under
030 terms of your choice, provided that you also meet, for each linked
031 independent module, the terms and conditions of the license of that
032 module. An independent module is a module which is not derived from
033 or based on this library. If you modify this library, you may extend
034 this exception to your version of the library, but you are not
035 obligated to do so. If you do not wish to do so, delete this
036 exception statement from your version. */
037
038
039 package java.security;
040
041 import gnu.java.security.Engine;
042
043 import java.io.IOException;
044 import java.lang.reflect.InvocationTargetException;
045 import java.security.spec.AlgorithmParameterSpec;
046 import java.security.spec.InvalidParameterSpecException;
047
048 /**
049 * <code>AlgorithmParameters</code> is an Algorithm Parameters class which
050 * provides an interface through which the user can manage the parameters of an
051 * Algorithm.
052 *
053 * @author Mark Benvenuto
054 * @since 1.2
055 * @see AlgorithmParameterSpec
056 * @see java.security.spec.DSAParameterSpec
057 * @see KeyPairGenerator
058 */
059 public class AlgorithmParameters
060 {
061 /** Service name for algorithm parameters. */
062 private static final String ALGORITHM_PARAMETERS = "AlgorithmParameters";
063
064 private AlgorithmParametersSpi paramSpi;
065 private Provider provider;
066 private String algorithm;
067
068 /**
069 * Constructs a new instance of <code>AlgorithmParameters</code>.
070 *
071 * @param paramSpi
072 * the engine to use.
073 * @param provider
074 * the provider to use.
075 * @param algorithm
076 * the algorithm to use.
077 */
078 protected AlgorithmParameters(AlgorithmParametersSpi paramSpi,
079 Provider provider, String algorithm)
080 {
081 this.paramSpi = paramSpi;
082 this.provider = provider;
083 this.algorithm = algorithm;
084 }
085
086 /** @return A string with the name of the algorithm used. */
087 public final String getAlgorithm()
088 {
089 return algorithm;
090 }
091
092 /**
093 * Returns a new instance of <code>AlgorithmParameters</code> representing
094 * the specified algorithm parameters.
095 * <p>
096 * The returned <code>AlgorithmParameters</code> must still be initialized
097 * with an <code>init()</code> method.
098 *
099 * @param algorithm the algorithm to use.
100 * @return the new instance repesenting the desired algorithm.
101 * @throws NoSuchAlgorithmException if the algorithm is not implemented by any
102 * provider.
103 * @throws IllegalArgumentException if <code>algorithm</code> is
104 * <code>null</code> or is an empty string.
105 */
106 public static AlgorithmParameters getInstance(String algorithm)
107 throws NoSuchAlgorithmException
108 {
109 Provider[] p = Security.getProviders();
110 NoSuchAlgorithmException lastException = null;
111 for (int i = 0; i < p.length; i++)
112 try
113 {
114 return getInstance(algorithm, p[i]);
115 }
116 catch (NoSuchAlgorithmException x)
117 {
118 lastException = x;
119 }
120 if (lastException != null)
121 throw lastException;
122 throw new NoSuchAlgorithmException(algorithm);
123 }
124
125 /**
126 * Returns a new instance of <code>AlgorithmParameters</code> representing
127 * the specified algorithm parameters from a named provider.
128 * <p>
129 * The returned <code>AlgorithmParameters</code> must still be intialized
130 * with an <code>init()</code> method.
131 * </p>
132 *
133 * @param algorithm the algorithm to use.
134 * @param provider the name of the {@link Provider} to use.
135 * @return the new instance repesenting the desired algorithm.
136 * @throws NoSuchAlgorithmException if the algorithm is not implemented by the
137 * named provider.
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.
141 */
142 public static AlgorithmParameters getInstance(String algorithm,
143 String provider)
144 throws NoSuchAlgorithmException, NoSuchProviderException
145 {
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);
152 if (p == null)
153 throw new NoSuchProviderException(provider);
154 return getInstance(algorithm, p);
155 }
156
157 /**
158 * Returns a new instance of <code>AlgorithmParameters</code> representing
159 * the specified algorithm parameters from the specified {@link Provider}.
160 * <p>
161 * The returned <code>AlgorithmParameters</code> must still be intialized
162 * with an <code>init()</code> method.
163 *
164 * @param algorithm the algorithm to use.
165 * @param provider the {@link Provider} to use.
166 * @return the new instance repesenting the desired algorithm.
167 * @throws NoSuchAlgorithmException if the algorithm is not implemented by the
168 * {@link Provider}.
169 * @throws IllegalArgumentException if either <code>algorithm</code> or
170 * <code>provider</code> is <code>null</code>, or if
171 * <code>algorithm</code> is an empty string.
172 * @since 1.4
173 */
174 public static AlgorithmParameters getInstance(String algorithm,
175 Provider provider)
176 throws NoSuchAlgorithmException
177 {
178 StringBuilder sb = new StringBuilder("AlgorithmParameters for algorithm [")
179 .append(algorithm).append("] from provider[")
180 .append(provider).append("] could not be created");
181 Throwable cause;
182 try
183 {
184 Object spi = Engine.getInstance(ALGORITHM_PARAMETERS, algorithm, provider);
185 return new AlgorithmParameters((AlgorithmParametersSpi) spi,
186 provider,
187 algorithm);
188 }
189 catch (InvocationTargetException x)
190 {
191 cause = x.getCause();
192 if (cause instanceof NoSuchAlgorithmException)
193 throw (NoSuchAlgorithmException) cause;
194 if (cause == null)
195 cause = x;
196 }
197 catch (ClassCastException x)
198 {
199 cause = x;
200 }
201 NoSuchAlgorithmException x = new NoSuchAlgorithmException(sb.toString());
202 x.initCause(cause);
203 throw x;
204 }
205
206 /** @return the provider of this parameter object. */
207 public final Provider getProvider()
208 {
209 return provider;
210 }
211
212 /**
213 * Initializes the engine with the specified {@link AlgorithmParameterSpec}.
214 *
215 * @param paramSpec
216 * A {@link AlgorithmParameterSpec} to use.
217 * @throws InvalidParameterSpecException
218 * if <code>paramSpec</code> is invalid.
219 */
220 public final void init(AlgorithmParameterSpec paramSpec)
221 throws InvalidParameterSpecException
222 {
223 paramSpi.engineInit(paramSpec);
224 }
225
226 /**
227 * Initializes the engine with the specified parameters stored in the byte
228 * array and decodes them according to the ASN.1 specification. If the ASN.1
229 * specification exists then it succeeds otherwise an {@link IOException} is
230 * thrown.
231 *
232 * @param params
233 * the parameters to use.
234 * @throws IOException
235 * if a decoding error occurs.
236 */
237 public final void init(byte[]params) throws IOException
238 {
239 paramSpi.engineInit(params);
240 }
241
242 /**
243 * Initializes the engine with the specified parameters stored in the byte
244 * array and decodes them according to the specified decoding specification.
245 * If <code>format</code> is <code>null</code>, then this method decodes the
246 * byte array using the ASN.1 specification if it exists, otherwise it throws
247 * an {@link IOException}.
248 *
249 * @param params
250 * the parameters to use.
251 * @param format
252 * the name of decoding format to use.
253 * @throws IOException
254 * if a decoding error occurs.
255 */
256 public final void init(byte[]params, String format) throws IOException
257 {
258 paramSpi.engineInit(params, format);
259 }
260
261 /**
262 * Returns a new instance of <code>AlgorithmParameters</code> as a
263 * designated parameter specification {@link Class}.
264 *
265 * @param paramSpec
266 * the {@link Class} to use.
267 * @return the parameter specification.
268 * @throws InvalidParameterSpecException
269 * if <code>paramSpec</code> is invalid.
270 */
271 public final <T extends AlgorithmParameterSpec>
272 T getParameterSpec(Class<T> paramSpec)
273 throws InvalidParameterSpecException
274 {
275 return paramSpi.engineGetParameterSpec(paramSpec);
276 }
277
278 /**
279 * Returns the parameters in the default encoding format. The primary encoding
280 * format is ASN.1 if it exists for the specified type.
281 *
282 * @return byte array representing the parameters.
283 */
284 public final byte[] getEncoded() throws IOException
285 {
286 return paramSpi.engineGetEncoded();
287 }
288
289 /**
290 * Returns the parameters in the specified encoding format. If
291 * <code>format</code> is <code>null</code> then the ASN.1 encoding
292 * format is used if it exists for the specified type.
293 *
294 * @param format
295 * the name of the encoding format to use.
296 * @return the parameters encoded using the specified encoding scheme.
297 * @throws IOException
298 * if an encoding exception occurs, or if this parameter object has
299 * not been initialized.
300 */
301 public final byte[] getEncoded(String format) throws IOException
302 {
303 return paramSpi.engineGetEncoded(format);
304 }
305
306 /**
307 * Returns a string representation of the encoded form.
308 *
309 * @return a string representation of the encoded form.
310 */
311 public final String toString()
312 {
313 return paramSpi.engineToString();
314 }
315 }