Source for javax.net.ssl.KeyManagerFactory

   1: /* KeyManagerFactory.java -- factory for key managers.
   2:    Copyright (C) 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 javax.net.ssl;
  40: 
  41: import gnu.java.security.Engine;
  42: 
  43: import java.lang.reflect.InvocationTargetException;
  44: import java.security.AccessController;
  45: import java.security.InvalidAlgorithmParameterException;
  46: import java.security.KeyStore;
  47: import java.security.KeyStoreException;
  48: import java.security.NoSuchAlgorithmException;
  49: import java.security.NoSuchProviderException;
  50: import java.security.PrivilegedAction;
  51: import java.security.Provider;
  52: import java.security.Security;
  53: import java.security.UnrecoverableKeyException;
  54: 
  55: /**
  56:  * A class that creates key manager implementations based on a
  57:  * requested algorithm.
  58:  *
  59:  * @author Casey Marshall (rsdio@metastatic.org)
  60:  */
  61: public class KeyManagerFactory
  62: {
  63: 
  64:   // Constants and fields.
  65:   // ------------------------------------------------------------------
  66: 
  67:   /** The service name for key manager factories. */
  68:   private static final String KEY_MANAGER_FACTORY = "KeyManagerFactory";
  69: 
  70:   /** The system default trust manager algorithm. */
  71:   private static final String DEFAULT_ALGORITHM = "JessieX509";
  72: 
  73:   /** The underlying engine. */
  74:   private final KeyManagerFactorySpi kmfSpi;
  75: 
  76:   /** The provider of this implementation. */
  77:   private final Provider provider;
  78: 
  79:   /** The name of this algorithm. */
  80:   private final String algorithm;
  81: 
  82:   // Constructor.
  83:   // ------------------------------------------------------------------
  84: 
  85:   /**
  86:    * Create a new key manager factory.
  87:    *
  88:    * @param kmfSpi The underlying engine.
  89:    * @param provider The engine's provider.
  90:    * @param algorithm The name of this algorithm.
  91:    */
  92:   protected KeyManagerFactory(KeyManagerFactorySpi kmfSpi,
  93:                               Provider provider, String algorithm)
  94:   {
  95:     this.kmfSpi = kmfSpi;
  96:     this.provider = provider;
  97:     this.algorithm = algorithm;
  98:   }
  99: 
 100:   // Class methods.
 101:   // ------------------------------------------------------------------
 102: 
 103:   /**
 104:    * Get the default algorithm name. This value may be specified at
 105:    * run-time via the security property
 106:    * "ssl.KeyManagerFactory.algorithm". If this property is
 107:    * not specified, this method returns "JessieX509".
 108:    *
 109:    * @return The default key manager factory algorithm's name.
 110:    */
 111:   public static final String getDefaultAlgorithm()
 112:   {
 113:     String alg = null;
 114:     try
 115:       {
 116:         alg = (String) AccessController.doPrivileged(
 117:           new PrivilegedAction()
 118:           {
 119:             public Object run()
 120:             {
 121:               return Security.getProperty("ssl.KeyManagerFactory.algorithm");
 122:             }
 123:           }
 124:         );
 125:       }
 126:     catch (SecurityException se)
 127:       {
 128:       }
 129:     if (alg == null)
 130:       alg = DEFAULT_ALGORITHM;
 131:     return alg;
 132:   }
 133: 
 134:   /**
 135:    * Get an instance of the named key manager factory, from the first
 136:    * provider that implements it.
 137:    *
 138:    * @param algorithm The type of key manager factory to get.
 139:    * @return An appropriate implementation of that algoritm.
 140:    * @throws NoSuchAlgorithmException If no provider implements the
 141:    *   requested algorithm.
 142:    */
 143:   public static final KeyManagerFactory getInstance(String algorithm)
 144:     throws NoSuchAlgorithmException
 145:   {
 146:     Provider[] provs = Security.getProviders();
 147:     for (int i = 0; i < provs.length; i++)
 148:       {
 149:         try
 150:           {
 151:             return getInstance(algorithm, provs[i]);
 152:           }
 153:         catch (NoSuchAlgorithmException ignore)
 154:           {
 155:           }
 156:       }
 157:     throw new NoSuchAlgorithmException(algorithm);
 158:   }
 159: 
 160:   /**
 161:    * Get an instance of the named key manager factory, from the named
 162:    * provider.
 163:    *
 164:    * @param algorithm The type of key manager factory to get.
 165:    * @param provider The name of the provider to get the
 166:    *   implementation from.
 167:    * @return An appropriate implementation of that algorithm.
 168:    * @throws NoSuchAlgorithmException If the provider does not
 169:    *   implement the requested algorithm.
 170:    * @throws NoSuchProviderException If the named provider does not
 171:    *   exist.
 172:    */
 173:   public static final KeyManagerFactory getInstance(String algorithm, String provider)
 174:     throws NoSuchAlgorithmException, NoSuchProviderException
 175:   {
 176:     if (provider == null)
 177:       throw new IllegalArgumentException("provider is null");
 178:     Provider p = Security.getProvider(provider);
 179:     if (p == null)
 180:       throw new NoSuchProviderException(provider);
 181:     return getInstance(algorithm, p);
 182:   }
 183: 
 184:   /**
 185:    * Get an instance of the named key manager factory, from the given
 186:    * provider.
 187:    *
 188:    * @param algorithm The type of key manager factory to get.
 189:    * @param provider The provider to get the implementation from.
 190:    * @return An appropriate implementation of that algorithm.
 191:    * @throws NoSuchAlgorithmException If the provider does not
 192:    *   implement the requested algorithm.
 193:    * @throws IllegalArgumentException If <i>provider</i> is null.
 194:    */
 195:   public static final KeyManagerFactory getInstance(String algorithm, Provider provider)
 196:     throws NoSuchAlgorithmException
 197:   {
 198:     if (provider == null)
 199:       throw new IllegalArgumentException("provider is null");
 200:     try
 201:       {
 202:         return new KeyManagerFactory((KeyManagerFactorySpi)
 203:           Engine.getInstance(KEY_MANAGER_FACTORY, algorithm, provider),
 204:           provider, algorithm);
 205:       }
 206:     catch (InvocationTargetException ite)
 207:       {
 208:         throw new NoSuchAlgorithmException(algorithm);
 209:       }
 210:     catch (ClassCastException cce)
 211:       {
 212:         throw new NoSuchAlgorithmException(algorithm);
 213:       }
 214:   }
 215: 
 216:   // Instance methods.
 217:   // -------------------------------------------------------------------
 218: 
 219:   /**
 220:    * Returns the name of this key manager factory algorithm.
 221:    *
 222:    * @return The name of this key manager factory algorithm.
 223:    */
 224:   public final String getAlgorithm()
 225:   {
 226:     return algorithm;
 227:   }
 228: 
 229:   /**
 230:    * Get an array of key managers appropriate for this algorithm, with
 231:    * the most preferred manager first.
 232:    *
 233:    * @return The array of key managers.
 234:    */
 235:   public final KeyManager[] getKeyManagers()
 236:   {
 237:     return kmfSpi.engineGetKeyManagers();
 238:   }
 239: 
 240:   /**
 241:    * Returns the provider of this implementation.
 242:    *
 243:    * @return The provider of this implementation.
 244:    */
 245:   public final Provider getProvider()
 246:   {
 247:     return provider;
 248:   }
 249: 
 250:   /**
 251:    * Initialize this instance with an implementation-dependent
 252:    * parameter object.
 253:    *
 254:    * @param params The parameters to initialize with.
 255:    * @throws InvalidAlgorithmParameterException If the specified
 256:    *   parameters are inappropriate.
 257:    */
 258:   public final void init(ManagerFactoryParameters params)
 259:     throws InvalidAlgorithmParameterException
 260:   {
 261:     kmfSpi.engineInit(params);
 262:   }
 263: 
 264:   /**
 265:    * Initialize this instance with a key store and a password for
 266:    * private key entries.
 267:    *
 268:    * @param store The key store to read.
 269:    * @param passwd The password protecting private keys in the store.
 270:    * @throws KeyStoreException If an error occurs reading the keys.
 271:    * @throws NoSuchAlgorithmException If an algorithm (such as a
 272:    *   certificate algorithm) is not available.
 273:    * @throws UnrecoverableKeyException If the password is incorrect.
 274:    */
 275:   public final void init(KeyStore store, char[] passwd)
 276:     throws KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException
 277:   {
 278:     kmfSpi.engineInit(store, passwd);
 279:   }
 280: }