Frames | No Frames |
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: /** 91: * Create an instance of <code>ExemptionMechanism</code> for a designated 92: * <code>mechanism</code> from the first Security Provider offering it. 93: * 94: * @param mechanism the name of the exemption mechanism to create. 95: * @return a newly created instance of <code>ExemptionMechanism</code>. 96: * @throws IllegalArgumentException if the provider is null. 97: * @throws NoSuchAlgorithmException if no such exemption mechanism is 98: * available from any known Security Provider. 99: * @throws IllegalArgumentException if <code>mechanism</code> is 100: * <code>null</code> or is an empty string. 101: */ 102: public static final ExemptionMechanism getInstance(String mechanism) 103: throws NoSuchAlgorithmException 104: { 105: Provider[] p = Security.getProviders(); 106: NoSuchAlgorithmException lastException = null; 107: for (int i = 0; i < p.length; i++) 108: try 109: { 110: return getInstance(mechanism, p[i]); 111: } 112: catch (NoSuchAlgorithmException x) 113: { 114: lastException = x; 115: } 116: if (lastException != null) 117: throw lastException; 118: throw new NoSuchAlgorithmException(mechanism); 119: } 120: 121: /** 122: * Create an instance of <code>ExemptionMechanism</code> for a designated 123: * <code>mechanism</code> from a named <code>provider</code>. 124: * 125: * @param mechanism the name of the exemption mechanism to create. 126: * @param provider the security provider to provide the exemption 127: * <code>mechanism</code>. 128: * @return a newly created instance of <code>ExemptionMechanism</code>. 129: * @throws NoSuchAlgorithmException if no such exemption mechanism is 130: * available from the named <code>provider</code>. 131: * @throws NoSuchProviderException if no Security Provider with the designated 132: * name is known to the underlying JVM. 133: * @throws IllegalArgumentException if either <code>mechanism</code> or 134: * <code>provider</code> is <code>null</code>, or if 135: * <code>mechanism</code> is an empty string. 136: */ 137: public static final ExemptionMechanism getInstance(String mechanism, 138: String provider) 139: throws NoSuchAlgorithmException, NoSuchProviderException 140: { 141: if (provider == null) 142: throw new IllegalArgumentException("provider MUST NOT be null"); 143: Provider p = Security.getProvider(provider); 144: if (p == null) 145: throw new NoSuchProviderException(provider); 146: return getInstance(mechanism, p); 147: } 148: 149: /** 150: * Create an instance of <code>ExemptionMechanism</code> for a designated 151: * <code>mechanism</code> from a designated <code>provider</code>. 152: * 153: * @param mechanism the name of the exemption mechanism to create. 154: * @param provider the security provider to provide the exemption 155: * <code>mechanism</code>. 156: * @return a newly created instance of <code>ExemptionMechanism</code>. 157: * @throws NoSuchAlgorithmException if an exemption mechanism could not be 158: * created. 159: * @throws IllegalArgumentException if either <code>mechanism</code> or 160: * <code>provider</code> is <code>null</code>, or if 161: * <code>mechanism</code> is an empty string. 162: */ 163: public static final ExemptionMechanism getInstance(String mechanism, 164: Provider provider) 165: throws NoSuchAlgorithmException 166: { 167: StringBuilder sb = new StringBuilder("ExemptionMechanism [") 168: .append(mechanism).append("] from provider[") 169: .append(provider).append("] could not be created"); 170: Throwable cause; 171: try 172: { 173: Object spi = Engine.getInstance(SERVICE, mechanism, provider); 174: return new ExemptionMechanism((ExemptionMechanismSpi) spi, 175: provider, 176: mechanism); 177: } 178: catch (InvocationTargetException x) 179: { 180: cause = x.getCause(); 181: if (cause instanceof NoSuchAlgorithmException) 182: throw (NoSuchAlgorithmException) cause; 183: if (cause == null) 184: cause = x; 185: } 186: catch (ClassCastException x) 187: { 188: cause = x; 189: } 190: NoSuchAlgorithmException x = new NoSuchAlgorithmException(sb.toString()); 191: x.initCause(cause); 192: throw x; 193: } 194: 195: public final byte[] genExemptionBlob() 196: throws IllegalStateException, ExemptionMechanismException 197: { 198: if (virgin) 199: { 200: throw new IllegalStateException("not initialized"); 201: } 202: return emSpi.engineGenExemptionBlob(); 203: } 204: 205: public final int genExemptionBlob(byte[] output) 206: throws IllegalStateException, ExemptionMechanismException, 207: ShortBufferException 208: { 209: return genExemptionBlob(output, 0); 210: } 211: 212: public final int genExemptionBlob(byte[] output, int outputOffset) 213: throws IllegalStateException, ExemptionMechanismException, 214: ShortBufferException 215: { 216: if (virgin) 217: { 218: throw new IllegalStateException("not initialized"); 219: } 220: return emSpi.engineGenExemptionBlob(output, outputOffset); 221: } 222: 223: public final String getName() 224: { 225: return mechanism; 226: } 227: 228: public final int getOutputSize(int inputLength) throws IllegalStateException 229: { 230: if (virgin) 231: { 232: throw new IllegalStateException("not initialized"); 233: } 234: return emSpi.engineGetOutputSize(inputLength); 235: } 236: 237: public final Provider getProvider() 238: { 239: return provider; 240: } 241: 242: public final void init(Key key) 243: throws ExemptionMechanismException, InvalidKeyException 244: { 245: emSpi.engineInit(key); 246: virgin = false; 247: } 248: 249: public final void init(Key key, AlgorithmParameters params) 250: throws ExemptionMechanismException, InvalidAlgorithmParameterException, 251: InvalidKeyException 252: { 253: emSpi.engineInit(key, params); 254: virgin = false; 255: } 256: 257: public final void init(Key key, AlgorithmParameterSpec params) 258: throws ExemptionMechanismException, InvalidAlgorithmParameterException, 259: InvalidKeyException 260: { 261: emSpi.engineInit(key, params); 262: virgin = false; 263: } 264: 265: public final boolean isCryptoAllowed(Key key) 266: throws ExemptionMechanismException 267: { 268: return true; 269: } 270: 271: protected void finalize() 272: { 273: } 274: }