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