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.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 org.apache.tools.ant.types.Commandline;
  57. import java.util.Enumeration;
  58. import java.util.Vector;
  59. /**
  60. * Generates a key.
  61. *
  62. * @author Peter Donald <a href="mailto:donaldp@mad.scientist.com">donaldp@mad.scientist.com</a>
  63. */
  64. public class GenerateKey extends Task {
  65. public static class DnameParam {
  66. private String name;
  67. private String value;
  68. public void setName(String name) {
  69. this.name = name;
  70. }
  71. public String getName() {
  72. return name;
  73. }
  74. public void setValue(String value) {
  75. this.value = value;
  76. }
  77. public String getValue() {
  78. return value;
  79. }
  80. }
  81. public static class DistinguishedName {
  82. private String name;
  83. private String path;
  84. private Vector params = new Vector();
  85. public Object createParam() {
  86. DnameParam param = new DnameParam();
  87. params.addElement(param);
  88. return param;
  89. }
  90. public Enumeration getParams() {
  91. return params.elements();
  92. }
  93. public String toString() {
  94. final int size = params.size();
  95. final StringBuffer sb = new StringBuffer();
  96. boolean firstPass = true;
  97. for( int i = 0; i < size; i++ ) {
  98. if( !firstPass ) {
  99. sb.append(" ,");
  100. }
  101. firstPass = false;
  102. final DnameParam param = (DnameParam)params.elementAt( i );
  103. sb.append( encode( param.getName() ) );
  104. sb.append( '=' );
  105. sb.append( encode( param.getValue() ) );
  106. }
  107. return sb.toString();
  108. }
  109. public String encode( final String string ) {
  110. int end = string.indexOf(',');
  111. if( -1 == end ) return string;
  112. final StringBuffer sb = new StringBuffer();
  113. int start = 0;
  114. while( -1 != end )
  115. {
  116. sb.append( string.substring( start, end ) );
  117. sb.append( "\\," );
  118. start = end + 1;
  119. end = string.indexOf( ',', start );
  120. }
  121. sb.append( string.substring( start ) );
  122. return sb.toString();
  123. }
  124. }
  125. /**
  126. * The alias of signer.
  127. */
  128. protected String alias;
  129. /**
  130. * The name of keystore file.
  131. */
  132. protected String keystore;
  133. protected String storepass;
  134. protected String storetype;
  135. protected String keypass;
  136. protected String sigalg;
  137. protected String keyalg;
  138. protected String dname;
  139. protected DistinguishedName expandedDname;
  140. protected int keysize;
  141. protected int validity;
  142. protected boolean verbose;
  143. public DistinguishedName createDname() throws BuildException {
  144. if( null != expandedDname ) {
  145. throw new BuildException("DName sub-element can only be specified once.");
  146. }
  147. if( null != dname ) {
  148. throw new BuildException("It is not possible to specify dname both as attribute and element.");
  149. }
  150. expandedDname = new DistinguishedName();
  151. return expandedDname;
  152. }
  153. public void setDname(final String dname) {
  154. if( null != expandedDname ) {
  155. throw new BuildException("It is not possible to specify dname both as attribute and element.");
  156. }
  157. this.dname = dname;
  158. }
  159. public void setAlias(final String alias) {
  160. this.alias = alias;
  161. }
  162. public void setKeystore(final String keystore) {
  163. this.keystore = keystore;
  164. }
  165. public void setStorepass(final String storepass) {
  166. this.storepass = storepass;
  167. }
  168. public void setStoretype(final String storetype) {
  169. this.storetype = storetype;
  170. }
  171. public void setKeypass(final String keypass) {
  172. this.keypass = keypass;
  173. }
  174. public void setSigalg(final String sigalg) {
  175. this.sigalg = sigalg;
  176. }
  177. public void setKeyalg(final String keyalg) {
  178. this.keyalg = keyalg;
  179. }
  180. public void setKeysize(final String keysize) throws BuildException {
  181. try { this.keysize = Integer.parseInt(keysize); }
  182. catch(final NumberFormatException nfe)
  183. {
  184. throw new BuildException( "KeySize attribute should be a integer" );
  185. }
  186. }
  187. public void setValidity(final String validity) throws BuildException {
  188. try { this.validity = Integer.parseInt(validity); }
  189. catch(final NumberFormatException nfe)
  190. {
  191. throw new BuildException( "Validity attribute should be a integer" );
  192. }
  193. }
  194. public void setVerbose(final String verbose) {
  195. this.verbose = project.toBoolean(verbose);
  196. }
  197. public void execute() throws BuildException {
  198. if (project.getJavaVersion().equals(Project.JAVA_1_1)) {
  199. throw new BuildException("The genkey task is only available on JDK versions 1.2 or greater");
  200. }
  201. if (null == alias) {
  202. throw new BuildException("alias attribute must be set");
  203. }
  204. if (null == storepass) {
  205. throw new BuildException("storepass attribute must be set");
  206. }
  207. if (null == dname && null == expandedDname) {
  208. throw new BuildException("dname must be set");
  209. }
  210. final StringBuffer sb = new StringBuffer();
  211. sb.append("keytool -genkey ");
  212. if (verbose) {
  213. sb.append("-v ");
  214. }
  215. sb.append("-alias \"");
  216. sb.append(alias);
  217. sb.append("\" ");
  218. if (null != dname) {
  219. sb.append("-dname \"");
  220. sb.append(dname);
  221. sb.append("\" ");
  222. }
  223. if (null != expandedDname) {
  224. sb.append("-dname \"");
  225. sb.append(expandedDname);
  226. sb.append("\" ");
  227. }
  228. if (null != keystore) {
  229. sb.append("-keystore \"");
  230. sb.append(keystore);
  231. sb.append("\" ");
  232. }
  233. if (null != storepass) {
  234. sb.append("-storepass \"");
  235. sb.append(storepass);
  236. sb.append("\" ");
  237. }
  238. if (null != storetype) {
  239. sb.append("-storetype \"");
  240. sb.append(storetype);
  241. sb.append("\" ");
  242. }
  243. sb.append("-keypass \"");
  244. if (null != keypass) {
  245. sb.append(keypass);
  246. }
  247. else {
  248. sb.append(storepass);
  249. }
  250. sb.append("\" ");
  251. if (null != sigalg) {
  252. sb.append("-sigalg \"");
  253. sb.append(sigalg);
  254. sb.append("\" ");
  255. }
  256. if (null != keyalg) {
  257. sb.append("-keyalg \"");
  258. sb.append(keyalg);
  259. sb.append("\" ");
  260. }
  261. if (0 < keysize) {
  262. sb.append("-keysize \"");
  263. sb.append(keysize);
  264. sb.append("\" ");
  265. }
  266. if (0 < validity) {
  267. sb.append("-validity \"");
  268. sb.append(validity);
  269. sb.append("\" ");
  270. }
  271. log("Generating Key for " + alias );
  272. final ExecTask cmd = (ExecTask) project.createTask("exec");
  273. cmd.setCommand(new Commandline(sb.toString()));
  274. cmd.setFailonerror(true);
  275. cmd.setTaskName( getTaskName() );
  276. cmd.execute();
  277. }
  278. }