1 /* AlgorithmParameters.java --- Algorithm Parameters Implementation Class
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.security.Engine;
43 import java.io.IOException;
44 import java.lang.reflect.InvocationTargetException;
45 import java.security.spec.AlgorithmParameterSpec;
46 import java.security.spec.InvalidParameterSpecException;
49 * <code>AlgorithmParameters</code> is an Algorithm Parameters class which
50 * provides an interface through which the user can manage the parameters of an
53 * @author Mark Benvenuto
55 * @see AlgorithmParameterSpec
56 * @see java.security.spec.DSAParameterSpec
57 * @see KeyPairGenerator
59 public class AlgorithmParameters
61 /** Service name for algorithm parameters. */
62 private static final String ALGORITHM_PARAMETERS = "AlgorithmParameters";
64 private AlgorithmParametersSpi paramSpi;
65 private Provider provider;
66 private String algorithm;
69 * Constructs a new instance of <code>AlgorithmParameters</code>.
74 * the provider to use.
76 * the algorithm to use.
78 protected AlgorithmParameters(AlgorithmParametersSpi paramSpi,
79 Provider provider, String algorithm)
81 this.paramSpi = paramSpi;
82 this.provider = provider;
83 this.algorithm = algorithm;
86 /** @return A string with the name of the algorithm used. */
87 public final String getAlgorithm()
93 * Returns a new instance of <code>AlgorithmParameters</code> representing
94 * the specified algorithm parameters.
96 * The returned <code>AlgorithmParameters</code> must still be initialized
97 * with an <code>init()</code> method.
99 * @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
103 * @throws IllegalArgumentException if <code>algorithm</code> is
104 * <code>null</code> or is an empty string.
106 public static AlgorithmParameters getInstance(String algorithm)
107 throws NoSuchAlgorithmException
109 Provider[] p = Security.getProviders();
110 NoSuchAlgorithmException lastException = null;
111 for (int i = 0; i < p.length; i++)
114 return getInstance(algorithm, p[i]);
116 catch (NoSuchAlgorithmException x)
120 if (lastException != null)
122 throw new NoSuchAlgorithmException(algorithm);
126 * Returns a new instance of <code>AlgorithmParameters</code> representing
127 * the specified algorithm parameters from a named provider.
129 * The returned <code>AlgorithmParameters</code> must still be intialized
130 * with an <code>init()</code> method.
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
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 AlgorithmParameters 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 instance of <code>AlgorithmParameters</code> representing
159 * the specified algorithm parameters from the specified {@link Provider}.
161 * The returned <code>AlgorithmParameters</code> must still be intialized
162 * with an <code>init()</code> method.
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
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.
174 public static AlgorithmParameters getInstance(String algorithm,
176 throws NoSuchAlgorithmException
178 StringBuilder sb = new StringBuilder("AlgorithmParameters for algorithm [")
179 .append(algorithm).append("] from provider[")
180 .append(provider).append("] could not be created");
184 Object spi = Engine.getInstance(ALGORITHM_PARAMETERS, algorithm, provider);
185 return new AlgorithmParameters((AlgorithmParametersSpi) spi,
189 catch (InvocationTargetException x)
191 cause = x.getCause();
192 if (cause instanceof NoSuchAlgorithmException)
193 throw (NoSuchAlgorithmException) cause;
197 catch (ClassCastException x)
201 NoSuchAlgorithmException x = new NoSuchAlgorithmException(sb.toString());
206 /** @return the provider of this parameter object. */
207 public final Provider getProvider()
213 * Initializes the engine with the specified {@link AlgorithmParameterSpec}.
216 * A {@link AlgorithmParameterSpec} to use.
217 * @throws InvalidParameterSpecException
218 * if <code>paramSpec</code> is invalid.
220 public final void init(AlgorithmParameterSpec paramSpec)
221 throws InvalidParameterSpecException
223 paramSpi.engineInit(paramSpec);
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
233 * the parameters to use.
234 * @throws IOException
235 * if a decoding error occurs.
237 public final void init(byte[]params) throws IOException
239 paramSpi.engineInit(params);
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}.
250 * the parameters to use.
252 * the name of decoding format to use.
253 * @throws IOException
254 * if a decoding error occurs.
256 public final void init(byte[]params, String format) throws IOException
258 paramSpi.engineInit(params, format);
262 * Returns a new instance of <code>AlgorithmParameters</code> as a
263 * designated parameter specification {@link Class}.
266 * the {@link Class} to use.
267 * @return the parameter specification.
268 * @throws InvalidParameterSpecException
269 * if <code>paramSpec</code> is invalid.
271 public final <T extends AlgorithmParameterSpec>
272 T getParameterSpec(Class<T> paramSpec)
273 throws InvalidParameterSpecException
275 return paramSpi.engineGetParameterSpec(paramSpec);
279 * Returns the parameters in the default encoding format. The primary encoding
280 * format is ASN.1 if it exists for the specified type.
282 * @return byte array representing the parameters.
284 public final byte[] getEncoded() throws IOException
286 return paramSpi.engineGetEncoded();
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.
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.
301 public final byte[] getEncoded(String format) throws IOException
303 return paramSpi.engineGetEncoded(format);
307 * Returns a string representation of the encoded form.
309 * @return a string representation of the encoded form.
311 public final String toString()
313 return paramSpi.engineToString();