Source for javax.net.ssl.TrustManagerFactory

   1: /* TrustManagerFactory.java -- factory for trust 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: 
  54: /**
  55:  * A factory for creating trust manager objects.
  56:  */
  57: public class TrustManagerFactory
  58: {
  59: 
  60:   // Constants and fields.
  61:   // -------------------------------------------------------------------------
  62: 
  63:   /** The service name for trust manager factories. */
  64:   private static final String TRUST_MANAGER_FACTORY = "TrustManagerFactory";
  65: 
  66:   /** The system default trust manager algorithm. */
  67:   private static final String DEFAULT_ALGORITHM = "JessieX509";
  68: 
  69:   /** The underlying engine class. */
  70:   private final TrustManagerFactorySpi tmfSpi;
  71: 
  72:   /** The provider of the engine class. */
  73:   private final Provider provider;
  74: 
  75:   /** The name of this trust manager algorithm. */
  76:   private final String algorithm;
  77: 
  78:   // Constructor.
  79:   // -------------------------------------------------------------------------
  80: 
  81:   /**
  82:    * Creates a new trust manager factory.
  83:    *
  84:    * @param tmfSpi The underlying engine class.
  85:    * @param provider The provider of the engine class.
  86:    * @param algorithm The trust manager algorithm name.
  87:    */
  88:   protected TrustManagerFactory(TrustManagerFactorySpi tmfSpi,
  89:                                 Provider provider, String algorithm)
  90:   {
  91:     this.tmfSpi = tmfSpi;
  92:     this.provider = provider;
  93:     this.algorithm = algorithm;
  94:   }
  95: 
  96:   // Class methods.
  97:   // -------------------------------------------------------------------------
  98: 
  99:   /**
 100:    * Returns an instance of a trust manager factory for the given algorithm
 101:    * from the first provider that implements it.
 102:    *
 103:    * @param algorithm The name of the algorithm to get.
 104:    * @return The instance of the trust manager factory.
 105:    * @throws NoSuchAlgorithmException If no provider implements the given
 106:    *   algorithm.
 107:    */
 108:   public static final TrustManagerFactory getInstance(String algorithm)
 109:     throws NoSuchAlgorithmException
 110:   {
 111:     Provider[] provs = Security.getProviders();
 112:     for (int i = 0; i < provs.length; i++)
 113:       {
 114:         try
 115:           {
 116:             return getInstance(algorithm, provs[i]);
 117:           }
 118:         catch (NoSuchAlgorithmException ignore)
 119:           {
 120:           }
 121:       }
 122:     throw new NoSuchAlgorithmException(algorithm);
 123:   }
 124: 
 125:   /**
 126:    * Returns an instance of a trust manager factory for the given algorithm
 127:    * from the named provider.
 128:    *
 129:    * @param algorithm The name of the algorithm to get.
 130:    * @param provider The name of the provider to get the instance from.
 131:    * @return The instance of the trust manager factory.
 132:    * @throws NoSuchAlgorithmException If the provider does not implement the
 133:    *   given algorithm.
 134:    * @throws NoSuchProviderException If there is no such named provider.
 135:    * @throws IllegalArgumentException If the provider argument is null.
 136:    */
 137:   public static final TrustManagerFactory getInstance(String algorithm,
 138:                                                       String provider)
 139:     throws NoSuchAlgorithmException, NoSuchProviderException
 140:   {
 141:     if (provider == null)
 142:       {
 143:         throw new IllegalArgumentException();
 144:       }
 145:     Provider p = Security.getProvider(provider);
 146:     if (p == null)
 147:       {
 148:         throw new NoSuchProviderException(provider);
 149:       }
 150:     return getInstance(algorithm, p);
 151:   }
 152: 
 153:   /**
 154:    * Returns an instance of a trust manager factory for the given algorithm
 155:    * from the specified provider.
 156:    *
 157:    * @param algorithm The name of the algorithm to get.
 158:    * @param provider The provider to get the instance from.
 159:    * @return The instance of the trust manager factory.
 160:    * @throws NoSuchAlgorithmException If the provider does not implement the
 161:    *   given algorithm.
 162:    * @throws IllegalArgumentException If the provider argument is null.
 163:    */
 164:   public static final TrustManagerFactory getInstance(String algorithm,
 165:                                                       Provider provider)
 166:     throws NoSuchAlgorithmException
 167:   {
 168:     if (provider == null)
 169:       {
 170:         throw new IllegalArgumentException();
 171:       }
 172:     try
 173:       {
 174:         return new TrustManagerFactory((TrustManagerFactorySpi)
 175:           Engine.getInstance(TRUST_MANAGER_FACTORY, algorithm, provider),
 176:           provider, algorithm);
 177:       }
 178:     catch (InvocationTargetException ite)
 179:       {
 180:         throw new NoSuchAlgorithmException(algorithm);
 181:       }
 182:     catch (ClassCastException cce)
 183:       {
 184:         throw new NoSuchAlgorithmException(algorithm);
 185:       }
 186:   }
 187: 
 188:   /**
 189:    * Returns the default algorithm for trust manager factories. The value
 190:    * returned is either the value of the security property
 191:    * "ssl.TrustManagerFactory.algorithm" if it is set, or the value "JessieX509"
 192:    * if not.
 193:    *
 194:    * @return The default algorithm name.
 195:    * @see Security.getProperty(java.lang.String)
 196:    */
 197:   public static final String getDefaultAlgorithm()
 198:   {
 199:     String alg = null;
 200:     try
 201:       {
 202:         alg = (String) AccessController.doPrivileged(
 203:           new PrivilegedAction()
 204:           {
 205:             public Object run()
 206:             {
 207:               return Security.getProperty("ssl.TrustManagerFactory.algorithm");
 208:             }
 209:           }
 210:         );
 211:       }
 212:     catch (SecurityException se)
 213:       {
 214:       }
 215:     if (alg == null)
 216:       alg = DEFAULT_ALGORITHM;
 217:     return alg;
 218:   }
 219: 
 220:   // Instance methods.
 221:   // -------------------------------------------------------------------------
 222: 
 223:   /**
 224:    * Returns the name of this trust manager algorithm.
 225:    *
 226:    * @return The algorithm name.
 227:    */
 228:   public final String getAlgorithm()
 229:   {
 230:     return algorithm;
 231:   }
 232: 
 233:   /**
 234:    * Returns the provider of the underlying implementation.
 235:    *
 236:    * @return The provider.
 237:    */
 238:   public final Provider getProvider()
 239:   {
 240:     return provider;
 241:   }
 242: 
 243:   /**
 244:    * Returns the trust managers created by this factory.
 245:    *
 246:    * @return The trust managers.
 247:    */
 248:   public final TrustManager[] getTrustManagers()
 249:   {
 250:     return tmfSpi.engineGetTrustManagers();
 251:   }
 252: 
 253:   /**
 254:    * Initialize this instance with some algorithm-specific parameters.
 255:    *
 256:    * @param params The parameters.
 257:    * @throws InvalidAlgorithmParameterException If the supplied parameters
 258:    *   are inappropriate for this instance.
 259:    */
 260:   public final void init(ManagerFactoryParameters params)
 261:     throws InvalidAlgorithmParameterException
 262:   {
 263:     tmfSpi.engineInit(params);
 264:   }
 265: 
 266:   /**
 267:    * Initialize this instance with a key store. The key store may be null,
 268:    * in which case a default will be used.
 269:    *
 270:    * @param store The key store.
 271:    * @throws KeyStoreException If there is a problem reading from the
 272:    *   key store.
 273:    */
 274:   public final void init(KeyStore store) throws KeyStoreException
 275:   {
 276:     tmfSpi.engineInit(store);
 277:   }
 278: }