1 package org.codehaus.mojo.keytool;
2
3 /*
4 * Copyright 2005-2013 The Codehaus
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License" );
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 import org.apache.maven.plugin.MojoExecutionException;
20 import org.apache.maven.plugins.annotations.Mojo;
21 import org.apache.maven.plugins.annotations.Parameter;
22 import org.apache.maven.shared.utils.cli.Commandline;
23 import org.codehaus.mojo.keytool.requests.KeyToolGenerateKeyPairRequest;
24 import org.codehaus.plexus.util.StringUtils;
25
26 import java.io.File;
27
28 /**
29 * To generate a key pair into a keystore.
30 * <p/>
31 * <p/>
32 * Implemented as a wrapper around the SDK {@code keytool -genkey} (jdk 1.5) {@code keytool -genkeypair} (jdk 1.6)
33 * command.
34 * <p/>
35 * See <a href="http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/keytool.html">keystore documentation</a>.
36 *
37 * @author tchemit <chemit@codelutin.com>
38 * @since 1.2
39 */
40 @Mojo(name = "generateKeyPair", requiresProject = true)
41 public class GenerateKeyPairMojo
42 extends AbstractKeyToolRequestWithKeyStoreAndAliasParametersMojo<KeyToolGenerateKeyPairRequest>
43 {
44
45 /**
46 * Key algorithm name.
47 * <p/>
48 * See <a href="http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/keytool.html#Commands">options</a>.
49 *
50 * @since 1.2
51 */
52 @Parameter
53 private String keyalg;
54
55 /**
56 * Key bit size.
57 * <p/>
58 * See <a href="http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/keytool.html#Commands">options</a>.
59 *
60 * @since 1.2
61 */
62 @Parameter
63 private String keysize;
64
65 /**
66 * Key password.
67 * <p/>
68 * See <a href="http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/keytool.html#Commands">options</a>.
69 *
70 * @since 1.2
71 */
72 @Parameter
73 private String keypass;
74
75 /**
76 * Signature algorithm name.
77 * <p/>
78 * See <a href="http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/keytool.html#Commands">options</a>.
79 *
80 * @since 1.2
81 */
82 @Parameter
83 private String sigalg;
84
85 /**
86 * Validity number of days.
87 * <p/>
88 * See <a href="http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/keytool.html#Commands">options</a>.
89 *
90 * @since 1.2
91 */
92 @Parameter
93 private String validity;
94
95 /**
96 * Certificate validity start date/time.
97 * <p/>
98 * See <a href="http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/keytool.html#Commands">options</a>.
99 *
100 * @since 1.2
101 */
102 @Parameter
103 private String startdate;
104
105 /**
106 * X.509 extension.
107 * <p/>
108 * See <a href="http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/keytool.html#Commands">options</a>.
109 *
110 * @since 1.2
111 */
112 @Parameter
113 private String ext;
114
115 /**
116 * Distinguished name.
117 * <p/>
118 * See <a href="http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/keytool.html#Commands">options</a>.
119 *
120 * @since 1.2
121 */
122 @Parameter
123 private String dname;
124
125 /**
126 * If value is {@code true}, then will do nothing if keystore already exists.
127 *
128 * @since 1.3
129 */
130 @Parameter
131 private boolean skipIfExist;
132
133 /**
134 * Default contructor.
135 */
136 public GenerateKeyPairMojo()
137 {
138 super( KeyToolGenerateKeyPairRequest.class );
139 }
140
141 @Override
142 public void execute()
143 throws MojoExecutionException
144 {
145
146 if ( skipIfExist )
147 {
148
149 // check if keystore already exist
150 File keystoreFile = getKeystoreFile();
151 boolean keystoreFileExists = keystoreFile.exists();
152
153 if ( keystoreFileExists )
154 {
155 getLog().info( "Skip execution, keystore already exists at " + keystoreFile );
156 setSkip( true );
157 }
158
159 }
160 super.execute();
161 }
162
163 /**
164 * {@inheritDoc}
165 */
166 @Override
167 protected KeyToolGenerateKeyPairRequest createKeytoolRequest()
168 {
169 KeyToolGenerateKeyPairRequest request = super.createKeytoolRequest();
170
171 request.setKeyalg( this.keyalg );
172 request.setKeysize( this.keysize );
173 request.setKeypass( this.keypass );
174 request.setSigalg( this.sigalg );
175 request.setDname( this.dname );
176 request.setStartdate( this.startdate );
177 request.setExt( this.ext );
178 request.setValidity( this.validity );
179 return request;
180 }
181
182 /**
183 * {@inheritDoc}
184 */
185 @Override
186 protected String getCommandlineInfo( Commandline commandLine )
187 {
188 String commandLineInfo = super.getCommandlineInfo( commandLine );
189
190 commandLineInfo = StringUtils.replace( commandLineInfo, this.keypass, "'*****'" );
191
192 return commandLineInfo;
193 }
194 }