001    /* AlgorithmParameterGenerator.java --- Algorithm Parameter Generator
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.lang.reflect.InvocationTargetException;
044    import java.security.spec.AlgorithmParameterSpec;
045    
046    /**
047     * <code>AlgorithmParameterGenerator</code> is used to generate algorithm
048     * parameters for specified algorithms.
049     * 
050     * <p>In case the client does not explicitly initialize the
051     * <code>AlgorithmParameterGenerator</code> (via a call to an
052     * <code>init()</code> method), each provider must supply (and document) a
053     * default initialization. For example, the <b>GNU</b> provider uses a default
054     * modulus prime size of <code>1024</code> bits for the generation of <i>DSA</i>
055     * parameters.
056     *
057     * @author Mark Benvenuto
058     * @since 1.2
059     * @see AlgorithmParameters
060     * @see AlgorithmParameterSpec
061     */
062    public class AlgorithmParameterGenerator
063    {
064      /** Service name for algorithm parameter generators. */
065      private static final String ALGORITHM_PARAMETER_GENERATOR =
066        "AlgorithmParameterGenerator";
067    
068      private AlgorithmParameterGeneratorSpi paramGenSpi;
069      private Provider provider;
070      private String algorithm;
071    
072      /**
073       * Constructs a new instance of <code>AlgorithmParameterGenerator</code>.
074       * 
075       * @param paramGenSpi
076       *          the generator to use.
077       * @param provider
078       *          the provider to use.
079       * @param algorithm
080       *          the algorithm to use.
081       */
082      protected AlgorithmParameterGenerator(AlgorithmParameterGeneratorSpi
083                                            paramGenSpi, Provider provider,
084                                            String algorithm)
085      {
086        this.paramGenSpi = paramGenSpi;
087        this.provider = provider;
088        this.algorithm = algorithm;
089      }
090    
091      /** @return the name of the algorithm. */
092      public final String getAlgorithm()
093      {
094        return algorithm;
095      }
096    
097      /**
098       * Returns a new <code>AlgorithmParameterGenerator</code> instance which
099       * generates algorithm parameters for the specified algorithm.
100       * 
101       * @param algorithm the name of algorithm to use.
102       * @return the new instance.
103       * @throws NoSuchAlgorithmException if <code>algorithm</code> is not
104       *           implemented by any provider.
105       * @throws IllegalArgumentException if <code>algorithm</code> is
106       *           <code>null</code> or is an empty string.
107       */
108      public static AlgorithmParameterGenerator 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 <code>AlgorithmParameterGenerator</code> instance which
129       * generates algorithm parameters for the specified algorithm.
130       * 
131       * @param algorithm the name of algorithm to use.
132       * @param provider the name of the {@link Provider} to use.
133       * @return the new instance.
134       * @throws NoSuchAlgorithmException if the algorithm is not implemented by the
135       *           named provider.
136       * @throws NoSuchProviderException if the named provider was not found.
137       * @throws IllegalArgumentException if either <code>algorithm</code> or
138       *           <code>provider</code> is <code>null</code> or empty.
139       */
140      public static AlgorithmParameterGenerator getInstance(String algorithm,
141                                                            String provider)
142          throws NoSuchAlgorithmException, NoSuchProviderException
143      {
144        if (provider == null)
145          throw new IllegalArgumentException("provider MUST NOT be null");
146        provider = provider.trim();
147        if (provider.length() == 0)
148          throw new IllegalArgumentException("provider MUST NOT be empty");
149        Provider p = Security.getProvider(provider);
150        if (p == null)
151          throw new NoSuchProviderException(provider);
152        return getInstance(algorithm, p);
153      }
154    
155      /**
156       * Returns a new <code>AlgorithmParameterGenerator</code> instance which
157       * generates algorithm parameters for the specified algorithm.
158       * 
159       * @param algorithm the name of algorithm to use.
160       * @param provider the {@link Provider} to use.
161       * @return the new instance.
162       * @throws NoSuchAlgorithmException if the algorithm is not implemented by
163       *           {@link Provider}.
164       * @throws IllegalArgumentException if either <code>algorithm</code> or
165       *           <code>provider</code> is <code>null</code>, or if
166       *           <code>algorithm</code> is an empty string.
167       * @since 1.4
168       * @see Provider
169       */
170      public static AlgorithmParameterGenerator getInstance(String algorithm,
171                                                            Provider provider)
172          throws NoSuchAlgorithmException
173      {
174        StringBuilder sb = new StringBuilder()
175            .append("AlgorithmParameterGenerator for algorithm [")
176            .append(algorithm).append("] from provider[")
177            .append(provider).append("] could not be created");
178        Throwable cause;
179        try
180          {
181            Object spi = Engine.getInstance(ALGORITHM_PARAMETER_GENERATOR,
182                                            algorithm,
183                                            provider);
184            return new AlgorithmParameterGenerator((AlgorithmParameterGeneratorSpi) spi,
185                                                   provider,
186                                                   algorithm);
187          }
188        catch (InvocationTargetException x)
189          {
190            cause = x.getCause();
191            if (cause instanceof NoSuchAlgorithmException)
192              throw (NoSuchAlgorithmException) cause;
193            if (cause == null)
194              cause = x;
195          }
196        catch (ClassCastException x)
197          {
198            cause = x;
199          }
200        NoSuchAlgorithmException x = new NoSuchAlgorithmException(sb.toString());
201        x.initCause(cause);
202        throw x;
203      }
204    
205      /** @return the {@link Provider} of this generator. */
206      public final Provider getProvider()
207      {
208        return provider;
209      }
210    
211      /**
212       * Initializes this instance with the specified size. Since no source of
213       * randomness is supplied, a default one will be used.
214       * 
215       * @param size
216       *          size (in bits) to use.
217       */
218      public final void init(int size)
219      {
220        init(size, new SecureRandom());
221      }
222    
223      /**
224       * Initializes this instance with the specified key-size and source of
225       * randomness.
226       * 
227       * @param size
228       *          the size (in bits) to use.
229       * @param random
230       *          the {@link SecureRandom} to use.
231       */
232      public final void init(int size, SecureRandom random)
233      {
234        paramGenSpi.engineInit(size, random);
235      }
236    
237      /**
238       * Initializes this instance with the specified {@link AlgorithmParameterSpec}.
239       * Since no source of randomness is supplied, a default one will be used.
240       * 
241       * @param genParamSpec
242       *          the {@link AlgorithmParameterSpec} to use.
243       * @throws InvalidAlgorithmParameterException
244       *           if <code>genParamSpec</code> is invalid.
245       */
246      public final void init(AlgorithmParameterSpec genParamSpec)
247          throws InvalidAlgorithmParameterException
248      {
249        init(genParamSpec, new SecureRandom());
250      }
251    
252      /**
253       * Initializes this instance with the specified {@link AlgorithmParameterSpec}
254       * and source of randomness.
255       * 
256       * @param genParamSpec
257       *          the {@link AlgorithmParameterSpec} to use.
258       * @param random
259       *          the {@link SecureRandom} to use.
260       * @throws InvalidAlgorithmParameterException
261       *           if <code>genParamSpec</code> is invalid.
262       */
263      public final void init(AlgorithmParameterSpec genParamSpec,
264                             SecureRandom random)
265        throws InvalidAlgorithmParameterException
266      {
267        paramGenSpi.engineInit(genParamSpec, random);
268      }
269    
270      /** @return a new instance of {@link AlgorithmParameters}. */
271      public final AlgorithmParameters generateParameters()
272      {
273        return paramGenSpi.engineGenerateParameters();
274      }
275    }