1 /* ExemptionMechanism.java -- Generic crypto-weakening mechanism.
2 Copyright (C) 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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)
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.
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
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
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. */
41 import gnu.java.security.Engine;
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;
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>.
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.
63 * @author Casey Marshall (csm@gnu.org)
66 public class ExemptionMechanism
69 // Constants and fields.
70 // ------------------------------------------------------------------------
72 private static final String SERVICE = "ExemptionMechanism";
73 private ExemptionMechanismSpi emSpi;
74 private Provider provider;
75 private String mechanism;
76 private boolean virgin;
79 // ------------------------------------------------------------------------
81 protected ExemptionMechanism(ExemptionMechanismSpi emSpi, Provider provider,
85 this.provider = provider;
86 this.mechanism = mechanism;
91 * Create an instance of <code>ExemptionMechanism</code> for a designated
92 * <code>mechanism</code> from the first Security Provider offering it.
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.
102 public static final ExemptionMechanism getInstance(String mechanism)
103 throws NoSuchAlgorithmException
105 Provider[] p = Security.getProviders();
106 NoSuchAlgorithmException lastException = null;
107 for (int i = 0; i < p.length; i++)
110 return getInstance(mechanism, p[i]);
112 catch (NoSuchAlgorithmException x)
116 if (lastException != null)
118 throw new NoSuchAlgorithmException(mechanism);
122 * Create an instance of <code>ExemptionMechanism</code> for a designated
123 * <code>mechanism</code> from a named <code>provider</code>.
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.
137 public static final ExemptionMechanism getInstance(String mechanism,
139 throws NoSuchAlgorithmException, NoSuchProviderException
141 if (provider == null)
142 throw new IllegalArgumentException("provider MUST NOT be null");
143 Provider p = Security.getProvider(provider);
145 throw new NoSuchProviderException(provider);
146 return getInstance(mechanism, p);
150 * Create an instance of <code>ExemptionMechanism</code> for a designated
151 * <code>mechanism</code> from a designated <code>provider</code>.
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
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.
163 public static final ExemptionMechanism getInstance(String mechanism,
165 throws NoSuchAlgorithmException
167 StringBuilder sb = new StringBuilder("ExemptionMechanism [")
168 .append(mechanism).append("] from provider[")
169 .append(provider).append("] could not be created");
173 Object spi = Engine.getInstance(SERVICE, mechanism, provider);
174 return new ExemptionMechanism((ExemptionMechanismSpi) spi,
178 catch (InvocationTargetException x)
180 cause = x.getCause();
181 if (cause instanceof NoSuchAlgorithmException)
182 throw (NoSuchAlgorithmException) cause;
186 catch (ClassCastException x)
190 NoSuchAlgorithmException x = new NoSuchAlgorithmException(sb.toString());
195 public final byte[] genExemptionBlob()
196 throws IllegalStateException, ExemptionMechanismException
200 throw new IllegalStateException("not initialized");
202 return emSpi.engineGenExemptionBlob();
205 public final int genExemptionBlob(byte[] output)
206 throws IllegalStateException, ExemptionMechanismException,
209 return genExemptionBlob(output, 0);
212 public final int genExemptionBlob(byte[] output, int outputOffset)
213 throws IllegalStateException, ExemptionMechanismException,
218 throw new IllegalStateException("not initialized");
220 return emSpi.engineGenExemptionBlob(output, outputOffset);
223 public final String getName()
228 public final int getOutputSize(int inputLength) throws IllegalStateException
232 throw new IllegalStateException("not initialized");
234 return emSpi.engineGetOutputSize(inputLength);
237 public final Provider getProvider()
242 public final void init(Key key)
243 throws ExemptionMechanismException, InvalidKeyException
245 emSpi.engineInit(key);
249 public final void init(Key key, AlgorithmParameters params)
250 throws ExemptionMechanismException, InvalidAlgorithmParameterException,
253 emSpi.engineInit(key, params);
257 public final void init(Key key, AlgorithmParameterSpec params)
258 throws ExemptionMechanismException, InvalidAlgorithmParameterException,
261 emSpi.engineInit(key, params);
265 public final boolean isCryptoAllowed(Key key)
266 throws ExemptionMechanismException
271 protected void finalize()