/JDK1.7-Java SE Development Kit 7u80/src/java/security/AlgorithmParameters.java

https://github.com/zxiaofan/JDK · Java · 398 lines · 108 code · 21 blank · 269 comment · 17 complexity · 9c85acd26b84e8eb6f661148264f5240 MD5 · raw file

  1. /*
  2. * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
  3. * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  4. *
  5. *
  6. *
  7. *
  8. *
  9. *
  10. *
  11. *
  12. *
  13. *
  14. *
  15. *
  16. *
  17. *
  18. *
  19. *
  20. *
  21. *
  22. *
  23. *
  24. */
  25. package java.security;
  26. import java.io.*;
  27. import java.security.spec.AlgorithmParameterSpec;
  28. import java.security.spec.InvalidParameterSpecException;
  29. /**
  30. * This class is used as an opaque representation of cryptographic parameters.
  31. *
  32. * <p>An <code>AlgorithmParameters</code> object for managing the parameters
  33. * for a particular algorithm can be obtained by
  34. * calling one of the <code>getInstance</code> factory methods
  35. * (static methods that return instances of a given class).
  36. *
  37. * <p>Once an <code>AlgorithmParameters</code> object is obtained, it must be
  38. * initialized via a call to <code>init</code>, using an appropriate parameter
  39. * specification or parameter encoding.
  40. *
  41. * <p>A transparent parameter specification is obtained from an
  42. * <code>AlgorithmParameters</code> object via a call to
  43. * <code>getParameterSpec</code>, and a byte encoding of the parameters is
  44. * obtained via a call to <code>getEncoded</code>.
  45. *
  46. * <p> Every implementation of the Java platform is required to support the
  47. * following standard <code>AlgorithmParameters</code> algorithms:
  48. * <ul>
  49. * <li><tt>AES</tt></li>
  50. * <li><tt>DES</tt></li>
  51. * <li><tt>DESede</tt></li>
  52. * <li><tt>DiffieHellman</tt></li>
  53. * <li><tt>DSA</tt></li>
  54. * </ul>
  55. * These algorithms are described in the <a href=
  56. * "{@docRoot}/../technotes/guides/security/StandardNames.html#AlgorithmParameters">
  57. * AlgorithmParameters section</a> of the
  58. * Java Cryptography Architecture Standard Algorithm Name Documentation.
  59. * Consult the release documentation for your implementation to see if any
  60. * other algorithms are supported.
  61. *
  62. * @author Jan Luehe
  63. *
  64. *
  65. * @see java.security.spec.AlgorithmParameterSpec
  66. * @see java.security.spec.DSAParameterSpec
  67. * @see KeyPairGenerator
  68. *
  69. * @since 1.2
  70. */
  71. public class AlgorithmParameters {
  72. // The provider
  73. private Provider provider;
  74. // The provider implementation (delegate)
  75. private AlgorithmParametersSpi paramSpi;
  76. // The algorithm
  77. private String algorithm;
  78. // Has this object been initialized?
  79. private boolean initialized = false;
  80. /**
  81. * Creates an AlgorithmParameters object.
  82. *
  83. * @param paramSpi the delegate
  84. * @param provider the provider
  85. * @param algorithm the algorithm
  86. */
  87. protected AlgorithmParameters(AlgorithmParametersSpi paramSpi,
  88. Provider provider, String algorithm)
  89. {
  90. this.paramSpi = paramSpi;
  91. this.provider = provider;
  92. this.algorithm = algorithm;
  93. }
  94. /**
  95. * Returns the name of the algorithm associated with this parameter object.
  96. *
  97. * @return the algorithm name.
  98. */
  99. public final String getAlgorithm() {
  100. return this.algorithm;
  101. }
  102. /**
  103. * Returns a parameter object for the specified algorithm.
  104. *
  105. * <p> This method traverses the list of registered security Providers,
  106. * starting with the most preferred Provider.
  107. * A new AlgorithmParameters object encapsulating the
  108. * AlgorithmParametersSpi implementation from the first
  109. * Provider that supports the specified algorithm is returned.
  110. *
  111. * <p> Note that the list of registered providers may be retrieved via
  112. * the {@link Security#getProviders() Security.getProviders()} method.
  113. *
  114. * <p> The returned parameter object must be initialized via a call to
  115. * <code>init</code>, using an appropriate parameter specification or
  116. * parameter encoding.
  117. *
  118. * @param algorithm the name of the algorithm requested.
  119. * See the AlgorithmParameters section in the <a href=
  120. * "{@docRoot}/../technotes/guides/security/StandardNames.html#AlgorithmParameters">
  121. * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
  122. * for information about standard algorithm names.
  123. *
  124. * @return the new parameter object.
  125. *
  126. * @exception NoSuchAlgorithmException if no Provider supports an
  127. * AlgorithmParametersSpi implementation for the
  128. * specified algorithm.
  129. *
  130. * @see Provider
  131. */
  132. public static AlgorithmParameters getInstance(String algorithm)
  133. throws NoSuchAlgorithmException {
  134. try {
  135. Object[] objs = Security.getImpl(algorithm, "AlgorithmParameters",
  136. (String)null);
  137. return new AlgorithmParameters((AlgorithmParametersSpi)objs[0],
  138. (Provider)objs[1],
  139. algorithm);
  140. } catch(NoSuchProviderException e) {
  141. throw new NoSuchAlgorithmException(algorithm + " not found");
  142. }
  143. }
  144. /**
  145. * Returns a parameter object for the specified algorithm.
  146. *
  147. * <p> A new AlgorithmParameters object encapsulating the
  148. * AlgorithmParametersSpi implementation from the specified provider
  149. * is returned. The specified provider must be registered
  150. * in the security provider list.
  151. *
  152. * <p> Note that the list of registered providers may be retrieved via
  153. * the {@link Security#getProviders() Security.getProviders()} method.
  154. *
  155. * <p>The returned parameter object must be initialized via a call to
  156. * <code>init</code>, using an appropriate parameter specification or
  157. * parameter encoding.
  158. *
  159. * @param algorithm the name of the algorithm requested.
  160. * See the AlgorithmParameters section in the <a href=
  161. * "{@docRoot}/../technotes/guides/security/StandardNames.html#AlgorithmParameters">
  162. * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
  163. * for information about standard algorithm names.
  164. *
  165. * @param provider the name of the provider.
  166. *
  167. * @return the new parameter object.
  168. *
  169. * @exception NoSuchAlgorithmException if an AlgorithmParametersSpi
  170. * implementation for the specified algorithm is not
  171. * available from the specified provider.
  172. *
  173. * @exception NoSuchProviderException if the specified provider is not
  174. * registered in the security provider list.
  175. *
  176. * @exception IllegalArgumentException if the provider name is null
  177. * or empty.
  178. *
  179. * @see Provider
  180. */
  181. public static AlgorithmParameters getInstance(String algorithm,
  182. String provider)
  183. throws NoSuchAlgorithmException, NoSuchProviderException
  184. {
  185. if (provider == null || provider.length() == 0)
  186. throw new IllegalArgumentException("missing provider");
  187. Object[] objs = Security.getImpl(algorithm, "AlgorithmParameters",
  188. provider);
  189. return new AlgorithmParameters((AlgorithmParametersSpi)objs[0],
  190. (Provider)objs[1],
  191. algorithm);
  192. }
  193. /**
  194. * Returns a parameter object for the specified algorithm.
  195. *
  196. * <p> A new AlgorithmParameters object encapsulating the
  197. * AlgorithmParametersSpi implementation from the specified Provider
  198. * object is returned. Note that the specified Provider object
  199. * does not have to be registered in the provider list.
  200. *
  201. * <p>The returned parameter object must be initialized via a call to
  202. * <code>init</code>, using an appropriate parameter specification or
  203. * parameter encoding.
  204. *
  205. * @param algorithm the name of the algorithm requested.
  206. * See the AlgorithmParameters section in the <a href=
  207. * "{@docRoot}/../technotes/guides/security/StandardNames.html#AlgorithmParameters">
  208. * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
  209. * for information about standard algorithm names.
  210. *
  211. * @param provider the name of the provider.
  212. *
  213. * @return the new parameter object.
  214. *
  215. * @exception NoSuchAlgorithmException if an AlgorithmParameterGeneratorSpi
  216. * implementation for the specified algorithm is not available
  217. * from the specified Provider object.
  218. *
  219. * @exception IllegalArgumentException if the provider is null.
  220. *
  221. * @see Provider
  222. *
  223. * @since 1.4
  224. */
  225. public static AlgorithmParameters getInstance(String algorithm,
  226. Provider provider)
  227. throws NoSuchAlgorithmException
  228. {
  229. if (provider == null)
  230. throw new IllegalArgumentException("missing provider");
  231. Object[] objs = Security.getImpl(algorithm, "AlgorithmParameters",
  232. provider);
  233. return new AlgorithmParameters((AlgorithmParametersSpi)objs[0],
  234. (Provider)objs[1],
  235. algorithm);
  236. }
  237. /**
  238. * Returns the provider of this parameter object.
  239. *
  240. * @return the provider of this parameter object
  241. */
  242. public final Provider getProvider() {
  243. return this.provider;
  244. }
  245. /**
  246. * Initializes this parameter object using the parameters
  247. * specified in <code>paramSpec</code>.
  248. *
  249. * @param paramSpec the parameter specification.
  250. *
  251. * @exception InvalidParameterSpecException if the given parameter
  252. * specification is inappropriate for the initialization of this parameter
  253. * object, or if this parameter object has already been initialized.
  254. */
  255. public final void init(AlgorithmParameterSpec paramSpec)
  256. throws InvalidParameterSpecException
  257. {
  258. if (this.initialized)
  259. throw new InvalidParameterSpecException("already initialized");
  260. paramSpi.engineInit(paramSpec);
  261. this.initialized = true;
  262. }
  263. /**
  264. * Imports the specified parameters and decodes them according to the
  265. * primary decoding format for parameters. The primary decoding
  266. * format for parameters is ASN.1, if an ASN.1 specification for this type
  267. * of parameters exists.
  268. *
  269. * @param params the encoded parameters.
  270. *
  271. * @exception IOException on decoding errors, or if this parameter object
  272. * has already been initialized.
  273. */
  274. public final void init(byte[] params) throws IOException {
  275. if (this.initialized)
  276. throw new IOException("already initialized");
  277. paramSpi.engineInit(params);
  278. this.initialized = true;
  279. }
  280. /**
  281. * Imports the parameters from <code>params</code> and decodes them
  282. * according to the specified decoding scheme.
  283. * If <code>format</code> is null, the
  284. * primary decoding format for parameters is used. The primary decoding
  285. * format is ASN.1, if an ASN.1 specification for these parameters
  286. * exists.
  287. *
  288. * @param params the encoded parameters.
  289. *
  290. * @param format the name of the decoding scheme.
  291. *
  292. * @exception IOException on decoding errors, or if this parameter object
  293. * has already been initialized.
  294. */
  295. public final void init(byte[] params, String format) throws IOException {
  296. if (this.initialized)
  297. throw new IOException("already initialized");
  298. paramSpi.engineInit(params, format);
  299. this.initialized = true;
  300. }
  301. /**
  302. * Returns a (transparent) specification of this parameter object.
  303. * <code>paramSpec</code> identifies the specification class in which
  304. * the parameters should be returned. It could, for example, be
  305. * <code>DSAParameterSpec.class</code>, to indicate that the
  306. * parameters should be returned in an instance of the
  307. * <code>DSAParameterSpec</code> class.
  308. *
  309. * @param paramSpec the specification class in which
  310. * the parameters should be returned.
  311. *
  312. * @return the parameter specification.
  313. *
  314. * @exception InvalidParameterSpecException if the requested parameter
  315. * specification is inappropriate for this parameter object, or if this
  316. * parameter object has not been initialized.
  317. */
  318. public final <T extends AlgorithmParameterSpec>
  319. T getParameterSpec(Class<T> paramSpec)
  320. throws InvalidParameterSpecException
  321. {
  322. if (this.initialized == false) {
  323. throw new InvalidParameterSpecException("not initialized");
  324. }
  325. return paramSpi.engineGetParameterSpec(paramSpec);
  326. }
  327. /**
  328. * Returns the parameters in their primary encoding format.
  329. * The primary encoding format for parameters is ASN.1, if an ASN.1
  330. * specification for this type of parameters exists.
  331. *
  332. * @return the parameters encoded using their primary encoding format.
  333. *
  334. * @exception IOException on encoding errors, or if this parameter object
  335. * has not been initialized.
  336. */
  337. public final byte[] getEncoded() throws IOException
  338. {
  339. if (this.initialized == false) {
  340. throw new IOException("not initialized");
  341. }
  342. return paramSpi.engineGetEncoded();
  343. }
  344. /**
  345. * Returns the parameters encoded in the specified scheme.
  346. * If <code>format</code> is null, the
  347. * primary encoding format for parameters is used. The primary encoding
  348. * format is ASN.1, if an ASN.1 specification for these parameters
  349. * exists.
  350. *
  351. * @param format the name of the encoding format.
  352. *
  353. * @return the parameters encoded using the specified encoding scheme.
  354. *
  355. * @exception IOException on encoding errors, or if this parameter object
  356. * has not been initialized.
  357. */
  358. public final byte[] getEncoded(String format) throws IOException
  359. {
  360. if (this.initialized == false) {
  361. throw new IOException("not initialized");
  362. }
  363. return paramSpi.engineGetEncoded(format);
  364. }
  365. /**
  366. * Returns a formatted string describing the parameters.
  367. *
  368. * @return a formatted string describing the parameters, or null if this
  369. * parameter object has not been initialized.
  370. */
  371. public final String toString() {
  372. if (this.initialized == false) {
  373. return null;
  374. }
  375. return paramSpi.engineToString();
  376. }
  377. }