1. /*
  2. * @(#)AlgorithmParameters.java 1.19 00/02/02
  3. *
  4. * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package java.security;
  11. import java.io.*;
  12. import java.security.spec.AlgorithmParameterSpec;
  13. import java.security.spec.InvalidParameterSpecException;
  14. /**
  15. * This class is used as an opaque representation of cryptographic parameters.
  16. *
  17. * <p>An <code>AlgorithmParameters</code> object for managing the parameters
  18. * for a particular algorithm can be obtained by
  19. * calling one of the <code>getInstance</code> factory methods
  20. * (static methods that return instances of a given class).
  21. *
  22. * <p>There are two ways to request such an implementation: by
  23. * specifying either just an algorithm name, or both an algorithm name
  24. * and a package provider.
  25. *
  26. * <ul>
  27. *
  28. * <li>If just an algorithm name is specified, the system will
  29. * determine if there is an AlgorithmParameters
  30. * implementation for the algorithm requested
  31. * available in the environment, and if there is more than one, if
  32. * there is a preferred one.
  33. *
  34. * <li>If both an algorithm name and a package provider are specified,
  35. * the system will determine if there is an implementation
  36. * in the package requested, and throw an exception if there
  37. * is not.
  38. *
  39. * </ul>
  40. *
  41. * <p>Once an <code>AlgorithmParameters</code> object is returned, it must be
  42. * initialized via a call to <code>init</code>, using an appropriate parameter
  43. * specification or parameter encoding.
  44. *
  45. * <p>A transparent parameter specification is obtained from an
  46. * <code>AlgorithmParameters</code> object via a call to
  47. * <code>getParameterSpec</code>, and a byte encoding of the parameters is
  48. * obtained via a call to <code>getEncoded</code>.
  49. *
  50. * @author Jan Luehe
  51. *
  52. * @version 1.19, 02/02/00
  53. *
  54. * @see java.security.spec.AlgorithmParameterSpec
  55. * @see java.security.spec.DSAParameterSpec
  56. * @see KeyPairGenerator
  57. *
  58. * @since 1.2
  59. */
  60. public class AlgorithmParameters {
  61. // The provider
  62. private Provider provider;
  63. // The provider implementation (delegate)
  64. private AlgorithmParametersSpi paramSpi;
  65. // The algorithm
  66. private String algorithm;
  67. // Has this object been initialized?
  68. private boolean initialized = false;
  69. /**
  70. * Creates an AlgorithmParameters object.
  71. *
  72. * @param paramSpi the delegate
  73. * @param provider the provider
  74. * @param algorithm the algorithm
  75. */
  76. protected AlgorithmParameters(AlgorithmParametersSpi paramSpi,
  77. Provider provider, String algorithm)
  78. {
  79. this.paramSpi = paramSpi;
  80. this.provider = provider;
  81. this.algorithm = algorithm;
  82. }
  83. /**
  84. * Returns the name of the algorithm associated with this parameter object.
  85. *
  86. * @return the algorithm name.
  87. */
  88. public final String getAlgorithm() {
  89. return this.algorithm;
  90. }
  91. /**
  92. * Generates a parameter object for the specified algorithm.
  93. *
  94. * <p>If the default provider package provides an implementation of the
  95. * requested algorithm, an instance of AlgorithmParameters containing that
  96. * implementation is returned.
  97. * If the algorithm is not available in the default
  98. * package, other packages are searched.
  99. *
  100. * <p>The returned parameter object must be initialized via a call to
  101. * <code>init</code>, using an appropriate parameter specification or
  102. * parameter encoding.
  103. *
  104. * @param algorithm the name of the algorithm requested.
  105. *
  106. * @return the new parameter object.
  107. *
  108. * @exception NoSuchAlgorithmException if the algorithm is
  109. * not available in the environment.
  110. */
  111. public static AlgorithmParameters getInstance(String algorithm)
  112. throws NoSuchAlgorithmException {
  113. try {
  114. Object[] objs = Security.getImpl(algorithm, "AlgorithmParameters",
  115. null);
  116. return new AlgorithmParameters((AlgorithmParametersSpi)objs[0],
  117. (Provider)objs[1],
  118. algorithm);
  119. } catch(NoSuchProviderException e) {
  120. throw new NoSuchAlgorithmException(algorithm + " not found");
  121. }
  122. }
  123. /**
  124. * Generates a parameter object for the specified algorithm, as supplied
  125. * by the specified provider, if such an algorithm is available from the
  126. * provider.
  127. *
  128. * <p>The returned parameter object must be initialized via a call to
  129. * <code>init</code>, using an appropriate parameter specification or
  130. * parameter encoding.
  131. *
  132. * @param algorithm the name of the algorithm requested.
  133. *
  134. * @param provider the name of the provider.
  135. *
  136. * @return the new parameter object.
  137. *
  138. * @exception NoSuchAlgorithmException if the algorithm is
  139. * not available in the package supplied by the requested
  140. * provider.
  141. *
  142. * @exception NoSuchProviderException if the provider is not
  143. * available in the environment.
  144. *
  145. * @see Provider
  146. */
  147. public static AlgorithmParameters getInstance(String algorithm,
  148. String provider)
  149. throws NoSuchAlgorithmException, NoSuchProviderException
  150. {
  151. if (provider == null || provider.length() == 0)
  152. throw new IllegalArgumentException("missing provider");
  153. Object[] objs = Security.getImpl(algorithm, "AlgorithmParameters",
  154. provider);
  155. return new AlgorithmParameters((AlgorithmParametersSpi)objs[0],
  156. (Provider)objs[1],
  157. algorithm);
  158. }
  159. /**
  160. * Returns the provider of this parameter object.
  161. *
  162. * @return the provider of this parameter object
  163. */
  164. public final Provider getProvider() {
  165. return this.provider;
  166. }
  167. /**
  168. * Initializes this parameter object using the parameters
  169. * specified in <code>paramSpec</code>.
  170. *
  171. * @param paramSpec the parameter specification.
  172. *
  173. * @exception InvalidParameterSpecException if the given parameter
  174. * specification is inappropriate for the initialization of this parameter
  175. * object, or if this parameter object has already been initialized.
  176. */
  177. public final void init(AlgorithmParameterSpec paramSpec)
  178. throws InvalidParameterSpecException
  179. {
  180. if (this.initialized)
  181. throw new InvalidParameterSpecException("already initialized");
  182. paramSpi.engineInit(paramSpec);
  183. this.initialized = true;
  184. }
  185. /**
  186. * Imports the specified parameters and decodes them according to the
  187. * primary decoding format for parameters. The primary decoding
  188. * format for parameters is ASN.1, if an ASN.1 specification for this type
  189. * of parameters exists.
  190. *
  191. * @param params the encoded parameters.
  192. *
  193. * @exception IOException on decoding errors, or if this parameter object
  194. * has already been initialized.
  195. */
  196. public final void init(byte[] params) throws IOException {
  197. if (this.initialized)
  198. throw new IOException("already initialized");
  199. paramSpi.engineInit(params);
  200. this.initialized = true;
  201. }
  202. /**
  203. * Imports the parameters from <code>params</code> and decodes them
  204. * according to the specified decoding scheme.
  205. * If <code>format</code> is null, the
  206. * primary decoding format for parameters is used. The primary decoding
  207. * format is ASN.1, if an ASN.1 specification for these parameters
  208. * exists.
  209. *
  210. * @param params the encoded parameters.
  211. *
  212. * @param format the name of the decoding scheme.
  213. *
  214. * @exception IOException on decoding errors, or if this parameter object
  215. * has already been initialized.
  216. */
  217. public final void init(byte[] params, String format) throws IOException {
  218. if (this.initialized)
  219. throw new IOException("already initialized");
  220. paramSpi.engineInit(params, format);
  221. this.initialized = true;
  222. }
  223. /**
  224. * Returns a (transparent) specification of this parameter object.
  225. * <code>paramSpec</code> identifies the specification class in which
  226. * the parameters should be returned. It could, for example, be
  227. * <code>DSAParameterSpec.class</code>, to indicate that the
  228. * parameters should be returned in an instance of the
  229. * <code>DSAParameterSpec</code> class.
  230. *
  231. * @param paramSpec the specification class in which
  232. * the parameters should be returned.
  233. *
  234. * @return the parameter specification.
  235. *
  236. * @exception InvalidParameterSpecException if the requested parameter
  237. * specification is inappropriate for this parameter object, or if this
  238. * parameter object has not been initialized.
  239. */
  240. public final AlgorithmParameterSpec getParameterSpec(Class paramSpec)
  241. throws InvalidParameterSpecException
  242. {
  243. if (this.initialized == false) {
  244. throw new InvalidParameterSpecException("not initialized");
  245. }
  246. return paramSpi.engineGetParameterSpec(paramSpec);
  247. }
  248. /**
  249. * Returns the parameters in their primary encoding format.
  250. * The primary encoding format for parameters is ASN.1, if an ASN.1
  251. * specification for this type of parameters exists.
  252. *
  253. * @return the parameters encoded using their primary encoding format.
  254. *
  255. * @exception IOException on encoding errors, or if this parameter object
  256. * has not been initialized.
  257. */
  258. public final byte[] getEncoded() throws IOException
  259. {
  260. if (this.initialized == false) {
  261. throw new IOException("not initialized");
  262. }
  263. return paramSpi.engineGetEncoded();
  264. }
  265. /**
  266. * Returns the parameters encoded in the specified scheme.
  267. * If <code>format</code> is null, the
  268. * primary encoding format for parameters is used. The primary encoding
  269. * format is ASN.1, if an ASN.1 specification for these parameters
  270. * exists.
  271. *
  272. * @param format the name of the encoding format.
  273. *
  274. * @return the parameters encoded using the specified encoding scheme.
  275. *
  276. * @exception IOException on encoding errors, or if this parameter object
  277. * has not been initialized.
  278. */
  279. public final byte[] getEncoded(String format) throws IOException
  280. {
  281. if (this.initialized == false) {
  282. throw new IOException("not initialized");
  283. }
  284. return paramSpi.engineGetEncoded(format);
  285. }
  286. /**
  287. * Returns a formatted string describing the parameters.
  288. *
  289. * @return a formatted string describing the parameters, or null if this
  290. * parameter object has not been initialized.
  291. */
  292. public final String toString() {
  293. if (this.initialized == false) {
  294. return null;
  295. }
  296. return paramSpi.engineToString();
  297. }
  298. }