Source for javax.crypto.ExemptionMechanism

   1: /* ExemptionMechanism.java -- Generic crypto-weakening mechanism.
   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.AlgorithmParameters;
  45: import java.security.InvalidAlgorithmParameterException;
  46: import java.security.InvalidKeyException;
  47: import java.security.Key;
  48: import java.security.NoSuchAlgorithmException;
  49: import java.security.NoSuchProviderException;
  50: import java.security.Provider;
  51: import java.security.Security;
  52: import java.security.spec.AlgorithmParameterSpec;
  53: 
  54: /**
  55:  * An exemption mechanism, which will conditionally allow cryptography
  56:  * where it is not normally allowed, implements things such as <i>key
  57:  * recovery</i>, <i>key weakening</i>, or <i>key escrow</i>.
  58:  *
  59:  * <p><b>Implementation note</b>: this class is present for
  60:  * API-compatibility only; it is not actually used anywhere in this library
  61:  * and this library does not, in general, support crypto weakening.
  62:  *
  63:  * @author Casey Marshall (csm@gnu.org)
  64:  * @since 1.4
  65:  */
  66: public class ExemptionMechanism
  67: {
  68: 
  69:   // Constants and fields.
  70:   // ------------------------------------------------------------------------
  71: 
  72:   private static final String SERVICE = "ExemptionMechanism";
  73:   private ExemptionMechanismSpi emSpi;
  74:   private Provider provider;
  75:   private String mechanism;
  76:   private boolean virgin;
  77: 
  78:   // Constructor.
  79:   // ------------------------------------------------------------------------
  80: 
  81:   protected ExemptionMechanism(ExemptionMechanismSpi emSpi, Provider provider,
  82:                                String mechanism)
  83:   {
  84:     this.emSpi = emSpi;
  85:     this.provider = provider;
  86:     this.mechanism = mechanism;
  87:     virgin = true;
  88:   }
  89: 
  90:   // Class methods.
  91:   // ------------------------------------------------------------------------
  92: 
  93:   public static final ExemptionMechanism getInstance(String mechanism)
  94:   throws NoSuchAlgorithmException
  95:   {
  96:     Provider[] provs = Security.getProviders();
  97:     String msg = "";
  98:     for (int i = 0; i < provs.length; i++)
  99:       {
 100:         try
 101:           {
 102:             return getInstance(mechanism, provs[i]);
 103:           }
 104:         catch (NoSuchAlgorithmException nsae)
 105:           {
 106:             msg = nsae.getMessage();
 107:           }
 108:       }
 109:     throw new NoSuchAlgorithmException(msg);
 110:   }
 111: 
 112:   public static final ExemptionMechanism getInstance(String mechanism,
 113:                                                      String provider)
 114:     throws NoSuchAlgorithmException, NoSuchProviderException
 115:   {
 116:     Provider p = Security.getProvider(provider);
 117:     if (p == null)
 118:       {
 119:         throw new NoSuchProviderException(provider);
 120:       }
 121:     return getInstance(mechanism, p);
 122:   }
 123: 
 124:   public static final ExemptionMechanism getInstance(String mechanism,
 125:                                                      Provider provider)
 126:     throws NoSuchAlgorithmException
 127:   {
 128:     try
 129:       {
 130:         return new ExemptionMechanism((ExemptionMechanismSpi)
 131:           Engine.getInstance(SERVICE, mechanism, provider),
 132:           provider, mechanism);
 133:       }
 134:     catch (InvocationTargetException ite)
 135:       {
 136:         if (ite.getCause() instanceof NoSuchAlgorithmException)
 137:           throw (NoSuchAlgorithmException) ite.getCause();
 138:         else
 139:           throw new NoSuchAlgorithmException(mechanism);
 140:       }
 141:     catch (ClassCastException cce)
 142:       {
 143:         throw new NoSuchAlgorithmException(mechanism);
 144:       }
 145:   }
 146: 
 147:   // Instance methods.
 148:   // ------------------------------------------------------------------------
 149: 
 150:   public final byte[] genExemptionBlob()
 151:     throws IllegalStateException, ExemptionMechanismException
 152:   {
 153:     if (virgin)
 154:       {
 155:         throw new IllegalStateException("not initialized");
 156:       }
 157:     return emSpi.engineGenExemptionBlob();
 158:   }
 159: 
 160:   public final int genExemptionBlob(byte[] output)
 161:     throws IllegalStateException, ExemptionMechanismException,
 162:            ShortBufferException
 163:   {
 164:     return genExemptionBlob(output, 0);
 165:   }
 166: 
 167:   public final int genExemptionBlob(byte[] output, int outputOffset)
 168:     throws IllegalStateException, ExemptionMechanismException,
 169:            ShortBufferException
 170:   {
 171:     if (virgin)
 172:       {
 173:         throw new IllegalStateException("not initialized");
 174:       }
 175:     return emSpi.engineGenExemptionBlob(output, outputOffset);
 176:   }
 177: 
 178:   public final String getName()
 179:   {
 180:     return mechanism;
 181:   }
 182: 
 183:   public final int getOutputSize(int inputLength) throws IllegalStateException
 184:   {
 185:     if (virgin)
 186:       {
 187:         throw new IllegalStateException("not initialized");
 188:       }
 189:     return emSpi.engineGetOutputSize(inputLength);
 190:   }
 191: 
 192:   public final Provider getProvider()
 193:   {
 194:     return provider;
 195:   }
 196: 
 197:   public final void init(Key key)
 198:     throws ExemptionMechanismException, InvalidKeyException
 199:   {
 200:     emSpi.engineInit(key);
 201:     virgin = false;
 202:   }
 203: 
 204:   public final void init(Key key, AlgorithmParameters params)
 205:     throws ExemptionMechanismException, InvalidAlgorithmParameterException,
 206:            InvalidKeyException
 207:   {
 208:     emSpi.engineInit(key, params);
 209:     virgin = false;
 210:   }
 211: 
 212:   public final void init(Key key, AlgorithmParameterSpec params)
 213:     throws ExemptionMechanismException, InvalidAlgorithmParameterException,
 214:            InvalidKeyException
 215:   {
 216:     emSpi.engineInit(key, params);
 217:     virgin = false;
 218:   }
 219: 
 220:   public final boolean isCryptoAllowed(Key key)
 221:     throws ExemptionMechanismException
 222:   {
 223:     return true;
 224:   }
 225: 
 226:   protected void finalize()
 227:   {
 228:   }
 229: }