Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / javax / crypto / ExemptionMechanism.java
blobb00ff0d5ffe0e4b7abdb388561b5ca343dcbe4d6
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)
9 any later version.
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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
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.
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. */
39 package javax.crypto;
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;
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>.
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)
64 * @since 1.4
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;
78 // Constructor.
79 // ------------------------------------------------------------------------
81 protected ExemptionMechanism(ExemptionMechanismSpi emSpi, Provider provider,
82 String mechanism)
84 this.emSpi = emSpi;
85 this.provider = provider;
86 this.mechanism = mechanism;
87 virgin = true;
90 // Class methods.
91 // ------------------------------------------------------------------------
93 public static final ExemptionMechanism getInstance(String mechanism)
94 throws NoSuchAlgorithmException
96 Provider[] provs = Security.getProviders();
97 String msg = "";
98 for (int i = 0; i < provs.length; i++)
102 return getInstance(mechanism, provs[i]);
104 catch (NoSuchAlgorithmException nsae)
106 msg = nsae.getMessage();
109 throw new NoSuchAlgorithmException(msg);
112 public static final ExemptionMechanism getInstance(String mechanism,
113 String provider)
114 throws NoSuchAlgorithmException, NoSuchProviderException
116 Provider p = Security.getProvider(provider);
117 if (p == null)
119 throw new NoSuchProviderException(provider);
121 return getInstance(mechanism, p);
124 public static final ExemptionMechanism getInstance(String mechanism,
125 Provider provider)
126 throws NoSuchAlgorithmException
130 return new ExemptionMechanism((ExemptionMechanismSpi)
131 Engine.getInstance(SERVICE, mechanism, provider),
132 provider, mechanism);
134 catch (InvocationTargetException ite)
136 if (ite.getCause() instanceof NoSuchAlgorithmException)
137 throw (NoSuchAlgorithmException) ite.getCause();
138 else
139 throw new NoSuchAlgorithmException(mechanism);
141 catch (ClassCastException cce)
143 throw new NoSuchAlgorithmException(mechanism);
147 // Instance methods.
148 // ------------------------------------------------------------------------
150 public final byte[] genExemptionBlob()
151 throws IllegalStateException, ExemptionMechanismException
153 if (virgin)
155 throw new IllegalStateException("not initialized");
157 return emSpi.engineGenExemptionBlob();
160 public final int genExemptionBlob(byte[] output)
161 throws IllegalStateException, ExemptionMechanismException,
162 ShortBufferException
164 return genExemptionBlob(output, 0);
167 public final int genExemptionBlob(byte[] output, int outputOffset)
168 throws IllegalStateException, ExemptionMechanismException,
169 ShortBufferException
171 if (virgin)
173 throw new IllegalStateException("not initialized");
175 return emSpi.engineGenExemptionBlob(output, outputOffset);
178 public final String getName()
180 return mechanism;
183 public final int getOutputSize(int inputLength) throws IllegalStateException
185 if (virgin)
187 throw new IllegalStateException("not initialized");
189 return emSpi.engineGetOutputSize(inputLength);
192 public final Provider getProvider()
194 return provider;
197 public final void init(Key key)
198 throws ExemptionMechanismException, InvalidKeyException
200 emSpi.engineInit(key);
201 virgin = false;
204 public final void init(Key key, AlgorithmParameters params)
205 throws ExemptionMechanismException, InvalidAlgorithmParameterException,
206 InvalidKeyException
208 emSpi.engineInit(key, params);
209 virgin = false;
212 public final void init(Key key, AlgorithmParameterSpec params)
213 throws ExemptionMechanismException, InvalidAlgorithmParameterException,
214 InvalidKeyException
216 emSpi.engineInit(key, params);
217 virgin = false;
220 public final boolean isCryptoAllowed(Key key)
221 throws ExemptionMechanismException
223 return true;
226 protected void finalize()