Source for javax.crypto.SecretKeyFactory

   1: /* SecretKeyFactory.java -- Factory for creating secret keys.
   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.crypto;
  40: 
  41: import gnu.java.security.Engine;
  42: 
  43: import java.lang.reflect.InvocationTargetException;
  44: import java.security.InvalidKeyException;
  45: import java.security.NoSuchAlgorithmException;
  46: import java.security.NoSuchProviderException;
  47: import java.security.Provider;
  48: import java.security.Security;
  49: import java.security.spec.InvalidKeySpecException;
  50: import java.security.spec.KeySpec;
  51: 
  52: /**
  53:  * A secret key factory translates {@link SecretKey} objects to and from
  54:  * {@link java.security.spec.KeySpec} objects, and can translate between
  55:  * different vendors' representations of {@link SecretKey} objects (for
  56:  * security or semantics; whichever applies).
  57:  *
  58:  * @author Casey Marshall (csm@gnu.org)
  59:  * @since 1.4
  60:  * @see SecretKey
  61:  */
  62: public class SecretKeyFactory
  63: {
  64: 
  65:   // Constants and fields.
  66:   // ------------------------------------------------------------------------
  67: 
  68:   private static final String SERVICE = "SecretKeyFactory";
  69: 
  70:   /** The underlying factory implementation. */
  71:   private SecretKeyFactorySpi skfSpi;
  72: 
  73:   /** The provider of the implementation. */
  74:   private Provider provider;
  75: 
  76:   /** The name of the algorithm. */
  77:   private String algorithm;
  78: 
  79:   // Constructor.
  80:   // ------------------------------------------------------------------------
  81: 
  82:   /**
  83:    * Create a new secret key factory.
  84:    *
  85:    * @param skfSpi   The underlying factory implementation.
  86:    * @param provider The provider.
  87:    * @param algorithm The algorithm name.
  88:    */
  89:   protected SecretKeyFactory(SecretKeyFactorySpi skfSpi, Provider provider,
  90:                              String algorithm)
  91:   {
  92:     this.skfSpi = skfSpi;
  93:     this.provider = provider;
  94:     this.algorithm = algorithm;
  95:   }
  96: 
  97:   /**
  98:    * Create a new secret key factory from the first appropriate instance.
  99:    * 
 100:    * @param algorithm The algorithm name.
 101:    * @return The appropriate key factory, if found.
 102:    * @throws NoSuchAlgorithmException If no provider implements the specified
 103:    *           algorithm.
 104:    * @throws IllegalArgumentException if <code>algorithm</code> is
 105:    *           <code>null</code> or is an empty string.
 106:    */
 107:   public static final SecretKeyFactory getInstance(String algorithm)
 108:       throws NoSuchAlgorithmException
 109:   {
 110:     Provider[] p = Security.getProviders();
 111:     NoSuchAlgorithmException lastException = null;
 112:     for (int i = 0; i < p.length; i++)
 113:       try
 114:         {
 115:           return getInstance(algorithm, p[i]);
 116:         }
 117:       catch (NoSuchAlgorithmException x)
 118:         {
 119:           lastException = x;
 120:         }
 121:       if (lastException != null)
 122:         throw lastException;
 123:       throw new NoSuchAlgorithmException(algorithm);
 124:   }
 125: 
 126:   /**
 127:    * Create a new secret key factory from the named provider.
 128:    * 
 129:    * @param algorithm The algorithm name.
 130:    * @param provider The provider name.
 131:    * @return The appropriate key factory, if found.
 132:    * @throws NoSuchAlgorithmException If the named provider does not implement
 133:    *           the algorithm.
 134:    * @throws NoSuchProviderException If the named provider does not exist.
 135:    * @throws IllegalArgumentException if either <code>algorithm</code> or
 136:    *           <code>provider</code> is <code>null</code>, or if
 137:    *           <code>algorithm</code> is an empty string.
 138:    */
 139:   public static final SecretKeyFactory getInstance(String algorithm,
 140:                                                    String provider)
 141:       throws NoSuchAlgorithmException, NoSuchProviderException
 142:   {
 143:     if (provider == null)
 144:       throw new IllegalArgumentException("provider MUST NOT be null");
 145:     Provider p = Security.getProvider(provider);
 146:     if (p == null)
 147:       throw new NoSuchProviderException(provider);
 148:     return getInstance(algorithm, p);
 149:   }
 150: 
 151:   /**
 152:    * Create a new secret key factory from the specified provider.
 153:    * 
 154:    * @param algorithm The algorithm name.
 155:    * @param provider The provider.
 156:    * @return The appropriate key factory, if found.
 157:    * @throws NoSuchAlgorithmException If the provider does not implement the
 158:    *           algorithm.
 159:    * @throws IllegalArgumentException if either <code>algorithm</code> or
 160:    *           <code>provider</code> is <code>null</code>, or if
 161:    *           <code>algorithm</code> is an empty string.
 162:    */
 163:   public static final SecretKeyFactory getInstance(String algorithm,
 164:                                                    Provider provider)
 165:       throws NoSuchAlgorithmException
 166:   {
 167:     StringBuilder sb = new StringBuilder("SecretKeyFactory algorithm [")
 168:         .append(algorithm).append("] from provider[")
 169:         .append(provider).append("] could not be created");
 170:     Throwable cause;
 171:     try
 172:       {
 173:         Object spi = Engine.getInstance(SERVICE, algorithm, provider);
 174:         return new SecretKeyFactory((SecretKeyFactorySpi) spi, provider, algorithm);
 175:       }
 176:     catch (InvocationTargetException x)
 177:       {
 178:         cause = x.getCause();
 179:         if (cause instanceof NoSuchAlgorithmException)
 180:           throw (NoSuchAlgorithmException) cause;
 181:         if (cause == null)
 182:           cause = x;
 183:       }
 184:     catch (ClassCastException x)
 185:       {
 186:         cause = x;
 187:       }
 188:     NoSuchAlgorithmException x = new NoSuchAlgorithmException(sb.toString());
 189:     x.initCause(cause);
 190:     throw x;
 191:   }
 192: 
 193:   /**
 194:    * Generate a secret key from a key specification, if possible.
 195:    *
 196:    * @param keySpec The key specification.
 197:    * @return The secret key.
 198:    * @throws java.security.InvalidKeySpecException If the key specification
 199:    *         cannot be transformed into a secret key.
 200:    */
 201:   public final SecretKey generateSecret(KeySpec keySpec)
 202:     throws InvalidKeySpecException
 203:   {
 204:     return skfSpi.engineGenerateSecret(keySpec);
 205:   }
 206: 
 207:   /**
 208:    * Get the algorithm name.
 209:    *
 210:    * @return The algorithm name.
 211:    */
 212:   public final String getAlgorithm()
 213:   {
 214:     return algorithm;
 215:   }
 216: 
 217:   /**
 218:    * Get the key specification from a secret key.
 219:    *
 220:    * @param key     The secret key.
 221:    * @param keySpec The target key specification class.
 222:    * @return The key specification.
 223:    * @throws java.security.spec.InvalidKeySpecException If the secret key cannot
 224:    *         be transformed into the specified key specification.
 225:    */
 226:   public final KeySpec getKeySpec(SecretKey key, Class keySpec)
 227:     throws InvalidKeySpecException
 228:   {
 229:     return skfSpi.engineGetKeySpec(key, keySpec);
 230:   }
 231: 
 232:   /**
 233:    * Get the provider of this implementation.
 234:    *
 235:    * @return The provider.
 236:    */
 237:   public final Provider getProvider()
 238:   {
 239:     return provider;
 240:   }
 241: 
 242:   /**
 243:    * Translate a secret key into another form.
 244:    *
 245:    * @param key The key to translate.
 246:    * @return The translated key.
 247:    * @throws java.security.InvalidKeyException If the argument cannot be
 248:    *         translated.
 249:    */
 250:   public final SecretKey translateKey(SecretKey key)
 251:     throws InvalidKeyException
 252:   {
 253:     return skfSpi.engineTranslateKey(key);
 254:   }
 255: }