ExemptionMechanism.java 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /* ExemptionMechanism.java -- Generic crypto-weakening mechanism.
  2. Copyright (C) 2004 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package javax.crypto;
  32. import gnu.java.security.Engine;
  33. import java.lang.reflect.InvocationTargetException;
  34. import java.security.AlgorithmParameters;
  35. import java.security.InvalidAlgorithmParameterException;
  36. import java.security.InvalidKeyException;
  37. import java.security.Key;
  38. import java.security.NoSuchAlgorithmException;
  39. import java.security.NoSuchProviderException;
  40. import java.security.Provider;
  41. import java.security.Security;
  42. import java.security.spec.AlgorithmParameterSpec;
  43. /**
  44. * An exemption mechanism, which will conditionally allow cryptography
  45. * where it is not normally allowed, implements things such as <i>key
  46. * recovery</i>, <i>key weakening</i>, or <i>key escrow</i>.
  47. *
  48. * <p><b>Implementation note</b>: this class is present for
  49. * API-compatibility only; it is not actually used anywhere in this library
  50. * and this library does not, in general, support crypto weakening.
  51. *
  52. * @author Casey Marshall (csm@gnu.org)
  53. * @since 1.4
  54. */
  55. public class ExemptionMechanism
  56. {
  57. // Constants and fields.
  58. // ------------------------------------------------------------------------
  59. private static final String SERVICE = "ExemptionMechanism";
  60. private ExemptionMechanismSpi emSpi;
  61. private Provider provider;
  62. private String mechanism;
  63. private boolean virgin;
  64. // Constructor.
  65. // ------------------------------------------------------------------------
  66. protected ExemptionMechanism(ExemptionMechanismSpi emSpi, Provider provider,
  67. String mechanism)
  68. {
  69. this.emSpi = emSpi;
  70. this.provider = provider;
  71. this.mechanism = mechanism;
  72. virgin = true;
  73. }
  74. /**
  75. * Create an instance of <code>ExemptionMechanism</code> for a designated
  76. * <code>mechanism</code> from the first Security Provider offering it.
  77. *
  78. * @param mechanism the name of the exemption mechanism to create.
  79. * @return a newly created instance of <code>ExemptionMechanism</code>.
  80. * @throws IllegalArgumentException if the provider is null.
  81. * @throws NoSuchAlgorithmException if no such exemption mechanism is
  82. * available from any known Security Provider.
  83. * @throws IllegalArgumentException if <code>mechanism</code> is
  84. * <code>null</code> or is an empty string.
  85. */
  86. public static final ExemptionMechanism getInstance(String mechanism)
  87. throws NoSuchAlgorithmException
  88. {
  89. Provider[] p = Security.getProviders();
  90. NoSuchAlgorithmException lastException = null;
  91. for (int i = 0; i < p.length; i++)
  92. try
  93. {
  94. return getInstance(mechanism, p[i]);
  95. }
  96. catch (NoSuchAlgorithmException x)
  97. {
  98. lastException = x;
  99. }
  100. if (lastException != null)
  101. throw lastException;
  102. throw new NoSuchAlgorithmException(mechanism);
  103. }
  104. /**
  105. * Create an instance of <code>ExemptionMechanism</code> for a designated
  106. * <code>mechanism</code> from a named <code>provider</code>.
  107. *
  108. * @param mechanism the name of the exemption mechanism to create.
  109. * @param provider the security provider to provide the exemption
  110. * <code>mechanism</code>.
  111. * @return a newly created instance of <code>ExemptionMechanism</code>.
  112. * @throws NoSuchAlgorithmException if no such exemption mechanism is
  113. * available from the named <code>provider</code>.
  114. * @throws NoSuchProviderException if no Security Provider with the designated
  115. * name is known to the underlying JVM.
  116. * @throws IllegalArgumentException if either <code>mechanism</code> or
  117. * <code>provider</code> is <code>null</code>, or if
  118. * <code>mechanism</code> is an empty string.
  119. */
  120. public static final ExemptionMechanism getInstance(String mechanism,
  121. String provider)
  122. throws NoSuchAlgorithmException, NoSuchProviderException
  123. {
  124. if (provider == null)
  125. throw new IllegalArgumentException("provider MUST NOT be null");
  126. Provider p = Security.getProvider(provider);
  127. if (p == null)
  128. throw new NoSuchProviderException(provider);
  129. return getInstance(mechanism, p);
  130. }
  131. /**
  132. * Create an instance of <code>ExemptionMechanism</code> for a designated
  133. * <code>mechanism</code> from a designated <code>provider</code>.
  134. *
  135. * @param mechanism the name of the exemption mechanism to create.
  136. * @param provider the security provider to provide the exemption
  137. * <code>mechanism</code>.
  138. * @return a newly created instance of <code>ExemptionMechanism</code>.
  139. * @throws NoSuchAlgorithmException if an exemption mechanism could not be
  140. * created.
  141. * @throws IllegalArgumentException if either <code>mechanism</code> or
  142. * <code>provider</code> is <code>null</code>, or if
  143. * <code>mechanism</code> is an empty string.
  144. */
  145. public static final ExemptionMechanism getInstance(String mechanism,
  146. Provider provider)
  147. throws NoSuchAlgorithmException
  148. {
  149. StringBuilder sb = new StringBuilder("ExemptionMechanism [")
  150. .append(mechanism).append("] from provider[")
  151. .append(provider).append("] could not be created");
  152. Throwable cause;
  153. try
  154. {
  155. Object spi = Engine.getInstance(SERVICE, mechanism, provider);
  156. return new ExemptionMechanism((ExemptionMechanismSpi) spi,
  157. provider,
  158. mechanism);
  159. }
  160. catch (InvocationTargetException x)
  161. {
  162. cause = x.getCause();
  163. if (cause instanceof NoSuchAlgorithmException)
  164. throw (NoSuchAlgorithmException) cause;
  165. if (cause == null)
  166. cause = x;
  167. }
  168. catch (ClassCastException x)
  169. {
  170. cause = x;
  171. }
  172. NoSuchAlgorithmException x = new NoSuchAlgorithmException(sb.toString());
  173. x.initCause(cause);
  174. throw x;
  175. }
  176. public final byte[] genExemptionBlob()
  177. throws IllegalStateException, ExemptionMechanismException
  178. {
  179. if (virgin)
  180. {
  181. throw new IllegalStateException("not initialized");
  182. }
  183. return emSpi.engineGenExemptionBlob();
  184. }
  185. public final int genExemptionBlob(byte[] output)
  186. throws IllegalStateException, ExemptionMechanismException,
  187. ShortBufferException
  188. {
  189. return genExemptionBlob(output, 0);
  190. }
  191. public final int genExemptionBlob(byte[] output, int outputOffset)
  192. throws IllegalStateException, ExemptionMechanismException,
  193. ShortBufferException
  194. {
  195. if (virgin)
  196. {
  197. throw new IllegalStateException("not initialized");
  198. }
  199. return emSpi.engineGenExemptionBlob(output, outputOffset);
  200. }
  201. public final String getName()
  202. {
  203. return mechanism;
  204. }
  205. public final int getOutputSize(int inputLength) throws IllegalStateException
  206. {
  207. if (virgin)
  208. {
  209. throw new IllegalStateException("not initialized");
  210. }
  211. return emSpi.engineGetOutputSize(inputLength);
  212. }
  213. public final Provider getProvider()
  214. {
  215. return provider;
  216. }
  217. public final void init(Key key)
  218. throws ExemptionMechanismException, InvalidKeyException
  219. {
  220. emSpi.engineInit(key);
  221. virgin = false;
  222. }
  223. public final void init(Key key, AlgorithmParameters params)
  224. throws ExemptionMechanismException, InvalidAlgorithmParameterException,
  225. InvalidKeyException
  226. {
  227. emSpi.engineInit(key, params);
  228. virgin = false;
  229. }
  230. public final void init(Key key, AlgorithmParameterSpec params)
  231. throws ExemptionMechanismException, InvalidAlgorithmParameterException,
  232. InvalidKeyException
  233. {
  234. emSpi.engineInit(key, params);
  235. virgin = false;
  236. }
  237. public final boolean isCryptoAllowed(Key key)
  238. throws ExemptionMechanismException
  239. {
  240. return true;
  241. }
  242. protected void finalize()
  243. {
  244. }
  245. }