| 1 | /* KeyPairGenerator.java --- Key Pair Generator Class
|
|---|
| 2 | Copyright (C) 1999, 2002 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., 59 Temple Place, Suite 330, Boston, MA
|
|---|
| 19 | 02111-1307 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 | package java.security;
|
|---|
| 39 |
|
|---|
| 40 | import java.security.spec.AlgorithmParameterSpec;
|
|---|
| 41 |
|
|---|
| 42 | /**
|
|---|
| 43 | KeyPairGenerator is the class used to generate key pairs
|
|---|
| 44 | for a security algorithm.
|
|---|
| 45 |
|
|---|
| 46 | The KeyPairGenerator is created with the getInstance()
|
|---|
| 47 | methods. The class is used to generate public and private
|
|---|
| 48 | keys for an algorithm and associate it with
|
|---|
| 49 | algorithm parameters.
|
|---|
| 50 |
|
|---|
| 51 | @author Mark Benvenuto
|
|---|
| 52 | */
|
|---|
| 53 | public abstract class KeyPairGenerator extends KeyPairGeneratorSpi
|
|---|
| 54 | {
|
|---|
| 55 | Provider provider;
|
|---|
| 56 | private String algorithm;
|
|---|
| 57 |
|
|---|
| 58 | /**
|
|---|
| 59 | Constructs a new KeyPairGenerator
|
|---|
| 60 |
|
|---|
| 61 | @param algorithm the algorithm to use
|
|---|
| 62 | */
|
|---|
| 63 | protected KeyPairGenerator(String algorithm)
|
|---|
| 64 | {
|
|---|
| 65 | this.algorithm = algorithm;
|
|---|
| 66 | this.provider = null;
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | /**
|
|---|
| 70 | Returns the name of the algorithm used
|
|---|
| 71 |
|
|---|
| 72 | @return A string with the name of the algorithm
|
|---|
| 73 | */
|
|---|
| 74 | public String getAlgorithm()
|
|---|
| 75 | {
|
|---|
| 76 | return algorithm;
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | /**
|
|---|
| 80 | Gets an instance of the KeyPairGenerator class
|
|---|
| 81 | which generates key pairs for the specified algorithm.
|
|---|
| 82 | If the algorithm is not found then, it throws NoSuchAlgorithmException.
|
|---|
| 83 |
|
|---|
| 84 | @param algorithm the name of algorithm to choose
|
|---|
| 85 | @return a AlgorithmParameterGenerator repesenting the desired algorithm
|
|---|
| 86 |
|
|---|
| 87 | @throws NoSuchAlgorithmException if the algorithm is not implemented by
|
|---|
| 88 | providers
|
|---|
| 89 | */
|
|---|
| 90 | public static KeyPairGenerator getInstance(String algorithm) throws
|
|---|
| 91 | NoSuchAlgorithmException
|
|---|
| 92 | {
|
|---|
| 93 | Provider[] p = Security.getProviders();
|
|---|
| 94 |
|
|---|
| 95 | for (int i = 0; i < p.length; i++)
|
|---|
| 96 | {
|
|---|
| 97 | try
|
|---|
| 98 | {
|
|---|
| 99 | return getInstance(algorithm, p[i]);
|
|---|
| 100 | }
|
|---|
| 101 | catch (NoSuchAlgorithmException ignored) {}
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | throw new NoSuchAlgorithmException(algorithm);
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | /**
|
|---|
| 108 | Gets an instance of the KeyPairGenerator class
|
|---|
| 109 | which generates key pairs for the specified algorithm.
|
|---|
| 110 | If the algorithm is not found then, it throws NoSuchAlgorithmException.
|
|---|
| 111 |
|
|---|
| 112 | @param algorithm the name of algorithm to choose
|
|---|
| 113 | @param provider the name of the provider to find the algorithm in
|
|---|
| 114 | @return a AlgorithmParameterGenerator repesenting the desired algorithm
|
|---|
| 115 |
|
|---|
| 116 | @throws NoSuchAlgorithmException if the algorithm is not implemented by
|
|---|
| 117 | the provider
|
|---|
| 118 | @throws NoSuchProviderException if the provider is not found
|
|---|
| 119 | */
|
|---|
| 120 | public static KeyPairGenerator getInstance(String algorithm, String provider)
|
|---|
| 121 | throws NoSuchAlgorithmException, NoSuchProviderException
|
|---|
| 122 | {
|
|---|
| 123 | Provider p = Security.getProvider(provider);
|
|---|
| 124 | if (p == null)
|
|---|
| 125 | throw new NoSuchProviderException(provider);
|
|---|
| 126 |
|
|---|
| 127 | return getInstance(algorithm, p);
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | private static KeyPairGenerator getInstance(String algorithm, Provider p)
|
|---|
| 131 | throws NoSuchAlgorithmException
|
|---|
| 132 | {
|
|---|
| 133 | // try the name as is
|
|---|
| 134 | String className = p.getProperty("KeyPairGenerator." + algorithm);
|
|---|
| 135 | if (className == null) { // try all uppercase
|
|---|
| 136 | String upper = algorithm.toUpperCase();
|
|---|
| 137 | className = p.getProperty("KeyPairGenerator." + upper);
|
|---|
| 138 | if (className == null) { // try if it's an alias
|
|---|
| 139 | String alias = p.getProperty("Alg.Alias.KeyPairGenerator." + algorithm);
|
|---|
| 140 | if (alias == null) { // try all-uppercase alias name
|
|---|
| 141 | alias = p.getProperty("Alg.Alias.KeyPairGenerator." + upper);
|
|---|
| 142 | if (alias == null) { // spit the dummy
|
|---|
| 143 | throw new NoSuchAlgorithmException(algorithm);
|
|---|
| 144 | }
|
|---|
| 145 | }
|
|---|
| 146 | className = p.getProperty("KeyPairGenerator." + alias);
|
|---|
| 147 | if (className == null) {
|
|---|
| 148 | throw new NoSuchAlgorithmException(algorithm);
|
|---|
| 149 | }
|
|---|
| 150 | }
|
|---|
| 151 | }
|
|---|
| 152 | return getInstance(className, algorithm, p);
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | private static KeyPairGenerator getInstance(String classname,
|
|---|
| 156 | String algorithm,
|
|---|
| 157 | Provider provider)
|
|---|
| 158 | throws NoSuchAlgorithmException
|
|---|
| 159 | {
|
|---|
| 160 | try
|
|---|
| 161 | {
|
|---|
| 162 | Object o = Class.forName(classname).newInstance();
|
|---|
| 163 | KeyPairGenerator kpg;
|
|---|
| 164 | if (o instanceof KeyPairGeneratorSpi)
|
|---|
| 165 | kpg = new DummyKeyPairGenerator((KeyPairGeneratorSpi) o, algorithm);
|
|---|
| 166 | else
|
|---|
| 167 | {
|
|---|
| 168 | kpg = (KeyPairGenerator) o;
|
|---|
| 169 | kpg.algorithm = algorithm;
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | kpg.provider = provider;
|
|---|
| 173 | return kpg;
|
|---|
| 174 | }
|
|---|
| 175 | catch (ClassNotFoundException cnfe)
|
|---|
| 176 | {
|
|---|
| 177 | throw new NoSuchAlgorithmException("Class not found");
|
|---|
| 178 | }
|
|---|
| 179 | catch (InstantiationException ie)
|
|---|
| 180 | {
|
|---|
| 181 | throw new NoSuchAlgorithmException("Class instantiation failed");
|
|---|
| 182 | }
|
|---|
| 183 | catch (IllegalAccessException iae)
|
|---|
| 184 | {
|
|---|
| 185 | throw new NoSuchAlgorithmException("Illegal Access");
|
|---|
| 186 | }
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | /**
|
|---|
| 190 | Gets the provider that the class is from.
|
|---|
| 191 |
|
|---|
| 192 | @return the provider of this class
|
|---|
| 193 | */
|
|---|
| 194 | public final Provider getProvider()
|
|---|
| 195 | {
|
|---|
| 196 | return provider;
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | /**
|
|---|
| 200 | Initializes the KeyPairGenerator for the specified key size.
|
|---|
| 201 | (Since no source of randomness is specified, a default one is
|
|---|
| 202 | provided.)
|
|---|
| 203 |
|
|---|
| 204 | @param keysize Size of key to generate
|
|---|
| 205 | */
|
|---|
| 206 | public void initialize(int keysize)
|
|---|
| 207 | {
|
|---|
| 208 | initialize(keysize, new SecureRandom());
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 | /**
|
|---|
| 212 | Initializes the KeyPairGenerator for the specified key size
|
|---|
| 213 | and specified SecureRandom.
|
|---|
| 214 |
|
|---|
| 215 | @param keysize Size of key to generate
|
|---|
| 216 | @param random SecureRandom to use
|
|---|
| 217 |
|
|---|
| 218 | @since JDK 1.2
|
|---|
| 219 | */
|
|---|
| 220 | public void initialize(int keysize, SecureRandom random)
|
|---|
| 221 | {
|
|---|
| 222 | initialize(keysize, random);
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | /**
|
|---|
| 226 | Initializes the KeyPairGenerator with the specified
|
|---|
| 227 | AlgorithmParameterSpec class.
|
|---|
| 228 | (Since no source of randomness is specified, a default one is
|
|---|
| 229 | provided.)
|
|---|
| 230 |
|
|---|
| 231 | @param params AlgorithmParameterSpec to initialize with
|
|---|
| 232 |
|
|---|
| 233 | @since JDK 1.2
|
|---|
| 234 | */
|
|---|
| 235 | public void initialize(AlgorithmParameterSpec params)
|
|---|
| 236 | throws InvalidAlgorithmParameterException
|
|---|
| 237 | {
|
|---|
| 238 | initialize(params, new SecureRandom());
|
|---|
| 239 | }
|
|---|
| 240 |
|
|---|
| 241 | /**
|
|---|
| 242 | Initializes the KeyPairGenerator with the specified
|
|---|
| 243 | AlgorithmParameterSpec class and specified SecureRandom.
|
|---|
| 244 |
|
|---|
| 245 | @param params AlgorithmParameterSpec to initialize with
|
|---|
| 246 | @param random SecureRandom to use
|
|---|
| 247 |
|
|---|
| 248 | @since JDK 1.2
|
|---|
| 249 | */
|
|---|
| 250 | public void initialize(AlgorithmParameterSpec params, SecureRandom random)
|
|---|
| 251 | throws InvalidAlgorithmParameterException
|
|---|
| 252 | {
|
|---|
| 253 | super.initialize(params, random);
|
|---|
| 254 | }
|
|---|
| 255 |
|
|---|
| 256 | /**
|
|---|
| 257 | Generates a KeyPair according the rules for the algorithm.
|
|---|
| 258 | Unless intialized, algorithm defaults will be used. It
|
|---|
| 259 | creates a unique key pair each time.
|
|---|
| 260 |
|
|---|
| 261 | Same as generateKeyPair();
|
|---|
| 262 |
|
|---|
| 263 | @return a key pair
|
|---|
| 264 | */
|
|---|
| 265 | public final KeyPair genKeyPair()
|
|---|
| 266 | {
|
|---|
| 267 | try
|
|---|
| 268 | {
|
|---|
| 269 | return getInstance("DSA", "GNU").generateKeyPair();
|
|---|
| 270 | }
|
|---|
| 271 | catch (Exception e)
|
|---|
| 272 | {
|
|---|
| 273 | System.err.println("genKeyPair failed: " + e);
|
|---|
| 274 | e.printStackTrace();
|
|---|
| 275 | return null;
|
|---|
| 276 | }
|
|---|
| 277 | }
|
|---|
| 278 |
|
|---|
| 279 | /**
|
|---|
| 280 | Generates a KeyPair according the rules for the algorithm.
|
|---|
| 281 | Unless intialized, algorithm defaults will be used. It
|
|---|
| 282 | creates a unique key pair each time.
|
|---|
| 283 |
|
|---|
| 284 | Same as genKeyPair();
|
|---|
| 285 |
|
|---|
| 286 | @return a key pair
|
|---|
| 287 | */
|
|---|
| 288 | public KeyPair generateKeyPair()
|
|---|
| 289 | {
|
|---|
| 290 | return genKeyPair();
|
|---|
| 291 | }
|
|---|
| 292 | }
|
|---|