You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

GenerateKey.java 11 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package org.apache.tools.ant.taskdefs;
  19. import java.util.Enumeration;
  20. import java.util.Vector;
  21. import org.apache.tools.ant.BuildException;
  22. import org.apache.tools.ant.Task;
  23. import org.apache.tools.ant.types.Commandline;
  24. import org.apache.tools.ant.util.JavaEnvUtils;
  25. /**
  26. * Generates a key in a keystore.
  27. *
  28. * @since Ant 1.2
  29. *
  30. * @ant.task name="genkey" category="java"
  31. */
  32. public class GenerateKey extends Task {
  33. /**
  34. * A DistinguishedName parameter.
  35. * This is a nested element in a dname nested element.
  36. */
  37. public static class DnameParam {
  38. private String name;
  39. private String value;
  40. /**
  41. * Set the name attribute.
  42. * @param name a <code>String</code> value
  43. */
  44. public void setName(String name) {
  45. this.name = name;
  46. }
  47. /**
  48. * Get the name attribute.
  49. * @return the name.
  50. */
  51. public String getName() {
  52. return name;
  53. }
  54. /**
  55. * Set the value attribute.
  56. * @param value a <code>String</code> value
  57. */
  58. public void setValue(String value) {
  59. this.value = value;
  60. }
  61. /**
  62. * Get the value attribute.
  63. * @return the value.
  64. */
  65. public String getValue() {
  66. return value;
  67. }
  68. }
  69. /**
  70. * A class corresponding to the dname nested element.
  71. */
  72. public static class DistinguishedName {
  73. private Vector<DnameParam> params = new Vector<DnameParam>();
  74. /**
  75. * Create a param nested element.
  76. * @return a DnameParam object to be configured.
  77. */
  78. public Object createParam() {
  79. DnameParam param = new DnameParam();
  80. params.addElement(param);
  81. return param;
  82. }
  83. /**
  84. * Get the nested parameters.
  85. * @return an enumeration of the nested parameters.
  86. */
  87. public Enumeration<DnameParam> getParams() {
  88. return params.elements();
  89. }
  90. /**
  91. * Generate a string rep of this distinguished name.
  92. * The format is each of the parameters (name = value)
  93. * separated by ','.
  94. * This is used on the command line.
  95. * @return a string rep of this name
  96. */
  97. public String toString() {
  98. final int size = params.size();
  99. final StringBuffer sb = new StringBuffer();
  100. boolean firstPass = true;
  101. for (int i = 0; i < size; i++) {
  102. if (!firstPass) {
  103. sb.append(" ,");
  104. }
  105. firstPass = false;
  106. final DnameParam param = (DnameParam) params.elementAt(i);
  107. sb.append(encode(param.getName()));
  108. sb.append('=');
  109. sb.append(encode(param.getValue()));
  110. }
  111. return sb.toString();
  112. }
  113. /**
  114. * Encode a name or value.
  115. * The encoded result is the same as the input string
  116. * except that each ',' is replaced by a '\,'.
  117. * @param string the value to be encoded
  118. * @return the encoded value.
  119. */
  120. public String encode(final String string) {
  121. int end = string.indexOf(',');
  122. if (-1 == end) {
  123. return string;
  124. }
  125. final StringBuffer sb = new StringBuffer();
  126. int start = 0;
  127. while (-1 != end) {
  128. sb.append(string.substring(start, end));
  129. sb.append("\\,");
  130. start = end + 1;
  131. end = string.indexOf(',', start);
  132. }
  133. sb.append(string.substring(start));
  134. return sb.toString();
  135. }
  136. }
  137. // CheckStyle:VisibilityModifier OFF - bc
  138. /**
  139. * The alias of signer.
  140. */
  141. protected String alias;
  142. /**
  143. * The name of keystore file.
  144. */
  145. protected String keystore;
  146. protected String storepass;
  147. protected String storetype;
  148. protected String keypass;
  149. protected String sigalg;
  150. protected String keyalg;
  151. protected String dname;
  152. protected DistinguishedName expandedDname;
  153. protected int keysize;
  154. protected int validity;
  155. protected boolean verbose;
  156. // CheckStyle:VisibilityModifier ON
  157. /**
  158. * Distinguished name list.
  159. *
  160. * @return Distinguished name container.
  161. * @throws BuildException If specified more than once or dname
  162. * attribute is used.
  163. */
  164. public DistinguishedName createDname() throws BuildException {
  165. if (null != expandedDname) {
  166. throw new BuildException("DName sub-element can only be "
  167. + "specified once.");
  168. }
  169. if (null != dname) {
  170. throw new BuildException("It is not possible to specify dname "
  171. + " both as attribute and element.");
  172. }
  173. expandedDname = new DistinguishedName();
  174. return expandedDname;
  175. }
  176. /**
  177. * The distinguished name for entity.
  178. *
  179. * @param dname distinguished name
  180. */
  181. public void setDname(final String dname) {
  182. if (null != expandedDname) {
  183. throw new BuildException("It is not possible to specify dname "
  184. + " both as attribute and element.");
  185. }
  186. this.dname = dname;
  187. }
  188. /**
  189. * The alias to add under.
  190. *
  191. * @param alias alias to add under
  192. */
  193. public void setAlias(final String alias) {
  194. this.alias = alias;
  195. }
  196. /**
  197. * Keystore location.
  198. *
  199. * @param keystore location
  200. */
  201. public void setKeystore(final String keystore) {
  202. this.keystore = keystore;
  203. }
  204. /**
  205. * Password for keystore integrity.
  206. * Must be at least 6 characters long.
  207. * @param storepass password
  208. */
  209. public void setStorepass(final String storepass) {
  210. this.storepass = storepass;
  211. }
  212. /**
  213. * Keystore type.
  214. *
  215. * @param storetype type
  216. */
  217. public void setStoretype(final String storetype) {
  218. this.storetype = storetype;
  219. }
  220. /**
  221. * Password for private key (if different).
  222. *
  223. * @param keypass password
  224. */
  225. public void setKeypass(final String keypass) {
  226. this.keypass = keypass;
  227. }
  228. /**
  229. * The algorithm to use in signing.
  230. *
  231. * @param sigalg algorithm
  232. */
  233. public void setSigalg(final String sigalg) {
  234. this.sigalg = sigalg;
  235. }
  236. /**
  237. * The method to use when generating name-value pair.
  238. * @param keyalg algorithm
  239. */
  240. public void setKeyalg(final String keyalg) {
  241. this.keyalg = keyalg;
  242. }
  243. /**
  244. * Indicates the size of key generated.
  245. *
  246. * @param keysize size of key
  247. * @throws BuildException If not an Integer
  248. * @todo Could convert this to a plain Integer setter.
  249. */
  250. public void setKeysize(final String keysize) throws BuildException {
  251. try {
  252. this.keysize = Integer.parseInt(keysize);
  253. } catch (final NumberFormatException nfe) {
  254. throw new BuildException("KeySize attribute should be a integer");
  255. }
  256. }
  257. /**
  258. * Indicates how many days certificate is valid.
  259. *
  260. * @param validity days valid
  261. * @throws BuildException If not an Integer
  262. */
  263. public void setValidity(final String validity) throws BuildException {
  264. try {
  265. this.validity = Integer.parseInt(validity);
  266. } catch (final NumberFormatException nfe) {
  267. throw new BuildException("Validity attribute should be a integer");
  268. }
  269. }
  270. /**
  271. * If true, verbose output when signing.
  272. * @param verbose verbose or not
  273. */
  274. public void setVerbose(final boolean verbose) {
  275. this.verbose = verbose;
  276. }
  277. /**
  278. * Execute the task.
  279. * @throws BuildException on error
  280. */
  281. public void execute() throws BuildException {
  282. if (null == alias) {
  283. throw new BuildException("alias attribute must be set");
  284. }
  285. if (null == storepass) {
  286. throw new BuildException("storepass attribute must be set");
  287. }
  288. if (null == dname && null == expandedDname) {
  289. throw new BuildException("dname must be set");
  290. }
  291. final StringBuffer sb = new StringBuffer();
  292. sb.append("-genkey ");
  293. if (verbose) {
  294. sb.append("-v ");
  295. }
  296. sb.append("-alias \"");
  297. sb.append(alias);
  298. sb.append("\" ");
  299. if (null != dname) {
  300. sb.append("-dname \"");
  301. sb.append(dname);
  302. sb.append("\" ");
  303. }
  304. if (null != expandedDname) {
  305. sb.append("-dname \"");
  306. sb.append(expandedDname);
  307. sb.append("\" ");
  308. }
  309. if (null != keystore) {
  310. sb.append("-keystore \"");
  311. sb.append(keystore);
  312. sb.append("\" ");
  313. }
  314. if (null != storepass) {
  315. sb.append("-storepass \"");
  316. sb.append(storepass);
  317. sb.append("\" ");
  318. }
  319. if (null != storetype) {
  320. sb.append("-storetype \"");
  321. sb.append(storetype);
  322. sb.append("\" ");
  323. }
  324. sb.append("-keypass \"");
  325. if (null != keypass) {
  326. sb.append(keypass);
  327. } else {
  328. sb.append(storepass);
  329. }
  330. sb.append("\" ");
  331. if (null != sigalg) {
  332. sb.append("-sigalg \"");
  333. sb.append(sigalg);
  334. sb.append("\" ");
  335. }
  336. if (null != keyalg) {
  337. sb.append("-keyalg \"");
  338. sb.append(keyalg);
  339. sb.append("\" ");
  340. }
  341. if (0 < keysize) {
  342. sb.append("-keysize \"");
  343. sb.append(keysize);
  344. sb.append("\" ");
  345. }
  346. if (0 < validity) {
  347. sb.append("-validity \"");
  348. sb.append(validity);
  349. sb.append("\" ");
  350. }
  351. log("Generating Key for " + alias);
  352. final ExecTask cmd = new ExecTask(this);
  353. cmd.setExecutable(JavaEnvUtils.getJdkExecutable("keytool"));
  354. Commandline.Argument arg = cmd.createArg();
  355. arg.setLine(sb.toString());
  356. cmd.setFailonerror(true);
  357. cmd.setTaskName(getTaskName());
  358. cmd.execute();
  359. }
  360. }