1
|
|
/*
|
2
|
|
* The Apache Software License, Version 1.1
|
3
|
|
*
|
4
|
|
* Copyright (c) 2000-2003 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 "Ant" 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
|
|
|
56
|
|
import java.io.File;
|
57
|
|
import java.io.IOException;
|
58
|
|
import java.util.Enumeration;
|
59
|
|
import java.util.Vector;
|
60
|
|
import java.util.zip.ZipEntry;
|
61
|
|
import java.util.zip.ZipFile;
|
62
|
|
import org.apache.tools.ant.BuildException;
|
63
|
|
import org.apache.tools.ant.DirectoryScanner;
|
64
|
|
import org.apache.tools.ant.Task;
|
65
|
|
import org.apache.tools.ant.types.FileSet;
|
66
|
|
import org.apache.tools.ant.util.JavaEnvUtils;
|
67
|
|
|
68
|
|
/**
|
69
|
|
* Signs jar or zip files with the javasign command line tool. The
|
70
|
|
* tool detailed dependency checking: files are only signed if they
|
71
|
|
* are not signed. The <tt>signjar</tt> attribute can point to the file to
|
72
|
|
* generate; if this file exists then
|
73
|
|
* its modification date is used as a cue as to whether to resign any JAR file.
|
74
|
|
* <br>
|
75
|
|
* <strong>Note:</strong> Requires Java 1.2 or later. </p>
|
76
|
|
|
77
|
|
*
|
78
|
|
* @author Peter Donald
|
79
|
|
* <a href="mailto:donaldp@apache.org">donaldp@apache.org</a>
|
80
|
|
* @author Nick Fortescue
|
81
|
|
* <a href="mailto:nick@ox.compsoc.net">nick@ox.compsoc.net</a>
|
82
|
|
* @since Ant 1.1
|
83
|
|
* @ant.task category="java"
|
84
|
|
*/
|
85
|
|
public class SignJar extends Task {
|
86
|
|
|
87
|
|
/**
|
88
|
|
* The name of the jar file.
|
89
|
|
*/
|
90
|
|
protected File jar;
|
91
|
|
|
92
|
|
/**
|
93
|
|
* The alias of signer.
|
94
|
|
*/
|
95
|
|
protected String alias;
|
96
|
|
|
97
|
|
/**
|
98
|
|
* The name of keystore file.
|
99
|
|
*/
|
100
|
|
private String keystore;
|
101
|
|
|
102
|
|
protected String storepass;
|
103
|
|
protected String storetype;
|
104
|
|
protected String keypass;
|
105
|
|
protected String sigfile;
|
106
|
|
protected File signedjar;
|
107
|
|
protected boolean verbose;
|
108
|
|
protected boolean internalsf;
|
109
|
|
protected boolean sectionsonly;
|
110
|
|
|
111
|
|
/** The maximum amount of memory to use for Jar signer */
|
112
|
|
private String maxMemory;
|
113
|
|
|
114
|
|
/**
|
115
|
|
* the filesets of the jars to sign
|
116
|
|
*/
|
117
|
|
protected Vector filesets = new Vector();
|
118
|
|
|
119
|
|
/**
|
120
|
|
* Whether to assume a jar which has an appropriate .SF file in is already
|
121
|
|
* signed.
|
122
|
|
*/
|
123
|
|
protected boolean lazy;
|
124
|
|
|
125
|
|
|
126
|
|
/**
|
127
|
|
* Set the maximum memory to be used by the jarsigner process
|
128
|
|
*
|
129
|
|
* @param max a string indicating the maximum memory according to the
|
130
|
|
* JVM conventions (e.g. 128m is 128 Megabytes)
|
131
|
|
*/
|
132
|
3
|
public void setMaxmemory(String max) {
|
133
|
3
|
maxMemory = max;
|
134
|
|
}
|
135
|
|
|
136
|
|
/**
|
137
|
|
* the jar file to sign; required
|
138
|
|
*/
|
139
|
5
|
public void setJar(final File jar) {
|
140
|
5
|
this.jar = jar;
|
141
|
|
}
|
142
|
|
|
143
|
|
/**
|
144
|
|
* the alias to sign under; required
|
145
|
|
*/
|
146
|
5
|
public void setAlias(final String alias) {
|
147
|
5
|
this.alias = alias;
|
148
|
|
}
|
149
|
|
|
150
|
|
/**
|
151
|
|
* keystore location; required
|
152
|
|
*/
|
153
|
5
|
public void setKeystore(final String keystore) {
|
154
|
5
|
this.keystore = keystore;
|
155
|
|
}
|
156
|
|
|
157
|
|
/**
|
158
|
|
* password for keystore integrity; required
|
159
|
|
*/
|
160
|
5
|
public void setStorepass(final String storepass) {
|
161
|
5
|
this.storepass = storepass;
|
162
|
|
}
|
163
|
|
|
164
|
|
/**
|
165
|
|
* keystore type; optional
|
166
|
|
*/
|
167
|
0
|
public void setStoretype(final String storetype) {
|
168
|
0
|
this.storetype = storetype;
|
169
|
|
}
|
170
|
|
|
171
|
|
/**
|
172
|
|
* password for private key (if different); optional
|
173
|
|
*/
|
174
|
0
|
public void setKeypass(final String keypass) {
|
175
|
0
|
this.keypass = keypass;
|
176
|
|
}
|
177
|
|
|
178
|
|
/**
|
179
|
|
* name of .SF/.DSA file; optional
|
180
|
|
*/
|
181
|
1
|
public void setSigfile(final String sigfile) {
|
182
|
1
|
this.sigfile = sigfile;
|
183
|
|
}
|
184
|
|
|
185
|
|
/**
|
186
|
|
* name of signed JAR file; optional
|
187
|
|
*/
|
188
|
0
|
public void setSignedjar(final File signedjar) {
|
189
|
0
|
this.signedjar = signedjar;
|
190
|
|
}
|
191
|
|
|
192
|
|
/**
|
193
|
|
* Enable verbose output when signing
|
194
|
|
* ; optional: default false
|
195
|
|
*/
|
196
|
0
|
public void setVerbose(final boolean verbose) {
|
197
|
0
|
this.verbose = verbose;
|
198
|
|
}
|
199
|
|
|
200
|
|
/**
|
201
|
|
* Flag to include the .SF file inside the signature;
|
202
|
|
* optional; default false
|
203
|
|
*/
|
204
|
0
|
public void setInternalsf(final boolean internalsf) {
|
205
|
0
|
this.internalsf = internalsf;
|
206
|
|
}
|
207
|
|
|
208
|
|
/**
|
209
|
|
* flag to compute hash of entire manifest;
|
210
|
|
* optional, default false
|
211
|
|
*/
|
212
|
0
|
public void setSectionsonly(final boolean sectionsonly) {
|
213
|
0
|
this.sectionsonly = sectionsonly;
|
214
|
|
}
|
215
|
|
|
216
|
|
/**
|
217
|
|
* flag to control whether the presence of a signature
|
218
|
|
* file means a JAR is signed;
|
219
|
|
* optional, default false
|
220
|
|
*/
|
221
|
0
|
public void setLazy(final boolean lazy) {
|
222
|
0
|
this.lazy = lazy;
|
223
|
|
}
|
224
|
|
|
225
|
|
/**
|
226
|
|
* Adds a set of files to sign
|
227
|
|
* @since Ant 1.4
|
228
|
|
*/
|
229
|
0
|
public void addFileset(final FileSet set) {
|
230
|
0
|
filesets.addElement(set);
|
231
|
|
}
|
232
|
|
|
233
|
|
|
234
|
|
/**
|
235
|
|
* sign the jar(s)
|
236
|
|
*/
|
237
|
5
|
public void execute() throws BuildException {
|
238
|
5
|
if (null == jar && null == filesets) {
|
239
|
0
|
throw new BuildException("jar must be set through jar attribute "
|
240
|
|
+ "or nested filesets");
|
241
|
|
}
|
242
|
5
|
if (null != jar) {
|
243
|
5
|
doOneJar(jar, signedjar);
|
244
|
5
|
return;
|
245
|
|
} else {
|
246
|
|
//Assume null != filesets
|
247
|
|
|
248
|
|
// deal with the filesets
|
249
|
0
|
for (int i = 0; i < filesets.size(); i++) {
|
250
|
0
|
FileSet fs = (FileSet) filesets.elementAt(i);
|
251
|
0
|
DirectoryScanner ds = fs.getDirectoryScanner(getProject());
|
252
|
0
|
String[] jarFiles = ds.getIncludedFiles();
|
253
|
0
|
for (int j = 0; j < jarFiles.length; j++) {
|
254
|
0
|
doOneJar(new File(fs.getDir(getProject()), jarFiles[j]), null);
|
255
|
|
}
|
256
|
|
}
|
257
|
|
}
|
258
|
|
}
|
259
|
|
|
260
|
|
/**
|
261
|
|
* sign one jar
|
262
|
|
*/
|
263
|
5
|
private void doOneJar(File jarSource, File jarTarget)
|
264
|
|
throws BuildException {
|
265
|
5
|
if (JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_1)) {
|
266
|
0
|
throw new BuildException("The signjar task is only available on "
|
267
|
|
+ "JDK versions 1.2 or greater");
|
268
|
|
}
|
269
|
|
|
270
|
5
|
if (null == alias) {
|
271
|
0
|
throw new BuildException("alias attribute must be set");
|
272
|
|
}
|
273
|
|
|
274
|
5
|
if (null == storepass) {
|
275
|
0
|
throw new BuildException("storepass attribute must be set");
|
276
|
|
}
|
277
|
|
|
278
|
5
|
if (isUpToDate(jarSource, jarTarget)) {
|
279
|
0
|
return;
|
280
|
|
}
|
281
|
|
|
282
|
5
|
final ExecTask cmd = (ExecTask) getProject().createTask("exec");
|
283
|
5
|
cmd.setExecutable(JavaEnvUtils.getJdkExecutable("jarsigner"));
|
284
|
|
|
285
|
5
|
if (maxMemory != null) {
|
286
|
3
|
cmd.createArg().setValue("-J-Xmx" + maxMemory);
|
287
|
|
}
|
288
|
|
|
289
|
5
|
if (null != keystore) {
|
290
|
|
// is the keystore a file
|
291
|
5
|
File keystoreFile = getProject().resolveFile(keystore);
|
292
|
5
|
if (keystoreFile.exists()) {
|
293
|
4
|
cmd.createArg().setValue("-keystore");
|
294
|
4
|
cmd.createArg().setValue(keystoreFile.getPath());
|
295
|
|
} else {
|
296
|
|
// must be a URL - just pass as is
|
297
|
1
|
cmd.createArg().setValue("-keystore");
|
298
|
1
|
cmd.createArg().setValue(keystore);
|
299
|
|
}
|
300
|
|
}
|
301
|
|
|
302
|
5
|
if (null != storepass) {
|
303
|
5
|
cmd.createArg().setValue("-storepass");
|
304
|
5
|
cmd.createArg().setValue(storepass);
|
305
|
|
}
|
306
|
|
|
307
|
5
|
if (null != storetype) {
|
308
|
0
|
cmd.createArg().setValue("-storetype");
|
309
|
0
|
cmd.createArg().setValue(storetype);
|
310
|
|
}
|
311
|
|
|
312
|
5
|
if (null != keypass) {
|
313
|
0
|
cmd.createArg().setValue("-keypass");
|
314
|
0
|
cmd.createArg().setValue(keypass);
|
315
|
|
}
|
316
|
|
|
317
|
5
|
if (null != sigfile) {
|
318
|
1
|
cmd.createArg().setValue("-sigfile");
|
319
|
1
|
cmd.createArg().setValue(sigfile);
|
320
|
|
}
|
321
|
|
|
322
|
5
|
if (null != jarTarget) {
|
323
|
0
|
cmd.createArg().setValue("-signedjar");
|
324
|
0
|
cmd.createArg().setValue(jarTarget.toString());
|
325
|
|
}
|
326
|
|
|
327
|
5
|
if (verbose) {
|
328
|
0
|
cmd.createArg().setValue("-verbose");
|
329
|
|
}
|
330
|
|
|
331
|
5
|
if (internalsf) {
|
332
|
0
|
cmd.createArg().setValue("-internalsf");
|
333
|
|
}
|
334
|
|
|
335
|
5
|
if (sectionsonly) {
|
336
|
0
|
cmd.createArg().setValue("-sectionsonly");
|
337
|
|
}
|
338
|
|
|
339
|
5
|
cmd.createArg().setValue(jarSource.toString());
|
340
|
|
|
341
|
5
|
cmd.createArg().setValue(alias);
|
342
|
|
|
343
|
5
|
log("Signing Jar : " + jarSource.getAbsolutePath());
|
344
|
5
|
cmd.setFailonerror(true);
|
345
|
5
|
cmd.setTaskName(getTaskName());
|
346
|
5
|
cmd.execute();
|
347
|
|
}
|
348
|
|
|
349
|
5
|
protected boolean isUpToDate(File jarFile, File signedjarFile) {
|
350
|
5
|
if (null == jarFile) {
|
351
|
0
|
return false;
|
352
|
|
}
|
353
|
|
|
354
|
5
|
if (null != signedjarFile) {
|
355
|
|
|
356
|
0
|
if (!jarFile.exists()) {
|
357
|
0
|
return false;
|
358
|
|
}
|
359
|
0
|
if (!signedjarFile.exists()) {
|
360
|
0
|
return false;
|
361
|
|
}
|
362
|
0
|
if (jarFile.equals(signedjarFile)) {
|
363
|
0
|
return false;
|
364
|
|
}
|
365
|
0
|
if (signedjarFile.lastModified() > jarFile.lastModified()) {
|
366
|
0
|
return true;
|
367
|
|
}
|
368
|
|
} else {
|
369
|
5
|
if (lazy) {
|
370
|
0
|
return isSigned(jarFile);
|
371
|
|
}
|
372
|
|
}
|
373
|
|
|
374
|
5
|
return false;
|
375
|
|
}
|
376
|
|
|
377
|
0
|
protected boolean isSigned(File file) {
|
378
|
0
|
final String SIG_START = "META-INF/";
|
379
|
0
|
final String SIG_END = ".SF";
|
380
|
|
|
381
|
0
|
if (!file.exists()) {
|
382
|
0
|
return false;
|
383
|
|
}
|
384
|
0
|
ZipFile jarFile = null;
|
385
|
0
|
try {
|
386
|
0
|
jarFile = new ZipFile(file);
|
387
|
0
|
if (null == alias) {
|
388
|
0
|
Enumeration entries = jarFile.entries();
|
389
|
0
|
while (entries.hasMoreElements()) {
|
390
|
0
|
String name = ((ZipEntry) entries.nextElement()).getName();
|
391
|
0
|
if (name.startsWith(SIG_START) && name.endsWith(SIG_END)) {
|
392
|
0
|
return true;
|
393
|
|
}
|
394
|
|
}
|
395
|
0
|
return false;
|
396
|
|
} else {
|
397
|
0
|
return jarFile.getEntry(SIG_START + alias.toUpperCase() +
|
398
|
|
SIG_END) != null;
|
399
|
|
}
|
400
|
|
} catch (IOException e) {
|
401
|
0
|
return false;
|
402
|
|
} finally {
|
403
|
0
|
if (jarFile != null) {
|
404
|
0
|
try {
|
405
|
0
|
jarFile.close();
|
406
|
|
} catch (IOException e) {
|
407
|
|
}
|
408
|
|
}
|
409
|
|
}
|
410
|
|
}
|
411
|
|
}
|
412
|
|
|
413
|
|
|