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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 1999 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowlegement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowlegement may appear in the software itself,
  24. * if and wherever such third-party acknowlegements normally appear.
  25. *
  26. * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Group.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.apache.tools.ant.taskdefs;
  55. import org.apache.tools.ant.*;
  56. import java.util.Enumeration;
  57. import java.util.Vector;
  58. /**
  59. * Generates a key.
  60. *
  61. * @author Peter Donald <a href="mailto:donaldp@mad.scientist.com">donaldp@mad.scientist.com</a>
  62. */
  63. public class GenerateKey extends Task {
  64. public static class DnameParam {
  65. private String name;
  66. private String value;
  67. public void setName(String name) {
  68. this.name = name;
  69. }
  70. public String getName() {
  71. return name;
  72. }
  73. public void setValue(String value) {
  74. this.value = value;
  75. }
  76. public String getValue() {
  77. return value;
  78. }
  79. }
  80. public static class DistinguishedName {
  81. private String name;
  82. private String path;
  83. private Vector params = new Vector();
  84. public Object createParam() {
  85. DnameParam param = new DnameParam();
  86. params.addElement(param);
  87. return param;
  88. }
  89. public Enumeration getParams() {
  90. return params.elements();
  91. }
  92. public String toString() {
  93. final int size = params.size();
  94. final StringBuffer sb = new StringBuffer();
  95. boolean firstPass = true;
  96. for( int i = 0; i < size; i++ ) {
  97. if( !firstPass ) {
  98. sb.append(" ,");
  99. }
  100. firstPass = false;
  101. final DnameParam param = (DnameParam)params.elementAt( i );
  102. sb.append( encode( param.getName() ) );
  103. sb.append( '=' );
  104. sb.append( encode( param.getValue() ) );
  105. }
  106. return sb.toString();
  107. }
  108. public String encode( final String string ) {
  109. int end = string.indexOf(',');
  110. if( -1 == end ) return string;
  111. final StringBuffer sb = new StringBuffer();
  112. int start = 0;
  113. while( -1 != end )
  114. {
  115. sb.append( string.substring( start, end ) );
  116. sb.append( "\\," );
  117. start = end + 1;
  118. end = string.indexOf( ',', start );
  119. }
  120. sb.append( string.substring( start ) );
  121. return sb.toString();
  122. }
  123. }
  124. /**
  125. * The alias of signer.
  126. */
  127. protected String alias;
  128. /**
  129. * The name of keystore file.
  130. */
  131. protected String keystore;
  132. protected String storepass;
  133. protected String storetype;
  134. protected String keypass;
  135. protected String sigalg;
  136. protected String keyalg;
  137. protected String dname;
  138. protected DistinguishedName expandedDname;
  139. protected int keysize;
  140. protected int validity;
  141. protected boolean verbose;
  142. public DistinguishedName createDname() throws BuildException {
  143. if( null != expandedDname ) {
  144. throw new BuildException("DName sub-element can only be specified once.");
  145. }
  146. if( null != dname ) {
  147. throw new BuildException("It is not possible to specify dname both as attribute and element.");
  148. }
  149. expandedDname = new DistinguishedName();
  150. return expandedDname;
  151. }
  152. public void setDname(final String dname) {
  153. if( null != expandedDname ) {
  154. throw new BuildException("It is not possible to specify dname both as attribute and element.");
  155. }
  156. this.dname = dname;
  157. }
  158. public void setAlias(final String alias) {
  159. this.alias = alias;
  160. }
  161. public void setKeystore(final String keystore) {
  162. this.keystore = keystore;
  163. }
  164. public void setStorepass(final String storepass) {
  165. this.storepass = storepass;
  166. }
  167. public void setStoretype(final String storetype) {
  168. this.storetype = storetype;
  169. }
  170. public void setKeypass(final String keypass) {
  171. this.keypass = keypass;
  172. }
  173. public void setSigalg(final String sigalg) {
  174. this.sigalg = sigalg;
  175. }
  176. public void setKeyalg(final String keyalg) {
  177. this.keyalg = keyalg;
  178. }
  179. public void setKeysize(final String keysize) throws BuildException {
  180. try { this.keysize = Integer.parseInt(keysize); }
  181. catch(final NumberFormatException nfe)
  182. {
  183. throw new BuildException( "KeySize attribute should be a integer" );
  184. }
  185. }
  186. public void setValidity(final String validity) throws BuildException {
  187. try { this.validity = Integer.parseInt(validity); }
  188. catch(final NumberFormatException nfe)
  189. {
  190. throw new BuildException( "Validity attribute should be a integer" );
  191. }
  192. }
  193. public void setVerbose(final String verbose) {
  194. this.verbose = project.toBoolean(verbose);
  195. }
  196. public void execute() throws BuildException {
  197. if (project.getJavaVersion().equals(Project.JAVA_1_1)) {
  198. throw new BuildException("The genkey task is only available on JDK versions 1.2 or greater");
  199. }
  200. if (null == alias) {
  201. throw new BuildException("alias attribute must be set");
  202. }
  203. if (null == storepass) {
  204. throw new BuildException("storepass attribute must be set");
  205. }
  206. if (null == dname && null == expandedDname) {
  207. throw new BuildException("dname must be set");
  208. }
  209. final StringBuffer sb = new StringBuffer();
  210. sb.append("keytool -genkey ");
  211. if (verbose) {
  212. sb.append("-v ");
  213. }
  214. sb.append("-alias \"");
  215. sb.append(alias);
  216. sb.append("\" ");
  217. if (null != dname) {
  218. sb.append("-dname \"");
  219. sb.append(dname);
  220. sb.append("\" ");
  221. }
  222. if (null != expandedDname) {
  223. sb.append("-dname \"");
  224. sb.append(expandedDname);
  225. sb.append("\" ");
  226. }
  227. if (null != keystore) {
  228. sb.append("-keystore \"");
  229. sb.append(keystore);
  230. sb.append("\" ");
  231. }
  232. if (null != storepass) {
  233. sb.append("-storepass \"");
  234. sb.append(storepass);
  235. sb.append("\" ");
  236. }
  237. if (null != storetype) {
  238. sb.append("-storetype \"");
  239. sb.append(storetype);
  240. sb.append("\" ");
  241. }
  242. sb.append("-keypass \"");
  243. if (null != keypass) {
  244. sb.append(keypass);
  245. }
  246. else {
  247. sb.append(storepass);
  248. }
  249. sb.append("\" ");
  250. if (null != sigalg) {
  251. sb.append("-sigalg \"");
  252. sb.append(sigalg);
  253. sb.append("\" ");
  254. }
  255. if (null != keyalg) {
  256. sb.append("-keyalg \"");
  257. sb.append(keyalg);
  258. sb.append("\" ");
  259. }
  260. if (0 < keysize) {
  261. sb.append("-keysize \"");
  262. sb.append(keysize);
  263. sb.append("\" ");
  264. }
  265. if (0 < validity) {
  266. sb.append("-validity \"");
  267. sb.append(validity);
  268. sb.append("\" ");
  269. }
  270. log("Generating Key for " + alias );
  271. final Exec cmd = (Exec) project.createTask("exec");
  272. cmd.setCommand(sb.toString());
  273. cmd.setFailonerror("true");
  274. cmd.setTaskName( getTaskName() );
  275. cmd.execute();
  276. }
  277. }