OSDN Git Service

Merged gcj-eclipse branch to trunk.
[pf3gnuchains/gcc-fork.git] / libjava / classpath / java / security / AlgorithmParameters.java
1 /* 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.security.Engine;
42
43 import java.io.IOException;
44 import java.lang.reflect.InvocationTargetException;
45 import java.security.spec.AlgorithmParameterSpec;
46 import java.security.spec.InvalidParameterSpecException;
47
48 /**
49  * <code>AlgorithmParameters</code> is an Algorithm Parameters class which
50  * provides an interface through which the user can manage the parameters of an
51  * Algorithm.
52  *
53  * @author Mark Benvenuto
54  * @since 1.2
55  * @see AlgorithmParameterSpec
56  * @see java.security.spec.DSAParameterSpec
57  * @see KeyPairGenerator
58  */
59 public class AlgorithmParameters
60 {
61   /** Service name for algorithm parameters. */
62   private static final String ALGORITHM_PARAMETERS = "AlgorithmParameters";
63
64   private AlgorithmParametersSpi paramSpi;
65   private Provider provider;
66   private String algorithm;
67
68   /**
69    * Constructs a new instance of <code>AlgorithmParameters</code>.
70    * 
71    * @param paramSpi
72    *          the engine to use.
73    * @param provider
74    *          the provider to use.
75    * @param algorithm
76    *          the algorithm to use.
77    */
78   protected AlgorithmParameters(AlgorithmParametersSpi paramSpi,
79                                 Provider provider, String algorithm)
80   {
81     this.paramSpi = paramSpi;
82     this.provider = provider;
83     this.algorithm = algorithm;
84   }
85
86   /** @return A string with the name of the algorithm used. */
87   public final String getAlgorithm()
88   {
89     return algorithm;
90   }
91
92   /**
93    * Returns a new instance of <code>AlgorithmParameters</code> representing
94    * the specified algorithm parameters.
95    * <p>
96    * The returned <code>AlgorithmParameters</code> must still be initialized
97    * with an <code>init()</code> method.
98    * 
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
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 }