Frames | No Frames |
1: /* AlgorithmParameterGenerator.java --- Algorithm Parameter Generator 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.security.spec.AlgorithmParameterSpec; 44: 45: /** 46: * <code>AlgorithmParameterGenerator</code> is used to generate algorithm 47: * parameters for specified algorithms. 48: * 49: * <p>In case the client does not explicitly initialize the 50: * <code>AlgorithmParameterGenerator</code> (via a call to an 51: * <code>init()</code> method), each provider must supply (and document) a 52: * default initialization. For example, the <b>GNU</b> provider uses a default 53: * modulus prime size of <code>1024</code> bits for the generation of <i>DSA</i> 54: * parameters. 55: * 56: * @author Mark Benvenuto 57: * @since 1.2 58: * @see AlgorithmParameters 59: * @see AlgorithmParameterSpec 60: */ 61: public class AlgorithmParameterGenerator 62: { 63: /** Service name for algorithm parameter generators. */ 64: private static final String ALGORITHM_PARAMETER_GENERATOR = 65: "AlgorithmParameterGenerator"; 66: 67: private AlgorithmParameterGeneratorSpi paramGenSpi; 68: private Provider provider; 69: private String algorithm; 70: 71: /** 72: * Constructs a new instance of <code>AlgorithmParameterGenerator</code>. 73: * 74: * @param paramGenSpi 75: * the generator to use. 76: * @param provider 77: * the provider to use. 78: * @param algorithm 79: * the algorithm to use. 80: */ 81: protected AlgorithmParameterGenerator(AlgorithmParameterGeneratorSpi 82: paramGenSpi, Provider provider, 83: String algorithm) 84: { 85: this.paramGenSpi = paramGenSpi; 86: this.provider = provider; 87: this.algorithm = algorithm; 88: } 89: 90: /** @return the name of the algorithm. */ 91: public final String getAlgorithm() 92: { 93: return algorithm; 94: } 95: 96: /** 97: * Returns a new <code>AlgorithmParameterGenerator</code> instance which 98: * generates algorithm parameters for the specified algorithm. 99: * 100: * @param algorithm 101: * the name of algorithm to use. 102: * @return the new instance. 103: * @throws NoSuchAlgorithmException 104: * if <code>algorithm</code> is not implemented by any provider. 105: */ 106: public static AlgorithmParameterGenerator getInstance(String algorithm) 107: throws NoSuchAlgorithmException 108: { 109: Provider[] p = Security.getProviders(); 110: for (int i = 0; i < p.length; i++) 111: try 112: { 113: return getInstance(algorithm, p[i]); 114: } 115: catch (NoSuchAlgorithmException e) 116: { 117: // Ignore. 118: } 119: 120: throw new NoSuchAlgorithmException(algorithm); 121: } 122: 123: /** 124: * Returns a new <code>AlgorithmParameterGenerator</code> instance which 125: * generates algorithm parameters for the specified algorithm. 126: * 127: * @param algorithm 128: * the name of algorithm to use. 129: * @param provider 130: * the name of the {@link Provider} to use. 131: * @return the new instance. 132: * @throws NoSuchAlgorithmException 133: * if the algorithm is not implemented by the named provider. 134: * @throws NoSuchProviderException 135: * if the named provider was not found. 136: */ 137: public static AlgorithmParameterGenerator getInstance(String algorithm, 138: String provider) 139: throws NoSuchAlgorithmException, NoSuchProviderException 140: { 141: if (provider == null || provider.length() == 0) 142: throw new IllegalArgumentException("Illegal provider"); 143: 144: Provider p = Security.getProvider(provider); 145: if (p == null) 146: throw new NoSuchProviderException(provider); 147: 148: return getInstance(algorithm, p); 149: } 150: 151: /** 152: * Returns a new <code>AlgorithmParameterGenerator</code> instance which 153: * generates algorithm parameters for the specified algorithm. 154: * 155: * @param algorithm 156: * the name of algorithm to use. 157: * @param provider 158: * the {@link Provider} to use. 159: * @return the new instance. 160: * @throws NoSuchAlgorithmException 161: * if the algorithm is not implemented by {@link Provider}. 162: * @since 1.4 163: * @see Provider 164: */ 165: public static AlgorithmParameterGenerator getInstance(String algorithm, 166: Provider provider) 167: throws NoSuchAlgorithmException 168: { 169: if (provider == null) 170: throw new IllegalArgumentException("Illegal provider"); 171: 172: try 173: { 174: return new AlgorithmParameterGenerator( 175: (AlgorithmParameterGeneratorSpi) Engine.getInstance( 176: ALGORITHM_PARAMETER_GENERATOR, algorithm, provider), 177: provider, algorithm); 178: } 179: catch (java.lang.reflect.InvocationTargetException ite) 180: { 181: throw new NoSuchAlgorithmException(algorithm); 182: } 183: catch (ClassCastException cce) 184: { 185: throw new NoSuchAlgorithmException(algorithm); 186: } 187: } 188: 189: /** @return the {@link Provider} of this generator. */ 190: public final Provider getProvider() 191: { 192: return provider; 193: } 194: 195: /** 196: * Initializes this instance with the specified size. Since no source of 197: * randomness is supplied, a default one will be used. 198: * 199: * @param size 200: * size (in bits) to use. 201: */ 202: public final void init(int size) 203: { 204: init(size, new SecureRandom()); 205: } 206: 207: /** 208: * Initializes this instance with the specified key-size and source of 209: * randomness. 210: * 211: * @param size 212: * the size (in bits) to use. 213: * @param random 214: * the {@link SecureRandom} to use. 215: */ 216: public final void init(int size, SecureRandom random) 217: { 218: paramGenSpi.engineInit(size, random); 219: } 220: 221: /** 222: * Initializes this instance with the specified {@link AlgorithmParameterSpec}. 223: * Since no source of randomness is supplied, a default one will be used. 224: * 225: * @param genParamSpec 226: * the {@link AlgorithmParameterSpec} to use. 227: * @throws InvalidAlgorithmParameterException 228: * if <code>genParamSpec</code> is invalid. 229: */ 230: public final void init(AlgorithmParameterSpec genParamSpec) 231: throws InvalidAlgorithmParameterException 232: { 233: init(genParamSpec, new SecureRandom()); 234: } 235: 236: /** 237: * Initializes this instance with the specified {@link AlgorithmParameterSpec} 238: * and source of randomness. 239: * 240: * @param genParamSpec 241: * the {@link AlgorithmParameterSpec} to use. 242: * @param random 243: * the {@link SecureRandom} to use. 244: * @throws InvalidAlgorithmParameterException 245: * if <code>genParamSpec</code> is invalid. 246: */ 247: public final void init(AlgorithmParameterSpec genParamSpec, 248: SecureRandom random) 249: throws InvalidAlgorithmParameterException 250: { 251: paramGenSpi.engineInit(genParamSpec, random); 252: } 253: 254: /** @return a new instance of {@link AlgorithmParameters}. */ 255: public final AlgorithmParameters generateParameters() 256: { 257: return paramGenSpi.engineGenerateParameters(); 258: } 259: }