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 12 kB

11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. public boolean isComplete() {
  69. return name != null && value != null;
  70. }
  71. }
  72. /**
  73. * A class corresponding to the dname nested element.
  74. */
  75. public static class DistinguishedName {
  76. private Vector<DnameParam> params = new Vector<DnameParam>();
  77. /**
  78. * Create a param nested element.
  79. * @return a DnameParam object to be configured.
  80. */
  81. public Object createParam() {
  82. DnameParam param = new DnameParam();
  83. params.addElement(param);
  84. return param;
  85. }
  86. /**
  87. * Get the nested parameters.
  88. * @return an enumeration of the nested parameters.
  89. */
  90. public Enumeration<DnameParam> getParams() {
  91. return params.elements();
  92. }
  93. /**
  94. * Generate a string rep of this distinguished name.
  95. * The format is each of the parameters (name = value)
  96. * separated by ','.
  97. * This is used on the command line.
  98. * @return a string rep of this name
  99. */
  100. public String toString() {
  101. final int size = params.size();
  102. final StringBuffer sb = new StringBuffer();
  103. boolean firstPass = true;
  104. for (int i = 0; i < size; i++) {
  105. if (!firstPass) {
  106. sb.append(" ,");
  107. }
  108. firstPass = false;
  109. final DnameParam param = (DnameParam) params.elementAt(i);
  110. if (param.isComplete()) {
  111. sb.append(encode(param.getName()));
  112. sb.append('=');
  113. sb.append(encode(param.getValue()));
  114. }
  115. }
  116. return sb.toString();
  117. }
  118. /**
  119. * Encode a name or value.
  120. * The encoded result is the same as the input string
  121. * except that each ',' is replaced by a '\,'.
  122. * @param string the value to be encoded
  123. * @return the encoded value.
  124. */
  125. public String encode(final String string) {
  126. int end = string.indexOf(',');
  127. if (-1 == end) {
  128. return string;
  129. }
  130. final StringBuffer sb = new StringBuffer();
  131. int start = 0;
  132. while (-1 != end) {
  133. sb.append(string.substring(start, end));
  134. sb.append("\\,");
  135. start = end + 1;
  136. end = string.indexOf(',', start);
  137. }
  138. sb.append(string.substring(start));
  139. return sb.toString();
  140. }
  141. }
  142. // CheckStyle:VisibilityModifier OFF - bc
  143. /**
  144. * The alias of signer.
  145. */
  146. protected String alias;
  147. /**
  148. * The name of keystore file.
  149. */
  150. protected String keystore;
  151. protected String storepass;
  152. protected String storetype;
  153. protected String keypass;
  154. protected String sigalg;
  155. protected String keyalg;
  156. protected String saname;
  157. protected String dname;
  158. protected DistinguishedName expandedDname;
  159. protected int keysize;
  160. protected int validity;
  161. protected boolean verbose;
  162. // CheckStyle:VisibilityModifier ON
  163. /**
  164. * Distinguished name list.
  165. *
  166. * @return Distinguished name container.
  167. * @throws BuildException If specified more than once or dname
  168. * attribute is used.
  169. */
  170. public DistinguishedName createDname() throws BuildException {
  171. if (null != expandedDname) {
  172. throw new BuildException("DName sub-element can only be "
  173. + "specified once.");
  174. }
  175. if (null != dname) {
  176. throw new BuildException("It is not possible to specify dname "
  177. + " both as attribute and element.");
  178. }
  179. expandedDname = new DistinguishedName();
  180. return expandedDname;
  181. }
  182. /**
  183. * The distinguished name for entity.
  184. *
  185. * @param dname distinguished name
  186. */
  187. public void setDname(final String dname) {
  188. if (null != expandedDname) {
  189. throw new BuildException("It is not possible to specify dname "
  190. + " both as attribute and element.");
  191. }
  192. this.dname = dname;
  193. }
  194. /**
  195. * The subject alternative name for entity.
  196. *
  197. * @param saname subject alternative name
  198. * @since Ant 1.9.14
  199. */
  200. public void setSaname(final String saname) {
  201. if (JavaEnvUtils.isAtLeastJavaVersion(JavaEnvUtils.JAVA_1_7)) {
  202. this.saname = saname;
  203. } else {
  204. log("The SubjectAlternativeName extension is not available for "
  205. +"the Java Version being used.");
  206. }
  207. }
  208. /**
  209. * The alias to add under.
  210. *
  211. * @param alias alias to add under
  212. */
  213. public void setAlias(final String alias) {
  214. this.alias = alias;
  215. }
  216. /**
  217. * Keystore location.
  218. *
  219. * @param keystore location
  220. */
  221. public void setKeystore(final String keystore) {
  222. this.keystore = keystore;
  223. }
  224. /**
  225. * Password for keystore integrity.
  226. * Must be at least 6 characters long.
  227. * @param storepass password
  228. */
  229. public void setStorepass(final String storepass) {
  230. this.storepass = storepass;
  231. }
  232. /**
  233. * Keystore type.
  234. *
  235. * @param storetype type
  236. */
  237. public void setStoretype(final String storetype) {
  238. this.storetype = storetype;
  239. }
  240. /**
  241. * Password for private key (if different).
  242. *
  243. * @param keypass password
  244. */
  245. public void setKeypass(final String keypass) {
  246. this.keypass = keypass;
  247. }
  248. /**
  249. * The algorithm to use in signing.
  250. *
  251. * @param sigalg algorithm
  252. */
  253. public void setSigalg(final String sigalg) {
  254. this.sigalg = sigalg;
  255. }
  256. /**
  257. * The method to use when generating name-value pair.
  258. * @param keyalg algorithm
  259. */
  260. public void setKeyalg(final String keyalg) {
  261. this.keyalg = keyalg;
  262. }
  263. /**
  264. * Indicates the size of key generated.
  265. *
  266. * @param keysize size of key
  267. * @throws BuildException If not an Integer
  268. * @todo Could convert this to a plain Integer setter.
  269. */
  270. public void setKeysize(final String keysize) throws BuildException {
  271. try {
  272. this.keysize = Integer.parseInt(keysize);
  273. } catch (final NumberFormatException nfe) {
  274. throw new BuildException("KeySize attribute should be a integer");
  275. }
  276. }
  277. /**
  278. * Indicates how many days certificate is valid.
  279. *
  280. * @param validity days valid
  281. * @throws BuildException If not an Integer
  282. */
  283. public void setValidity(final String validity) throws BuildException {
  284. try {
  285. this.validity = Integer.parseInt(validity);
  286. } catch (final NumberFormatException nfe) {
  287. throw new BuildException("Validity attribute should be a integer");
  288. }
  289. }
  290. /**
  291. * If true, verbose output when signing.
  292. * @param verbose verbose or not
  293. */
  294. public void setVerbose(final boolean verbose) {
  295. this.verbose = verbose;
  296. }
  297. /**
  298. * Execute the task.
  299. * @throws BuildException on error
  300. */
  301. public void execute() throws BuildException {
  302. if (null == alias) {
  303. throw new BuildException("alias attribute must be set");
  304. }
  305. if (null == storepass) {
  306. throw new BuildException("storepass attribute must be set");
  307. }
  308. if (null == dname && null == expandedDname) {
  309. throw new BuildException("dname must be set");
  310. }
  311. final StringBuffer sb = new StringBuffer();
  312. sb.append("-genkey ");
  313. if (verbose) {
  314. sb.append("-v ");
  315. }
  316. sb.append("-alias \"");
  317. sb.append(alias);
  318. sb.append("\" ");
  319. if (null != dname) {
  320. sb.append("-dname \"");
  321. sb.append(dname);
  322. sb.append("\" ");
  323. }
  324. if (null != expandedDname) {
  325. sb.append("-dname \"");
  326. sb.append(expandedDname);
  327. sb.append("\" ");
  328. }
  329. if (null != keystore) {
  330. sb.append("-keystore \"");
  331. sb.append(keystore);
  332. sb.append("\" ");
  333. }
  334. if (null != storepass) {
  335. sb.append("-storepass \"");
  336. sb.append(storepass);
  337. sb.append("\" ");
  338. }
  339. if (null != storetype) {
  340. sb.append("-storetype \"");
  341. sb.append(storetype);
  342. sb.append("\" ");
  343. }
  344. sb.append("-keypass \"");
  345. if (null != keypass) {
  346. sb.append(keypass);
  347. } else {
  348. sb.append(storepass);
  349. }
  350. sb.append("\" ");
  351. if (null != sigalg) {
  352. sb.append("-sigalg \"");
  353. sb.append(sigalg);
  354. sb.append("\" ");
  355. }
  356. if (null != keyalg) {
  357. sb.append("-keyalg \"");
  358. sb.append(keyalg);
  359. sb.append("\" ");
  360. }
  361. if (0 < keysize) {
  362. sb.append("-keysize \"");
  363. sb.append(keysize);
  364. sb.append("\" ");
  365. }
  366. if (0 < validity) {
  367. sb.append("-validity \"");
  368. sb.append(validity);
  369. sb.append("\" ");
  370. }
  371. if (null != saname) {
  372. sb.append("-ext ");
  373. sb.append("\"san=");
  374. sb.append(saname);
  375. sb.append("\" ");
  376. }
  377. log("Generating Key for " + alias);
  378. final ExecTask cmd = new ExecTask(this);
  379. cmd.setExecutable(JavaEnvUtils.getJdkExecutable("keytool"));
  380. Commandline.Argument arg = cmd.createArg();
  381. arg.setLine(sb.toString());
  382. cmd.setFailonerror(true);
  383. cmd.setTaskName(getTaskName());
  384. cmd.execute();
  385. }
  386. }