1. /*
  2. * @(#)AlgorithmParameters.java 1.24 04/05/05
  3. *
  4. * Copyright 2004 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.24, 05/05/04
  50. *
  51. * @see java.security.spec.AlgorithmParameterSpec
  52. * @see java.security.spec.DSAParameterSpec
  53. * @see KeyPairGenerator
  54. *
  55. * @since 1.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 paramSpi 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. (String)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. * @exception IllegalArgumentException if the provider name is null
  143. * or empty.
  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. * Generates a parameter object for the specified algorithm, as supplied
  161. * by the specified provider, if such an algorithm is available from the
  162. * provider. Note: the <code>provider</code> doesn't have to be registered.
  163. *
  164. * <p>The returned parameter object must be initialized via a call to
  165. * <code>init</code>, using an appropriate parameter specification or
  166. * parameter encoding.
  167. *
  168. * @param algorithm the name of the algorithm requested.
  169. *
  170. * @param provider the name of the provider.
  171. *
  172. * @return the new parameter object.
  173. *
  174. * @exception NoSuchAlgorithmException if the algorithm is
  175. * not available in the package supplied by the requested
  176. * provider.
  177. *
  178. * @exception IllegalArgumentException if the <code>provider</code> is
  179. * null.
  180. *
  181. * @see Provider
  182. *
  183. * @since 1.4
  184. */
  185. public static AlgorithmParameters getInstance(String algorithm,
  186. Provider provider)
  187. throws NoSuchAlgorithmException
  188. {
  189. if (provider == null)
  190. throw new IllegalArgumentException("missing provider");
  191. Object[] objs = Security.getImpl(algorithm, "AlgorithmParameters",
  192. provider);
  193. return new AlgorithmParameters((AlgorithmParametersSpi)objs[0],
  194. (Provider)objs[1],
  195. algorithm);
  196. }
  197. /**
  198. * Returns the provider of this parameter object.
  199. *
  200. * @return the provider of this parameter object
  201. */
  202. public final Provider getProvider() {
  203. return this.provider;
  204. }
  205. /**
  206. * Initializes this parameter object using the parameters
  207. * specified in <code>paramSpec</code>.
  208. *
  209. * @param paramSpec the parameter specification.
  210. *
  211. * @exception InvalidParameterSpecException if the given parameter
  212. * specification is inappropriate for the initialization of this parameter
  213. * object, or if this parameter object has already been initialized.
  214. */
  215. public final void init(AlgorithmParameterSpec paramSpec)
  216. throws InvalidParameterSpecException
  217. {
  218. if (this.initialized)
  219. throw new InvalidParameterSpecException("already initialized");
  220. paramSpi.engineInit(paramSpec);
  221. this.initialized = true;
  222. }
  223. /**
  224. * Imports the specified parameters and decodes them according to the
  225. * primary decoding format for parameters. The primary decoding
  226. * format for parameters is ASN.1, if an ASN.1 specification for this type
  227. * of parameters exists.
  228. *
  229. * @param params the encoded parameters.
  230. *
  231. * @exception IOException on decoding errors, or if this parameter object
  232. * has already been initialized.
  233. */
  234. public final void init(byte[] params) throws IOException {
  235. if (this.initialized)
  236. throw new IOException("already initialized");
  237. paramSpi.engineInit(params);
  238. this.initialized = true;
  239. }
  240. /**
  241. * Imports the parameters from <code>params</code> and decodes them
  242. * according to the specified decoding scheme.
  243. * If <code>format</code> is null, the
  244. * primary decoding format for parameters is used. The primary decoding
  245. * format is ASN.1, if an ASN.1 specification for these parameters
  246. * exists.
  247. *
  248. * @param params the encoded parameters.
  249. *
  250. * @param format the name of the decoding scheme.
  251. *
  252. * @exception IOException on decoding errors, or if this parameter object
  253. * has already been initialized.
  254. */
  255. public final void init(byte[] params, String format) throws IOException {
  256. if (this.initialized)
  257. throw new IOException("already initialized");
  258. paramSpi.engineInit(params, format);
  259. this.initialized = true;
  260. }
  261. /**
  262. * Returns a (transparent) specification of this parameter object.
  263. * <code>paramSpec</code> identifies the specification class in which
  264. * the parameters should be returned. It could, for example, be
  265. * <code>DSAParameterSpec.class</code>, to indicate that the
  266. * parameters should be returned in an instance of the
  267. * <code>DSAParameterSpec</code> class.
  268. *
  269. * @param paramSpec the specification class in which
  270. * the parameters should be returned.
  271. *
  272. * @return the parameter specification.
  273. *
  274. * @exception InvalidParameterSpecException if the requested parameter
  275. * specification is inappropriate for this parameter object, or if this
  276. * parameter object has not been initialized.
  277. */
  278. public final <T extends AlgorithmParameterSpec>
  279. T getParameterSpec(Class<T> paramSpec)
  280. throws InvalidParameterSpecException
  281. {
  282. if (this.initialized == false) {
  283. throw new InvalidParameterSpecException("not initialized");
  284. }
  285. return paramSpi.engineGetParameterSpec(paramSpec);
  286. }
  287. /**
  288. * Returns the parameters in their primary encoding format.
  289. * The primary encoding format for parameters is ASN.1, if an ASN.1
  290. * specification for this type of parameters exists.
  291. *
  292. * @return the parameters encoded using their primary encoding format.
  293. *
  294. * @exception IOException on encoding errors, or if this parameter object
  295. * has not been initialized.
  296. */
  297. public final byte[] getEncoded() throws IOException
  298. {
  299. if (this.initialized == false) {
  300. throw new IOException("not initialized");
  301. }
  302. return paramSpi.engineGetEncoded();
  303. }
  304. /**
  305. * Returns the parameters encoded in the specified scheme.
  306. * If <code>format</code> is null, the
  307. * primary encoding format for parameters is used. The primary encoding
  308. * format is ASN.1, if an ASN.1 specification for these parameters
  309. * exists.
  310. *
  311. * @param format the name of the encoding format.
  312. *
  313. * @return the parameters encoded using the specified encoding scheme.
  314. *
  315. * @exception IOException on encoding errors, or if this parameter object
  316. * has not been initialized.
  317. */
  318. public final byte[] getEncoded(String format) throws IOException
  319. {
  320. if (this.initialized == false) {
  321. throw new IOException("not initialized");
  322. }
  323. return paramSpi.engineGetEncoded(format);
  324. }
  325. /**
  326. * Returns a formatted string describing the parameters.
  327. *
  328. * @return a formatted string describing the parameters, or null if this
  329. * parameter object has not been initialized.
  330. */
  331. public final String toString() {
  332. if (this.initialized == false) {
  333. return null;
  334. }
  335. return paramSpi.engineToString();
  336. }
  337. }