AlgorithmParameters.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /* AlgorithmParameters.java --- Algorithm Parameters Implementation Class
  2. Copyright (C) 1999 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., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 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 java.security.spec.InvalidParameterSpecException;
  33. import java.security.spec.AlgorithmParameterSpec;
  34. import java.io.IOException;
  35. /**
  36. AlgorithmParameters is the Algorithm Parameters class which
  37. provides an interface through which to modify parameters for
  38. classes. This class is used to manage the algorithm parameters.
  39. @since JDK 1.2
  40. @author Mark Benvenuto
  41. */
  42. public class AlgorithmParameters
  43. {
  44. private AlgorithmParametersSpi paramSpi;
  45. private Provider provider;
  46. private String algorithm;
  47. /**
  48. Creates an instance of AlgorithmParameters
  49. @param paramSpi A parameters engine to use
  50. @param provider A provider to use
  51. @param algorithm The algorithm
  52. */
  53. protected AlgorithmParameters(AlgorithmParametersSpi paramSpi,
  54. Provider provider, String algorithm)
  55. {
  56. this.paramSpi = paramSpi;
  57. this.provider = provider;
  58. this.algorithm = algorithm;
  59. }
  60. /**
  61. Returns the name of the algorithm used
  62. @return A string with the name of the algorithm
  63. */
  64. public final String getAlgorithm()
  65. {
  66. return algorithm;
  67. }
  68. /**
  69. Gets an instance of the AlgorithmParameters class representing
  70. the specified algorithm parameters. If the algorithm is not
  71. found then, it throws NoSuchAlgorithmException.
  72. The returned AlgorithmParameters must still be intialized with
  73. init().
  74. @param algorithm the name of algorithm to choose
  75. @return a AlgorithmParameters repesenting the desired algorithm
  76. @throws NoSuchAlgorithmException if the algorithm is not implemented by providers
  77. */
  78. public static AlgorithmParameters getInstance(String algorithm) throws
  79. NoSuchAlgorithmException
  80. {
  81. Provider[] p = Security.getProviders();
  82. for (int i = 0; i < p.length; i++)
  83. {
  84. String classname =
  85. p[i].getProperty("AlgorithmParameters." + algorithm);
  86. if (classname != null)
  87. return getInstance(classname, algorithm, p[i]);
  88. }
  89. throw new NoSuchAlgorithmException(algorithm);
  90. }
  91. /**
  92. Gets an instance of the AlgorithmParameters class representing
  93. the specified algorithm parameters from the specified provider.
  94. If the algorithm is not found then, it throws
  95. NoSuchAlgorithmException. If the provider is not found, then
  96. it throws NoSuchProviderException.
  97. The returned AlgorithmParameters must still be intialized with
  98. init().
  99. @param algorithm the name of algorithm to choose
  100. @param provider the name of the provider to find the algorithm in
  101. @return a AlgorithmParameters repesenting the desired algorithm
  102. @throws NoSuchAlgorithmException if the algorithm is not implemented by the provider
  103. @throws NoSuchProviderException if the provider is not found
  104. */
  105. public static AlgorithmParameters getInstance(String algorithm,
  106. String provider) throws
  107. NoSuchAlgorithmException, NoSuchProviderException
  108. {
  109. Provider p = Security.getProvider(provider);
  110. if (p == null)
  111. throw new NoSuchProviderException();
  112. return getInstance(p.getProperty("AlgorithmParameters." + algorithm),
  113. algorithm, p);
  114. }
  115. private static AlgorithmParameters getInstance(String classname,
  116. String algorithm,
  117. Provider provider)
  118. throws NoSuchAlgorithmException
  119. {
  120. try
  121. {
  122. return new AlgorithmParameters((AlgorithmParametersSpi) Class.
  123. forName(classname).newInstance(),
  124. provider, algorithm);
  125. }
  126. catch (ClassNotFoundException cnfe)
  127. {
  128. throw new NoSuchAlgorithmException("Class not found");
  129. }
  130. catch (InstantiationException ie)
  131. {
  132. throw new NoSuchAlgorithmException("Class instantiation failed");
  133. }
  134. catch (IllegalAccessException iae)
  135. {
  136. throw new NoSuchAlgorithmException("Illegal Access");
  137. }
  138. }
  139. /**
  140. Gets the provider that the class is from.
  141. @return the provider of this class
  142. */
  143. public final Provider getProvider()
  144. {
  145. return provider;
  146. }
  147. /**
  148. Initializes the engine with the specified
  149. AlgorithmParameterSpec class.
  150. @param paramSpec A AlgorithmParameterSpec to initialize with
  151. @throws InvalidParameterSpecException For an inapporiate ParameterSpec class
  152. */
  153. public final void init(AlgorithmParameterSpec paramSpec) throws
  154. InvalidParameterSpecException
  155. {
  156. paramSpi.engineInit(paramSpec);
  157. }
  158. /**
  159. Initializes the engine with the specified
  160. parameters stored in the byte array and decodes them
  161. according to the ASN.1 specification. If the ASN.1
  162. specification exists then it succeeds or else it throws
  163. IOException.
  164. @param params Parameters to initialize with
  165. @throws IOException Decoding Error
  166. */
  167. public final void init(byte[]params) throws IOException
  168. {
  169. paramSpi.engineInit(params);
  170. }
  171. /**
  172. Initializes the engine with the specified
  173. parameters stored in the byte array and decodes them
  174. according to the specified decoding specification.
  175. If format is null, then it is decoded using the ASN.1
  176. specification if it exists or else it throws
  177. IOException.
  178. @param params Parameters to initialize with
  179. @param format Name of decoding format to use
  180. @throws IOException Decoding Error
  181. */
  182. public final void init(byte[]params, String format) throws IOException
  183. {
  184. paramSpi.engineInit(params, format);
  185. }
  186. /**
  187. Returns a specification of this AlgorithmParameters object.
  188. paramSpec identifies the class to return the AlgortihmParameters
  189. in.
  190. @param paramSpec Class to return AlgorithmParameters in
  191. @return the parameter specification
  192. @throws InvalidParameterSpecException if the paramSpec is an invalid parameter class
  193. */
  194. public final AlgorithmParameterSpec getParameterSpec(Class paramSpec) throws
  195. InvalidParameterSpecException
  196. {
  197. return paramSpi.engineGetParameterSpec(paramSpec);
  198. }
  199. /**
  200. Returns the parameters in the default encoding format.
  201. The primary encoding format is ASN.1 format if it exists
  202. for the specified type.
  203. @return byte array representing the parameters
  204. */
  205. public final byte[] getEncoded() throws IOException
  206. {
  207. return paramSpi.engineGetEncoded();
  208. }
  209. /**
  210. Returns the parameters in the specified encoding format.
  211. If <code>format</code> is <code>null</code> then the
  212. primary encoding format is used, the ASN.1 format,
  213. if it exists for the specified type.
  214. @return byte array representing the parameters
  215. */
  216. public final byte[] getEncoded(String format) throws IOException
  217. {
  218. return paramSpi.engineGetEncoded(format);
  219. }
  220. /**
  221. Returns a string representation of the encoding format
  222. @return a string containing the string representation
  223. */
  224. public final String toString()
  225. {
  226. return paramSpi.engineToString();
  227. }
  228. }