Frames | No Frames |
1: /* KeyPairGenerator.java --- Key Pair Generator Class 2: Copyright (C) 1999, 2002, 2003, 2004, 2005 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>KeyPairGenerator</code> is a class used to generate key-pairs for a 47: * security algorithm. 48: * 49: * <p>The <code>KeyPairGenerator</code> is created with the 50: * <code>getInstance()</code> Factory methods. It is used to generate a pair of 51: * public and private keys for a specific algorithm and associate this key-pair 52: * with the algorithm parameters it was initialized with.</p> 53: * 54: * @see KeyPair 55: * @see AlgorithmParameterSpec 56: * @author Mark Benvenuto 57: * @author Casey Marshall 58: */ 59: public abstract class KeyPairGenerator extends KeyPairGeneratorSpi 60: { 61: /** The service name for key pair generators. */ 62: private static final String KEY_PAIR_GENERATOR = "KeyPairGenerator"; 63: 64: Provider provider; 65: private String algorithm; 66: 67: /** 68: * Constructs a new instance of <code>KeyPairGenerator</code>. 69: * 70: * @param algorithm 71: * the algorithm to use. 72: */ 73: protected KeyPairGenerator(String algorithm) 74: { 75: this.algorithm = algorithm; 76: this.provider = null; 77: } 78: 79: /** 80: * Returns the name of the algorithm used. 81: * 82: * @return the name of the algorithm used. 83: */ 84: public String getAlgorithm() 85: { 86: return algorithm; 87: } 88: 89: /** 90: * Returns a new instance of <code>KeyPairGenerator</code> which generates 91: * key-pairs for the specified algorithm. 92: * 93: * @param algorithm 94: * the name of the algorithm to use. 95: * @return a new instance repesenting the desired algorithm. 96: * @throws NoSuchAlgorithmException 97: * if the algorithm is not implemented by any provider. 98: */ 99: public static KeyPairGenerator getInstance(String algorithm) 100: throws NoSuchAlgorithmException 101: { 102: Provider[] p = Security.getProviders(); 103: for (int i = 0; i < p.length; i++) 104: { 105: try 106: { 107: return getInstance(algorithm, p[i]); 108: } 109: catch (NoSuchAlgorithmException e) 110: { 111: // Ignored. 112: } 113: } 114: 115: throw new NoSuchAlgorithmException(algorithm); 116: } 117: 118: /** 119: * Returns a new instance of <code>KeyPairGenerator</code> which generates 120: * key-pairs for the specified algorithm from a named provider. 121: * 122: * @param algorithm 123: * the name of the algorithm to use. 124: * @param provider 125: * the name of a {@link Provider} to use. 126: * @return a new instance repesenting the desired algorithm. 127: * @throws NoSuchAlgorithmException 128: * if the algorithm is not implemented by the named provider. 129: * @throws NoSuchProviderException 130: * if the named provider was not found. 131: */ 132: public static KeyPairGenerator getInstance(String algorithm, String provider) 133: throws NoSuchAlgorithmException, NoSuchProviderException 134: { 135: Provider p = Security.getProvider(provider); 136: if (p == null) 137: throw new NoSuchProviderException(provider); 138: 139: return getInstance(algorithm, p); 140: } 141: 142: /** 143: * Returns a new instance of <code>KeyPairGenerator</code> which generates 144: * key-pairs for the specified algorithm from a designated {@link Provider}. 145: * 146: * @param algorithm 147: * the name of the algorithm to use. 148: * @param provider 149: * the {@link Provider} to use. 150: * @return a new insatnce repesenting the desired algorithm. 151: * @throws IllegalArgumentException 152: * if <code>provider</code> is <code>null</code>. 153: * @throws NoSuchAlgorithmException 154: * if the algorithm is not implemented by the {@link Provider}. 155: * @since 1.4 156: * @see Provider 157: */ 158: public static KeyPairGenerator getInstance(String algorithm, 159: Provider provider) 160: throws NoSuchAlgorithmException 161: { 162: if (provider == null) 163: throw new IllegalArgumentException("Illegal provider"); 164: 165: Object o = null; 166: try 167: { 168: o = Engine.getInstance(KEY_PAIR_GENERATOR, algorithm, provider); 169: } 170: catch (java.lang.reflect.InvocationTargetException ite) 171: { 172: throw new NoSuchAlgorithmException(algorithm); 173: } 174: 175: KeyPairGenerator result = null; 176: if (o instanceof KeyPairGenerator) 177: { 178: result = (KeyPairGenerator) o; 179: result.algorithm = algorithm; 180: } 181: else if (o instanceof KeyPairGeneratorSpi) 182: result = new DummyKeyPairGenerator((KeyPairGeneratorSpi) o, algorithm); 183: 184: result.provider = provider; 185: return result; 186: } 187: 188: /** 189: * Returns the {@link Provider} of this instance. 190: * 191: * @return the {@link Provider} of this instance. 192: */ 193: public final Provider getProvider() 194: { 195: return provider; 196: } 197: 198: /** 199: * Initializes this instance for the specified key size. Since no source of 200: * randomness is specified, a default one will be used. 201: * 202: * @param keysize 203: * the size of keys to use. 204: */ 205: public void initialize(int keysize) 206: { 207: initialize(keysize, new SecureRandom()); 208: } 209: 210: /** 211: * Initializes this instance for the specified key size and 212: * {@link SecureRandom}. 213: * 214: * @param keysize 215: * the size of keys to use. 216: * @param random 217: * the {@link SecureRandom} to use. 218: * @since 1.2 219: */ 220: public void initialize(int keysize, SecureRandom random) 221: { 222: } 223: 224: /** 225: * Initializes this instance with the specified 226: * {@link AlgorithmParameterSpec}. Since no source of randomness is specified, 227: * a default one will be used. 228: * 229: * @param params 230: * the {@link AlgorithmParameterSpec} to use. 231: * @throws InvalidAlgorithmParameterException 232: * if the designated specifications are invalid. 233: * @since 1.2 234: */ 235: public void initialize(AlgorithmParameterSpec params) 236: throws InvalidAlgorithmParameterException 237: { 238: initialize(params, new SecureRandom()); 239: } 240: 241: /** 242: * Initializes this instance with the specified {@link AlgorithmParameterSpec} 243: * and {@link SecureRandom}. 244: * 245: * @param params 246: * the {@link AlgorithmParameterSpec} to use. 247: * @param random 248: * the {@link SecureRandom} to use. 249: * @throws InvalidAlgorithmParameterException 250: * if the designated specifications are invalid. 251: * @since 1.2 252: */ 253: public void initialize(AlgorithmParameterSpec params, SecureRandom random) 254: throws InvalidAlgorithmParameterException 255: { 256: super.initialize(params, random); 257: } 258: 259: /** 260: * Generates a new "DSA" {@link KeyPair} from the "GNU" security provider. 261: * 262: * <p>This method generates a unique key-pair each time it is called.</p> 263: * 264: * @return a new unique {@link KeyPair}. 265: * @see #generateKeyPair() 266: * @since 1.2 267: */ 268: public final KeyPair genKeyPair() 269: { 270: try 271: { 272: return getInstance("DSA", "GNU").generateKeyPair(); 273: } 274: catch (Exception e) 275: { 276: System.err.println("genKeyPair failed: " + e); 277: e.printStackTrace(); 278: return null; 279: } 280: } 281: 282: /** 283: * Generates a new "DSA" {@link KeyPair} from the "GNU" security provider. 284: * 285: * <p>This method generates a unique key pair each time it is called.</p> 286: * 287: * @return a new unique {@link KeyPair}. 288: * @see #genKeyPair() 289: */ 290: public KeyPair generateKeyPair() 291: { 292: return genKeyPair(); 293: } 294: }