KeyPairGenerator.java 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /* KeyPairGenerator.java --- Key Pair Generator Class
  2. Copyright (C) 1999, 2002, 2003, 2004, 2005 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., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 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 gnu.java.lang.CPStringBuilder;
  33. import gnu.java.security.Engine;
  34. import java.lang.reflect.InvocationTargetException;
  35. import java.security.spec.AlgorithmParameterSpec;
  36. /**
  37. * <code>KeyPairGenerator</code> is a class used to generate key-pairs for a
  38. * security algorithm.
  39. *
  40. * <p>The <code>KeyPairGenerator</code> is created with the
  41. * <code>getInstance()</code> Factory methods. It is used to generate a pair of
  42. * public and private keys for a specific algorithm and associate this key-pair
  43. * with the algorithm parameters it was initialized with.</p>
  44. *
  45. * @see KeyPair
  46. * @see AlgorithmParameterSpec
  47. * @author Mark Benvenuto
  48. * @author Casey Marshall
  49. */
  50. public abstract class KeyPairGenerator extends KeyPairGeneratorSpi
  51. {
  52. /** The service name for key pair generators. */
  53. private static final String KEY_PAIR_GENERATOR = "KeyPairGenerator";
  54. Provider provider;
  55. private String algorithm;
  56. /**
  57. * Constructs a new instance of <code>KeyPairGenerator</code>.
  58. *
  59. * @param algorithm
  60. * the algorithm to use.
  61. */
  62. protected KeyPairGenerator(String algorithm)
  63. {
  64. this.algorithm = algorithm;
  65. this.provider = null;
  66. }
  67. /**
  68. * Returns the name of the algorithm used.
  69. *
  70. * @return the name of the algorithm used.
  71. */
  72. public String getAlgorithm()
  73. {
  74. return algorithm;
  75. }
  76. /**
  77. * Returns a new instance of <code>KeyPairGenerator</code> which generates
  78. * key-pairs for the specified algorithm.
  79. *
  80. * @param algorithm the name of the algorithm to use.
  81. * @return a new instance repesenting the desired algorithm.
  82. * @throws NoSuchAlgorithmException if the algorithm is not implemented by any
  83. * provider.
  84. * @throws IllegalArgumentException if <code>algorithm</code> is
  85. * <code>null</code> or is an empty string.
  86. */
  87. public static KeyPairGenerator getInstance(String algorithm)
  88. throws NoSuchAlgorithmException
  89. {
  90. Provider[] p = Security.getProviders();
  91. NoSuchAlgorithmException lastException = null;
  92. for (int i = 0; i < p.length; i++)
  93. try
  94. {
  95. return getInstance(algorithm, p[i]);
  96. }
  97. catch (NoSuchAlgorithmException x)
  98. {
  99. lastException = x;
  100. }
  101. if (lastException != null)
  102. throw lastException;
  103. throw new NoSuchAlgorithmException(algorithm);
  104. }
  105. /**
  106. * Returns a new instance of <code>KeyPairGenerator</code> which generates
  107. * key-pairs for the specified algorithm from a named provider.
  108. *
  109. * @param algorithm the name of the algorithm to use.
  110. * @param provider the name of a {@link Provider} to use.
  111. * @return a new instance repesenting the desired algorithm.
  112. * @throws NoSuchAlgorithmException if the algorithm is not implemented by the
  113. * named provider.
  114. * @throws NoSuchProviderException if the named provider was not found.
  115. * @throws IllegalArgumentException if either <code>algorithm</code> or
  116. * <code>provider</code> is <code>null</code> or empty.
  117. */
  118. public static KeyPairGenerator getInstance(String algorithm, String provider)
  119. throws NoSuchAlgorithmException, NoSuchProviderException
  120. {
  121. if (provider == null)
  122. throw new IllegalArgumentException("provider MUST NOT be null");
  123. provider = provider.trim();
  124. if (provider.length() == 0)
  125. throw new IllegalArgumentException("provider MUST NOT be empty");
  126. Provider p = Security.getProvider(provider);
  127. if (p == null)
  128. throw new NoSuchProviderException(provider);
  129. return getInstance(algorithm, p);
  130. }
  131. /**
  132. * Returns a new instance of <code>KeyPairGenerator</code> which generates
  133. * key-pairs for the specified algorithm from a designated {@link Provider}.
  134. *
  135. * @param algorithm
  136. * the name of the algorithm to use.
  137. * @param provider
  138. * the {@link Provider} to use.
  139. * @return a new insatnce repesenting the desired algorithm.
  140. * @throws NoSuchAlgorithmException
  141. * if the algorithm is not implemented by the {@link Provider}.
  142. * @throws IllegalArgumentException if either <code>algorithm</code> or
  143. * <code>provider</code> is <code>null</code>, or if
  144. * <code>algorithm</code> is an empty string.
  145. * @since 1.4
  146. * @see Provider
  147. */
  148. public static KeyPairGenerator getInstance(String algorithm,
  149. Provider provider)
  150. throws NoSuchAlgorithmException
  151. {
  152. CPStringBuilder sb = new CPStringBuilder("KeyPairGenerator for algorithm [")
  153. .append(algorithm).append("] from provider[")
  154. .append(provider).append("] ");
  155. Object o;
  156. try
  157. {
  158. o = Engine.getInstance(KEY_PAIR_GENERATOR, algorithm, provider);
  159. }
  160. catch (InvocationTargetException x)
  161. {
  162. Throwable cause = x.getCause();
  163. if (cause instanceof NoSuchAlgorithmException)
  164. throw (NoSuchAlgorithmException) cause;
  165. if (cause == null)
  166. cause = x;
  167. sb.append("could not be created");
  168. NoSuchAlgorithmException y = new NoSuchAlgorithmException(sb.toString());
  169. y.initCause(cause);
  170. throw y;
  171. }
  172. KeyPairGenerator result;
  173. if (o instanceof KeyPairGenerator)
  174. {
  175. result = (KeyPairGenerator) o;
  176. result.algorithm = algorithm;
  177. }
  178. else if (o instanceof KeyPairGeneratorSpi)
  179. result = new DummyKeyPairGenerator((KeyPairGeneratorSpi) o, algorithm);
  180. else
  181. {
  182. sb.append("is of an unexpected Type: ").append(o.getClass().getName());
  183. throw new NoSuchAlgorithmException(sb.toString());
  184. }
  185. result.provider = provider;
  186. return result;
  187. }
  188. /**
  189. * Returns the {@link Provider} of this instance.
  190. *
  191. * @return the {@link Provider} of this instance.
  192. */
  193. public final Provider getProvider()
  194. {
  195. return provider;
  196. }
  197. /**
  198. * Initializes this instance for the specified key size. Since no source of
  199. * randomness is specified, a default one will be used.
  200. *
  201. * @param keysize
  202. * the size of keys to use.
  203. */
  204. public void initialize(int keysize)
  205. {
  206. initialize(keysize, new SecureRandom());
  207. }
  208. /**
  209. * Initializes this instance for the specified key size and
  210. * {@link SecureRandom}.
  211. *
  212. * @param keysize
  213. * the size of keys to use.
  214. * @param random
  215. * the {@link SecureRandom} to use.
  216. * @since 1.2
  217. */
  218. public void initialize(int keysize, SecureRandom random)
  219. {
  220. }
  221. /**
  222. * Initializes this instance with the specified
  223. * {@link AlgorithmParameterSpec}. Since no source of randomness is specified,
  224. * a default one will be used.
  225. *
  226. * @param params
  227. * the {@link AlgorithmParameterSpec} to use.
  228. * @throws InvalidAlgorithmParameterException
  229. * if the designated specifications are invalid.
  230. * @since 1.2
  231. */
  232. public void initialize(AlgorithmParameterSpec params)
  233. throws InvalidAlgorithmParameterException
  234. {
  235. initialize(params, new SecureRandom());
  236. }
  237. /**
  238. * Initializes this instance with the specified {@link AlgorithmParameterSpec}
  239. * and {@link SecureRandom}.
  240. *
  241. * @param params
  242. * the {@link AlgorithmParameterSpec} to use.
  243. * @param random
  244. * the {@link SecureRandom} to use.
  245. * @throws InvalidAlgorithmParameterException
  246. * if the designated specifications are invalid.
  247. * @since 1.2
  248. */
  249. public void initialize(AlgorithmParameterSpec params, SecureRandom random)
  250. throws InvalidAlgorithmParameterException
  251. {
  252. super.initialize(params, random);
  253. }
  254. /**
  255. * Generates a new "DSA" {@link KeyPair} from the "GNU" security provider.
  256. *
  257. * <p>This method generates a unique key-pair each time it is called.</p>
  258. *
  259. * @return a new unique {@link KeyPair}.
  260. * @see #generateKeyPair()
  261. * @since 1.2
  262. */
  263. public final KeyPair genKeyPair()
  264. {
  265. try
  266. {
  267. return getInstance("DSA", "GNU").generateKeyPair();
  268. }
  269. catch (Exception e)
  270. {
  271. System.err.println("genKeyPair failed: " + e);
  272. e.printStackTrace();
  273. return null;
  274. }
  275. }
  276. /**
  277. * Generates a new "DSA" {@link KeyPair} from the "GNU" security provider.
  278. *
  279. * <p>This method generates a unique key pair each time it is called.</p>
  280. *
  281. * @return a new unique {@link KeyPair}.
  282. * @see #genKeyPair()
  283. */
  284. public KeyPair generateKeyPair()
  285. {
  286. return genKeyPair();
  287. }
  288. }