libjava/ChangeLog:
[official-gcc.git] / libjava / classpath / java / security / AlgorithmParameters.java
blob3de8ad02c054259eda5b112edbd00874e6283e53
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)
9 any later version.
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
19 02110-1301 USA.
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.
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.lang.CPStringBuilder;
43 import gnu.java.security.Engine;
45 import java.io.IOException;
46 import java.lang.reflect.InvocationTargetException;
47 import java.security.spec.AlgorithmParameterSpec;
48 import java.security.spec.InvalidParameterSpecException;
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.
55 * @author Mark Benvenuto
56 * @since 1.2
57 * @see AlgorithmParameterSpec
58 * @see java.security.spec.DSAParameterSpec
59 * @see KeyPairGenerator
61 public class AlgorithmParameters
63 /** Service name for algorithm parameters. */
64 private static final String ALGORITHM_PARAMETERS = "AlgorithmParameters";
66 private AlgorithmParametersSpi paramSpi;
67 private Provider provider;
68 private String algorithm;
70 /**
71 * Constructs a new instance of <code>AlgorithmParameters</code>.
73 * @param paramSpi
74 * the engine to use.
75 * @param provider
76 * the provider to use.
77 * @param algorithm
78 * the algorithm to use.
80 protected AlgorithmParameters(AlgorithmParametersSpi paramSpi,
81 Provider provider, String algorithm)
83 this.paramSpi = paramSpi;
84 this.provider = provider;
85 this.algorithm = algorithm;
88 /** @return A string with the name of the algorithm used. */
89 public final String getAlgorithm()
91 return algorithm;
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.
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.
108 public static AlgorithmParameters getInstance(String algorithm)
109 throws NoSuchAlgorithmException
111 Provider[] p = Security.getProviders();
112 NoSuchAlgorithmException lastException = null;
113 for (int i = 0; i < p.length; i++)
116 return getInstance(algorithm, p[i]);
118 catch (NoSuchAlgorithmException x)
120 lastException = x;
122 if (lastException != null)
123 throw lastException;
124 throw new NoSuchAlgorithmException(algorithm);
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>
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.
144 public static AlgorithmParameters getInstance(String algorithm,
145 String provider)
146 throws NoSuchAlgorithmException, NoSuchProviderException
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);
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.
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
176 public static AlgorithmParameters getInstance(String algorithm,
177 Provider provider)
178 throws NoSuchAlgorithmException
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;
186 Object spi = Engine.getInstance(ALGORITHM_PARAMETERS, algorithm, provider);
187 return new AlgorithmParameters((AlgorithmParametersSpi) spi,
188 provider,
189 algorithm);
191 catch (InvocationTargetException x)
193 cause = x.getCause();
194 if (cause instanceof NoSuchAlgorithmException)
195 throw (NoSuchAlgorithmException) cause;
196 if (cause == null)
197 cause = x;
199 catch (ClassCastException x)
201 cause = x;
203 NoSuchAlgorithmException x = new NoSuchAlgorithmException(sb.toString());
204 x.initCause(cause);
205 throw x;
208 /** @return the provider of this parameter object. */
209 public final Provider getProvider()
211 return provider;
215 * Initializes the engine with the specified {@link AlgorithmParameterSpec}.
217 * @param paramSpec
218 * A {@link AlgorithmParameterSpec} to use.
219 * @throws InvalidParameterSpecException
220 * if <code>paramSpec</code> is invalid.
222 public final void init(AlgorithmParameterSpec paramSpec)
223 throws InvalidParameterSpecException
225 paramSpi.engineInit(paramSpec);
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.
234 * @param params
235 * the parameters to use.
236 * @throws IOException
237 * if a decoding error occurs.
239 public final void init(byte[]params) throws IOException
241 paramSpi.engineInit(params);
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}.
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.
258 public final void init(byte[]params, String format) throws IOException
260 paramSpi.engineInit(params, format);
264 * Returns a new instance of <code>AlgorithmParameters</code> as a
265 * designated parameter specification {@link Class}.
267 * @param paramSpec
268 * the {@link Class} to use.
269 * @return the parameter specification.
270 * @throws InvalidParameterSpecException
271 * if <code>paramSpec</code> is invalid.
273 public final <T extends AlgorithmParameterSpec>
274 T getParameterSpec(Class<T> paramSpec)
275 throws InvalidParameterSpecException
277 return paramSpi.engineGetParameterSpec(paramSpec);
281 * Returns the parameters in the default encoding format. The primary encoding
282 * format is ASN.1 if it exists for the specified type.
284 * @return byte array representing the parameters.
286 public final byte[] getEncoded() throws IOException
288 return paramSpi.engineGetEncoded();
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.
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.
303 public final byte[] getEncoded(String format) throws IOException
305 return paramSpi.engineGetEncoded(format);
309 * Returns a string representation of the encoded form.
311 * @return a string representation of the encoded form.
313 public final String toString()
315 return paramSpi.engineToString();