AlgorithmParameters.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /* AlgorithmParameters.java --- Algorithm Parameters Implementation Class
  2. Copyright (C) 1999, 2003, 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 java.security;
  32. import gnu.java.lang.CPStringBuilder;
  33. import gnu.java.security.Engine;
  34. import java.io.IOException;
  35. import java.lang.reflect.InvocationTargetException;
  36. import java.security.spec.AlgorithmParameterSpec;
  37. import java.security.spec.InvalidParameterSpecException;
  38. /**
  39. * <code>AlgorithmParameters</code> is an Algorithm Parameters class which
  40. * provides an interface through which the user can manage the parameters of an
  41. * Algorithm.
  42. *
  43. * @author Mark Benvenuto
  44. * @since 1.2
  45. * @see AlgorithmParameterSpec
  46. * @see java.security.spec.DSAParameterSpec
  47. * @see KeyPairGenerator
  48. */
  49. public class AlgorithmParameters
  50. {
  51. /** Service name for algorithm parameters. */
  52. private static final String ALGORITHM_PARAMETERS = "AlgorithmParameters";
  53. private AlgorithmParametersSpi paramSpi;
  54. private Provider provider;
  55. private String algorithm;
  56. /**
  57. * Constructs a new instance of <code>AlgorithmParameters</code>.
  58. *
  59. * @param paramSpi
  60. * the engine to use.
  61. * @param provider
  62. * the provider to use.
  63. * @param algorithm
  64. * the algorithm to use.
  65. */
  66. protected AlgorithmParameters(AlgorithmParametersSpi paramSpi,
  67. Provider provider, String algorithm)
  68. {
  69. this.paramSpi = paramSpi;
  70. this.provider = provider;
  71. this.algorithm = algorithm;
  72. }
  73. /** @return A string with the name of the algorithm used. */
  74. public final String getAlgorithm()
  75. {
  76. return algorithm;
  77. }
  78. /**
  79. * Returns a new instance of <code>AlgorithmParameters</code> representing
  80. * the specified algorithm parameters.
  81. * <p>
  82. * The returned <code>AlgorithmParameters</code> must still be initialized
  83. * with an <code>init()</code> method.
  84. *
  85. * @param algorithm the algorithm to use.
  86. * @return the new instance repesenting the desired algorithm.
  87. * @throws NoSuchAlgorithmException if the algorithm is not implemented by any
  88. * provider.
  89. * @throws IllegalArgumentException if <code>algorithm</code> is
  90. * <code>null</code> or is an empty string.
  91. */
  92. public static AlgorithmParameters getInstance(String algorithm)
  93. throws NoSuchAlgorithmException
  94. {
  95. Provider[] p = Security.getProviders();
  96. NoSuchAlgorithmException lastException = null;
  97. for (int i = 0; i < p.length; i++)
  98. try
  99. {
  100. return getInstance(algorithm, p[i]);
  101. }
  102. catch (NoSuchAlgorithmException x)
  103. {
  104. lastException = x;
  105. }
  106. if (lastException != null)
  107. throw lastException;
  108. throw new NoSuchAlgorithmException(algorithm);
  109. }
  110. /**
  111. * Returns a new instance of <code>AlgorithmParameters</code> representing
  112. * the specified algorithm parameters from a named provider.
  113. * <p>
  114. * The returned <code>AlgorithmParameters</code> must still be intialized
  115. * with an <code>init()</code> method.
  116. * </p>
  117. *
  118. * @param algorithm the algorithm to use.
  119. * @param provider the name of the {@link Provider} to use.
  120. * @return the new instance repesenting the desired algorithm.
  121. * @throws NoSuchAlgorithmException if the algorithm is not implemented by the
  122. * named provider.
  123. * @throws NoSuchProviderException if the named provider was not found.
  124. * @throws IllegalArgumentException if either <code>algorithm</code> or
  125. * <code>provider</code> is <code>null</code> or empty.
  126. */
  127. public static AlgorithmParameters getInstance(String algorithm,
  128. String provider)
  129. throws NoSuchAlgorithmException, NoSuchProviderException
  130. {
  131. if (provider == null)
  132. throw new IllegalArgumentException("provider MUST NOT be null");
  133. provider = provider.trim();
  134. if (provider.length() == 0)
  135. throw new IllegalArgumentException("provider MUST NOT be empty");
  136. Provider p = Security.getProvider(provider);
  137. if (p == null)
  138. throw new NoSuchProviderException(provider);
  139. return getInstance(algorithm, p);
  140. }
  141. /**
  142. * Returns a new instance of <code>AlgorithmParameters</code> representing
  143. * the specified algorithm parameters from the specified {@link Provider}.
  144. * <p>
  145. * The returned <code>AlgorithmParameters</code> must still be intialized
  146. * with an <code>init()</code> method.
  147. *
  148. * @param algorithm the algorithm to use.
  149. * @param provider the {@link Provider} to use.
  150. * @return the new instance repesenting the desired algorithm.
  151. * @throws NoSuchAlgorithmException if the algorithm is not implemented by the
  152. * {@link Provider}.
  153. * @throws IllegalArgumentException if either <code>algorithm</code> or
  154. * <code>provider</code> is <code>null</code>, or if
  155. * <code>algorithm</code> is an empty string.
  156. * @since 1.4
  157. */
  158. public static AlgorithmParameters getInstance(String algorithm,
  159. Provider provider)
  160. throws NoSuchAlgorithmException
  161. {
  162. CPStringBuilder sb = new CPStringBuilder("AlgorithmParameters for algorithm [")
  163. .append(algorithm).append("] from provider[")
  164. .append(provider).append("] could not be created");
  165. Throwable cause;
  166. try
  167. {
  168. Object spi = Engine.getInstance(ALGORITHM_PARAMETERS, algorithm, provider);
  169. return new AlgorithmParameters((AlgorithmParametersSpi) spi,
  170. provider,
  171. algorithm);
  172. }
  173. catch (InvocationTargetException x)
  174. {
  175. cause = x.getCause();
  176. if (cause instanceof NoSuchAlgorithmException)
  177. throw (NoSuchAlgorithmException) cause;
  178. if (cause == null)
  179. cause = x;
  180. }
  181. catch (ClassCastException x)
  182. {
  183. cause = x;
  184. }
  185. NoSuchAlgorithmException x = new NoSuchAlgorithmException(sb.toString());
  186. x.initCause(cause);
  187. throw x;
  188. }
  189. /** @return the provider of this parameter object. */
  190. public final Provider getProvider()
  191. {
  192. return provider;
  193. }
  194. /**
  195. * Initializes the engine with the specified {@link AlgorithmParameterSpec}.
  196. *
  197. * @param paramSpec
  198. * A {@link AlgorithmParameterSpec} to use.
  199. * @throws InvalidParameterSpecException
  200. * if <code>paramSpec</code> is invalid.
  201. */
  202. public final void init(AlgorithmParameterSpec paramSpec)
  203. throws InvalidParameterSpecException
  204. {
  205. paramSpi.engineInit(paramSpec);
  206. }
  207. /**
  208. * Initializes the engine with the specified parameters stored in the byte
  209. * array and decodes them according to the ASN.1 specification. If the ASN.1
  210. * specification exists then it succeeds otherwise an {@link IOException} is
  211. * thrown.
  212. *
  213. * @param params
  214. * the parameters to use.
  215. * @throws IOException
  216. * if a decoding error occurs.
  217. */
  218. public final void init(byte[]params) throws IOException
  219. {
  220. paramSpi.engineInit(params);
  221. }
  222. /**
  223. * Initializes the engine with the specified parameters stored in the byte
  224. * array and decodes them according to the specified decoding specification.
  225. * If <code>format</code> is <code>null</code>, then this method decodes the
  226. * byte array using the ASN.1 specification if it exists, otherwise it throws
  227. * an {@link IOException}.
  228. *
  229. * @param params
  230. * the parameters to use.
  231. * @param format
  232. * the name of decoding format to use.
  233. * @throws IOException
  234. * if a decoding error occurs.
  235. */
  236. public final void init(byte[]params, String format) throws IOException
  237. {
  238. paramSpi.engineInit(params, format);
  239. }
  240. /**
  241. * Returns a new instance of <code>AlgorithmParameters</code> as a
  242. * designated parameter specification {@link Class}.
  243. *
  244. * @param paramSpec
  245. * the {@link Class} to use.
  246. * @return the parameter specification.
  247. * @throws InvalidParameterSpecException
  248. * if <code>paramSpec</code> is invalid.
  249. */
  250. public final <T extends AlgorithmParameterSpec>
  251. T getParameterSpec(Class<T> paramSpec)
  252. throws InvalidParameterSpecException
  253. {
  254. return paramSpi.engineGetParameterSpec(paramSpec);
  255. }
  256. /**
  257. * Returns the parameters in the default encoding format. The primary encoding
  258. * format is ASN.1 if it exists for the specified type.
  259. *
  260. * @return byte array representing the parameters.
  261. */
  262. public final byte[] getEncoded() throws IOException
  263. {
  264. return paramSpi.engineGetEncoded();
  265. }
  266. /**
  267. * Returns the parameters in the specified encoding format. If
  268. * <code>format</code> is <code>null</code> then the ASN.1 encoding
  269. * format is used if it exists for the specified type.
  270. *
  271. * @param format
  272. * the name of the encoding format to use.
  273. * @return the parameters encoded using the specified encoding scheme.
  274. * @throws IOException
  275. * if an encoding exception occurs, or if this parameter object has
  276. * not been initialized.
  277. */
  278. public final byte[] getEncoded(String format) throws IOException
  279. {
  280. return paramSpi.engineGetEncoded(format);
  281. }
  282. /**
  283. * Returns a string representation of the encoded form.
  284. *
  285. * @return a string representation of the encoded form.
  286. */
  287. public final String toString()
  288. {
  289. return paramSpi.engineToString();
  290. }
  291. }